
    çg                     F    d dl Z d dlZd dlZd dlmZ  G d de          ZdS )    N)AbstractArchiveFileSystemc                        e Zd ZdZdZdZdZddddej        ddf fd	Z	e
 fd	            Zd
 Zd Zd Zd Z	 	 	 	 ddZddZ x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                     t                      j        | fi | |t          d          vrt          d| d          || _        t          |t          t          j        f          r%|dk    rd}	n|dz   }	t          j
        |f|	|d|pi }|| _        || _        |                                | _        t          j        | j        ||||          | _        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__s             V/var/www/html/ai-engine/env/lib/python3.11/site-packages/fsspec/implementations/zip.pyr   zZipFileSystem.__init__   s   8 	(((((s5zz!!;d;;;<<<	b3,-- 	s{{3J_ 9G9M2 B ',,..?G#!'
 
 
     c                 l    t                                          |                              d          S )N/)r   _strip_protocollstrip)clspathr(   s     r)   r-   zZipFileSystem._strip_protocolH   s+     ww&&t,,33C888r*   c                 V    t          | d          r|                                  | `d S d S )Nr   )hasattrcloser   r#   s    r)   __del__zZipFileSystem.__del__M   s4    4 	JJLLL	 	r*   c                 8    | j                                          dS )z;Commits any write changes to the file. Done on ``del`` too.N)r   r3   r4   s    r)   r3   zZipFileSystem.closeR   s    r*   c                    | j         | j        t          d          v r| j                                        }d |                     | j                                                  D             | _         |D ]}fdt          j        j	        D             }|
                    j                            d          j                                        rdndd           || j         |d         <   |d S d S )	Nwac                 h    i | ]/}|                     d           |                     d           ddd0S )r,   r   	directorynamesizetype)rstrip).0dirnames     r)   
<dictcomp>z+ZipFileSystem._get_dirs.<locals>.<dictcomp>[   sU         s###NN3//'& &  r*   c                 4    i | ]}|t          |d           S N)getattr)r@   szs     r)   rB   z+ZipFileSystem._get_dirs.<locals>.<dictcomp>d   s'    OOOQ1d++OOOr*   r,   r:   filer;   r<   )r"   r   r   r   infolist_all_dirnamesnamelistr    ZipInfo	__slots__updatefilenamer?   	file_sizeis_dir)r#   filesfrG   s      @r)   	_get_dirszZipFileSystem._get_dirsV   s   >!TY#d))%;%; H%%''E   $11$(2C2C2E2EFF  DN  	. 	.OOOOW_5NOOO !
 1 1# 6 6 !01

!F    -.qy))+ &<%;	. 	.r*   c                 .     | j         j        ||fi | d S rD   )r   writestr)r#   r0   valuer&   s       r)   	pipe_filezZipFileSystem.pipe_filen   s'    $0000000r*   rbc                    |                      |          }d|v rI| j        t          d          v r3|                     |          rt	          d          t          |          d| j        v rd|v rt	          d          | j                            ||                    d          | j	                  }d|v r/| 
                    |          }|d         |_        |d         |_        |S )	Nr   r8   z7ZipFS can only be open for reading or writing, not bothwr   )force_zip64r=   r<   )r-   r   r   existsOSErrorFileNotFoundErrorr   r   stripr   infor=   r<   )	r#   r0   r   
block_size
autocommitcache_optionsr&   outra   s	            r)   _openzZipFileSystem._openr   s     ##D))$;;49D		11{{4   YWXXX#D)))$)tSTTThmmD$**S//t?PmQQ$;;99T??DF|CHF|CH
r*   c                   
 dk     rt          d          |                    d          }t          t          d |                    d                              fd}|                                  i }|| j        v r.| j        |         d         dk    r| j        |         ||<   |r|n|gS | j                                        D ]O\  }}	|dk    s ||          s|	d         d	k    r|r||vr|	||                    d          <   B||vr	|r|	nd ||<   Pr6|	                    d          

fd
|                                D             }|r|nt          |          S )N   zmaxdepth must be at least 1r,   c                      t          |           S rD   boolrF   s    r)   <lambda>z$ZipFileSystem.find.<locals>.<lambda>   s    477 r*   c                     t          d |                     d                    }t          d t          |          D                       S )Nc                      t          |           S rD   rj   rl   s    r)   rm   z>ZipFileSystem.find.<locals>._matching_starts.<locals>.<lambda>   s    $q'' r*   r,   c              3   (   K   | ]\  }}||k    V  d S rD    )r@   r   r   s      r)   	<genexpr>z?ZipFileSystem.find.<locals>._matching_starts.<locals>.<genexpr>   s*      FF$!QqAvFFFFFFr*   )filtersplitallr   )	file_path
file_parts
path_partss     r)   _matching_startsz,ZipFileSystem.find.<locals>._matching_starts   sI     1 19??33G3GHHJFF#j**E*EFFFFFFr*   r>   rH   r   r:   c                 T    i | ]$\  }}|                     d           z
  k     !||%S )r,   )count)r@   kvmaxdepth
path_depths      r)   rB   z&ZipFileSystem.find.<locals>.<dictcomp>   s?       A1773<<*3Lx3W3W13W3W3Wr*   )r   r.   listrs   rt   rT   r"   itemsr`   r{   sorted)r#   r0   r~   withdirsdetailr&   ry   resultrv   	file_infor   rx   s     `       @@r)   findzZipFileSystem.find   s   HqLL:;;; {{3&!2!2DJJsOODDEE
	G 	G 	G 	G 	G 	 4>!!dnT&:6&Bf&L&L>$/F4L#/66$/$(N$8$8$:$: 	B 	B IyBJJ"2"29"="=J K// A ..7@ys334&&17$AIITy! 	CJ    !'  F  3vvVF^^3r*   )rY   NTN)NFF)__name__
__module____qualname____doc__root_markerr   cachabler    
ZIP_STOREDr   classmethodr-   r5   r3   rT   rX   rf   r   __classcell__)r(   s   @r)   r   r      s
         KHH &2 2 2 2 2 2h 9 9 9 9 [9  
  . . .01 1 1    .(4 (4 (4 (4 (4 (4 (4 (4r*   r   )r   r    r   fsspec.archiver   r   rq   r*   r)   <module>r      sq    				   4 4 4 4 4 4i4 i4 i4 i4 i4- i4 i4 i4 i4 i4r*   