o
    l>j-                     @   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	m
Z
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   )none_as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   B/home/thesage/.local/lib/python3.10/site-packages/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   _differenceL   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   Y      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__`   s   zInitializedState.__setstate__)	__name__
__module____qualname____doc__r   r   r(   r+   __classcell__r   r   r#   r   r   T   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>s   s    z-CompleteDirs._implied_dirs.<locals>.<genexpr>)r	   chainfrom_iterablemapr   _deduper   )namesparentsas_dirsr   r   r   _implied_dirsp   s   zCompleteDirs._implied_dirsc                    s   t   }|t| | S r   )r   namelistlistr<   )r    r9   r#   r   r   r=   v   s   
zCompleteDirs.namelistc                 C   s   t |  S r   )r   r=   r'   r   r   r   	_name_setz   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    namer9   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    rA   r#   r   r   rF      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)
isinstancer1   rI   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)rO   rR   rA   r   r   r   inject   s   zCompleteDirs.inject)r,   r-   r.   r/   staticmethodr<   r=   r?   rD   rF   classmethodrQ   rI   rM   rV   r0   r   r   r#   r   r1   e   s    



 r1   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   r[         
zFastLookup._namelistc                 C   rZ   r   )_name_set_propr'   r   r   r   r?      r\   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_propertyr[   r?   r_   r0   r   r   r#   r   rY      s    rY   c                 O   s   d}t | |||fS )N   r   )encodingr!   r"   stack_levelr   r   r   _extract_text_encoding   s   re   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)rY   rQ   rootat)r    rg   rh   r   r   r   r   3  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$   NotImplementedrg   rh   )r    otherr   r   r   __eq__@  s   zPath.__eq__c                 C   s   t | j| jfS r   )hashrg   rh   r'   r   r   r   __hash__I  r)   zPath.__hash__rK   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   rK   rn   bz*encoding args invalid for binary operation)is_dirIsADirectoryErrorexistsFileNotFoundErrorrg   openrh   
ValueErrorre   ioTextIOWrapper)r    rN   ro   r!   r"   zip_modestreamrc   r   r   r   ru   L  s   z	Path.openc                 C   s   | j r	t| j S | jS r   )rh   pathlibPurePosixPathrE   r'   r   r   r   _base`  s   z
Path._basec                 C   
   |   jS r   )r}   rA   r'   r   r   r   rA   c  r^   z	Path.namec                 C   r~   r   )r}   suffixr'   r   r   r   r   g  r^   zPath.suffixc                 C   r~   r   )r}   suffixesr'   r   r   r   r   k  r^   zPath.suffixesc                 C   r~   r   )r}   stemr'   r   r   r   r   o  r^   z	Path.stemc                 C   s   t | jj| jS r   )r{   r   rg   rE   joinpathrh   r'   r   r   r   rE   s  r%   zPath.filenamec                 O   sZ   t |i |\}}}| jd|g|R i |}| W  d    S 1 s&w   Y  d S )NrK   )re   ru   read)r    r!   r"   rc   strmr   r   r   	read_textw  s   $zPath.read_textc                 C   s6   |  d}| W  d    S 1 sw   Y  d S )Nrb)ru   r   )r    r   r   r   r   
read_bytes|  s   $zPath.read_bytesc                 C   s   t |jd| jdkS Nr@   )r   rB   rh   r   )r    r   r   r   r   	_is_child  s   zPath._is_childc                 C   s   |  | j|S r   )r$   rg   )r    rh   r   r   r   _next     z
Path._nextc                 C   s   | j  p	| j dS r   )rh   rH   r'   r   r   r   rq     s   zPath.is_dirc                 C   s   |   o|   S r   )rs   rq   r'   r   r   r   is_file     zPath.is_filec                 C   s   | j | j v S r   )rh   rg   r?   r'   r   r   r   rs     r)   zPath.existsc                 C   s.   |   stdt| j| j }t| j|S )NzCan't listdir a file)rq   NotADirectoryErrorr7   r   rg   r=   filterr   )r    subsr   r   r   iterdir  s   zPath.iterdirc                 C   s   t | j|S r   )r{   r|   rh   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.
           )rg   rF   rh   external_attrstatS_ISLNK)r    inforN   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)rv   reescaperh   r   compile	translate	fullmatchr7   r   r   rg   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    rj   extrar   r   r   relative_to  s   zPath.relative_toc                 C   s&   t | jjd}| jrt|| jS |S )Nz	:zipfile:)r   rg   rE   rh   r   join)r    rg   r   r   r   __str__  s   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   rh   r   rg   rD   )r    rj   nextr   r   r   r     s   zPath.joinpathc                 C   s6   | j s| jjS t| j d}|r|d7 }| |S r   )rh   rE   parentr   rB   r   r   )r    	parent_atr   r   r   r     s   
zPath.parent)rf   )rK   )"r,   r-   r.   r/   r   r   rk   rm   ru   r}   propertyrA   r   r   r   rE   r   r   r   r   rq   r   rs   r   r   r   r   r   r   r   r   r   __truediv__r   r   r   r   r   r      sJ    f
	




	r   )r/   r`   rw   r	   r{   r   r   r   rI   
_functoolsr   r   compat.py310r   r   r   __all__r   r   dictfromkeysr8   r   r   rM   r1   rY   re   r   r   r   r   r   <module>   s.    	K
