U
    P|jü/  ã                   @  s*  d Z ddlmZ ddlZddlZddlZddlmZmZm	Z	m
Z
mZmZmZmZmZmZmZ ddlmZ ddlmZ ddlmZmZ ddlmZmZ dd	lmZmZmZm Z m!Z!m"Z" d
Z#ede$e%ƒZ&ede$e%ƒZ'eddd�Z(eeeƒG dd„ dee'e&f ee' ƒƒƒZ)G dd„ de)e
e
f ƒZ*ddgZ+dS )zt
This module contains implementations of L{IFilePath} for zip files.

See the constructor of L{ZipArchive} for use.
é    )ÚannotationsN)ÚIOÚTYPE_CHECKINGÚAnyÚAnyStrÚDictÚGenericÚIterableÚListÚTupleÚTypeVarÚUnion)ÚZipFile)Úimplementer)ÚLiteralÚSelf)ÚcmpÚ
comparable)ÚAbstractFilePathÚFilePathÚ	IFilePathÚOtherAnyStrÚUnlistableErrorÚ_coerceToFilesystemEncodingú/Ú_ArchiveStrÚ_ZipStrÚ_ZipSelfzZipPath[Any, Any])Úboundc                   @  sr  e Zd ZU dZded< ddddœdd„Zd	d
dœdd„Zddœdd„Zeddœdd„ƒZ	ddœdd„Z
ddœdd„Zer†ddœdd„Zdddœd d!„Zdddœd"d#„Zd$dœd%d&„Zd$dœd'd(„Zd$dœd)d*„Zd$dœd+d,„Zd-dœd.d/„Zd0dœd1d2„Zddœd3d4„Zddœd5d6„ZdId8d9d:œd;d<„Zddœd=d>„Zd
dœd?d@„ZdAdœdBdC„ZdAdœdDdE„ZdAdœdFdG„ZdHS )JÚZipPathzF
    I represent a file or directory contained within a zip file.
    r   ÚpathzZipArchive[_ArchiveStr]ÚNone)ÚarchiveÚpathInArchiveÚreturnc                 C  sX   || _ || _t|j|ƒ| _t|tƒ}t||jƒ}| j |¡}tjj	|f|žŽ }|| _dS )z½
        Don't construct me directly.  Use C{ZipArchive.child()}.

        @param archive: a L{ZipArchive} instance.

        @param pathInArchive: a ZIP_PATH_SEP-separated string.
        N)
r"   r#   r   Ú_zipfileFilenameZ_nativePathInArchiveÚZIP_PATH_SEPÚsplitÚosr    Újoin)Úselfr"   r#   ÚsepZarchiveFilenameÚsegmentsZfakePath© r-   ú:/tmp/pip-unpacked-wheel-audpp6lv/twisted/python/zippath.pyÚ__init__=   s    
 ÿ
 ÿzZipPath.__init__ÚobjectÚint©Úotherr$   c                 C  s(   t |tƒstS t| j| jf|j|jfƒS ©N)Ú
isinstancer   ÚNotImplementedr   r"   r#   ©r*   r3   r-   r-   r.   Ú__cmp__W   s    

 
ÿzZipPath.__cmp__Ústr©r$   c                 C  sN   t | jtj | jj¡ƒg}| | j | j¡¡ t | jtjƒ}d| 	|¡›d�S )NzZipPath(ú))
r   r+   r(   r    Úabspathr"   Úextendr#   r'   r)   )r*   ÚpartsZossepr-   r-   r.   Ú__repr__^   s
    ÿzZipPath.__repr__c                 C  s   t | jtƒS )z–
        Return a zip directory separator.

        @return: The zip directory separator.
        @returntype: The same type as C{self.path}.
        )r   r    r&   ©r*   r-   r-   r.   r+   g   s    zZipPath.sepz=Union[ZipPath[_ZipStr, _ArchiveStr], ZipArchive[_ArchiveStr]]c                 C  s<   | j  | j¡}t|ƒdkr | jS t| j| j |dd… ¡ƒS )zk
        Return parent, discarding our own encoding in favor of whatever the
        archive's is.
        é   Néÿÿÿÿ)r#   r'   r+   Úlenr"   r   r)   )r*   Zsplitupr-   r-   r.   Ú_nativeParentq   s    zZipPath._nativeParentz9Union[ZipPath[_ZipStr, _ArchiveStr], ZipArchive[_ZipStr]]c                 C  s*   |   ¡ }t|tƒr&tt| j| jjƒƒS |S r4   )rD   r5   Ú
