o
    h                     @   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 )ZipFileSystemzRead/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rtd| d|| _t|ttjfr<|dkr*d}	n|d }	t	j
|f|	|d|p9i }|| _|| _| | _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
        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_64of	__enter__fozipfileZipFiler   	dir_cache)
selfr   r
   target_protocoltarget_optionsr   r   r   kwargsm	__class__ n/var/www/html/construction_image-detection-poc/venv/lib/python3.10/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*   H   s   zZipFileSystem._strip_protocolc                 C   s   t | dr|   | `d S 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/   r0   r'   r'   r(   r/   R   s   zZipFileSystem.closec                    s   | j d u s| jtdv rJ| j }dd | | j D | _ |D ]*  fddtjj	D }|
 jd j  r=dndd || j |d	 < q!d S d 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)r9   szr'   r(   r;   d   s    r)   r3   filer4   r5   )r   r
   r   r   infolist_all_dirnamesnamelistr   ZipInfo	__slots__updatefilenamer8   	file_sizeis_dir)r    filesfr'   r?   r(   	_get_dirsV   s    

zZipFileSystem._get_dirsc                 K   s   | j j||fi | d S r<   )r   writestr)r    r-   valuer#   r'   r'   r(   	pipe_filen   s   zZipFileSystem.pipe_filerbc           	      K   s   |  |}d|v r| jtdv r| |rtdt|d| jv r*d|v r*td| jj||d| j	d}d|v rJ| 
|}|d |_|d |_|S )	Nr   r2   z7ZipFS can only be open for reading or writing, not bothwr	   )force_zip64r6   r5   )r*   r
   r   existsOSErrorFileNotFoundErrorr   r   stripr   infor6   r5   )	r    r-   r
   
block_size
autocommitcache_optionsr#   outrX   r'   r'   r(   _openr   s   
	



zZipFileSystem._openc           
         s&   d ur dk rt d|d}ttdd |dfdd}|   i }|| jv rE| j| d d	krE| j| ||< |rB|S |gS | j D ]-\}}	|d
ksW||sWqJ|	d dkrk|rj||vrj|	||d< qJ||vrw|rs|	nd ||< qJ r|	d fdd| D }|r|S t
|S )N   zmaxdepth must be at least 1r)   c                 S      t | S r<   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   r_   r<   r`   rb   r'   r'   r(   rc      rd   z>ZipFileSystem.find.<locals>._matching_starts.<locals>.<lambda>r)   c                 s   s    | ]	\}}||kV  qd S r<   r'   )r9   r   r	   r'   r'   r(   	<genexpr>   s    z?ZipFileSystem.find.<locals>._matching_starts.<locals>.<genexpr>)filtersplitallr   )	file_path
file_parts)
path_partsr'   r(   _matching_starts   s   z,ZipFileSystem.find.<locals>._matching_startsr7   rA   r   r3   c                    s(   i | ]\}}| d   k r||qS )r)   )count)r9   kv)maxdepth
path_depthr'   r(   r;      s    "z&ZipFileSystem.find.<locals>.<dictcomp>)r   r+   listrf   rg   rM   r   itemsrW   rm   sorted)
r    r-   rp   withdirsdetailr#   rl   resultri   	file_infor'   )rp   rq   rk   r(   find   s6   

zZipFileSystem.find)rQ   NTN)NFF)__name__
__module____qualname____doc__root_markerr   cachabler   
ZIP_STOREDr   classmethodr*   r1   r/   rM   rP   r]   ry   __classcell__r'   r'   r%   r(   r      s2    4
r   )r   r   r   fsspec.archiver   r   r'   r'   r'   r(   <module>   s
    