
    Ib{/                        d Z ddlZddlZddlZddlZddlZddlZddlZddl	Z	ddl
Z
ddlmZ ddlmZmZmZmZ ddlmZ ddlmZ ddlmZ ddlmZ  ee	d	d
      Zd Zd Zd Zd Z d Z!d Z"d Z#d"dZ$d"dZ%e
jL                  dk(  re$Z'ne%Z'd#dZ(d Z)d#dZ*d#dZ+d Z,d Z-d Z.e
jL                  dk(  re-Z/ne.Z/d Z0d Z1d Z2d Z3d  Z4d! Z5y)$zNotebook related utilities    N)LooseVersion)quoteunquoteurlparseurljoin)pathname2url)Future)gen)	py3compat	UF_HIDDENi   c                 N    	 t        j                  |        y# t        $ r Y yw xY w)zcReplacement for `os.path.exists` which works for host mapped volumes
    on Windows containers
    FT)oslstatOSError)paths    0/usr/lib/python3/dist-packages/notebook/utils.pyexistsr      s+    
   s    	$$c                      | d   j                  d      }| d   j                  d      }| D cg c]  }|j                  d       }}dj                  d |D              }|rd|z   }|r|dz   }|dk(  rd}|S c c}w )zJoin components of url into a relative url

    Use to prevent double slash when joining subpath. This will leave the
    initial and final / in place
    r   /c              3   &   K   | ]	  }|s|  y wN ).0ss     r   	<genexpr>z url_path_join.<locals>.<genexpr>3   s     /AQa/s   z//)
startswithendswithstripjoin)piecesinitialfinalr   strippedresults         r   url_path_joinr&   *   s     Qi""3'G2J$E&,--H-XX///Fvv|f~vM .s   A6c                 J    t        |       j                  j                  d      S )z)Determine whether a given URL is absoluter   )r   r   r   )urls    r   url_is_absoluter)   9   s    C=((--    c                     | j                  t        j                        D cg c]  }t        |       }}|d   dk(  rd|d<   t	        | }|S c c}w )z"Convert a local file path to a URLr    r   )splitr   sepr   r&   )r   pr!   r(   s       r   path2urlr0   =   sO    !%BFF!35AuQx5F5bzRr


 CJ 6s   Ac                     | j                  d      D cg c]  }t        |       }}t        j                  j                  | }|S c c}w )z"Convert a URL to a local file pathr   )r-   r   r   r   r    )r(   r/   r!   r   s       r   url2pathr2   F   s=    #&99S>3awqz3F377<< DK 4s   Ac                     t        j                  | d      j                  d      }dj                  |D cg c]  }t	        |       c}      S c c}w )zVEscape special characters in a URL path

    Turns '/foo bar/' into '/foo%20bar/'
    utf8encodingr   )r   unicode_to_strr-   r    r   )r   partsr/   s      r   
url_escaper9   L   sB    
 $$TF;AA#FE88u-!U1X-..-s   Ac           
          dj                  t        j                  | d      j                  d      D cg c]"  }t        j                  t        |      d      $ c}      S c c}w )zXUnescape special characters in a URL path

    Turns '/foo%20bar/' into '/foo bar/'
    r   r4   r5   )r    r   r7   r-   str_to_unicoder   )r   r/   s     r   url_unescaper<   T   sY    
 88))$@FFsK 	  f=   s   'A"c                    t         j                  j                  |       j                  d      ryd}	 t        j
                  j                  j                  t        j                  |             }|dkD  r||z  ryy# t        $ r Y yw xY w)a  Is a file hidden?

    This only checks the file itself; it should be called in combination with
    checking the directory containing the file.

    Use is_hidden() instead to check the file and its parent directories.

    Parameters
    ----------
    abs_path : unicode
        The absolute path to check.
    stat_res : os.stat_result, optional
        Ignored on Windows, exists for compatibility with POSIX version of the
        function.
    .T   r   F)r   r   basenamer   ctypeswindllkernel32GetFileAttributesWr   cast_unicodeAttributeError)abs_pathstat_reswin32_FILE_ATTRIBUTE_HIDDENattrss       r   is_file_hidden_winrK   _   s      
