B
    3\(                 @   s0  d Z ddlm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	 yddl
mZ W n ek
rp   dd ZY nX yddlmZ W n" ek
r   G d	d
 d
ZY nX y(ddlmZmZmZmZ ddlmZ W n: ek
r   ddlmZmZmZ ddlmZmZ Y nX ddlmZ ddlmZ ddlmZ eeddZdd Z dd Z!dd Z"dd Z#dd Z$dd Z%dd  Z&d7d!d"Z'd8d#d$Z(ej)d%kre'Z*ne(Z*d9d'd(Z+d)d* Z,d:d+d,Z-d;d-d.Z.d/d0 Z/d1d2 Z0d3d4 Z1ej)d%kre0Z2ne1Z2d5d6 Z3yddl4Z4W n ek
r   Y nX ddl5Ze3ej_3dS )<zNotebook related utilities    )print_functionN)LooseVersion)isawaitablec             C   s   dS )z1If isawaitable is undefined, nothing is awaitableF )fr   r   -lib/python3.7/site-packages/notebook/utils.pyr      s    r   )Futurec               @   s   e Zd ZdZdS )ConcurrentFuturezDIf concurrent.futures isn't importable, nothing will be a c.f.FutureN)__name__
__module____qualname____doc__r   r   r   r   r	      s   r	   )quoteunquoteurlparseurljoin)pathname2url)r   r   r   )r   r   )gen)	py3compat	UF_HIDDENi   c             C   s(   yt |  W n tk
r"   dS X dS )zcReplacement for `os.path.exists` which works for host mapped volumes
    on Windows containers
    FT)oslstatOSError)pathr   r   r   exists/   s
    r   c              G   sf   | d  d}| d d}dd | D }ddd |D }|rJd| }|rV|d }|dkrbd}|S )	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             S   s   g | ]}| d qS )r   )strip).0sr   r   r   
<listcomp>B   s    z!url_path_join.<locals>.<listcomp>c             s   s   | ]}|r|V  qd S )Nr   )r   r   r   r   r   	<genexpr>C   s    z url_path_join.<locals>.<genexpr>z//)
startswithendswithjoin)piecesinitialfinalstrippedresultr   r   r   url_path_join:   s       r*   c             C   s   t | jdS )z)Determine whether a given URL is absoluter   )r   r   r"   )urlr   r   r   url_is_absoluteI   s    r,   c             C   s6   dd |  tjD }|d dkr*d|d< t| }|S )z"Convert a local file path to a URLc             S   s   g | ]}t |qS r   )r   )r   pr   r   r   r    O   s    zpath2url.<locals>.<listcomp>r    r   )splitr   sepr*   )r   r%   r+   r   r   r   path2urlM   s
    r1   c             C   s$   dd |  dD }tjj| }|S )z"Convert a URL to a local file pathc             S   s   g | ]}t |qS r   )r   )r   r-   r   r   r   r    X   s    zurl2path.<locals>.<listcomp>r   )r/   r   r   r$   )r+   r%   r   r   r   r   url2pathV   s    r2   c             C   s(   t j| ddd}ddd |D S )zVEscape special characters in a URL path

    Turns '/foo bar/' into '/foo%20bar/'
    utf8)encodingr   c             S   s   g | ]}t |qS r   )r   )r   r-   r   r   r   r    b   s    zurl_escape.<locals>.<listcomp>)r   unicode_to_strr/   r$   )r   partsr   r   r   
