U
    xÊ¹j¸  ã                   @   s8   d dl Z d dlZd dlZd dlmZ G dd„ deƒZdS )é    N)ÚAbstractArchiveFileSystemc                       s‚   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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|Ž |tdƒkr,td|› d�ƒ‚|| _t|ttjfƒrt|dkrPd}	n|d }	t	j
|f|	|dœ|pni —Ž}|| _|| _| ¡ | _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ÚosÚPathLikeÚfsspecÚopenÚforce_zip_64ZofÚ	__enter__ÚfoÚzipfileÚZipFiler   Ú	dir_cache)
Úselfr   r	   Ztarget_protocolZtarget_optionsr   r   r   ÚkwargsÚm©Ú	__class__© úY/var/www/html/bwave/python/venv/lib/python3.8/site-packages/fsspec/implementations/zip.pyr      s6    ÿ ÿÿ
ûzZipFileSystem.__init__c                    s   t ƒ  |¡ d¡S )Nú/)r   Ú_strip_protocolÚlstrip)ÚclsÚpathr!   r#   r$   r&   H   s    zZipFileSystem._strip_protocolc                 C   s   t | dƒr|  ¡  | `d S )Nr   )ÚhasattrÚcloser   ©r   r#   r#   r$   Ú__del__M   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+   R   s    zZipFileSystem.closec                    s˜   | j d ks| jtdƒk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>[   s   ûýz+ZipFileSystem._get_dirs.<locals>.<dictcomp>c                    s   i | ]}|t ˆ |d ƒ“qS ©N)Úgetattr)r5   Ús©Úzr#   r$   r7   d   s      r%   r/   Úfiler0   r1   )r   r	   r   r   ÚinfolistZ_all_dirnamesÚnamelistr   ÚZipInfoÚ	__slots__ÚupdateÚfilenamer4   Ú	file_sizeÚis_dir)r   ÚfilesÚfr#   r;   r$   Ú	_get_dirsV   s    
ú
ýÿzZipFileSystem._get_dirsc                 K   s   | j j||f|Ž d S r8   )r   Úwritestr)r   r)   Úvaluer   r#   r#   r$   Ú	pipe_filen   s    zZipFileSystem.pipe_fileÚrbc           	      K   s˜   |   |¡}d|kr:| jtdƒkr:|  |¡r2tdƒ‚t|ƒ‚d| jkrTd|krTtdƒ‚| jj|| d¡| j	d�}d|kr”|  
|¡}|d |_|d |_|S )	Nr   r.   z7ZipFS can only be open for reading or writing, not bothÚwr   )Úforce_zip64r2   r1   )r&   r	   r   ÚexistsÚOSErrorÚFileNotFoundErrorr   r   Ústripr   Úinfor2   r1   )	r   r)   r	   Ú
block_sizeZ
autocommitZcache_optionsr   ÚoutrS   r#   r#   r$   Ú_openr   s    	




zZipFileSystem._openc           
         s*  ˆ d k	rˆ dk rt dƒ‚| d¡}ttdd„ | d¡ƒƒ‰‡fdd„}|  ¡  i }|| jkrŠ| j| d d	krŠ| j| ||< |r„|S |gS | j ¡ D ]Z\}}	|d
ks®||ƒs®q”|	d dkrÖ|r”||kr”|	|| d¡< q”||kr”|ræ|	nd ||< q”ˆ �r| 	d¡‰‡ ‡fdd„| ¡ D ƒ}|�r"|S t
|ƒS )Né   zmaxdepth must be at least 1r%   c                 S   s   t | ƒS r8   ©Úbool©r:   r#   r#   r$   Ú<lambda>�   ó    z$ZipFileSystem.find.<locals>.<lambda>c                    s,   t dd„ |  d¡ƒ}tdd„ tˆ |ƒD ƒƒS )Nc                 S   s   t | ƒS r8   rX   rZ   r#   r#   r$   r[   “   r\   z>ZipFileSystem.find.<locals>._matching_starts.<locals>.<lambda>r%   c                 s   s   | ]\}}||kV  qd S r8   r#   )r5   r   r   r#   r#   r$   Ú	<genexpr>”   s     z?ZipFileSystem.find.<locals>._matching_starts.<locals>.<genexpr>)ÚfilterÚsplitÚallr   )Ú	file_pathZ
file_parts)Ú
path_partsr#   r$   Ú_matching_starts’   s    z,ZipFileSystem.find.<locals>._matching_startsr3   r=   r   r/   c                    s(   i | ] \}}|  d ¡ˆ ˆ k r||“qS )r%   )Úcount)r5   ÚkÚv)ÚmaxdepthÚ
path_depthr#   r$   r7   ®   s      z&ZipFileSystem.find.<locals>.<dictcomp>)r   r'   Úlistr^   r_   rH   r   ÚitemsrR   rd   Úsorted)
r   r)   rg   ZwithdirsÚdetailr   rc   Úresultra   Ú	file_infor#   )rg   rh   rb   r$   Úfind‰   s4    

ÿzZipFileSystem.find)rL   NTN)NFF)Ú__name__Ú
__module__Ú__qualname__Ú__doc__Zroot_markerr
   Zcachabler   Ú
ZIP_STOREDr   Úclassmethodr&   r-   r+   rH   rK   rV   ro   Ú__classcell__r#   r#   r!   r$   r      s0   ø4    ú
r   )r   r   r   Zfsspec.archiver   r   r#   r#   r#   r$   Ú<module>   s   