o
    Q `k;                     @   s  d Z ddlmZ ddlmZ ddlZddlZddlmZ ddlZddlm	Z	 ddl
mZ dd	lmZ dd
lmZmZ ddlmZ ddlmZ ddlmZ ddlmZ ddlmZ ddlmZmZmZmZ ddl m!Z! ddl"m#Z# ddl$m%Z% ej&rddlm'Z'm(Z(m)Z)m*Z*m+Z+m,Z,m-Z-m.Z.m/Z/m0Z0 ddlm1Z1 ddl2m3Z3 ej4dddZ5G dd deZ6G dd de#Z7ej8G dd de#Z9ej8G d d deZ:dS )!z(Manage the filesystem in a Zip archive.
    )print_function)unicode_literalsN)datetime   )errors)FS)	write_zip)ResourceTypeSeek)Info)
RawWrapper)Permissions)MemoryFS)open_fs)dirnameforcedirnormpathrelpath)datetime_to_epoch)WrapFS)	url_quote)
AnyBinaryIO
CollectionDictListOptionalSupportsIntTextTupleUnion)RawInfo)SubFSR	ReadZipFS)Zboundc                       sF   e Zd Z fddZdddZdddZejfdd	Zd
d Z	  Z
S )_ZipExtFilec                    s>   |j  | _ }||j| _d| _tt| ||d| d S )Nr   r)	_zipgetinfo	file_size_end_possuperr%   __init__open)selffsnamer'   	__class__ */usr/lib/python3/dist-packages/fs/zipfs.pyr-   /   s   z_ZipExtFile.__init__c                 C   .   | j |d u r	dn|}|  jt|7  _|S Nr6   )_freadr+   lenr/   sizebufr4   r4   r5   r:   6      z_ZipExtFile.readc                 C   r7   r8   )r9   read1r+   r;   r<   r4   r4   r5   r@   <   r?   z_ZipExtFile.read1c                 C   s   t |}|tjkr|| j7 }|tjks|tjkr$|dk r#td|n#|tjkr:|dkr4td||| j7 }ntd|tjtjtj|| jk rW| j	
| j| _d| _| || j  | jS )a  Change stream position.

        Change the stream position to the given byte offset. The
        offset is interpreted relative to the position indicated by
        ``whence``.

        Arguments:
            offset (int): the offset to the new position, in bytes.
            whence (int): the position reference. Possible values are:
                * `Seek.set`: start of stream (the default).
                * `Seek.current`: current position; offset may be negative.
                * `Seek.end`: end of stream; offset must be negative.

        Returns:
            int: the new absolute position.

        Raises:
            ValueError: when ``whence`` is not known, or ``offset``
                is invalid.

        Note:
            Zip compression does not support seeking, so the seeking
            is emulated. Seeking somewhere else than the current position
            will need to either:
                * reopen the file and restart decompression
                * read and discard data to advance in the file

        r   zNegative seek position {}zPositive seek position {}z+Invalid whence ({}, should be {}, {} or {}))intr
   Zcurrentr+   set
ValueErrorformatendr*   r'   r.   r1   r9   r:   )r/   offsetwhenceZ_whencer4   r4   r5   seekB   s*   



z_ZipExtFile.seekc                 C      | j S N)r+   r/   r4   r4   r5   tellw      z_ZipExtFile.tell)r6   )__name__
__module____qualname__r-   r:   r@   r
   rB   rH   rL   __classcell__r4   r4   r2   r5   r%   .   s    

5r%   c                   @   sB   e Zd ZdZdejddfddZejrdejddfddZ	d	S d	S )
ZipFSa  Read and write zip files.

    There are two ways to open a ZipFS for the use cases of reading
    a zip file, and creating a new one.

    If you open the ZipFS with  ``write`` set to `False` (the default)
    then the filesystem will be a read only filesystem which maps to
    the files and directories within the zip file. Files are
    decompressed on the fly when you open them.

    Here's how you might extract and print a readme from a zip file::

        with ZipFS('foo.zip') as zip_fs:
            readme = zip_fs.readtext('readme.txt')

    If you open the ZipFS with ``write`` set to `True`, then the ZipFS
    will be a empty temporary filesystem. Any files / directories you
    create in the ZipFS will be written in to a zip file when the ZipFS
    is closed.

    Here's how you might write a new zip file containing a readme.txt
    file::

        with ZipFS('foo.zip', write=True) as new_zip:
            new_zip.writetext(
                'readme.txt',
                'This zip file was written by PyFilesystem'
            )


    Arguments:
        file (str or io.IOBase): An OS filename, or an open file object.
        write (bool): Set to `True` to write a new zip file, or `False`
            (default) to read an existing zip file.
        compression (int): Compression to use (one of the constants
            defined in the `zipfile` module in the stdlib).
        temp_fs (str): An FS URL for the temporary filesystem used to
            store data prior to zipping.

    Futf-8temp://__ziptemp__c                 C   s    |r
t ||||dS t||dS )N)compressionencodingtemp_fs)rV   )
WriteZipFSr$   )clsfilewriterU   rV   rW   r4   r4   r5   __new__   s
   zZipFS.__new__c                 C   s   d S rJ   r4   )r/   rZ   r[   rU   rV   rW   r4   r4   r5   r-      s   	zZipFS.__init__N)
rN   rO   rP   __doc__zipfileZIP_DEFLATEDr\   typingTYPE_CHECKINGr-   r4   r4   r4   r5   rR   |   s    -
rR   c                       sf   e Zd ZdZejddf fdd	Zdd Zdd	 Zd
d Z	dd Z
 fddZ			dddZ  ZS )rX   zA writable zip file.
    rS   rT   c                    sH   || _ || _|| _|| _t|| _t| j | _t	t
| | j d S rJ   )_filerU   rV   _temp_fs_urlr   _temp_fsdictZgetmeta_metar,   rX   r-   )r/   rZ   rU   rV   rW   r2   r4   r5   r-      s   
zWriteZipFS.__init__c                 C   s   d}| | j| j| j| jS )Nz?WriteZipFS({!r}, compression={!r}, encoding={!r}, temp_fs={!r}))rD   rb   rU   rV   rc   )r/   tr4   r4   r5   __repr__   s   zWriteZipFS.__repr__c                 C      d | jS )Nz<zipfs-write '{}'>rD   rb   rK   r4   r4   r5   __str__      zWriteZipFS.__str__c                 C   s
   | j |fS rJ   rd   r/   pathr4   r4   r5   delegate_path   s   
zWriteZipFS.delegate_pathc                 C   rI   rJ   rm   rK   r4   r4   r5   delegate_fs   rM   zWriteZipFS.delegate_fsc                    s>   |   sz|   W | j  n| j  w tt|   d S rJ   )isclosedr   rd   closer,   rX   rK   r2   r4   r5   rs      s
   
zWriteZipFS.closeNc                 C   s4   |   st| j|p| j|p| j|p| jd dS dS )a5  Write zip to a file.

        Arguments:
            file (str or io.IOBase, optional): Destination file, may be
                a file name or an open file handle.
            compression (int, optional): Compression to use (one of the
                constants defined in the `zipfile` module in the stdlib).
            encoding (str, optional): The character encoding to use
                (default uses the encoding defined in
                `~WriteZipFS.__init__`).

        Note:
            This is called automatically when the ZipFS is closed.

        )rU   rV   N)rr   r   rd   rb   rU   rV   )r/   rZ   rU   rV   r4   r4   r5   r      s   
zWriteZipFS.write_zip)NNN)rN   rO   rP   r]   r^   r_   r-   rh   rk   rp   rq   rs   r   rQ   r4   r4   r2   r5   rX      s    rX   c                       s   e Zd ZdZddddddddZejjd( fdd	Zdd	 Z	d
d Z
dd Zedd Zd)ddZdd Zdd Z		d*ddZd+ddZdd Zdd  Z fd!d"Zd#d$ Zd,d&d'Z  ZS )-r$   zA readable zip file.
    TF)Zcase_insensitiveZnetworkZ	read_onlyZsupports_renameZthread_safeZunicode_pathsZvirtualrS   c                    s2   t t|   || _|| _t|d| _d | _d S )Nr&   )	r,   r$   r-   rb   rV   r^   ZipFiler'   _directory_fs)r/   rZ   rV   r2   r4   r5   r-   $  s
   
zReadZipFS.__init__c                 C   ri   )NzReadZipFS({!r})rj   rK   r4   r4   r5   rh   -  rl   zReadZipFS.__repr__c                 C   ri   )Nz<zipfs '{}'>rj   rK   r4   r4   r5   rk   1  rl   zReadZipFS.__str__c                 C   s6   t t|}| j|rt|}tjr|| jS |S )z+Convert a path to a zip file name.
        )	r   r   
_directoryisdirr   sixPY2encoderV   rn   r4   r4   r5   _path_to_zip_name5  s   zReadZipFS._path_to_zip_namec                 C   s   |    | jE | jdu rBt  | _}| j D ])}|}tjr&|| j	d}|
dr3|j|dd q|jt|dd || q| jW  d   S 1 sOw   Y  dS )zL`MemoryFS`: a filesystem with the same folder hierarchy as the zip.
        Nreplace/T)recreate)check_lockru   r   r'   Znamelistrx   ry   decoderV   endswithmakedirsr   Zcreate)r/   Z_fszip_nameZresource_namer4   r4   r5   rv   @  s   

$zReadZipFS._directoryNc                    sD  |  |}|pd}i }|dkr'ddd|d< d|v r#dttji|d< t|S | j|}|j|jd|d< h d	|s| 	|}z| j
| W n tyV   Y t|S w d|v rr jt|jrdtjntjtt j d
|d< d|v r fdd jD |d< d|v r jr jdkrdt jd? d@ d i|d< t|S )Nr4   r}    T)r1   is_dirZbasicdetailstype>   zipaccessr   )r=   r   Zmodifiedr   c                    s"   i | ]}| d s|t |qS )_)
startswithgetattr).0kZzip_infor4   r5   
<dictcomp>x  s    
z%ReadZipFS.getinfo.<locals>.<dictcomp>r      permissions   i  )mode)ZvalidatepathrA   r	   Z	directoryrv   r(   r1   r   
isdisjointr{   r'   KeyErrorr)   rZ   r   r   Z	date_time	__slots__Zexternal_attrZcreate_systemr   dumpr   )r/   ro   Z
namespaces_pathZraw_infoZ
basic_infor   r4   r   r5   r(   T  sR   
)



zReadZipFS.getinfoc                 C      |    t|rJ   r   r   ResourceReadOnly)r/   ro   infor4   r4   r5   setinfo     
zReadZipFS.setinfoc                 C   s   |    | j|S rJ   )r   rv   listdirrn   r4   r4   r5   r     s   zReadZipFS.listdirc                 C   r   rJ   r   )r/   ro   r   r~   r4   r4   r5   makedir  s   
zReadZipFS.makedirr&   r6   c                 K   sj   |    d|v sd|v sd|v rt|| j|s t|| j|r+t|| |}t	| |S )Nw+a)
r   r   r   rv   existsResourceNotFoundrw   ZFileExpectedr{   r%   )r/   ro   r   	bufferingkwargsr   r4   r4   r5   openbin  s   




zReadZipFS.openbinc                 C   r   rJ   r   rn   r4   r4   r5   remove  r   zReadZipFS.removec                 C   r   rJ   r   rn   r4   r4   r5   	removedir  r   zReadZipFS.removedirc                    s*   t t|   t| dr| j  d S d S )Nr'   )r,   r$   rs   hasattrr'   rK   r2   r4   r5   rs     s   
zReadZipFS.closec                 C   s8   |    | j|st|| |}| j|}|S rJ   )r   rv   isfiler   r   r{   r'   r:   )r/   ro   r   Z	zip_bytesr4   r4   r5   	readbytes  s   

zReadZipFS.readbytesdownloadc                 C   s@   |dkrt | jtjrt| j}t|}d||S t||)Nr0   zzip://{}!/{})
isinstancerb   rx   Zstring_typesr   rD   r   ZNoURL)r/   ro   ZpurposeZquoted_fileZquoted_pathr4   r4   r5   geturl  s
   
zReadZipFS.geturl)rS   rJ   )NF)r&   r6   )r   )rN   rO   rP   r]   rf   r   ZCreateFailedZ	catch_allr-   rh   rk   r{   propertyrv   r(   r   r   r   r   r   r   rs   r   r   rQ   r4   r4   r2   r5   r$     s:    


4


	);r]   Z
__future__r   r   r`   r^   r   rx   r   r   baser   compressr   Zenumsr	   r
   r   r   Ziotoolsr   r   r   Zmemoryfsr   Zopenerr   ro   r   r   r   r   timer   Zwrapfsr   Z
_url_toolsr   ra   r   r   r   r   r   r   r   r   r   r    r!   Zsubfsr"   TypeVarr#   r%   rR   Zpython_2_unicode_compatiblerX   r$   r4   r4   r4   r5   <module>   s>    0NKM