url_escape\   s    r7   c             C   s$   d dd tj| dddD S )zXUnescape special characters in a URL path

    Turns '/foo%20bar/' into '/foo bar/'
    r   c             S   s   g | ]}t jt|d dqS )r3   )r4   )r   Zstr_to_unicoder   )r   r-   r   r   r   r    j   s   z url_unescape.<locals>.<listcomp>r3   )r4   )r$   r   r5   r/   )r   r   r   r   url_unescaped   s    r8   c             C   s`   t j| drdS d}ytjjt	| }W n t
k
rF   Y nX |dkr\||@ r\dS dS )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kernel32ZGetFileAttributesWr   Zcast_unicodeAttributeError)abs_pathstat_resZwin32_FILE_ATTRIBUTE_HIDDENZattrsr   r   r   is_file_hidden_wino   s    rB   c          
   C   s   t j| drdS |dks*t|jrnyt | }W n4 tk
rl } z|jtj	krZdS  W dd}~X Y nX t
|jrt | t jt jB sdS t|ddt@ rdS dS )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.
    r9   TNFst_flagsr   )r   r   r;   r"   statS_ISLNKst_moder   errnoZENOENTS_ISDIRaccessX_OKR_OKgetattrr   )r@   rA   er   r   r   is_file_hidden_posix   s    rN   win32r.   c             C   s   t j| t j|krdS t| r(dS |sD| t jdd t j }| t|d }tdd |t jD rrdS t j| }xt|r|	|r||krt
|st j|}qyt |}W n tk
r   dS X t|ddt@ rdS t j|}qW dS )	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             s   s   | ]}| d V  qdS )r9   N)r"   )r   partr   r   r   r!      s    zis_hidden.<locals>.<genexpr>rC   )r   r   normpathis_file_hiddenr/   r0   lenanydirnamer"   r   r   r   rL   r   )r@   Zabs_rootZinside_rootr   str   r   r   	is_hidden   s,    rX   c             C   s,   t | }t |}|  | ko*||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   rD   lower)r   Z
other_pathZ	path_statZother_path_statr   r   r   samefile_simple   s    

rZ   c             C   s4   |  dd}dd |D }tjj|f| } | S )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   c             S   s   g | ]}|d kr|qS )r.   r   )r   r-   r   r   r   r      s    zto_os_path.<locals>.<listcomp>)r   r/   r   r   r$   )r   rootr6   r   r   r   
to_os_path  s    r\   c             C   sN   |  |r| t|d } | tjjtjj}dd |D }d|}|S )zConvert a filesystem path to an API path

    If given, root will be removed from the path.
    root must be a filesystem path already.
    Nc             S   s   g | ]}|d kr|qS )r.   r   )r   r-   r   r   r   r      s    zto_api_path.<locals>.<listcomp>r   )r"   rT   r   r   r   r0   r/   r$   )Zos_pathr[   r6   r   r   r   r   to_api_path  s    

r]   c             C   s*   yt | t |kS  tk
r$   dS X dS )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.
    TN)r   	TypeError)vZcheckr   r   r   check_version   s    r`   c             C   s   dd l }t|jjdd| S )Nr   rP   )r<   boolr=   r>   ZOpenProcess)pidr<   r   r   r   _check_pid_win32/  s    rc   c          
   C   s^   yt | d W nD tk
rT } z&|jtjkr2dS |jtjkrBdS  W dd}~X Y nX dS dS )z'Copy of IPython.utils.process.check_pidr   FTN)r   killr   rG   ZESRCHZEPERM)rb   errr   r   r   _check_pid_posix5  s    rf   c             C   sL   t | tr| S t| r t| S t | tr4t| S t }||  |S dS )zmLike tornado's gen.maybe_future

    but more compatible with asyncio for recent versions
    of tornado
    N)
isinstanceTornadoFuturer   asyncioZensure_futurer	   Zwrap_futureZ
set_result)objr   r   r   r   maybe_futureI  s    




rk   )N)N)r.   )r.   )r.   )6r   Z
__future__r   r<   rG   r   rD   sysZdistutils.versionr   inspectr   ImportErrorZconcurrent.futuresr   r	   Zurllib.parser   r   r   r   Zurllib.requestr   ZurllibZtornado.concurrentrh   Ztornador   Zipython_genutilsr   rL   r   r   r*   r,   r1   r2   r7   r8   rB   rN   platformrS   rX   rZ   r\   r]   r`   rc   rf   Z	check_pidrk   ri   Ztornado.genr   r   r   r   <module>   sl   	
 
'
2

