
    wg                     >    d dl Z d dlZd dlZd dlmZ  G d de      Zy)    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        
|   | 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|xs i }|| _        || _        |j                         | _        t        j                   | j                  ||||      | _        d	| _        y	)
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             _/home/mcse/projects/flask/flask-venv/lib/python3.12/site-packages/fsspec/implementations/zip.pyr   zZipFileSystem.__init__   s    8 	((s5z!vdV?;<<	b3,-s{3J_9G9M2B ',,.??GG#!'
     c                 @    t         |   |      j                  d      S )N/)r   _strip_protocollstrip)clspathr(   s     r)   r-   zZipFileSystem._strip_protocolH   s      w&t,33C88r*   c                 B    t        | d      r| j                          | `y y )Nr   )hasattrcloser   r#   s    r)   __del__zZipFileSystem.__del__M   s    4JJL  r*   c                 8    | j                   j                          y)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                  j	                         }| j                  | j                  j                               D ci c]'  }|j                  d      |j                  d      ddd) c}| _         |D ]  }t        j                  j                  D ci c]  }|t        ||d        }}|j                  |j                  j                  d      |j                  |j                         rdndd       || j                   |d   <    y y c c}w c c}w )Nwar,   r   	directory)namesizetypefiler:   )r"   r   r   r   infolist_all_dirnamesnamelistrstripr    ZipInfo	__slots__getattrupdatefilename	file_sizeis_dir)r#   filesdirnamezsfs         r)   	_get_dirszZipFileSystem._get_dirsV   s!   >>!TYY#d)%; HH%%'E  $11$((2C2C2EF  s##NN3/'& DN  	.5<__5N5NOQ1d++OO !

 1 1# 6 !01
 -.qy)	. &< Ps   *,D6?D;c                 @     | j                   j                  ||fi | y N)r   writestr)r#   r0   valuer&   s       r)   	pipe_filezZipFileSystem.pipe_filen   s    $00r*   c                    | j                  |      }d|v r>| j                  t        d      v r'| j                  |      rt	        d      t        |      d| j                  v rd|v rt	        d      | j                  j                  ||j                  d      | j                        }d|v r%| j                  |      }|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&   outr[   s	            r)   _openzZipFileSystem._openr   s     ##D)$;499D	1{{4 WXX#D))$))tSTThhmmD$**S/t?P?PmQ$;99T?DF|CHF|CH
r*   c                    ||dk  rt        d      |j                  d      }t        t        d |j	                  d                  fd}| j                          i }|| j                  v r.| j                  |   d   dk(  r| j                  |   ||<   |r|S |gS | j                  j                         D ]D  \  }}	|dk(  s	 ||      s|	d   d	k(  r|r||vr|	||j                  d      <   7||vs<|r|	nd ||<   F |rL|j                  d      }
|j                         D ci c]   \  }}|j                  d      |
z
  |k  s||" }}}|r|S t        |      S c c}}w )
N   zmaxdepth must be at least 1r,   c                     t        |       S rP   boolrL   s    r)   <lambda>z$ZipFileSystem.find.<locals>.<lambda>   s
    47 r*   c                 t    t        d | j                  d            }t        d t        |      D              S )Nc                     t        |       S rP   rd   rf   s    r)   rg   z>ZipFileSystem.find.<locals>._matching_starts.<locals>.<lambda>   s
    $q' r*   r,   c              3   ,   K   | ]  \  }}||k(    y wrP    ).0r   r   s      r)   	<genexpr>z?ZipFileSystem.find.<locals>._matching_starts.<locals>.<genexpr>   s     F$!QqAvFs   )filtersplitallr   )	file_path
file_parts
path_partss     r)   _matching_startsz,ZipFileSystem.find.<locals>._matching_starts   s1     19??33GHJF#j**EFFFr*   r<   r=   r   r9   )r   r.   listrn   ro   rN   r"   itemsrZ   countsorted)r#   r0   maxdepthwithdirsdetailr&   rt   resultrq   	file_info
path_depthkvrs   s                @r)   findzZipFileSystem.find   s   HqL:;; {{3&!2DJJsODE
	G 	 4>>!dnnT&:6&Bf&L>>$/F4L#6/$/$(NN$8$8$: 	B IyBJ"29"= K/ .7@ys34&17ITy!	B CJ!'A1773<*3Lx3W1F   v3VF^3s   $EE)rbNTN)NFF)__name__
__module____qualname____doc__root_markerr   cachabler    
ZIP_STOREDr   classmethodr-   r5   r3   rN   rS   r`   r   __classcell__)r(   s   @r)   r   r      s|     KHH &&2h 9 9
.01 .(4r*   r   )r   r    r   fsspec.archiver   r   rk   r*   r)   <module>r      s     	   4i4- i4r*   