3
§Yðié   ã               @   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ejdk rPd dlm	Z	 ne
Z	dgZdd„ Zdd	„ Ze	jZd
d„ ZG dd„ dejƒZG dd„ deƒZdd„ ZG dd„ dƒZdS )é    Né   é   )ÚOrderedDictÚPathc             C   s   t j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(''))
    []
    é   N)Ú	itertoolsÚisliceÚ	_ancestry)Úpath© r   ú$/tmp/pip-build-8om4l6ph/zipp/zipp.pyÚ_parents   s    r   c             c   s8   | j tjƒ} x&| r2| tjkr2| V  tj| ƒ\} }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 j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                   sH   e Zd ZdZedd„ ƒZ‡ fdd„Zdd„ Zdd	„ Ze	d
d„ ƒZ
‡  ZS )ÚCompleteDirszk
    A ZipFile subclass that ensures that implied directories
    are always included in the namelist.
    c             C   s.   t j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>P   s    z-CompleteDirs._implied_dirs.<locals>.<genexpr>)r   ÚchainÚfrom_iterableÚmapr   Ú_deduper   )ÚnamesÚparentsZas_dirsr   r   r   Ú_implied_dirsM   s    zCompleteDirs._implied_dirsc                s    t t| ƒjƒ }|t| j|ƒƒ S )N)Úsuperr   ÚnamelistÚlistr!   )Úselfr   )Ú	__class__r   r   r#   S   s    zCompleteDirs.namelistc             C   s   t | jƒ ƒS )N)r   r#   )r%   r   r   r   Ú	_name_setW   s    zCompleteDirs._name_setc             C   s,   | j ƒ }|d }||ko||k}|r(|S |S )zx
        If the name represents a directory, return that name
        as a directory (with the trailing slash).
        ú/)r'   )r%   Únamer   ÚdirnameZ	dir_matchr   r   r   Úresolve_dirZ   s    zCompleteDirs.resolve_dirc             C   s>   t |tƒr|S t |tjƒs&| t|ƒƒS d|jkr4t} | |_|S )zl
        Given a source (filename or zipfile), return an
        appropriate CompleteDirs subclass.
        Úr)Ú
isinstancer   ÚzipfileÚZipFileÚ_pathlib_compatÚmoder&   )ÚclsÚsourcer   r   r   Úmaked   s    

zCompleteDirs.make)Ú__name__Ú
__module__Ú__qualname__Ú__doc__Ústaticmethodr!   r#   r'   r+   Úclassmethodr4   Ú__classcell__r   r   )r&   r   r   G   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 jtƒ� | jS Q R X tt| ƒjƒ | _| jS )N)Ú
contextlibÚsuppressÚAttributeErrorZ_FastLookup__namesr"   r<   r#   )r%   )r&   r   r   r#   ~   s    zFastLookup.namelistc          
      s.   t jtƒ� | jS Q R X tt| ƒjƒ | _| jS )N)r=   r>   r?   Z_FastLookup__lookupr"   r<   r'   )r%   )r&   r   r   r'   „   s    zFastLookup._name_set)r5   r6   r7   r8   r#   r'   r;   r   r   )r&   r   r<   x   s   r<   c             C   s&   y| j ƒ S  tk
r    t| ƒS X dS )zi
    For path-like objects, convert to a filename for compatibility
    on Python 3.6.1 and earlier.
    N)Ú
__fspath__r?   Ústr)r
   r   r   r   r0   ‹   s    r0   c               @   sÒ   e Zd ZdZdZd-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eZed+d,„ ƒZdS )/r   u4  
    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()
    '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 j|ƒ| _|| _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)r<   r4   ÚrootÚat)r%   rC   rD   r   r   r   Ú__init__æ   s    
