o
    Ÿ�f  ã                   @   s€   d dl m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	e
Zdd„ Zdd„ ZG dd„ dƒZG d	d
„ d
ƒZdS )é    )ÚdivisionNc                 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(''))
    []
    é   N)Ú	itertoolsÚisliceÚ	_ancestry©Úpath© r	   ú&/usr/lib/python3/dist-packages/zipp.pyÚ_parents   s   r   c                 c   sN   � |   tj¡} | r!| tjkr%| V  t | ¡\} }| r#| tjksdS dS dS 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                       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 ©N)ÚlistÚmapÚ	_sanitizeÚsuperÚnamelist©Úself©Ú	__class__r	   r
   r   ?   s   zSanitizedNames.namelistc                 C   s`   dd„ }t jdd| t jd�}| dd¡}| d¡}d t||ƒ¡}|s'tdƒ‚|d|  d¡  S )	a]  
        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>   ú..Ú.r	   )Úpartr	   r	   r
   Úallowedf   ó   z)SanitizedNames._sanitize.<locals>.allowedz	^([A-Z]):z\1)Úflagsú\ú/zEmpty filename)	ÚreÚsubÚ
IGNORECASEÚreplacer   ÚjoinÚfilterÚ
ValueErrorÚendswith)Únamer   ÚbareÚcleanÚpartsÚjoinedr	   r	   r
   r   B   s   $
zSanitizedNames._sanitize)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   Ústaticmethodr   Ú__classcell__r	   r	   r   r
   r   :   s
    r   c                   @   sÜ   e Zd ZdZdZd,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ed$d%„ ƒZed&d'„ ƒZd(d)„ Zejd*k rleZd+S d+S )-ÚPathuš  
    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 = '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('abcde.zip', 'a.txt')
    >>> b
    Path('abcde.zip', 'b/')

    name property:

    >>> b.name
    'b'

    join with divide operator:

    >>> c = b / 'c.txt'
    >>> c
    Path('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:

    >>> str(c)
    'abcde.zip/b/c.txt'
    z>{self.__class__.__name__}({self.root.filename!r}, {self.at!r})Ú c                 C   s,   t |tjƒr|nt |  |¡¡| _|| _d S r   )Ú
isinstanceÚzipfileÚZipFileÚ_pathlib_compatÚrootÚat)r   r=   r>   r	   r	   r
   Ú__init__¶   s
   
ÿý
zPath.__init__c                 C   s&   z|   ¡ W S  ty   t| ƒ Y S w )zu
        For path-like objects, convert to a filename for compatibility
        on Python 3.6.1 and earlier.
        )Ú
__fspath__ÚAttributeErrorÚstrr   r	   r	   r
   r<   ¾   s
   
ÿzPath._pathlib_compatc                 C   ó   t  | jj| j¡S r   )Ú	functoolsÚpartialr=   Úopenr>   r   r	   r	   r
   rF   É   ó   z	Path.openc                 C   s   t  | j d¡¡S ©Nr#   )r   Úbasenamer>   r   r   r	   r	   r
   r,   Í   rG   z	Path.namec                 O   sH   |   ¡ �}tj|g|¢R i |¤Ž ¡ W  d   ƒ S 1 sw   Y  d S r   )rF   ÚioÚTextIOWrapperÚread)r   ÚargsÚkwargsÚstrmr	   r	   r
   Ú	read_textÑ   s   
$ÿzPath.read_textc                 C   s4   |   ¡ �}| ¡ W  d   ƒ S 1 sw   Y  d S r   )rF   rL   )r   rO   r	   r	   r
   Ú
read_bytesÕ   s   
$ÿzPath.read_bytesc                 C   s   t  |j d¡¡| j d¡kS rH   )r   Údirnamer>   r   )r   r   r	   r	   r
   Ú	_is_childÙ   s   zPath._is_childc                 C   s   t | j|ƒS r   )r7   r=   )r   r>   r	   r	   r
   Ú_nextÜ   r    z
Path._nextc                 C   s   | j  p	| j  d¡S rH   )r>   r+   r   r	   r	   r
   Úis_dirß   s   zPath.is_dirc                 C   s
   |   ¡  S r   )rU   r   r	   r	   r
   Úis_fileâ   s   
zPath.is_filec                 C   s   | j |  ¡ v S r   )r>   Ú_namesr   r	   r	   r
   Úexistså   ó   zPath.existsc                 C   s,   |   ¡ stdƒ‚t| j|  ¡ ƒ}t| j|ƒS )NzCan't listdir a file)rU   r*   r   rT   rW   r)   rS   )r   Úsubsr	   r	   r
   Úiterdirè   s   zPath.iterdirc                 C   rC   r   )r   r(   r=   Úfilenamer>   r   r	   r	   r
   Ú__str__î   s   zPath.__str__c                 C   s   | j j| d�S )Nr   )Ú_Path__reprÚformatr   r	   r	   r
   Ú__repr__ñ   rY   zPath.__repr__c                 C   sP   |   |¡}t | j|¡}t | j|d¡}|  ¡ }|  ||vr%||v r%|¡S |¡S )Nr8   )r<   r   r(   r>   rW   rT   )r   ÚaddÚnextÚnext_dirÚnamesr	   r	   r
   Újoinpathô   s
   
 zPath.joinpathc                    s   t  ‡ fdd„ˆ D ƒ¡S )Nc                 3   s2   � | ]}t |ƒD ]}|d  ˆ vr|d  V  qqdS )r#   N)r   )Ú.0r,   Úparent©rd   r	   r
   Ú	<genexpr>ÿ   s   € ýýüz%Path._implied_dirs.<locals>.<genexpr>)Úmore_itertoolsÚunique_everseenrh   r	   rh   r
   Ú_implied_dirsý   s   þzPath._implied_dirsc                 C   s   |t |  |¡ƒ S r   )r   rl   )Úclsrd   r	   r	   r
   Ú_add_implied_dirs  rG   zPath._add_implied_dirsc                 C   s(   t  | j d¡¡}|r|d7 }|  |¡S rH   )r   rR   r>   r   rT   )r   Ú	parent_atr	   r	   r
   rg   
  s   
zPath.parentc                 C   s   |   tttj| j ¡ ƒƒ¡S r   )rn   r   r   r   r   r=   r   r   r	   r	   r
   rW     s   zPath._names)é   N)r8   )r1   r2   r3   r4   r^   r?   r5   r<   ÚpropertyrF   r,   rP   rQ   rS   rT   rU   rV   rX   r[   r]   r`   re   Ú__truediv__rl   Úclassmethodrn   rg   rW   ÚsysÚversion_infoÚ__div__r	   r	   r	   r
   r7   s   s@    @








ÿr7   )Ú
__future__r   rJ   rt   r   r:   rD   r   r$   rj   ÚtypeÚ__metaclass__r   r   r   r7   r	   r	   r	   r
   Ú<module>   s   9