o
    i/                     @   s  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d Zdd Zdd Zdd Z dd Z!dd Z"dd Z#d9ddZ$d9ddZ%e
j&dkre$Z'ne%Z'd:dd Z(d!d" Z)d:d#d$Z*d:d%d&Z+d'd( Z,d)d* Z-d+d, Z.e
j&dkre-Z/ne.Z/d-d. Z0d/d0 Z1d1d2 Z2d3d4 Z3d5d6 Z4d7d8 Z5dS );zNotebook related utilities    N)LooseVersion)quoteunquoteurlparseurljoin)pathname2url)Future)gen)	py3compat	UF_HIDDENi   c                 C   s&   zt |  W dS  ty   Y dS w )zcReplacement for `os.path.exists` which works for host mapped volumes
    on Windows containers
    FT)oslstatOSErrorpath r   I/var/www/edux/Edux_v2/venv/lib/python3.10/site-packages/notebook/utils.pyexists   s   r   c                  G   sf   | d  d}| d d}dd | D }ddd |D }|r%d| }|r+|d }|dkr1d}|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>2   s    z!url_path_join.<locals>.<listcomp>c                 s   s    | ]}|r|V  qd S Nr   r   r   r   r   	<genexpr>3   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_absolute9   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      g | ]}t |qS r   r   r   pr   r   r   r   ?       zpath2url.<locals>.<listcomp>r    r   )splitr   sepr%   )r   r    r&   r   r   r   path2url=   s
   r0   c                 C   s$   dd |  dD }tjj| }|S )z"Convert a URL to a local file pathc                 S   r(   r   )r   r*   r   r   r   r   H   r,   zurl2path.<locals>.<listcomp>r   )r.   r   r   r   )r&   r    r   r   r   r   url2pathF   s   r1   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   r(   r   r)   r*   r   r   r   r   R   r,   zurl_escape.<locals>.<listcomp>)r
   unicode_to_strr.   r   )r   partsr   r   r   
url_escapeL   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 )r2   r3   )r
   str_to_unicoder   r*   r   r   r   r   Y   s    z url_unescape.<locals>.<listcomp>r2   r3   )r   r
   r5   r.   r   r   r   r   url_unescapeT   s   
r9   c                 C   s`   t j| drdS d}ztjjt	| }W n
 t
y#   Y dS w |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kernel32GetFileAttributesWr
   cast_unicodeAttributeError)abs_pathstat_reswin32_FILE_ATTRIBUTE_HIDDENattrsr   r   r   is_file_hidden_win_   s   rG   c              
   C   s   t j| drdS |du st|jr7zt | }W n ty6 } z|jtj	kr1W Y d}~dS  d}~ww t
|jrIt | t jt jB sIdS t|ddt@ rSd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.
    r:   TNFst_flagsr   )r   r   r<   r   statS_ISLNKst_moder   errnoENOENTS_ISDIRaccessX_OKR_OKgetattrr   )rC   rD   er   r   r   is_file_hidden_posix   s"   rT   win32r-   c                 C   s  t j| t j|krdS t| rdS |s"| t jdd t j }| t|d }tdd |t jD r9dS t j| }|r|	|r||krt
|sUt j|}q?zt |}W n
 tyf   Y dS w t|ddt@ rqdS t j|}|r|	|r||ksJd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 )r:   N)r   )r   partr   r   r   r      s    zis_hidden.<locals>.<genexpr>rH   )r   r   normpathis_file_hiddenr.   r/   lenanydirnamer   r   r   r   rR   r   )rC   abs_rootinside_rootr   str   r   r   	is_hidden   s0   r`   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   rI   lower)r   
other_path	path_statother_path_statr   r   r   samefile_simple   s
   

re   c                 C   s>   |  dd}dd |D }tjj|g|R  } tj| 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      g | ]}|d kr|qS r-   r   r*   r   r   r   r          zto_os_path.<locals>.<listcomp>)r   r.   r   r   r   rX   )r   rootr6   r   r   r   
to_os_path   s   rj   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   rf   rg   r   r*   r   r   r   r     rh   zto_api_path.<locals>.<listcomp>r   )r   rZ   r   r   r   r/   r.   r   )os_pathri   r6   r   r   r   r   to_api_path  s   

rl   c                 C   s(   z	t | t |kW S  ty   Y dS 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checkr   r   r   check_version  s
   rp   c                 C   s4   dd l }|jjdd| }|r|jj| t|S )Nr   rV   )r=   r>   r?   OpenProcessCloseHandlebool)pidr=   handler   r   r   _check_pid_win32  s
   rv   c              
   C   sb   z	t | d W dS  ty0 } z|jtjkrW Y d}~dS |jtjkr+W Y d}~dS  d}~ww )z'Copy of IPython.utils.process.check_pidr   NFT)r   killr   rL   ESRCHEPERM)rt   errr   r   r   _check_pid_posix'  s   	r{   c                 C   sB   t | r
t| S t| tjjrt| S t }|	|  |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fr   r   r   maybe_future;  s   



r   c                    s    t  s S  fd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               
      s   d} zt  }W n ty   d} Y nw | rd} | r%t  }t | z| }W |S  tyR } zt|dkrGt  }W Y d }~|S W Y d }~|S d }~ww )NFTz"This event loop is already running)	r~   get_event_loopRuntimeError	is_closednew_event_loopset_event_looprun_until_completestrr   )create_new_event_looploopr$   rS   maybe_asyncr   r   wrapped_  s,   

zrun_sync.<locals>.wrapped)r|   r}   )r   r   r   r   r   run_syncL  s   
r   c                 C      |  ddS )zREncodes a UNIX socket path string from a socket path for the `http+unix` URI form.r   %2Freplacesocket_pathr   r   r   urlencode_unix_socket_pathu     r   c                 C   r   )zWDecodes a UNIX sock path string from an encoded sock path for the `http+unix` URI form.r   r   r   r   r   r   r   urldecode_unix_socket_pathz  r   r   c                 C   s   dt |  S )zJEncodes a UNIX socket URL from a socket path for the `http+unix` URI form.zhttp+unix://)r   r   r   r   r   urlencode_unix_socket  s   r   c                 C   sh   t j| sdS z&zttjtj}||  W n ty'   Y W |  dS w W |  dS |  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connectr   close)r   sockr   r   r   unix_socket_in_use  s   r   r   rg   )6__doc__r~   concurrent.futuresr   r=   rL   r|   r   r   rI   sysdistutils.versionr   urllib.parser   r   r   r   urllib.requestr   tornado.concurrentr   TornadoFuturetornador	   ipython_genutilsr
   rR   r   r   r%   r'   r0   r1   r7   r9   rG   rT   platformrY   r`   re   rj   rl   rp   rv   r{   	check_pidr   r   r   r   r   r   r   r   r   r   <module>   sZ    	

 
'
2



)