ó
ù2¢\c           @  s°  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 Wn e k
 r’ d „  Z n Xy d d l m Z Wn$ e k
 rÍ d d% d	 „  ƒ  YZ n Xy6 d d
 l m Z m Z m Z m Z d d l m Z WnC e k
 rId d l m Z m Z m Z d d l m Z m Z n Xd 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 j* d k rûe( Z+ n e) Z+ d d „ Z, d „  Z- d d „ Z. d d  „ Z/ d! „  Z0 d" „  Z1 d# „  Z2 e j* d k rae1 Z3 n e2 Z3 d$ „  Z4 y d d l5 Z5 Wn e k
 r“n Xd d l6 Z e4 e j _4 d S(&   s   Notebook related utilitiesiÿÿÿÿ(   t   print_functionN(   t   LooseVersion(   t   isawaitablec         C  s   t  S(   s1   If isawaitable is undefined, nothing is awaitable(   t   False(   t   f(    (    s-   lib/python2.7/site-packages/notebook/utils.pyR      s    (   t   Futuret   ConcurrentFuturec           B  s   e  Z d  Z RS(   sD   If concurrent.futures isn't importable, nothing will be a c.f.Future(   t   __name__t
   __module__t   __doc__(    (    (    s-   lib/python2.7/site-packages/notebook/utils.pyR      s   (   t   quotet   unquotet   urlparset   urljoin(   t   pathname2url(   R
   R   R   (   R   R   (   t   gen(   t	   py3compatt	   UF_HIDDENi €  c         C  s*   y t  j |  ƒ Wn t k
 r% t SXt S(   sc   Replacement for `os.path.exists` which works for host mapped volumes
    on Windows containers
    (   t   ost   lstatt   OSErrorR   t   True(   t   path(    (    s-   lib/python2.7/site-packages/notebook/utils.pyt   exists/   s
    c          G  s    |  d j  d ƒ } |  d j d ƒ } g  |  D] } | j d ƒ ^ q- } d j d „  | Dƒ ƒ } | rt d | } n  | r‡ | d } n  | d k rœ d } n  | S(   s›   Join components of url into a relative url

    Use to prevent double slash when joining subpath. This will leave the
    initial and final / in place
    i    t   /iÿÿÿÿc         s  s   |  ] } | r | Vq d  S(   N(    (   t   .0t   s(    (    s-   lib/python2.7/site-packages/notebook/utils.pys	   <genexpr>C   s    s   //(   t
   startswitht   endswitht   stript   join(   t   piecest   initialt   finalR   t   strippedt   result(    (    s-   lib/python2.7/site-packages/notebook/utils.pyt   url_path_join:   s    "   	c         C  s   t  |  ƒ j j d ƒ S(   s)   Determine whether a given URL is absoluteR   (   R   R   R   (   t   url(    (    s-   lib/python2.7/site-packages/notebook/utils.pyt   url_is_absoluteI   s    c         C  sX   g  |  j  t j ƒ D] } t | ƒ ^ q } | d d k rH d | d <n  t | Œ  } | S(   s"   Convert a local file path to a URLiÿÿÿÿt    R   (   t   splitR   t   sepR
   R$   (   R   t   pR   R%   (    (    s-   lib/python2.7/site-packages/notebook/utils.pyt   path2urlM   s
    +c         C  s>   g  |  j  d ƒ D] } t | ƒ ^ q } t j j | Œ  } | S(   s"   Convert a URL to a local file pathR   (   R(   R   R   R   R   (   R%   R*   R   R   (    (    s-   lib/python2.7/site-packages/notebook/utils.pyt   url2pathV   s    (c         C  sD   t  j |  d d ƒj d ƒ } d j g  | D] } t | ƒ ^ q+ ƒ S(   sV   Escape special characters in a URL path

    Turns '/foo bar/' into '/foo%20bar/'
    t   encodingt   utf8R   u   /(   R   t   unicode_to_strR(   R   R
   (   R   t   partsR*   (    (    s-   lib/python2.7/site-packages/notebook/utils.pyt
   url_escape\   s    c         C  sM   d j  g  t j |  d d ƒj d ƒ D]! } t j t | ƒ d d ƒ^ q% ƒ S(   sX   Unescape special characters in a URL path

    Turns '/foo%20bar/' into '/foo bar/'
    u   /R-   R.   R   (   R   R   R/   R(   t   str_to_unicodeR   (   R   R*   (    (    s-   lib/python2.7/site-packages/notebook/utils.pyt   url_unescaped   s    c         C  sy   t  j j |  ƒ j d ƒ r t Sd } y" t j j j t	 j
 |  ƒ ƒ } Wn t k
 rZ n X| d k ru | | @ru t St S(   sÃ  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   .i   i    (   R   R   t   basenameR   R   t   ctypest   windllt   kernel32t   GetFileAttributesWR   t   cast_unicodet   AttributeErrorR   (   t   abs_patht   stat_rest   win32_FILE_ATTRIBUTE_HIDDENt   attrs(    (    s-   lib/python2.7/site-packages/notebook/utils.pyt   is_file_hidden_wino   s    c         C  sÕ   t  j j |  ƒ j d ƒ r t S| d k s= t j | j ƒ r‚ y t  j |  ƒ } Wq‚ t	 k
 r~ } | j
 t
 j k rx t S‚  q‚ Xn  t j | j ƒ r· t  j |  t  j t  j Bƒ s· t Sn  t | d d ƒ t @rÑ t St S(   sÖ  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.
    R4   t   st_flagsi    N(   R   R   R5   R   R   t   Nonet   statt   S_ISLNKt   st_modeR   t   errnot   ENOENTR   t   S_ISDIRt   accesst   X_OKt   R_OKt   getattrR   (   R<   R=   t   e(    (    s-   lib/python2.7/site-packages/notebook/utils.pyt   is_file_hidden_posix   s    
t   win32R'   c         C  sM  t  j j |  ƒ t  j j | ƒ k r( t St |  ƒ r8 t S| sa |  j t  j d ƒ d t  j } n  |  t | ƒ } t	 d „  | j t  j ƒ Dƒ ƒ r— t St  j j
 |  ƒ } x | rH| j | ƒ rH| | k rHt | ƒ sñ t  j j
 | ƒ } q¬ n  y t  j | ƒ } Wn t k
 rt SXt | d d ƒ t @r3t St  j j
 | ƒ } q¬ Wt S(   sä  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.
    i   i    c         s  s   |  ] } | j  d  ƒ Vq d S(   R4   N(   R   (   R   t   part(    (    s-   lib/python2.7/site-packages/notebook/utils.pys	   <genexpr>Ø   s    RA   (   R   R   t   normpathR   t   is_file_hiddenR   R(   R)   t   lent   anyt   dirnameR   R   R   R   RL   R   (   R<   t   abs_roott   inside_rootR   t   st(    (    s-   lib/python2.7/site-packages/notebook/utils.pyt	   is_hidden»   s,    $#"$c         C  s@   t  j |  ƒ } t  j | ƒ } |  j ƒ  | j ƒ  k o? | | k S(   so  
    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   RC   t   lower(   R   t
   other_patht	   path_statt   other_path_stat(    (    s-   lib/python2.7/site-packages/notebook/utils.pyt   samefile_simpleí   s    c         C  sV   |  j  d ƒ j d ƒ } g  | D] } | d k r | ^ q } t j j | | Œ }  |  S(   sŒ   Convert 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   t   rootR0   R*   (    (    s-   lib/python2.7/site-packages/notebook/utils.pyt
   to_os_path  s    %c         C  s~   |  j  | ƒ r" |  t | ƒ }  n  |  j t j j ƒ j t j j ƒ } g  | D] } | d k rM | ^ qM } d j | ƒ } | S(   sŒ   Convert a filesystem path to an API path

    If given, root will be removed from the path.
    root must be a filesystem path already.
    R'   R   (   R   RS   R   R   R   R)   R(   R   (   t   os_pathR_   R0   R*   R   (    (    s-   lib/python2.7/site-packages/notebook/utils.pyt   to_api_path  s    $%c         C  s3   y t  |  ƒ t  | ƒ k SWn t k
 r. t SXd S(   sû   check 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.
    N(   R   t	   TypeErrorR   (   t   vt   check(    (    s-   lib/python2.7/site-packages/notebook/utils.pyt   check_version   s    c         C  s+   d d  l  } t | j j j d d |  ƒ ƒ S(   Niÿÿÿÿi   i    (   R6   t   boolR7   R8   t   OpenProcess(   t   pidR6   (    (    s-   lib/python2.7/site-packages/notebook/utils.pyt   _check_pid_win32/  s    c         C  sa   y t  j |  d ƒ WnB t k
 rX } | j t j k r< t S| j t j k rR t S‚  n Xt Sd S(   s'   Copy of IPython.utils.process.check_pidi    N(   R   t   killR   RF   t   ESRCHR   t   EPERMR   (   Ri   t   err(    (    s-   lib/python2.7/site-packages/notebook/utils.pyt   _check_pid_posix5  s    c         C  sf   t  |  t ƒ r |  St |  ƒ r, t j |  ƒ St  |  t ƒ rH t j |  ƒ St ƒ  } | j |  ƒ | Sd S(   sm   Like tornado's gen.maybe_future

    but more compatible with asyncio for recent versions
    of tornado
    N(   t
   isinstancet   TornadoFutureR   t   asynciot   ensure_futureR   t   wrap_futuret
   set_result(   t   objR   (    (    s-   lib/python2.7/site-packages/notebook/utils.pyt   maybe_futureI  s    	(    (7   R	   t
   __future__R    R6   RF   R   RC   t   syst   distutils.versionR   t   inspectR   t   ImportErrort   concurrent.futuresR   R   t   urllib.parseR
   R   R   R   t   urllib.requestR   t   urllibt   tornado.concurrentRq   t   tornadoR   t   ipython_genutilsR   RL   R   R   R$   R&   R+   R,   R1   R3   RB   R@   RN   t   platformRR   RY   R^   R`   Rb   Rf   Rj   Ro   t	   check_pidRw   Rr   t   tornado.gen(    (    (    s-   lib/python2.7/site-packages/notebook/utils.pyt   <module>   sl   "								 '	2						