U
    -e'                     @   s   d dl Z d dlZddlmZ ddlmZ ddlmZmZm	Z	 i Z
edZdd Zd	Zdd
dZdddZG dd deZdd ZdS )    N   )AsyncFileSystem)_DEFAULT_CALLBACK)
filesystemget_filesystem_classsplit_protocolzfsspec.genericc                 K   s   t | f|t| < d S N)r   _generic_fs)protocolstorage_options r   O/var/www/html/Darija-Ai-Train/env/lib/python3.8/site-packages/fsspec/generic.pyset_generic_fs   s    r   defaultc                 C   s   |pt }|pt| d }|pi }|dkr0t|S |dkr@t| S |dkrXt|}| S |dkrtt|f||i S td| dS )zPick instance of backend FSr   r   genericcurrentoptionszUnknown FS resolution method: N)default_methodr   r   r	   r   r   get
ValueError)urlmethodr
   r   clsr   r   r   _resolve_fs   s    r   Fsize	differentc                    s  |pt f |pi }|||jddd |sHtd|jdddfdd  D }	tt|	 d |	D ]}
|j	|

dd qdd	   D  tt  d
  fdd D }   D ]p\}}|
}|krZ|dkr$| |< n4|dkrb|| | | krN| |< n
 | q| |< q rt   \}}tt| d |j||f| |rtt| d || dS )a  Sync files between two directory trees

    (experimental)

    Parameters
    ----------
    source: str
        Root of the directory tree to take files from.
    destination: str
        Root path to copy into. The contents of this location should be
        identical to the contents of ``source`` when done.
    delete_missing: bool
        If there are paths in the destination that don't exist in the
        source and this is True, delete them. Otherwise, leave them alone.
    source_field: str
        If ``update_field`` is "different", this is the key in the info
        of source files to consider for difference.
    dest_field: str
        If ``update_field`` is "different", this is the key in the info
        of destination files to consider for difference.
    update_cond: "different"|"always"|"never"
        If "always", every file is copied, regardless of whether it exists in
        the destination. If "never", files that exist in the destination are
        not copied again. If "different" (default), only copy if the info
        fields given by ``source_field`` and ``dest_field`` (usually "size")
        are different. Other comparisons may be added in the future.
    inst_kwargs: dict|None
        If ``fs`` is None, use this set of keyword arguments to make a
        GenericFileSystem instance
    fs: GenericFileSystem|None
        Instance to use if explicitly given. The instance defines how to
        to make downstream file system instances from paths.
    T)withdirsdetailzCan only rsync on a directoryc                    s0   g | ](\}}|d  dkr|  kr|qS )type	directoryreplace.0av)destination
