a
    yn„fA  ã                   @   s0   d dl Z d dlZd dlmZ G dd„ deƒZdS )é    N)ÚAbstractArchiveFileSystemc                       sx   e Zd ZdZdZdZdZddddejddf‡ fdd	„	Z	e
‡ fd
d„ƒZdd„ Zdd„ Zdd„ Zdd„ Zddd„Z‡  ZS )ÚZipFileSystemz©Read/Write contents of ZIP archive as a file-system

    Keeps file object open while instance lives.

    This class is pickleable, but not necessarily thread-safe
    Ú ÚzipFÚrNTc           
         sª   t ƒ j| fi |¤Ž |tdƒvr0td|› d�ƒ‚|| _t|tƒrr|dkrNd}	n|d }	tj|f|	|dœ|pli ¤Ž}|| _	|| _
| ¡ | _tj| j||||d�| _d	| _d	S )
a«  
        Parameters
        ----------
        fo: str or file-like
            Contains ZIP, and must exist. If a str, will fetch file using
            :meth:`~fsspec.open_files`, which must return one file exactly.
        mode: str
            Accept: "r", "w", "a"
        target_protocol: str (optional)
            If ``fo`` is a string, this value can be used to override the
            FS protocol inferred from a URL
        target_options: dict (optional)
            Kwargs passed when instantiating the target FS, if ``fo`` is
            a string.
        compression, allowZip64, compresslevel: passed to ZipFile
            Only relevant when creating a ZIP
        Zrwazmode 'z' no understoodÚazr+bÚb)ÚmodeÚprotocol)r	   ÚcompressionÚ
allowZip64ÚcompresslevelN)ÚsuperÚ__init__ÚsetÚ
ValueErrorr	   Ú
isinstanceÚstrÚfsspecÚopenÚforce_zip_64ZofÚ	__enter__ÚfoÚzipfileÚZipFiler   Ú	dir_cache)
Úselfr   r	   Ztarget_protocolZtarget_optionsr   r   r   ÚkwargsÚm©Ú	__class__© ú]/home/yuhw/wc/wire-cell-python/wcpy/lib/python3.9/site-packages/fsspec/implementations/zip.pyr      s4    
ÿÿÿ
ûzZipFileSystem.__init__c                    s   t ƒ  |¡ d¡S )Nú/)r   Ú_strip_protocolÚlstrip)ÚclsÚpathr   r!   r"   r$   G   s    zZipFileSystem._strip_protocolc                 C   s   t | dƒr|  ¡  | `d S )Nr   )ÚhasattrÚcloser   ©r   r!   r!   r"   Ú__del__L   s    
zZipFileSystem.__del__c                 C   s   | j  ¡  dS )z;Commits any write changes to the file. Done on ``del`` too.N)r   r)   r*   r!   r!   r"   r)   Q   s    zZipFileSystem.closec                    s˜   | j d u s| jtdƒv r”| j ¡ }dd„ |  | j ¡ ¡D ƒ| _ |D ]P‰ ‡ fdd„tjj	D ƒ}| 
ˆ j d¡ˆ jˆ  ¡ rzdnddœ¡ || j |d	 < qBd S )
NÚwac                 S   s&   i | ]}|  d ¡|  d ¡dddœ“qS )r#   r   Ú	directory©ÚnameÚsizeÚtype)Úrstrip)Ú.0Údirnamer!   r!   r"   Ú
<dictcomp>Z   s   ûýz+ZipFileSystem._get_dirs.<locals>.<dictcomp>c                    s   i | ]}|t ˆ |d ƒ“qS ©N)Úgetattr)r3   Ús©Úzr!   r"   r5   c   ó    r#   r-   Úfiler.   r/   )r   r	   r   r   ÚinfolistZ_all_dirnamesÚnamelistr   ÚZipInfoÚ	__slots__ÚupdateÚfilenamer2   Ú	file_sizeÚis_dir)r   ÚfilesÚfr!   r9   r"   Ú	_get_dirsU   s    
ú
ýÿzZipFileSystem._get_dirsc                 K   s   | j j||fi |¤Ž d S r6   )r   Úwritestr)r   r'   Úvaluer   r!   r!   r"   Ú	pipe_filem   s    zZipFileSystem.pipe_fileÚrbc           	      K   s˜   |   |¡}d|v r:| jtdƒv r:|  |¡r2tdƒ‚t|ƒ‚d| jv rTd|v rTtdƒ‚| jj|| d¡| j	d�}d|v r”|  
|¡}|d |_|d |_|S )	Nr   r,   z7ZipFS can only be open for reading or writing, not bothÚwr   )Úforce_zip64r0   r/   )r$   r	   r   ÚexistsÚOSErrorÚFileNotFoundErrorr   r   Ústripr   Úinfor0   r/   )	r   r'   r	   Ú
block_sizeZ
autocommitZcache_optionsr   ÚoutrR   r!   r!   r"   Ú_openq   s    	




zZipFileSystem._open)rK   NTN)Ú__name__Ú
__module__Ú__qualname__Ú__doc__Zroot_markerr
   Zcachabler   Ú
ZIP_STOREDr   Úclassmethodr$   r+   r)   rG   rJ   rU   Ú__classcell__r!   r!   r   r"   r      s.   ø4    úr   )r   r   Zfsspec.archiver   r   r!   r!   r!   r"   Ú<module>   s   