ww!,,S1"&&&99""8,
 19!<<  
 s   <A; ;	BBc                    t         j                  j                  |       j                  d      ry|t	        j
                  |j                        r	 t        j                  |       }t	        j                  |j                        r6t        j                  | t         j                  t         j                  z        syt        |dd      t        z  ryy# t        $ r(}|j                  t        j                  k(  rY d}~y d}~ww xY w)a  Is a file hidden?

    This only checks the file itself; it should be called in combination with
    checking the directory containing the file.

    Use is_hidden() instead to check the file and its parent directories.

    Parameters
    ----------
    abs_path : unicode
        The absolute path to check.
    stat_res : os.stat_result, optional
        The result of calling stat() on abs_path. If not passed, this function
        will call stat() internally.
    r>   TNFst_flagsr   )r   r   r@   r   statS_ISLNKst_moder   errnoENOENTS_ISDIRaccessX_OKR_OKgetattrr   )rG   rH   es      r   is_file_hidden_posixrY      s      
ww!,,S14<<(8(89	wwx(H ||H$$%yy277RWW#45 xQ')3  	ww%,,&	s   C 	DC>=C>>Dwin32c                 ,   t         j                  j                  |       t         j                  j                  |      k(  ryt        |       ry|s4| j	                  t         j
                  d      d   t         j
                  z   }| t        |      d }t        d |j	                  t         j
                        D              ryt         j                  j                  |       }|r|j                  |      r||k7  rt        |      s t         j                  j                  |      }C	 t        j                  |      }t        |dd      t        z  ryt         j                  j                  |      }|r|j                  |      r||k7  ry# t        $ r Y yw xY w)a  Is a file hidden or contained in a hidden directory?

    This will start with the rightmost path element and work backwards to the
    given root to see if a path is hidden or in a hidden directory. Hidden is
    determined by either name starting with '.' or the UF_HIDDEN flag as
    reported by stat.

    If abs_path is the same directory as abs_root, it will be visible even if
    that is a hidden folder. This only checks the visibility of files
    and directories *within* abs_root.

    Parameters
    ----------
    abs_path : unicode
        The absolute path to check for hidden directories.
    abs_root : unicode
        The absolute path of the root directory in which hidden directories
        should be checked for.
    FT   r   Nc              3   >   K   | ]  }|j                  d         yw)r>   N)r   )r   parts     r   r   zis_hidden.<locals>.<genexpr>   s     
FD4??3
Fs   rM   )r   r   normpathis_file_hiddenr-   r.   lenanydirnamer   r   r   r   rW   r   )rG   abs_rootinside_rootr   sts        r   	is_hiddenrg      s<   ( 
ww!RWW%5%5h%??h>>"&&!,Q/"&&83x=>*K

FK,=,=bff,E
FF 77??8$D
4??8,1Ad|77??4(D	$B 2z1%	1wwt$ 4??8,1A   		s   $F 	FFc                     t        j                  |       }t        j                  |      }| j                         |j                         k(  xr ||k(  S )ao  
    Fill in for os.path.samefile when it is unavailable (Windows+py2).

    Do a case-insensitive string comparison in this case
    plus comparing the full stat result (including times)
    because Windows + py2 doesn't support the stat fields
    needed for identifying if it's the same file (st_ino, st_dev).

    Only to be used if os.path.samefile is not available.

    Parameters
    -----------
    path:       String representing a path to a file
    other_path: String representing a path to another file

    Returns
    -----------
    same:   Boolean that is True if both path and other path are the same
    )r   rN   lower)r   
other_path	path_statother_path_stats       r   samefile_simplerm      sH    ( Iggj)OJJLJ,,.. )(*r*   c                     | j                  d      j                  d      }|D cg c]
  }|dk7  s	| }}t        j                  j                  |g| } | S c c}w )zConvert an API path to a filesystem path

    If given, root will be prepended to the path.
    root must be a filesystem path already.
    r   r,   )r   r-   r   r   r    )r   rootr8   r/   s       r   
to_os_pathrp      sV     JJsO!!#&E)1bQ)E)77<<%u%DK *s
   
