a
    Lfd4                     @   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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ZG dd deeejZG dd deZdddZG dd dZdS )    N   )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
   F/var/www/ai-form-bot/venv/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   L   s    zInitializedState.__init__c                 C   s   | j | jfS r   )r   r   r   r
   r
   r   __getstate__Q   s    zInitializedState.__getstate__c                    s   |\}}t  j|i | d S r   )r   r   )r   stater   r    r!   r
   r   __setstate__T   s    zInitializedState.__setstate__)__name__
__module____qualname____doc__r   r$   r&   __classcell__r
   r
   r!   r   r   G   s   r   c                       s,   e Zd ZdZ fddZedd Z  ZS )SanitizedNamesz7
    ZipFile mix-in to ensure names are sanitized.
    c                    s   t t| jt  S r   )listmap	_sanitizer   namelistr#   r!   r
   r   r0   ^   s    zSanitizedNames.namelistc                 C   s`   dd }t jdd| t jd}|dd}|d}dt||}|sNtd|d| d  S )	aa  
        Ensure a relative path with posix separators and no dot names.

        Modeled after
        https://github.com/python/cpython/blob/bcc1be39cb1d04ad9fc0bd1b9193d3972835a57c/Lib/zipfile/__init__.py#L1799-L1813
        but provides consistent cross-platform behavior.

        >>> san = SanitizedNames._sanitize
        >>> san('/foo/bar')
        'foo/bar'
        >>> san('//foo.txt')
        'foo.txt'
        >>> san('foo/.././bar.txt')
        'foo/bar.txt'
        >>> san('foo../.bar.txt')
        'foo../.bar.txt'
        >>> san('\\foo\\bar.txt')
        'foo/bar.txt'
        >>> san('D:\\foo.txt')
        'D/foo.txt'
        >>> san('\\\\server\\share\\file.txt')
        'server/share/file.txt'
        >>> san('\\\\?\\GLOBALROOT\\Volume3')
        '?/GLOBALROOT/Volume3'
        >>> san('\\\\.\\PhysicalDrive1\\root')
        'PhysicalDrive1/root'

        Retain any trailing slash.
        >>> san('abc/')
        'abc/'

        Raises a ValueError if the result is empty.
        >>> san('../..')
        Traceback (most recent call last):
        ...
        ValueError: Empty filename
        c                 S   s   | o
| dvS )N>   .z..r
   )partr
   r
   r   allowed   s    z)SanitizedNames._sanitize.<locals>.allowedz	^([A-Z]):z\1)flags\/zEmpty filename)	resub
IGNORECASEreplacer   joinfilter
ValueErrorendswith)namer3   barecleanpartsZjoinedr
   r
   r   r/   a   s    (
zSanitizedNames._sanitize)r'   r(   r)   r*   r0   staticmethodr/   r+   r
   r
   r!   r   r,   Y   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>       z-CompleteDirs._implied_dirs.<locals>.<genexpr>)r   chainfrom_iterabler.   r   _deduper   )namesparentsas_dirsr
   r
   r   _implied_dirs   s    zCompleteDirs._implied_dirsc                    s   t   }|t| | S r   )r   r0   r-   rO   )r   rL   r!   r
   r   r0      s    
zCompleteDirs.namelistc                 C   s   t |  S r   )r   r0   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).
        r6   )rP   )r   r?   rL   dirname	dir_matchr
   r
   r   resolve_dir   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.
        r6   filenameN)r   getinfoKeyErrorr>   rP   zipfileZipInfo)r   r?   r!   r
   r   rV      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)
isinstancerD   rX   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.
        rH   )rO   r0   writestr)r^   ra   r?   r
   r
   r   inject   s    zCompleteDirs.inject)r'   r(   r)   r*   rC   rO   r0   rP   rS   rV   classmethodr`   rX   r\   rd   r+   r
   r
   r!   r   rD      s   



