B
    Mhh)                 @   s   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gZ
dd Zdd ZejZd	d
 ZG dd dZG dd deejZG dd deZdddZG dd dZdS )    N   )text_encodingPathc             C   s   t t| ddS )a2  
    Given a path with elements separated by
    posixpath.sep, generate all parents of that path.

    >>> list(_parents('b/d'))
    ['b']
    >>> list(_parents('/b/d/'))
    ['/b']
    >>> list(_parents('b/d/f/'))
    ['b/d', 'b']
    >>> list(_parents('b'))
    []
    >>> list(_parents(''))
    []
    r   N)	itertoolsislice	_ancestry)path r	   //tmp/pip-install-vv81h98y/zipp/zipp/__init__.py_parents   s    r   c             c   s8   |  tj} x&| r2| tjkr2| V  t| \} }qW dS )aR  
    Given a path with elements separated by
    posixpath.sep, generate all elements of that path

    >>> list(_ancestry('b/d'))
    ['b/d', 'b']
    >>> list(_ancestry('/b/d/'))
    ['/b/d', '/b']
    >>> list(_ancestry('b/d/f/'))
    ['b/d/f', 'b/d', 'b']
    >>> list(_ancestry('b'))
    ['b']
    >>> list(_ancestry(''))
    []
    N)rstrip	posixpathsepsplit)r   tailr	   r	   r
   r   #   s    r   c             C   s   t t|j| S )zZ
    Return items in minuend not in subtrahend, retaining order
    with O(1) lookup.
    )r   filterfalseset__contains__)ZminuendZ
subtrahendr	   r	   r
   _difference=   s    r   c                   s4   e Zd ZdZ fddZdd Z fddZ  ZS )InitializedStatez?
    Mix-in to save the initialization state for pickling.
    c                s   || _ || _t j|| d S )N)_InitializedState__args_InitializedState__kwargssuper__init__)selfargskwargs)	__class__r	   r
   r   J   s    zInitializedState.__init__c             C   s   | j | jfS )N)r   r   )r   r	   r	   r
   __getstate__O   s    zInitializedState.__getstate__c                s   |\}}t  j|| d S )N)r   r   )r   stater   r   )r   r	   r
   __setstate__R   s    zInitializedState.__setstate__)__name__
__module____qualname____doc__r   r   r    __classcell__r	   r	   )r   r
   r   E   s   r   c                   sT   e Zd ZdZedd Z fddZdd Zdd	 Z fd
dZ	e
dd Z  ZS )CompleteDirsa8  
    A ZipFile subclass that ensures that implied directories
    are always included in the namelist.

    >>> list(CompleteDirs._implied_dirs(['foo/bar.txt', 'foo/bar/baz.txt']))
    ['foo/', 'foo/bar/']
    >>> list(CompleteDirs._implied_dirs(['foo/bar.txt', 'foo/bar/baz.txt', 'foo/bar/']))
    ['foo/']
    c             C   s.   t jtt| }dd |D }tt|| S )Nc             s   s   | ]}|t j V  qd S )N)r   r   ).0pr	   r	   r
   	<genexpr>e   s    z-CompleteDirs._implied_dirs.<locals>.<genexpr>)r   chainfrom_iterablemapr   _deduper   )namesparentsZas_dirsr	   r	   r
   _implied_dirsb   s    zCompleteDirs._implied_dirsc                s   t   }|t| | S )N)r   namelistlistr0   )r   r.   )r   r	   r
   r1   h   s    
zCompleteDirs.namelistc             C   s   t |  S )N)r   r1   )r   r	   r	   r
   	_name_setl   s    zCompleteDirs._name_setc             C   s,   |   }|d }||ko||k}|r(|S |S )zx
        If the name represents a directory, return that name
        as a directory (with the trailing slash).
        /)r3   )r   namer.   dirnameZ	dir_matchr	   r	   r
   resolve_diro   s    zCompleteDirs.resolve_dirc                sF   yt  |S  tk
r@   |dr2||  kr4 tj|dS X dS )z6
        Supplement getinfo for implied dirs.
        r4   )filenameN)r   getinfoKeyErrorendswithr3   zipfileZipInfo)r   r5   )r   r	   r
   r9   y   s    zCompleteDirs.getinfoc             C   s:   t |tr|S t |tjs"| |S d|jkr0t} | |_|S )zl
        Given a source (filename or zipfile), return an
        appropriate CompleteDirs subclass.
        r)
isinstancer&   r<   ZipFilemoder   )clssourcer	   r	   r
   make   s    

