o
    h5                     @  s   d dl mZ d dlZd dlZd dlZd dlZd dlZd dlmZ 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 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dZd ddZdd ZdS )!    )annotationsN)Optional   )AsyncFileSystem_run_coros_in_chunkssync_wrapper)DEFAULT_CALLBACK)
filesystemget_filesystem_classsplit_protocol	url_to_fszfsspec.genericc                 K  s   t | fi |t| < d S N)r	   _generic_fs)protocolstorage_options r   b/var/www/html/construction_image-detection-poc/venv/lib/python3.10/site-packages/fsspec/generic.pyset_generic_fs   s   r   defaultc                 C  s   |pt }|pt| d }|pi }|dkrt|S |dkr t| S |dkr,t|}| S |dkr@t| fi ||i \}}|S td| )zPick instance of backend FSr   r   genericcurrentoptionszUnknown FS resolution method: )	default_methodr   r	   r   r
   r   r   get
ValueError)urlmethodr   r   clsfs_r   r   r   _resolve_fs   s   r    Fsize	differentc                   s  |p
t di |pi }|||jddd |s&td|jdddfdd  D }	tt|	 d |	rV|j	fdd|	D dd d	d
   D  tt  d  fdd D }
 
  D ]J\}}|}|v r|dkr| |< q||dkrt|r||n|| }| }t|r||n|| }||kr| |< q| | q|| |< q|tt  d  rt   \}}|j||fi | tt|
 d |r|
r||
  S )aU  Sync files between two directory trees

    (experimental)

    Parameters
    ----------
    source: str
        Root of the directory tree to take files from. This must be a directory, but
        do not include any terminating "/" character
    destination: str
        Root path to copy into. The contents of this location should be
        identical to the contents of ``source`` when done. This will be made a
        directory, and the terminal "/" should not be included.
    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 | callable
        If ``update_field`` is "different", this is the key in the info
        of source files to consider for difference. Maybe a function of the
        info dict.
    dest_field: str | callable
        If ``update_field`` is "different", this is the key in the info
        of destination files to consider for difference. May be a function of
        the info dict.
    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.

    Returns
    -------
    dict of the copy operations that were performed, {source: destination}
    T)withdirsdetailzCan only rsync on a directoryc                   s0   g | ]\}}|d  dkr|  vr|qS )type	directoryreplace.0av)destination
otherfilessourcer   r   
<listcomp>f   
    zrsync.<locals>.<listcomp>z directories to createc                   s   g | ]}|  qS r   r'   )r*   dirn)r-   r/   r   r   r0   n   s    exist_okc                 S  s"   i | ]\}}|d  dkr||qS r%   filer   r)   r   r   r   
<dictcomp>p   s   " zrsync.<locals>.<dictcomp>z files to consider for copyc                   s0   g | ]\}}|  vr|d  dkr|qS r5   r'   )r*   or,   )allfilesr-   r/   r   r   r0   r   r1   alwaysr"   z files to copyz files to deleteNr   )GenericFileSystem_strip_protocolfindisdirr   itemsloggerdebuglenmake_many_dirscopyr(   callablepopzipcprm)r/   r-   delete_missingsource_field
dest_fieldupdate_condinst_kwargsr   kwargsdirs	to_deletekr,   	otherfileinf1v2inf2source_filestarget_filesr   )r9   r-   r.   r/   r   rsync+   sT   4







rY   c                      s   e Zd ZdZdZd1 fdd	Zdd Zdd	 Zd2ddZdd Z		d3ddZ
dd Zdd Zdd Zd4ddZdd ZdefddZd3d d!ZeeZ		"	
	
	
d5d6d/d0Z  ZS )7r;   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 jdi | 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
        Nr   )r   super__init__)selfr   rO   	__class__r   r   r[      s   zGenericFileSystem.__init__c                 C     t || j}|||S r   )r    r   unstrip_protocol_parentr\   pathr   r   r   r   ra      s   zGenericFileSystem._parentc                 C  r_   r   )r    r   r`   r<   rb   r   r   r   r<      s   z!GenericFileSystem._strip_protocolNFc                   s   t || j}|jr|j|f||dd|I d H }n|j|f||dd|}i }| D ]\}	}
|
 }
||	}||
d< |
||< q.|rH|S t|S )NT)maxdepthr#   r$   name)	r    r   
async_impl_findr=   r?   rD   r`   list)r\   rc   rd   r#   r$   rO   r   outresultrR   r,   re   r   r   r   rg      s4   

