a
    õ£Beœ+  ã                   @   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m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)Ú	translateÚ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   úP/home/yuhw/wc/wire-cell-python/wcpy/lib/python3.9/site-packages/zipp/__init__.pyÚ_parents   s    r   c                 c   s4   |   tj¡} | r0| tjkr0| V  t | ¡\} }q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__)ÚminuendÚ
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|i |¤Ž 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 r   )r   r   ©r   r   r   r   Ú__getstate__O   s    zInitializedState.__getstate__c                    s   |\}}t ƒ j|i |¤Ž d S r   )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                       sl   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e
ejejdœ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 r   )r   r   )Ú.0Úpr   r   r   Ú	<genexpr>e   ó    z-CompleteDirs._implied_dirs.<locals>.<genexpr>)r   ÚchainÚfrom_iterableÚmapr   Ú_deduper   )ÚnamesÚparentsÚas_dirsr   r   r   Ú_implied_dirsb   s    zCompleteDirs._implied_dirsc                    s   t ƒ  ¡ }|t|  |¡ƒ S r   )r   ÚnamelistÚlistr9   )r   r6   r"   r   r   r:   h   s    
zCompleteDirs.namelistc                 C   s   t |  ¡ ƒS r   )r   r:   r$   r   r   r   Ú	_name_setl   s    zCompleteDirs._name_setc                 C   s,   |   ¡ }|d }||vo||v }|r(|S |S )zx
        If the name represents a directory, return that name
        as a directory (with the trailing slash).
        ú/)r<   )r   Únamer6   ÚdirnameÚ	dir_matchr   r   r   Úresolve_diro   s    zCompleteDirs.resolve_dirc                    sJ   zt ƒ  |¡W S  tyD   | d¡r2||  ¡ vr4‚ tj|d� Y S 0 dS )z6
        Supplement getinfo for implied dirs.
        r=   )ÚfilenameN)r   ÚgetinfoÚKeyErrorÚendswithr<   ÚzipfileÚZipInfo)r   r>   r"   r   r   rC   y   s    zCompleteDirs.getinfoc                 C   s:   t |tƒr|S t |tjƒs"| |ƒS d|jvr0t} | |_|S )zl
        Given a source (filename or zipfile), return an
        appropriate CompleteDirs subclass.
        Úr)Ú
isinstancer-   rF   ÚZipFileÚmoder#   )ÚclsÚsourcer   r   r   Úmake„   s    

zCompleteDirs.make)ÚzfÚreturnc                 C   s$   |   | ¡ ¡D ]}| |d¡ q|S )z‰
        Given a writable zip file zf, inject directory entries for
        any directories implied by the presence of children.
        r1   )r9   r:   Úwritestr)rL   rO   r>   r   r   r   Úinject—   s    zCompleteDirs.inject)r(   r)   r*   r+   Ústaticmethodr9   r:   r<   rA   rC   ÚclassmethodrN   rF   rJ   rR   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                    sB   t  t¡� | jW  d   ƒ S 1 s&0    Y  tƒ  ¡ | _| jS r   )Ú
contextlibÚsuppressÚAttributeErrorÚ_FastLookup__namesr   r:   r$   r"   r   r   r:   ¨   s    $zFastLookup.namelistc                    sB   t  t¡� | jW  d   ƒ S 1 s&0    Y  tƒ  ¡ | _| jS r   )rV   rW   rX   Ú_FastLookup__lookupr   r<   r$   r"   r   r   r<   ®   s    $zFastLookup._name_set)r(   r)   r*   r+   r:   r<   r,   r   r   r"   r   rU   ¢   s   rU   c                 O   s   t | dƒ||fS )Né   r   )Úencodingr    r!   r   r   r   Ú_extract_text_encodingµ   s    r]   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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/d0„ Zd1d2„ Zd3d4„ Zd5d6„ Zd7d8„ Zd9d:„ ZeZ e
d;d<„ ƒZ!dS )?r   u¹  
    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

    >>> path = Path(zf)

    From there, several path operations are available.

    Directory iteration (including the zip file itself):

    >>> a, b = path.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.

    >>> str(path)
    'mem/abcde.zip/'
    >>> path.name
    'abcde.zip'
    >>> path.filename == pathlib.Path('mem/abcde.zip')
    True
    >>> str(path.parent)
    'mem'

    If the zipfile has no filename, such attribtues are not
    valid and accessing them will raise an Exception.

    >>> zf.filename = None
    >>> path.name
    Traceback (most recent call last):
    ...
    TypeError: ...

    >>> path.filename
    Traceback (most recent call last):
    ...
    TypeError: ...

    >>> path.parent
    Traceback (most recent call last):
    ...
    TypeError: ...

    # workaround python/cpython#106763
    >>> pass
    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)rU   rN   ÚrootÚat)r   r_   r`   r   r   r   r      s    
zPath.__init__c                 C   s(   | j |j urtS | j| jf|j|jfkS )zU
        >>> Path(zipfile.ZipFile(io.BytesIO(), 'w')) == 'foo'
        False
        )r#   ÚNotImplementedr_   r`   )r   Úotherr   r   r   Ú__eq__-  s    zPath.__eq__c                 C   s   t | j| jfƒS r   )Úhashr_   r`   r$   r   r   r   Ú__hash__6  s    zPath.__hash__rH   N©Úpwdc                O   sŽ   |   ¡ rt| ƒ‚|d }|  ¡ s0|dkr0t| ƒ‚| jj| j||d�}d|v r`|sT|r\tdƒ‚|S t|i |¤Ž\}}}t	j