AAc                 :   | j                  |      r| t        |      d } | j                  t        j                  j
                        j                  t        j                  j
                        }|D cg c]
  }|dk7  s	| }}dj                  |      }|S c c}w )zConvert a filesystem path to an API path

    If given, root will be removed from the path.
    root must be a filesystem path already.
    Nr,   r   )r   ra   r   r   r   r.   r-   r    )os_pathro   r8   r/   r   s        r   to_api_pathrs     s{     $#d)*%MM"''++&,,RWW[[9E)1bQ)E)88E?DK *s   4
B?Bc                 P    	 t        |       t        |      k\  S # t        $ r Y yw xY w)zcheck version string v >= check

    If dev/prerelease tags result in TypeError for string-number comparison,
    it is assumed that the dependency is satisfied.
    Users on dev branches are responsible for keeping their own packages up to date.
    T)r   	TypeError)vchecks     r   check_versionrx     s-    A,u"555 s    	%%c                     dd l }|j                  j                  j                  dd|       }|r%|j                  j                  j	                  |       t        |      S )Nr   r\   )rA   rB   rC   OpenProcessCloseHandlebool)pidrA   handles      r   _check_pid_win32r     sJ     ]]##//!C8F**F4<r*   c                     	 t        j                  | d       y# t        $ rJ}|j                  t        j                  k(  rY d}~y|j                  t        j
                  k(  rY d}~y d}~ww xY w)z'Copy of IPython.utils.process.check_pidr   TNF)r   killr   rQ   ESRCHEPERM)r}   errs     r   _check_pid_posixr   '  sT    

Q   99#YY%++%s     	A,A'A'&A''A,c                    t        j                  |       rt        j                  |       S t	        | t
        j                  j                        rt        j                  |       S t        j                         }|j                  |        |S )zxLike tornado's deprecated gen.maybe_future

    but more compatible with asyncio for recent versions
    of tornado
    )
inspectisawaitableasyncioensure_future
isinstance
concurrentfuturesr	   wrap_future
set_result)objfs     r   maybe_futurer   ;  si     3$$S))	C++22	3""3'' NN	Sr*   c                 J     t        j                         s S  fd} |       S )a  If async, runs maybe_async and blocks until it has executed,
    possibly creating an event loop.
    If not async, just returns maybe_async as it is the result of something
    that has already executed.
    Parameters
    ----------
    maybe_async : async or non-async object
        The object to be executed, if it is async.
    Returns
    -------
    result :
        Whatever the async object returns, or the object itself.
    c                  l   d} 	 t        j                         }|j                         rd} 	 | r)t        j                         }t        j
                  |       	 j                        }|S # t        $ r d} Y Lw xY w# t        $ r.}t        |      dk(  rt        j                        }Y d }~S d }~ww xY w)NFTz"This event loop is already running)	r   get_event_loop	is_closedRuntimeErrornew_event_loopset_event_looprun_until_completestrr   )create_new_event_looploopr%   rX   maybe_asyncs       r   wrappedzrun_sync.<locals>.wrapped_  s     %	-))+D ~~(,% ))+D""4(	<,,[9F
   	)$(!	)  	<1v== ..{;		<s)   A+ A< +A98A9<	B3#B..B3)r   r   )r   r   s   ` r   run_syncr   L  s(     {+& 9r*   c                 &    | j                  dd      S )zREncodes a UNIX socket path string from a socket path for the `http+unix` URI form.r   %2Freplacesocket_paths    r   urlencode_unix_socket_pathr   u  s    sE**r*   c                 &    | j                  dd      S )zWDecodes a UNIX sock path string from an encoded sock path for the `http+unix` URI form.r   r   r   r   s    r   urldecode_unix_socket_pathr   z  s    uc**r*   c                     dt        |        S )zJEncodes a UNIX socket URL from a socket path for the `http+unix` URI form.zhttp+unix://)r   r   s    r   urlencode_unix_socketr     s    4[ABCCr*   c                 V   t         j                  j                  |       sy	 t        j                  t        j                  t        j
                        }|j                  |        	 |j                          y# t        $ r Y j                          yw xY w# j                          w xY w)zSChecks whether a UNIX socket path on disk is in use by attempting to connect to it.FT)	r   r   r   socketAF_UNIXSOCK_STREAMconnectcloser   )r   socks     r   unix_socket_in_user     sy    77>>+&}}V^^V-?-?@[! 

   	


 	

s$   AA7 7	B B BB B(r   )r,   )6__doc__r   concurrent.futuresr   rA   rQ   r   r   r   rN   sysdistutils.versionr   urllib.parser   r   r   r   urllib.requestr   tornado.concurrentr	   TornadoFuturetornador
   ipython_genutilsr   rW   r   r   r&   r)   r0   r2   r9   r<   rK   rY   platformr`   rg   rm   rp   rs   rx   r   r   	check_pidr   r   r   r   r   r   r   r*   r   <module>r      s     
      	   
 * ; : ' 7  & D+u-	./@%N <<7'N)N0d*4	
 <<7 I I"&R+
+
D
r*   