rD   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   r0   r#   r!   r
   r   r0      s    $zFastLookup.namelistc                    sB   t t | jW  d    S 1 s&0    Y  t  | _| jS r   )rg   rh   ri   _FastLookup__lookupr   rP   r#   r!   r
   r   rP      s    $zFastLookup._name_set)r'   r(   r)   r*   r0   rP   r+   r
   r
   r!   r   rf      s   rf   c                 O   s$   t jjdk}d| }t| |||fS )Npypy   )sysimplementationr?   r   )encodingr   r    is_pypyZstack_levelr
   r
   r   _extract_text_encoding   s    rr   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)rf   r`   rootat)r   rt   ru   r
   r
   r   r   e  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"   NotImplementedrt   ru   )r   otherr
   r
   r   __eq__r  s    zPath.__eq__c                 C   s   t | j| jfS r   )hashrt   ru   r#   r
   r
   r   __hash__{  s    zPath.__hash__rZ   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   rZ   r{   bz*encoding args invalid for binary operation)is_dirIsADirectoryErrorexistsFileNotFoundErrorrt   openru   r=   rr   ioTextIOWrapper)r   r]   r|   r   r    zip_modestreamrp   r
   r
   r   r   ~  s    z	Path.openc                 C   s   t | jp| jjS r   )pathlibPurePosixPathru   rt   rU   r#   r
   r
   r   _base  s    z
Path._basec                 C   s
   |   jS r   )r   r?   r#   r
   r
   r   r?     s    z	Path.namec                 C   s
   |   jS r   )r   suffixr#   r
   r
   r   r     s    zPath.suffixc                 C   s
   |   jS r   )r   suffixesr#   r
   r
   r   r     s    zPath.suffixesc                 C   s
   |   jS r   )r   stemr#   r
   r
   r   r     s    z	Path.stemc                 C   s   t | jj| jS r   )r   r   rt   rU   joinpathru   r#   r
   r
   r   rU     s    zPath.filenamec                 O   sZ   t |i |\}}}| jd|g|R i |}| W  d    S 1 sL0    Y  d S )NrZ   )rr   r   read)r   r   r    rp   strmr
   r
   r   	read_text  s    zPath.read_textc                 C   s6   |  d}| W  d    S 1 s(0    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 Nr6   )r   rQ   ru   r   )r   r	   r
   r
   r   	_is_child  s    zPath._is_childc                 C   s   |  | j|S r   )r"   rt   )r   ru   r
   r
   r   _next  s    z
Path._nextc                 C   s   | j  p| j dS r   )ru   r>   r#   r
   r
   r   r~     s    zPath.is_dirc                 C   s   |   o|   S r   )r   r~   r#   r
   r
   r   is_file  s    zPath.is_filec                 C   s   | j | j v S r   )ru   rt   rP   r#   r
   r
   r   r     s    zPath.existsc                 C   s.   |   stdt| j| j }t| j|S )NzCan't listdir a file)r~   r=   r.   r   rt   r0   r<   r   )r   subsr
   r
   r   iterdir  s    zPath.iterdirc                 C   s   t | j|S r   )r   r   ru   match)r   path_patternr
   r
   r   r     s    z
Path.matchc                 C   s"   | j | j}|jd? }t|S )z8
        Return whether this path is a symlink.
           )rt   rV   ru   external_attrstatS_ISLNK)r   infor]   r
   r
   r   
is_symlink  s    
zPath.is_symlinkc                 C   sb   |st d|t| j}tdd}t||| j}dd | jj	D }t
| jt||S )NzUnacceptable pattern: r6   )sepsc                 s   s   | ]}|j V  qd S r   rT   )rE   datar
   r
   r   rG     rH   zPath.glob.<locals>.<genexpr>)r=   r7   escaperu   r   compile	translate	fullmatchrt   filelistr.   r   r<   )r   patternprefixtrmatchesrL   r
   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strr   )r   rw   extrar
   r
   r   relative_to  s    zPath.relative_toc                 C   s   t | jj| jS r   )r   r;   rt   rU   ru   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;   ru   r   rt   rS   )r   rw   nextr
   r
   r   r     s    zPath.joinpathc                 C   s6   | j s| jjS t| j d}|r,|d7 }| |S r   )ru   rU   parentr   rQ   r   r   )r   	parent_atr
   r
   r   r     s    zPath.parent)rs   )rZ   )"r'   r(   r)   r*   r   r   rx   rz   r   r   propertyr?   r   r   r   rU   r   r   r   r   r~   r   r   r   r   r   r   r   r   r   r   r   __truediv__r   r
   r
   r
   r   r      sH   f
	





)N)r   r   rX   r   rg   r   r7   r   rn   Zcompat.py310r   r   r   __all__r   r   dictfromkeysrK   r   r   r,   r\   rD   rf   rr   r   r
   r
   r
   r   <module>   s*   >K
