
    OOIdt                     R    d dl mZmZmZ d dlZd dlZd dlmZ  G d de          ZdS )    )absolute_importdivisionprint_functionN)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 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                    rt          j        |f|dz   |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b)modeprotocol)r   compression
allowZip64compresslevelN)super__init__set
ValueErrorr   
isinstancestrfsspecopenof	__enter__fozipfileZipFiler
   	dir_cache)
selfr   r   target_protocoltarget_optionsr   r   r   kwargs	__class__s
            :lib/python3.11/site-packages/fsspec/implementations/zip.pyr   zZipFileSystem.__init__   s    8 	(((((s5zz!!;d;;;<<<	b# 	o BPBVTV B ,,..?G#!'
 
 
     c                 l    t                                          |                              d          S )N/)r   _strip_protocollstrip)clspathr&   s     r'   r+   zZipFileSystem._strip_protocolD   s+     ww&&t,,33C888r(   c                 R    t          | d          r|                                  | `d S )Nr
   )hasattrcloser
   r"   s    r'   __del__zZipFileSystem.__del__I   s*    4 	JJLLLHHHr(   c                 8    | j                                          dS )z;Commits any write changes to the file. Done on ``del`` too.N)r
   r1   r2   s    r'   r1   zZipFileSystem.closeN   s    r(   c                    | j         | j        t          d          v r| j                                        }d |                     | j                                                  D             | _         |D ]jfdt          j        j	        D             }|
                    j        j                                        rdndd           || j         |d         <   id S d S )Nwac                 (    i | ]}|d z   |d z   dddS )r*   r   	directorynamesizetype ).0dirnames     r'   
<dictcomp>z+ZipFileSystem._get_dirs.<locals>.<dictcomp>W   s<        ##q+VV  r(   c                 4    i | ]}|t          |d           S N)getattr)r>   szs     r'   r@   z+ZipFileSystem._get_dirs.<locals>.<dictcomp>\   s'    OOOQ1d++OOOr(   r8   filer9   r:   )r!   r   r   r
   infolist_all_dirnamesnamelistr   ZipInfo	__slots__updatefilename	file_sizeis_dir)r"   filesfrE   s      @r'   	_get_dirszZipFileSystem._get_dirsR   s   >!TY#d))%;%; H%%''E #11$(2C2C2E2EFF  DN  	. 	.OOOOW_5NOOO !
 !01

!F    -.qy))# &<%;	. 	.r(   c                 .     | j         j        ||fi | d S rB   )r
   writestr)r"   r.   valuer%   s       r'   	pipe_filezZipFileSystem.pipe_filef   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                    }d|v r/| 	                    |          }|d         |_
        |d         |_        |S )Nr   r6   z7ZipFS can only be open for reading or writing, not bothwr   r;   r:   )r+   r   r   existsIOErrorFileNotFoundErrorr
   r   stripinfor;   r:   )	r"   r.   r   
block_size
autocommitcache_optionsr%   outr^   s	            r'   _openzZipFileSystem._openj   s     ##D))$;;49D		11{{4   YWXXX#D)))$)tSTTThmmD$**S//22$;;99T??DF|CHF|CH
r(   )rW   NTN)__name__
__module____qualname____doc__root_markerr   cachabler   
ZIP_STOREDr   classmethodr+   r3   r1   rR   rV   rc   __classcell__)r&   s   @r'   r   r   	   s          KHH &- - - - - -^ 9 9 9 9 [9  
  . . .(1 1 1        r(   r   )	
__future__r   r   r   r   r   fsspec.archiver   r   r=   r(   r'   <module>ro      s    @ @ @ @ @ @ @ @ @ @   4 4 4 4 4 4v v v v v- v v v v vr(   