ZipArchiver   r    r"   r%   )r*   Úparentr-   r-   r.   rF   }   s    
ÿzZipPath.parentzCIterable[Union[ZipPath[_ZipStr, _ArchiveStr], ZipArchive[_ZipStr]]]c                 C  s   d S r4   r-   r@   r-   r-   r.   Úparents‡   s    zZipPath.parentsr   z!ZipPath[OtherAnyStr, _ArchiveStr]©r    r$   c                 C  s,   t |tƒ}t || jƒ}t| j| ||g¡ƒS )a&  
        Return a new ZipPath representing a path in C{self.archive} which is
        a child of this path.

        @note: Requesting the C{".."} (or other special name) child will not
            cause L{InsecurePath} to be raised since these names do not have
            any special meaning inside a zip archive.  Be particularly
            careful with the C{path} attribute (if you absolutely must use
            it) as this means it may include special names with special
            meaning outside of the context of a zip archive.
        )r   r&   r#   r   r"   r)   )r*   r    Újoinerr#   r-   r-   r.   ÚchildŒ   s    
zZipPath.childc                 C  s*   |   ¡ }t|tƒr| jn|}| |¡}|S r4   )rF   r5   rE   r"   rJ   )r*   r    rF   ZrightTypedParentrJ   r-   r-   r.   Úsiblingœ   s    
zZipPath.siblingÚboolc                 C  s   |   ¡ p|  ¡ S r4   )ÚisdirÚisfiler@   r-   r-   r.   Úexists¥   s    zZipPath.existsc                 C  s   | j | jjkS r4   )r#   r"   Úchildmapr@   r-   r-   r.   rM   ¨   s    zZipPath.isdirc                 C  s   | j | jjjkS r4   )r#   r"   ÚzipfileÚ
NameToInfor@   r-   r-   r.   rN   «   s    zZipPath.isfilec                 C  s   dS )NFr-   r@   r-   r-   r.   Úislink®   s    zZipPath.islinkzList[_ZipStr]c                   sd   ˆ   ¡ rPˆ  ¡ r>tˆ jjˆ jƒ}‡ fdd„ˆ jj|  ¡ D ƒS tt	t
jdƒƒ‚ntt	t
jdƒƒ‚d S )Nc                   s   g | ]}t ˆ j|ƒ‘qS r-   )r   r    )Ú.0Zeachr@   r-   r.   Ú
<listcomp>·   s   ÿz#ZipPath.listdir.<locals>.<listcomp>zLeaf zip entry listedzNon-existent zip entry listed)rO   rM   r   r"   r%   r#   rP   Úkeysr   ÚOSErrorÚerrnoÚENOTDIRÚENOENT)r*   ZparentArchivePathr-   r@   r.   Úlistdir±   s     ÿ
þ
ÿzZipPath.listdirzTuple[_ZipStr, _ZipStr]c                 C  s   t j | j¡S )zQ
        Return a value similar to that returned by C{os.path.splitext}.
        )r(   r    Úsplitextr@   r-   r-   r.   r\   Â   s    zZipPath.splitextc                 C  s   | j  | j¡d S )NrB   )r#   r'   r+   r@   r-   r-   r.   ÚbasenameË   s    zZipPath.basenamec                 C  s
   |   ¡ jS r4   )rF   r    r@   r-   r-   r.   ÚdirnameÎ   s    zZipPath.dirnameÚrzLiteral[('r', 'w')]z	IO[bytes])Úmoder$   c                 C  s   t d| jƒ}| jjj||d�S )NÚ )r`   )r   r#   r"   rQ   Úopen)r*   r`   r#   r-   r-   r.   rb   Ó   s    zZipPath.openc                 C  s   d S r4   r-   r@   r-   r-   r.   ÚchangedÚ   s    zZipPath.changedc                 C  s   t d| jƒ}| jjj| jS )zR
        Retrieve this file's size.

        @return: file size, in bytes
        ra   )r   r#   r"   rQ   rR   Ú	file_size©r*   r#   r-   r-   r.   ÚgetsizeÝ   s    zZipPath.getsizeÚfloatc                 C  s
   | j  ¡ S )z±
        Retrieve this file's last access-time.  This is the same as the last access
        time for the archive.

        @return: a number of seconds since the epoch
        )r"   ÚgetAccessTimer@   r-   r-   r.   rh   æ   s    zZipPath.getAccessTimec                 C  s&   t d| jƒ}t | jjj| jd ¡S )z¸
        Retrieve this file's last modification time.  This is the time of
        modification recorded in the zipfile.

        @return: a number of seconds since the epoch.
        ra   )r   r   r   )r   r#   ÚtimeÚmktimer"   rQ   rR   Ú	date_timere   r-   r-   r.   ÚgetModificationTimeï   s    ÿzZipPath.getModificationTimec                 C  s   |   ¡ S )zÏ
        Retrieve this file's last modification time.  This name is provided for
        compatibility, and returns the same value as getmtime.

        @return: a number of seconds since the epoch.
        )rl   r@   r-   r-   r.   ÚgetStatusChangeTimeû   s    zZipPath.getStatusChangeTimeN)r_   )Ú__name__Ú