zGenericFileSystem._findc                   s^   t || j}|jr|j|fi |I d H }n	|j|fi |}| }||d |d< |S )Nre   )r    r   rf   _infoinforD   r`   )r\   r   rO   r   ri   r   r   r   rk      s   zGenericFileSystem._infoTc                   s   t || j}|jr|j|fddi|I d H }n|j|fddi|}dd |D }|D ]}||d |d< q-|r=|S dd |D S )Nr$   Tc                 S  s   g | ]}|  qS r   )rD   r*   r8   r   r   r   r0          z)GenericFileSystem._ls.<locals>.<listcomp>re   c                 S  s   g | ]}|d  qS )re   r   rm   r   r   r   r0      rn   )r    r   rf   _lslsr`   )r\   r   r$   rO   r   ri   r8   r   r   r   ro      s   zGenericFileSystem._lsc                   s>   t || j}|jr|j|fi |I d H S |j|fi |S r   )r    r   rf   	_cat_filecat_file)r\   r   rO   r   r   r   r   rq      s
   zGenericFileSystem._cat_filec                   sB   t || j}|jr|j||fi |I d H S |j||fi |S r   )r    r   rf   
_pipe_file	pipe_file)r\   rc   valuerO   r   r   r   r   rs      s
   zGenericFileSystem._pipe_filec                   s^   |}t |tr|g}t|d | j}|jr$|j|fi |I d H  d S |j|fi | d S )Nr   )
isinstancestrr    r   rf   _rmrI   )r\   r   rO   urlsr   r   r   r   rx     s   
zGenericFileSystem._rmc                   sJ   t d| t|| j}|jr|j||dI d H  d S |j||d d S )NzMake dir %sr3   )r@   rA   r    r   rf   	_makedirsmakedirs)r\   rc   r4   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)rY   )r\   r/   r-   rO   r   r   r   rY     s   zGenericFileSystem.rsynci   c                   s  t || j}t || j}||u r+|jr!|j||fi |I d H S |j||fi |S ddd}zt|dr?||dI d H n	|j|dfi |}	|t	|	j
I d H  t|drb||dI d H n	|j|dfi |}
|	j
d u sx|
 |	j
k rt	|	|I d H }|	j
d u r|snt	|
|I d H  ||
  |	j
d u sx|
 |	j
k sxW zt	|
 I d H  t	|	 I d H  W d S  ty   Y d S w zt	|
 I d H  t	|	 I d H  W w  ty   Y w w )Nr   none)	blocksize
cache_type
open_asyncrbwb)r    r   rf   _cp_filecp_filehasattrr   openset_sizemaybe_awaitr!   tellreadwriteabsolute_updateclose	NameError)r\   r   url2r}   callbackrO   r   fs2kwf1f2datar   r   r   r     sL   
zGenericFileSystem._cp_filec                   sZ   t |d | jjr fdd|D }t|I d H  d S |D ]	}j| d q!d S )Nr   c                   s   g | ]	}j | d qS )r3   )rz   )r*   ur4   r   r   r   r0   K  s    z5GenericFileSystem._make_many_dirs.<locals>.<listcomp>r3   )r    r   rf   r   r{   )r\   ry   r4   corosr   r   r   r   _make_many_dirsH  s   z!GenericFileSystem._make_many_dirsignorepath1	list[str]path2	recursiveboolon_errorrw   rd   Optional[int]
batch_sizetempdirOptional[str]c              	     s   |rt t|d | j}	t|d | j}
|	|
u r3|	jr)|	j||fi |I d H S |	j||fi |S t|	||
||||dI d H  d S )Nr   r   )NotImplementedErrorr    r   rf   _copyrD   copy_file_op)r\   r   r   r   r   rd   r   r   rO   r   r   r   r   r   r   S  s   zGenericFileSystem._copy)r   )NFF)T)F)Fr   NNN)r   r   r   r   r   r   r   rw   rd   r   r   r   r   r   )__name__
__module____qualname____doc__r   r[   ra   r<   rg   rk   ro   rq   rs   rx   rz   rY   r   r   r   r   rC   r   __classcell__r   r   r]   r   r;      s4    





+	r;      r   c           	   	     sd   dd l }p
| z  fddt||D }t||dI d H  W t d S t w )Nr   c                   s4   g | ]\}}t  ||tjt jd qS )r   )_copy_file_oposrc   joinuuiduuid4hex)r*   u1u2fs1r   r   r   r   r   r0   u  s    	z copy_file_op.<locals>.<listcomp>)r   )tempfilemkdtemprG   r   shutilrmtree)	r   url1r   r   r   r   r   r   r   r   r   r   r   n  s   	r   c              
     s   |dkrdnt }td|| z5| jr| ||I d H  n| || |jr1|||I d H  n||| t	| td|| W d S  |y_ } ztd|| W Y d }~d S d }~ww )Nraiser   zCopy %s -> %szCopy %s -> %s; donez ignoring cp exception for %s: %s)
	Exceptionr@   rA   rf   	_get_fileget_file	_put_fileput_filer   unlink)r   r   r   r   localr   exer   r   r   r     s    
r   c                   s   t | r| I d H S | S r   )inspectiscoroutine)corr   r   r   r     s   

r   )NNN)Fr!   r!   r"   NN)Nr   r   )r   )
__future__r   r   loggingr   r   r   typingr   asynr   r   r   	callbacksr   corer	   r
   r   r   r   	getLoggerr@   r   r   r    rY   r;   r   r   r   r   r   r   r   <module>   s8    


h ]