zPath.__init__r,   N)Úpwdc            O   st   | j ƒ rt| ƒ‚|d }| jƒ  r2|dkr2t| ƒ‚| jj| j||d�}d|krb|sV|r^tdƒ‚|S 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,   )rF   Úbz*encoding args invalid for binary operation)
Úis_dirÚIsADirectoryErrorÚexistsÚFileNotFoundErrorrC   ÚopenrD   Ú
ValueErrorÚioÚTextIOWrapper)r%   r1   rF   ÚargsÚkwargsZzip_modeÚstreamr   r   r   rL   ó   s    z	Path.openc             C   s   t j| jƒjp| jjS )N)Úpathlibr   rD   r)   Úfilename)r%   r   r   r   r)     s    z	Path.namec             C   s   t j| jƒjp| jjS )N)rS   r   rD   ÚsuffixrT   )r%   r   r   r   rU   	  s    zPath.suffixc             C   s   t j| jƒjp| jjS )N)rS   r   rD   ÚsuffixesrT   )r%   r   r   r   rV     s    zPath.suffixesc             C   s   t j| jƒjp| jjS )N)rS   r   rD   ÚstemrT   )r%   r   r   r   rW     s    z	Path.stemc             C   s   t j| jjƒj| jƒS )N)rS   r   rC   rT   ÚjoinpathrD   )r%   r   r   r   rT     s    zPath.filenamec          	   O   s$   | j d|ž|Ž�
}|jƒ S Q R X d S )Nr,   )r,   )rL   Úread)r%   rP   rQ   Ústrmr   r   r   Ú	read_text  s    zPath.read_textc          	   C   s   | j dƒ�
}|jƒ S Q R X d S )NÚrb)rL   rY   )r%   rZ   r   r   r   Ú
read_bytes  s    zPath.read_bytesc             C   s   t j|jjdƒƒ| jjdƒkS )Nr(   )r   r*   rD   r   )r%   r
   r   r   r   Ú	_is_child!  s    zPath._is_childc             C   s   | j | j|ƒS )N)r&   rC   )r%   rD   r   r   r   Ú_next$  s    z
Path._nextc             C   s   | j  p| j jdƒS )Nr(   )rD   Úendswith)r%   r   r   r   rH   '  s    zPath.is_dirc             C   s   | j ƒ o| jƒ  S )N)rJ   rH   )r%   r   r   r   Úis_file*  s    zPath.is_filec             C   s   | j | jjƒ kS )N)rD   rC   r'   )r%   r   r   r   rJ   -  s    zPath.existsc             C   s.   | j ƒ stdƒ‚t| j| jjƒ ƒ}t| j|ƒS )NzCan't listdir a file)rH   rM   r   r_   rC   r#   Úfilterr^   )r%   Úsubsr   r   r   Úiterdir0  s    zPath.iterdirc             C   s   t j| jj| jƒS )N)r   ÚjoinrC   rT   rD   )r%   r   r   r   Ú__str__6  s    zPath.__str__c             C   s   | j j| d�S )N)r%   )Ú_Path__reprÚformat)r%   r   r   r   Ú__repr__9  s    zPath.__repr__c             G   s*   t j| jftt|ƒžŽ }| j| jj|ƒƒS )N)r   re   rD   r   r0   r_   rC   r+   )r%   ÚotherÚnextr   r   r   rX   <  s    zPath.joinpathc             C   s6   | j s| jjS tj| j jdƒƒ}|r,|d7 }| j|ƒS )Nr(   )rD   rT   Úparentr   r*   r   r_   )r%   Z	parent_atr   r   r   rl   B  s    zPath.parent)rB   )r,   )r5   r6   r7   r8   rg   rE   rL   Úpropertyr)   rU   rV   rW   rT   r[   r]   r^   r_   rH   ra   rJ   rd   rf   ri   rX   Ú__truediv__rl   r   r   r   r   r   –   s,   L
)r   r   )rN   r   r.   r   r=   ÚsysrS   Úversion_infoÚcollectionsr   ÚdictÚ__all__r   r	   Úfromkeysr   r   r/   r   r<   r0   r   r   r   r   r   Ú<module>   s$   
1