zCompleteDirs.make)r!   r"   r#   r$   staticmethodr0   r1   r3   r7   r9   classmethodrD   r%   r	   r	   )r   r
   r&   W   s   	
r&   c                   s,   e Zd ZdZ fddZ fddZ  ZS )
FastLookupzV
    ZipFile subclass to ensure implicit
    dirs exist and are resolved rapidly.
    c          	      s*   t t | jS Q R X t  | _| jS )N)
contextlibsuppressAttributeErrorZ_FastLookup__namesr   r1   )r   )r   r	   r
   r1      s    zFastLookup.namelistc          	      s*   t t | jS Q R X t  | _| jS )N)rH   rI   rJ   Z_FastLookup__lookupr   r3   )r   )r   r	   r
   r3      s    zFastLookup._name_set)r!   r"   r#   r$   r1   r3   r%   r	   r	   )r   r
   rG      s   rG   c             O   s   t | d||fS )N   )r   )encodingr   r   r	   r	   r
   _extract_text_encoding   s    rM   c               @   s  e Zd ZdZdZd=ddZdd Zdd	 Zd>ddddZe	dd Z
e	dd Ze	dd Ze	dd Z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d'd( Zd)d* Zd+d, Zd-d. Zd/d0 Zd1d2 Zd3d4 Zd5d6 Zd7d8 Zd9d: ZeZ e	d;d< Z!dS )?r   uD  
    A pathlib-compatible interface for zip files.

    Consider a zip file with this structure::

        .
        ├── a.txt
        └── b
            ├── c.txt
            └── d
                └── e.txt

    >>> data = io.BytesIO()
    >>> zf = zipfile.ZipFile(data, 'w')
    >>> zf.writestr('a.txt', 'content of a')
    >>> zf.writestr('b/c.txt', 'content of c')
    >>> zf.writestr('b/d/e.txt', 'content of e')
    >>> zf.filename = 'mem/abcde.zip'

    Path accepts the zipfile object itself or a filename

    >>> root = Path(zf)

    From there, several path operations are available.

    Directory iteration (including the zip file itself):

    >>> a, b = root.iterdir()
    >>> a
    Path('mem/abcde.zip', 'a.txt')
    >>> b
    Path('mem/abcde.zip', 'b/')

    name property:

    >>> b.name
    'b'

    join with divide operator:

    >>> c = b / 'c.txt'
    >>> c
    Path('mem/abcde.zip', 'b/c.txt')
    >>> c.name
    'c.txt'

    Read text:

    >>> c.read_text(encoding='utf-8')
    'content of c'

    existence:

    >>> c.exists()
    True
    >>> (b / 'missing.txt').exists()
    False

    Coercion to string:

    >>> import os
    >>> str(c).replace(os.sep, posixpath.sep)
    'mem/abcde.zip/b/c.txt'

    At the root, ``name``, ``filename``, and ``parent``
    resolve to the zipfile. Note these attributes are not
    valid and will raise a ``ValueError`` if the zipfile
    has no filename.

    >>> root.name
    'abcde.zip'
    >>> str(root.filename).replace(os.sep, posixpath.sep)
    'mem/abcde.zip'
    >>> str(root.parent)
    'mem'
    z>{self.__class__.__name__}({self.root.filename!r}, {self.at!r}) c             C   s   t || _|| _dS )aX  
        Construct a Path from a ZipFile or filename.

        Note: When the source is an existing ZipFile object,
        its type (__class__) will be mutated to a
        specialized type. If the caller wishes to retain the
        original type, the caller should either create a
        separate ZipFile object or pass a filename.
        N)rG   rD   rootat)r   rO   rP   r	   r	   r
   r      s    