otherfilessourcer   r   
<listcomp>W   s    zrsync.<locals>.<listcomp>z directories to createexist_okc                 S   s"   i | ]\}}|d  dkr||qS r   filer   r"   r   r   r   
<dictcomp>`   s       zrsync.<locals>.<dictcomp>z files to consider for copyc                    s0   g | ](\}}|  kr|d  dkr|qS r,   r    )r#   or%   )allfilesr&   r(   r   r   r)   b   s    alwaysr   z files to copyz files to deleteN)GenericFileSystem_strip_protocolfindisdirr   itemsloggerdebuglenZmkdirsr!   copypopzipcprm)r(   r&   Zdelete_missingZsource_fieldZ
dest_fieldZupdate_condZinst_kwargsfskwargsdirsZdirnZ	to_deletekr%   Z	otherfileZsource_filesZtarget_filesr   )r0   r&   r'   r(   r   rsync$   sF    ,








rC   c                       s   e Zd ZdZdZd fdd	Zdd Zdd
dZdd Zd ddZ	dd Z
dd Zdd Zd!ddZdd ZdefddZ  ZS )"r2   a^  Wrapper over all other FS types

    <experimental!>

    This implementation is a single unified interface to be able to run FS operations
    over generic URLs, and dispatch to the specific implementations using the URL
    protocol prefix.

    Note: instances of this FS are always async, even if you never use it with any async
    backend.
    r   r   c                    s   || _ tt| jf | dS )aK  

        Parameters
        ----------
        default_method: str (optional)
            Defines how to configure backend FS instances. Options are:
            - "default": instantiate like FSClass(), with no
              extra arguments; this is the default instance of that FS, and can be
              configured via the config system
            - "generic": takes instances from the `_generic_fs` dict in this module,
              which you must populate before use. Keys are by protocol
            - "current": takes the most recently instantiated version of each FS
        N)r   superr2   __init__)selfr   r@   	__class__r   r   rE      s    zGenericFileSystem.__init__c                 C   s   t || j}|||S r   )r   r   unstrip_protocolr3   )rF   pathr?   r   r   r   r3      s    z!GenericFileSystem._strip_protocolNFc                    s   t || j}|jr4|j|f|||d|I d H }n|j|f|||d|}i }| D ]"\}	}
||	}||
d< |
||< qZ|r|S t|S )N)maxdepthr   r   name)r   r   
async_impl_findr4   r6   rI   list)rF   rJ   rK   r   r   r@   r?   outresultrB   r%   rL   r   r   r   rN      s8        

zGenericFileSystem._findc                    sL   t || j}|jr(|j|f|I d H }n|j|f|}||d |d< |S )NrL   )r   r   rM   _infoinforI   )rF   r   r@   r?   rP   r   r   r   rR      s    zGenericFileSystem._infoTc                    s|   t || j}|jr0|j|fddi|I d H }n|j|fddi|}|D ]}||d |d< qJ|rj|S dd |D S d S )Nr   TrL   c                 S   s   g | ]}|d  qS )rL   r   )r#   r/   r   r   r   r)      s     z)GenericFileSystem._ls.<locals>.<listcomp>)r   r   rM   _lsZlsrI   )rF   r   r   r@   r?   rP   r/   r   r   r   rT      s    zGenericFileSystem._lsc                    s8   t || j}|jr&|j|f|I d H S |j|f|S d S r   )r   r   rM   	_cat_fileZcat_filerF   r   r@   r?   r   r   r   rU      s    zGenericFileSystem._cat_filec                    s<   t || j}|jr(|j||f|I d H S |j||f|S d S r   )r   r   rM   
_pipe_fileZ	pipe_file)rF   rJ   valuer@   r?   r   r   r   rW      s    zGenericFileSystem._pipe_filec                    s:   t || j}|jr(|j|f|I d H  n|j|f| d S r   )r   r   rM   _rmr>   rV   r   r   r   rY      s    zGenericFileSystem._rmc                    s:   t || j}|jr(|j||dI d H  n|j||d d S )Nr*   )r   r   rM   	_makedirsmakedirs)rF   rJ   r+   r?   r   r   r   rZ      s    zGenericFileSystem._makedirsc                 K   s   t ||fd| i| dS )z[Sync files between two directory trees

        See `func:rsync` for more details.
        r?   N)rC   )rF   r(   r&   r@   r   r   r   rC      s    zGenericFileSystem.rsynci   c                    sx  t || j}t || j}||krL|jr<|j||f|I d H S |j||f|S ddd}zt|drt|	|dI d H n|j
|df|}
|t|
jI d H  t|dr|	|dI d H n|j
|df|}	|
jd ks|	 |
jk r.t|
|I d H }|
jd kr
|s
q.t|	|I d H  ||	  qW 5 z(t|	 I d H  t|
 I d H  W n tk
rp   Y nX X d S )Nr   none)	blocksizeZ
cache_type
open_asyncrbwb)r   r   rM   _cp_fileZcp_filemaybe_awaitclose	NameErrorhasattrr^   openZset_sizer   tellreadwriteZabsolute_update)rF   r   url2r]   callbackr@   r?   Zfs2kwf2f1datar   r   r   ra      s8    
zGenericFileSystem._cp_file)r   )NFF)T)F)__name__
__module____qualname____doc__r
   rE   r3   rN   rR   rT   rU   rW   rY   rZ   rC   r   ra   __classcell__r   r   rG   r   r2      s    
 

r2   c                    s   t | r| I d H S | S d S r   )inspectiscoroutine)Zcorr   r   r   rb   *  s    

rb   )NNN)Fr   r   r   NN)ru   loggingZasynr   	callbacksr   corer   r   r   r	   	getLoggerr7   r   r   r   rC   r2   rb   r   r   r   r   <module>   s&   

      
[ ,