__module__Ú__qualname__Ú__doc__Ú__annotations__r/   r8   r?   Úpropertyr+   rD   rF   r   rG   rJ   rK   rO   rM   rN   rS   r[   r\   r]   r^   rb   rc   rf   rh   rl   rm   r-   r-   r-   r.   r   4   s6   
						r   c                   @  s¢   e Zd ZU dZded< eddœdd„ƒZddd	œd
d„Zdddœdd„Zdddœdd„Z	ddœdd„Z
ddœdd„Zddœdd„Zddœdd„Zd dœd!d"„Zd#S )$rE   aì  
    I am a L{FilePath}-like object which can wrap a zip archive as if it were a
    directory.

    It works similarly to L{FilePath} in L{bytes} and L{unicode} handling --
    instantiating with a L{bytes} will return a "bytes mode" L{ZipArchive},
    and instantiating with a L{unicode} will return a "text mode"
    L{ZipArchive}. Methods that return new L{ZipArchive} or L{ZipPath}
    instances will be in the mode of the argument to the creator method,
    converting if required.
    r   r%   r   r:   c                 C  s   | S r4   r-   r@   r-   r-   r.   r"     s    zZipArchive.archiver!   )ÚarchivePathnamer$   c                 C  sÒ   || _ ttd|ƒƒ| _| jj}|dk	s.tdƒ‚t||ƒ| _t|dƒ| _i | _| j 	¡ D ]v}t| j |ƒ 
| j¡}tt|ƒƒD ]F}||  }| j |d| … ¡}|| jkr²i | j|< d| j| |< qzt|dƒ}qVdS )zÍ
        Create a ZipArchive, treating the archive at archivePathname as a zip
        file.

        @param archivePathname: a L{bytes} or L{unicode}, naming a path in the
            filesystem.
        ra   Nz7zipfile must have filename when initialized with a pathrA   )r    r   r   rQ   ÚfilenameÚAssertionErrorr%   r#   rP   Únamelistr'   r+   ÚrangerC   r)   )r*   rt   ZzfnameÚnameZ	splitNameÚxrJ   rF   r-   r-   r.   r/     s&    ÿþ


zZipArchive.__init__r0   r1   r2   c                 C  s   t |tƒstS t| j|jƒS r4   )r5   rE   r6   r   r    r7   r-   r-   r.   r8   6  s    
zZipArchive.__cmp__r   zZipPath[OtherAnyStr, AnyStr]rH   c                 C  s
   t | |ƒS )zà
        Create a ZipPath pointing at a path within the archive.

        @param path: a L{bytes} or L{unicode} with no path separators in it
            (either '/' or the system path separator, if it's different).
        )r   )r*   r    r-   r-   r.   rJ   ;  s    zZipArchive.childrL   c                 C  s   t | jƒ ¡ S )zC
        Returns C{True} if the underlying archive exists.
        )r   r%   rO   r@   r-   r-   r.   rO   D  s    zZipArchive.existsrg   c                 C  s   t | jƒ ¡ S )z=
        Return the archive file's last access time.
        )r   r%   rh   r@   r-   r-   r.   rh   J  s    zZipArchive.getAccessTimec                 C  s   t | jƒ ¡ S )z>
        Return the archive file's modification time.
        )r   r%   rl   r@   r-   r-   r.   rl   P  s    zZipArchive.getModificationTimec                 C  s   t | jƒ ¡ S )z?
        Return the archive file's status change time.
        )r   r%   rm   r@   r-   r-   r.   rm   V  s    zZipArchive.getStatusChangeTimer9   c                 C  s   dt j | j¡›d�S )NzZipArchive(r;   )r(   r    r<   r@   r-   r-   r.   r?   \  s    zZipArchive.__repr__N)rn   ro   rp   rq   rr   rs   r"   r/   r8   rJ   rO   rh   rl   rm   r?   r-   r-   r-   r.   rE     s   
	rE   ),rq   Ú
__future__r   rX   r(   ri   Útypingr   r   r   r   r   r   r	   r
   r   r   r   rQ   r   Zzope.interfacer   Ztyping_extensionsr   r   Ztwisted.python.compatr   r   Ztwisted.python.filepathr   r   r   r   r   r   r&   Úbytesr9   r   r   r   r   rE   Ú__all__r-   r-   r-   r.   Ú<module>   s(   4 	" P[