U
    -eP                     @   s   d dl mZmZmZ d dlmZ d dlmZmZm	Z	m
Z
mZmZmZ d dlZd dlmZ d dlmZ d dlmZ d dlmZ d dlmZ ee
e	ee
e	Zed	ejege	ejZeed
ZeddejfddZ G dd deZ!dS )    )absolute_importdivisionprint_function)contextmanager)	CFUNCTYPEPOINTERc_int
c_longlongc_void_pcastcreate_string_bufferN)
open_files)AbstractArchiveFileSystem)
MemoryFile)DEFAULT_BLOCK_SIZEread_set_seek_callback
NO_OPEN_CBallc              	   #   s   t | t t fdd}fdd}t|}t|}trRtj}tj}	nt	j
tj}t	j
tj}	t	j
||0}
t|
| t|
d|||	 t	j
|
V  W 5 Q R X dS )zRead an archive from a seekable file-like object.

    The `file` object must support the standard `readinto` and 'seek' methods.
    c                    s$     }t|tt}|d< |S )Nr   )readintor   r   r
   )	archive_pcontextZptrptrlengthbufZbuf_pfile b/var/www/html/Darija-Ai-Train/env/lib/python3.8/site-packages/fsspec/implementations/libarchive.py	read_func,   s    
z custom_reader.<locals>.read_funcc                    s     ||   S )N)seektell)r   r   offsetwhence)r   r   r   	seek_func5   s    z custom_reader.<locals>.seek_funcN)r   r   r
   ffiZREAD_CALLBACKSEEK_CALLBACKnew_apir   ZNO_CLOSE_CB
libarchivereadZOPEN_CALLBACKZVOID_CBZCLOSE_CALLBACKZnew_archive_readr   Z	read_openZArchiveRead)r   format_nameZfilter_name
block_sizer   r"   Zread_cbZseek_cbZopen_cbZclose_cbr   r   r   r   custom_reader#   s    
	

r*   c                       sf   e Zd ZdZdZdZdZddddef fdd	Ze	d	d
 Z
e fddZdd ZdddZ  ZS )LibArchiveFileSystemay  Compressed archives as a file-system (read-only)

    Supports the following formats:
    tar, pax , cpio, ISO9660, zip, mtree, shar, ar, raw, xar, lha/lzh, rar
    Microsoft CAB, 7-Zip, WARC

    See the libarchive documentation for further restrictions.
    https://www.libarchive.org/

    Keeps file object open while instance lives. It only works in seekable
    file-like objects. In case the filesystem does not support this kind of
    file object, it is recommended to cache locally.

    This class is pickleable, but not necessarily thread-safe (depends on the
    platform). See libarchive documentation for details.
     r&   FrNc                    s   t  j| f| |dkr tdt|trft|fd|i|p<i }t|dkr^td|||d }|| _|	 | _
|| _d| _dS )aB  
        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
            Currently, only 'r' accepted
        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.
        r-   z%Only read from archive files acceptedprotocol   z2Path "{}" did not resolve to exactlyone file: "{}"r   N)super__init__
ValueError
isinstancestrr   lenformatZof	__enter__for)   	dir_cache)selfr8   modeZtarget_protocolZtarget_optionsr)   kwargsfiles	__class__r   r   r1   `   s"    
 
zLibArchiveFileSystem.__init__c              	   c   s2   | j d t| j | jd}|V  W 5 Q R X d S )Nr   )r)   )r8   r   r*   r)   )r:   arcr   r   r   _open_archive   s    z"LibArchiveFileSystem._open_archivec                    s   t  |dS )N/)r0   _strip_protocollstrip)clspathr>   r   r   rC      s    z$LibArchiveFileSystem._strip_protocolc              	      s   dddddddd| j d k	r"d S i | _ g }|  }|D ]t  jsL jsLq:| j d	d
 | t jD   fdd
D } jrdnd|d< | j || j |d < q:W 5 Q R X | j dd
 | |D  d S )Npathnamesizectimer;   uidgidmtime)namerH   createdr;   rJ   rK   rL   c                 S   s"   i | ]}|d  |d  dddqS rB   r   	directory)rM   rH   typer   .0dirnamer   r   r   
<dictcomp>   s
   z2LibArchiveFileSystem._get_dirs.<locals>.<dictcomp>c                    s   i | ]}|t  | qS r   )getattr)rS   keyentryfieldsr   r   rU      s      rP   r   rQ   rM   c                 S   s"   i | ]}|d  |d  dddqS rO   r   rR   r   r   r   rU      s    )	r9   rA   isdirisfileupdateZ_all_dirnamessetrM   append)r:   Z
list_namesr@   fr   rX   r   	_get_dirs   s<    


zLibArchiveFileSystem._get_dirsrbTc              	   K   s   |  |}|dkrtt }|  H}|D ]<}	|	j|kr:q*|	jdkrH qh|	|	jD ]}
|
} q*qTtq*W 5 Q R X t| ||dS )Nrb   r   )fsrF   data)	rC   NotImplementedErrorbytesrA   rG   rH   Z
get_blocksr2   r   )r:   rF   r;   r)   Z
autocommitZcache_optionsr<   rd   r@   rY   blockr   r   r   _open   s    	



zLibArchiveFileSystem._open)rb   NTN)__name__
__module____qualname____doc__Zroot_markerr.   Zcachabler   r1   r   rA   classmethodrC   ra   rh   __classcell__r   r   r>   r   r+   J   s(   (
-    r+   )"
__future__r   r   r   
contextlibr   ctypesr   r   r   r	   r
   r   r   r&   Zlibarchive.ffir#   Zfsspecr   Zfsspec.archiver   Zfsspec.implementations.memoryr   Zfsspec.utilsr   r$   Zc_archive_pZ	check_intr   hasattrr%   Z	page_sizer*   r+   r   r   r   r   <module>   s&   $
   
&