o
    5‘ð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 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 )zÒ
A Path-like interface for zipfiles.

This codebase is shared between zipfile.Path in the stdlib
and zipp in PyPI. See
https://github.com/python/importlib_metadata/wiki/Development-Methodology
for more detail.
é    Né   )Úsave_method_args)Útext_encoding)Ú
TranslatorÚ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   ú_/root/parts/websockify/install/lib/python3.10/site-packages/setuptools/_vendor/zipp/__init__.pyÚ_parents   s   r   c                 c   sB   � |   tj¡} |   tj¡r| V  t | ¡\} }|   tj¡sdS dS )aÔ  
    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(''))
    []

    Multiple separators are treated like a single.

    >>> list(_ancestry('//b//d///f//'))
    ['//b//d///f', '//b//d', '//b']
    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   Ú_differenceM   s   r   c                       s8   e Zd ZdZe‡ 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©ÚsuperÚ__init__)ÚselfÚargsÚkwargs©Ú	__class__r   r   r   Z   ó   zInitializedState.__init__c                 C   s   | j j| j jfS r   )Ú_saved___init__r   r    ©r   r   r   r   Ú__getstate__^   ó   zInitializedState.__getstate__c                    s   |\}}t ƒ j|i |¤Ž d S r   r   )r   Ústater   r    r!   r   r   Ú__setstate__a   s   zInitializedState.__setstate__)	Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r   r&   r)   Ú__classcell__r   r   r!   r   r   U   s    r   c                       sn   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
dejdejf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>t   s   € z-CompleteDirs._implied_dirs.<locals>.<genexpr>)r   ÚchainÚfrom_iterableÚmapr   Ú_deduper   )ÚnamesÚparentsÚas_dirsr   r   r   Ú_implied_dirsq   s   zCompleteDirs._implied_dirsc                    s   t ƒ  ¡ }|t|  |¡ƒ S r   )r   ÚnamelistÚlistr:   )r   r7   r!   r   r   r;   w   s   
zCompleteDirs.namelistc                 C   s   t |  ¡ ƒS r   )r   r;   r%   r   r   r   Ú	_name_set{   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   Únamer7   ÚdirnameÚ	dir_matchr   r   r   Úresolve_dir~   s   zCompleteDirs.resolve_dirc                    sF   zt ƒ  |¡W S  ty"   | d¡r||  ¡ vr‚ tj|d� Y S w )z6
        Supplement getinfo for implied dirs.
        r>   )Úfilename)r   ÚgetinfoÚKeyErrorÚendswithr=   ÚzipfileÚZipInfo)r   r?   r!   r   r   rD   ˆ   s   ýzCompleteDirs.getinfoc                 C   s:   t |tƒr|S t |tjƒs| |ƒS d|jvrt} | |_|S )zl
        Given a source (filename or zipfile), return an
        appropriate CompleteDirs subclass.
        Úr)Ú
isinstancer/   rG   Ú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.
        ó    )r:   r;   Úwritestr)rM   rP   r?   r   r   r   Úinject¦   s   zCompleteDirs.inject)r*   r+   r,   r-   Ústaticmethodr:   r;   r=   rB   rD   ÚclassmethodrO   rG   rK   rT   r.   r   r   r!   r   r/   f   s    



 r/   c                       sH   e Zd ZdZdd„ Zej‡ fdd„ƒZdd„ Zej‡ fdd	„ƒZ	‡  Z
S )
Ú
FastLookupzV
    ZipFile subclass to ensure implicit
    dirs exist and are resolved rapidly.
    c                 C   ó   | j S r   )Ú	_namelistr%   r   r   r   r;   ·   ó   zFastLookup.namelistc                    ó
   t ƒ  ¡ S r   )r   r;   r%   r!   r   r   rY   º   ó   
zFastLookup._namelistc                 C   rX   r   )Ú_name_set_propr%   r   r   r   r=   ¾   rZ   zFastLookup._name_setc                    r[   r   )r   r=   r%   r!   r   r   r]   Á   r\   zFastLookup._name_set_prop)r*   r+   r,   r-   r;   Ú	functoolsÚcached_propertyrY   r=   r]   r.   r   r   r!   r   rW   ±   s    rW   c                 O   s2   t jjdk}|ot jdk }d| }t| |ƒ||fS )NÚpypy)é   é   é   rb   )ÚsysÚimplementationr?   Úpypy_version_infor   )Úencodingr   r    Úis_pypyÚis_old_pypiÚstack_levelr   r   r   Ú_extract_text_encodingÆ   s   rk   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 :class:`importlib.resources.abc.Traversable` interface for zip files.

    Implements many of the features users enjoy from
    :class:`pathlib.Path`.

    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 attributes 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)rW   rO   ÚrootÚat)r   rm   rn   r   r   r   r   9  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"   ÚNotImplementedrm   rn   )r   Úotherr   r   r   Ú__eq__F  s   zPath.__eq__c                 C   s   t | j| jfƒS r   )Úhashrm   rn   r%   r   r   r   Ú__hash__O  r'   zPath.__hash__rI   N©Úpwdc                O   sŽ   |   ¡ rt| ƒ‚|d }|dkr|  ¡ st| ƒ‚| jj| j||d�}d|v r0|s*|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   rI   rt   Úbz*encoding args invalid for binary operation)Úis_dirÚIsADirectoryErrorÚexistsÚFileNotFoundErrorrm   Úopenrn   Ú