zPath.__init__c             C   s(   | j |j k	rtS | j| jf|j|jfkS )zU
        >>> Path(zipfile.ZipFile(io.BytesIO(), 'w')) == 'foo'
        False
        )r   NotImplementedrO   rP   )r   otherr	   r	   r
   __eq__  s    zPath.__eq__c             C   s   t | j| jfS )N)hashrO   rP   )r   r	   r	   r
   __hash__  s    zPath.__hash__r>   N)pwdc            O   s   |   rt| |d }|  s0|dkr0t| | jj| j||d}d|kr`|sT|r\td|S t||\}}}t	j
||f||S )z
        Open this entry as text or binary following the semantics
        of ``pathlib.Path.open()`` by passing arguments through
        to io.TextIOWrapper().
        r   r>   )rV   bz*encoding args invalid for binary operation)is_dirIsADirectoryErrorexistsFileNotFoundErrorrO   openrP   
ValueErrorrM   ioTextIOWrapper)r   rA   rV   r   r   Zzip_modestreamrL   r	   r	   r
   r\     s    z	Path.openc             C   s   t | jjp| jjS )N)pathlibr   rP   r5   r8   )r   r	   r	   r
   r5   -  s    z	Path.namec             C   s   t | jjp| jjS )N)ra   r   rP   suffixr8   )r   r	   r	   r
   rb   1  s    zPath.suffixc             C   s   t | jjp| jjS )N)ra   r   rP   suffixesr8   )r   r	   r	   r
   rc   5  s    zPath.suffixesc             C   s   t | jjp| jjS )N)ra   r   rP   stemr8   )r   r	   r	   r
   rd   9  s    z	Path.stemc             C   s   t | jj| jS )N)ra   r   rO   r8   joinpathrP   )r   r	   r	   r
   r8   =  s    zPath.filenamec          	   O   s8   t ||\}}}| jd|f||
}| S Q R X d S )Nr>   )rM   r\   read)r   r   r   rL   strmr	   r	   r
   	read_textA  s    zPath.read_textc          	   C   s   |  d
}| S Q R X d S )Nrb)r\   rf   )r   rg   r	   r	   r
   
read_bytesF  s    zPath.read_bytesc             C   s   t |jd| jdkS )Nr4   )r   r6   rP   r   )r   r   r	   r	   r
   	_is_childJ  s    zPath._is_childc             C   s   |  | j|S )N)r   rO   )r   rP   r	   r	   r
   _nextM  s    z
Path._nextc             C   s   | j  p| j dS )Nr4   )rP   r;   )r   r	   r	   r
   rX   P  s    zPath.is_dirc             C   s   |   o|   S )N)rZ   rX   )r   r	   r	   r
   is_fileS  s    zPath.is_filec             C   s   | j | j kS )N)rP   rO   r3   )r   r	   r	   r
   rZ   V  s    zPath.existsc             C   s.   |   stdt| j| j }t| j|S )NzCan't listdir a file)rX   r]   r,   rl   rO   r1   filterrk   )r   subsr	   r	   r
   iterdirY  s    zPath.iterdirc             C   s   t | j|S )N)ra   r   rP   match)r   Zpath_patternr	   r	   r
   rq   _  s    z
Path.matchc             C   s   dS )z]
        Return whether this path is a symlink. Always false (python/cpython#82102).
        Fr	   )r   r	   r	   r
   
is_symlinkb  s    zPath.is_symlinkc             c   s2   x,|   D ] }|V  | r
| E d H  q
W d S )N)rp   rX   _descendants)r   childr	   r	   r
   rs   h  s    zPath._descendantsc                s<   |st d|tt|j  fdd D S )NzUnacceptable pattern: c             3   s$   | ]} t |r|V  qd S )N)strrelative_to)r'   rt   )matchesr   r	   r
   r)   t  s   zPath.glob.<locals>.<genexpr>)r]   recompilefnmatch	translate	fullmatchrs   )r   patternr	   )rw   r   r
   globn  s
    z	Path.globc             C   s   |  d| S )Nz**/)r~   )r   r}   r	   r	   r
   rgloby  s    z
Path.rglobc             G   s   t t| t|j| S )N)r   relpathru   re   )r   rR   extrar	   r	   r
   rv   |  s    zPath.relative_toc             C   s   t | jj| jS )N)r   joinrO   r8   rP   )r   r	   r	   r
   __str__  s    zPath.__str__c             C   s   | j j| dS )N)r   )_Path__reprformat)r   r	   r	   r
   __repr__  s    zPath.__repr__c             G   s$   t j| jf| }| | j|S )N)r   r   rP   rl   rO   r7   )r   rR   nextr	   r	   r
   re     s    zPath.joinpathc             C   s6   | j s| jjS t| j d}|r,|d7 }| |S )Nr4   )rP   r8   parentr   r6   r   rl   )r   Z	parent_atr	   r	   r
   r     s    zPath.parent)rN   )r>   )"r!   r"   r#   r$   r   r   rS   rU   r\   propertyr5   rb   rc   rd   r8   rh   rj   rk   rl   rX   rm   rZ   rp   rq   rr   rs   r~   r   rv   r   r   re   __truediv__r   r	   r	   r	   r
   r      s<   L
	)N)r^   r   r<   r   rH   ra   rx   rz   Zpy310compatr   __all__r   r   dictfromkeysr-   r   r   r@   r&   rG   rM   r   r	   r	   r	   r
   <module>   s$   A
