U
    
W[$                     @   s   d Z ddlmZmZ ddlZddlZddlZddlmZ ddl	m
Z
mZ ddlmZmZmZ ddlmZ ddlmZ dd	lmZ d
Ze
eeG dd deZG dd de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.
    )absolute_importdivisionN)ZipFile)
comparablecmp)	IFilePathFilePathAbstractFilePath)_coerceToFilesystemEncoding)UnlistableError)implementer/c                   @   s   e Zd ZdZdd Zdd Z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d.d!d"Zd#d$ Zd%d& Zd'd( Zd)d* Zd+d, Zd-S )/ZipPathzF
    I represent a file or directory contained within a zip file.
    c                 C   sD   || _ || _t|t}t||jj}tjj|f| j	| | _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)
archivepathInArchiver
   ZIP_PATH_SEPzipfilefilenameospathjoinsplit)selfr   r   sepZarchiveFilename r   8/usr/lib/python3/dist-packages/twisted/python/zippath.py__init__%   s    
 

zZipPath.__init__c                 C   s(   t |tstS t| j| jf|j|jfS N)
isinstancer   NotImplementedr   r   r   )r   otherr   r   r   __cmp__9   s
    

zZipPath.__cmp__c                 C   sL   t | jtj| jjg}|| j| j t | jtj}d|	|f S )NzZipPath(%r))
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   H   s    zZipPath.sepc                 C   s<   | j | j}t|dkr | jS t| j| j|d d S )N   )r   r   r   lenr   r   r   )r   Zsplitupr   r   r   parentS   s    zZipPath.parentc                 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   Zjoinerr   r   r   r   childZ   s    
zZipPath.childc                 C   s   |   |S r   )r*   r+   r   r   r   r   r   siblingk   s    zZipPath.siblingc                 C   s   |   p|  S r   )isdirisfiler&   r   r   r   existso   s    zZipPath.existsc                 C   s   | j | jjkS r   )r   r   childmapr&   r   r   r   r.   s   s    zZipPath.isdirc                 C   s   | j | jjjkS r   )r   r   r   
NameToInfor&   r   r   r   r/   w   s    zZipPath.isfilec                 C   s   dS )NFr   r&   r   r   r   islink{   s    zZipPath.islinkc                 C   sL   |   r8|  r&t| jj| j  S ttt	j
dnttt	jdd S )NzLeaf zip entry listedzNon-existent zip entry listed)r0   r.   listr   r1   r   keysr   OSErrorerrnoZENOTDIRZENOENTr&   r   r   r   listdir   s    

zZipPath.listdirc                 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   r9      s    zZipPath.splitextc                 C   s   | j | jd S )Nr(   )r   r   r   r&   r   r   r   basename   s    zZipPath.basenamec                 C   s
   |   jS r   )r*   r   r&   r   r   r   dirname   s    zZipPath.dirnamerc                 C   s   t d| j}| jjj||dS )N )mode)r
   r   r   r   open)r   r>   r   r   r   r   r?      s    zZipPath.openc                 C   s   d S r   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
        r=   )r
   r   r   r   r2   	file_sizer   r   r   r   r   getsize   s    zZipPath.getsizec                 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   rD      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.
        r=   )r   r   r   )r
   r   timemktimer   r   r2   Z	date_timerB   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.
        )rG   r&   r   r   r   getStatusChangeTime   s    zZipPath.getStatusChangeTimeN)r<   )__name__
__module____qualname____doc__r   r!   r%   propertyr   r*   r+   r-   r0   r.   r/   r3   r8   r9   r:   r;   r?   r@   rC   rD   rG   rH   r   r   r   r   r      s.   





r   c                   @   sT   e Zd Z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S )
ZipArchivea  
    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.
    c                 C   s   | S r   r   r&   r   r   r   <lambda>       zZipArchive.<lambda>c                 C   s   || _ ttd|| _t|d| _i | _| j D ]v}t| j || j}t	t
|D ]F}||  }| j|d|  }|| jkri | j|< d| j| |< qVt|d}q2d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.
        r=   Nr'   )r   r   r
   r   r   r1   Znamelistr   r   ranger)   r   )r   ZarchivePathnamenamexr+   r*   r   r   r   r      s    


zZipArchive.__init__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+      s    zZipArchive.childc                 C   s   t | jj S )zC
        Returns C{True} if the underlying archive exists.
        )r   r   r   r0   r&   r   r   r   r0     s    zZipArchive.existsc                 C   s   t | jj S )z=
        Return the archive file's last access time.
        )r   r   r   rD   r&   r   r   r   rD     s    zZipArchive.getAccessTimec                 C   s   t | jj S )z>
        Return the archive file's modification time.
        )r   r   r   rG   r&   r   r   r   rG     s    zZipArchive.getModificationTimec                 C   s   t | jj S )z?
        Return the archive file's status change time.
        )r   r   r   rH   r&   r   r   r   rH     s    zZipArchive.getStatusChangeTimec                 C   s   dt j| jf S )NzZipArchive(%r))r   r   r"   r&   r   r   r   r%   #  s    zZipArchive.__repr__N)rI   rJ   rK   rL   rM   r   r   r+   r0   rD   rG   rH   r%   r   r   r   r   rN      s   
rN   )rL   Z
__future__r   r   r   rE   r7   r   r   Ztwisted.python.compatr   r   Ztwisted.python.filepathr   r   r	   r
   r   Zzope.interfacer   r   r   rN   __all__r   r   r   r   <module>   s"    5S