ValueErrorrk   ÚioÚTextIOWrapper)r   rL   ru   r   r    Úzip_modeÚstreamrg   r   r   r   r{   R  s   z	Path.openc                 C   s   | j r	t | j ¡S | jS r   )rn   ÚpathlibÚPurePosixPathrC   r%   r   r   r   Ú_basef  s   z
Path._basec                 C   ó
   |   ¡ jS r   )rƒ   r?   r%   r   r   r   r?   i  r\   z	Path.namec                 C   r„   r   )rƒ   Úsuffixr%   r   r   r   r…   m  r\   zPath.suffixc                 C   r„   r   )rƒ   Úsuffixesr%   r   r   r   r†   q  r\   zPath.suffixesc                 C   r„   r   )rƒ   Ústemr%   r   r   r   r‡   u  r\   z	Path.stemc                 C   s   t  | jj¡ | j¡S r   )r�   r   rm   rC   Újoinpathrn   r%   r   r   r   rC   y  r#   zPath.filenamec                 O   sZ   t |i |¤Ž\}}}| jd|g|¢R i |¤Ž�}| ¡ W  d   ƒ S 1 s&w   Y  d S )NrI   )rk   r{   Úread)r   r   r    rg   Ústrmr   r   r   Ú	read_text}  s   $ÿzPath.read_textc                 C   s6   |   d¡�}| ¡ W  d   ƒ S 1 sw   Y  d S )NÚrb)r{   r‰   )r   rŠ   r   r   r   Ú
read_bytes‚  s   $ÿzPath.read_bytesc                 C   s   t  |j d¡¡| j d¡kS ©Nr>   )r   r@   rn   r   )r   r
   r   r   r   Ú	_is_child†  s   zPath._is_childc                 C   s   |   | j|¡S r   )r"   rm   )r   rn   r   r   r   Ú_next‰  ó   z
Path._nextc                 C   s   | j  p	| j  d¡S rŽ   )rn   rF   r%   r   r   r   rw   Œ  s   zPath.is_dirc                 C   s   |   ¡ o|  ¡  S r   )ry   rw   r%   r   r   r   Úis_file�  ó   zPath.is_filec                 C   s   | j | j ¡ v S r   )rn   rm   r=   r%   r   r   r   ry   ’  r'   zPath.existsc                 C   s.   |   ¡ stdƒ‚t| j| j ¡ ƒ}t| j|ƒS )NzCan't listdir a file)rw   r|   r5   r�   rm   r;   Úfilterr�   )r   Úsubsr   r   r   Úiterdir•  s   zPath.iterdirc                 C   s   t  | j¡ |¡S r   )r�   r‚   rn   Úmatch)r   Úpath_patternr   r   r   r—   ›  r“   z
Path.matchc                 C   s"   | j  | j¡}|jd? }t |¡S )z8
        Return whether this path is a symlink.
        é   )rm   rD   rn   Úexternal_attrÚstatÚS_ISLNK)r   ÚinforL   r   r   r   Ú
is_symlinkž  s   

zPath.is_symlinkc                 C   sV   |s	t d|›�ƒ‚t | j¡}tdd�}t || |¡ ¡j}t| j	t
|| j ¡ ƒƒS )NzUnacceptable pattern: r>   )Úseps)r|   ÚreÚescapern   r   ÚcompileÚ	translateÚ	fullmatchr5   r�   r”   rm   r;   )r   ÚpatternÚprefixÚtrÚmatchesr   r   r   Úglob¦  s   
z	Path.globc                 C   s   |   d|› �¡S )Nz**/)r©   )r   r¥   r   r   r   Úrglob¯  r'   z
Path.rglobc                 G   s   t  t| ƒt|j|Ž ƒ¡S r   )r   ÚrelpathÚstrrˆ   )r   rp   Úextrar   r   r   Úrelative_to²  s   zPath.relative_toc                 C   s   t  | jj| j¡S r   )r   Újoinrm   rC   rn   r%   r   r   r   Ú__str__µ  r“   zPath.__str__c                 C   s   | j j| d�S )Nr%   )Ú_Path__reprÚformatr%   r   r   r   Ú__repr__¸  r‘   zPath.__repr__c                 G   s&   t j| jg|¢R Ž }|  | j |¡¡S r   )r   r¯   rn   r�   rm   rB   )r   rp   Únextr   r   r   rˆ   »  s   zPath.joinpathc                 C   s6   | j s| jjS t | j  d¡¡}|r|d7 }|  |¡S rŽ   )rn   rC   Úparentr   r@   r   r�   )r   Ú	parent_atr   r   r   rµ   Á  s   
zPath.parent)rl   )rI   )"r*   r+   r,   r-   r±   r   rq   rs   r{   rƒ   Úpropertyr?   r…   r†   r‡   rC   r‹   r�   r�   r�   rw   r’   ry   r–   r—   rž   r©   rª   r®   r°   r³   rˆ   Ú__truediv__rµ   r   r   r   r   r   Ð   sJ    f
	




	r   )r-   r^   r}   r   r�   r   r    r›   rd   rG   Ú
_functoolsr   Úcompat.py310r   r©   r   Ú__all__r   r	   ÚdictÚfromkeysr6   r   r   rK   r/   rW   rk   r   r   r   r   r   Ú<module>   s0    	K

