U
    9%e6                     @   s0   d dl Z d dlZd dlmZ G dd deZdS )    N)AbstractArchiveFileSystemc                       sx   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  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| |tdkr,td| d|| _t|tr\tj|f|d |d|pVi }|| _	|
 | _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b)modeprotocol)r   compression
allowZip64compresslevelN)super__init__set
ValueErrorr   
isinstancestrfsspecopenZof	__enter__fozipfileZipFiler   	dir_cache)	selfr   r   Ztarget_protocolZtarget_optionsr
   r   r   kwargs	__class__ Y/var/www/html/Darija-Ai-API/env/lib/python3.8/site-packages/fsspec/implementations/zip.pyr      s.    
 
zZipFileSystem.__init__c                    s   t  |dS )N/)r   _strip_protocollstrip)clspathr   r   r   r!   B   s    zZipFileSystem._strip_protocolc                 C   s   t | dr|   | `d S )Nr   )hasattrcloser   r   r   r   r   __del__G   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&   L   s    zZipFileSystem.closec                    s   | j d ks| jtdkr| j }dd | | j D | _ |D ]J  fddtjj	D }|
 j j  rtdndd || j |d < qBd S )	Nwac                 S   s"   i | ]}|d  |d  dddqS )r    r   	directorynamesizetyper   ).0dirnamer   r   r   
<dictcomp>U   s    z+ZipFileSystem._get_dirs.<locals>.<dictcomp>c                    s   i | ]}|t  |d qS N)getattr)r/   szr   r   r1   Z   s      r*   filer+   r,   )r   r   r   r   infolistZ_all_dirnamesnamelistr   ZipInfo	__slots__updatefilename	file_sizeis_dir)r   filesfr   r5   r   	_get_dirsP   s    
zZipFileSystem._get_dirsc                 K   s   | j j||f| d S r2   )r   writestr)r   r$   valuer   r   r   r   	pipe_filed   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||d}d|kr| 	|}|d |_
|d |_|S )Nr   r)   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_sizeZ
autocommitZcache_optionsr   outrL   r   r   r   _openh   s    	




zZipFileSystem._open)rF   NTN)__name__
__module____qualname____doc__Zroot_markerr	   Zcachabler   
ZIP_STOREDr   classmethodr!   r(   r&   rB   rE   rO   __classcell__r   r   r   r   r      s.   /    r   )r   r   Zfsspec.archiver   r   r   r   r   r   <module>   s   