||g|¢R i |¤ŽS )zª
        Open this entry as text or binary following the semantics
        of ``pathlib.Path.open()`` by passing arguments through
        to io.TextIOWrapper().
        r   rH   rf   Úbz*encoding args invalid for binary operation)Úis_dirÚIsADirectoryErrorÚexistsÚFileNotFoundErrorr_   Úopenr`   Ú
ValueErrorr]   ÚioÚTextIOWrapper)r   rK   rg   r    r!   Úzip_modeÚstreamr\   r   r   r   rm   9  s    z	Path.openc                 C   s   t  | jp| jj¡S r   )ÚpathlibÚPurePosixPathr`   r_   rB   r$   r   r   r   Ú_baseM  s    z
Path._basec                 C   s
   |   ¡ jS r   )ru   r>   r$   r   r   r   r>   P  s    z	Path.namec                 C   s
   |   ¡ jS r   )ru   Úsuffixr$   r   r   r   rv   T  s    zPath.suffixc                 C   s
   |   ¡ jS r   )ru   Úsuffixesr$   r   r   r   rw   X  s    zPath.suffixesc                 C   s
   |   ¡ jS r   )ru   Ústemr$   r   r   r   rx   \  s    z	Path.stemc                 C   s   t  | jj¡ | j¡S r   )rs   r   r_   rB   Újoinpathr`   r$   r   r   r   rB   `  s    zPath.filenamec                 O   sZ   t |i |¤Ž\}}}| jd|g|¢R i |¤Ž�}| ¡ W  d   ƒ S 1 sL0    Y  d S )NrH   )r]   rm   Úread)r   r    r!   r\   Ústrmr   r   r   Ú	read_textd  s    zPath.read_textc                 C   s6   |   d¡�}| ¡ W  d   ƒ S 1 s(0    Y  d S )NÚrb)rm   rz   )r   r{   r   r   r   Ú
read_bytesi  s    zPath.read_bytesc                 C   s   t  |j d¡¡| j d¡kS ©Nr=   )r   r?   r`   r   )r   r
   r   r   r   Ú	_is_childm  s    zPath._is_childc                 C   s   |   | j|¡S r   )r#   r_   )r   r`   r   r   r   Ú_nextp  s    z
Path._nextc                 C   s   | j  p| j  d¡S r   )r`   rE   r$   r   r   r   ri   s  s    zPath.is_dirc                 C   s   |   ¡ o|  ¡  S r   )rk   ri   r$   r   r   r   Úis_filev  s    zPath.is_filec                 C   s   | j | j ¡ v S r   )r`   r_   r<   r$   r   r   r   rk   y  s    zPath.existsc                 C   s.   |   ¡ stdƒ‚t| j| j ¡ ƒ}t| j|ƒS )NzCan't listdir a file)ri   rn   r4   r�   r_   r:   Úfilterr€   )r   Úsubsr   r   r   Úiterdir|  s    zPath.iterdirc                 C   s   t  | j¡ |¡S r   )rs   rt   r`   Úmatch)r   Úpath_patternr   r   r   r†   ‚  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_symlink…  s    zPath.is_symlinkc                 C   sJ   |st d|›�ƒ‚t | j¡}t |t|ƒ ¡j}t| jt	|| j
 ¡ ƒƒS )NzUnacceptable pattern: )rn   ÚreÚescaper`   Úcompiler   Ú	fullmatchr4   r�   rƒ   r_   r:   )r   ÚpatternÚprefixÚmatchesr   r   r   Úglob‹  s
    z	Path.globc                 C   s   |   d|› �¡S )Nz**/)r�   )r   r�   r   r   r   Úrglob“  s    z
Path.rglobc                 G   s   t  t| ƒt|j|Ž ƒ¡S r   )r   ÚrelpathÚstrry   )r   rb   Úextrar   r   r   Úrelative_to–  s    zPath.relative_toc                 C   s   t  | jj| j¡S r   )r   Újoinr_   rB   r`   r$   r   r   r   Ú__str__™  s    zPath.__str__c                 C   s   | j j| d�S )Nr$   )Ú_Path__reprÚformatr$   r   r   r   Ú__repr__œ  s    zPath.__repr__c                 G   s&   t j| jg|¢R Ž }|  | j |¡¡S r   )r   r–   r`   r�   r_   rA   )r   rb   Únextr   r   r   ry   Ÿ  s    zPath.joinpathc                 C   s6   | j s| jjS t | j  d¡¡}|r,|d7 }|  |¡S r   )r`   rB   Úparentr   r?   r   r�   )r   Ú	parent_atr   r   r   rœ   ¥  s    zPath.parent)r^   )rH   )"r(   r)   r*   r+   r˜   r   rc   re   rm   ru   Úpropertyr>   rv   rw   rx   rB   r|   r~   r€   r�   ri   r‚   rk   r…   r†   rˆ   r�   r‘   r•   r—   rš   ry   Ú__truediv__rœ   r   r   r   r   r   º   sH   c
	




)N)ro   r   rF   r   rV   rs   r‰   Zpy310compatr   r�   r   Ú__all__r   r	   ÚdictÚfromkeysr5   r   r   rJ   r-   rU   r]   r   r   r   r   r   Ú<module>   s$   K
