U
    xÊ¹j”Ž  ã                   @   s   d dl Z d dlZ d dlZd dl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 d dlmZmZ ddlmZ ddlmZ ddlmZmZmZ dd	lmZmZ dd
lmZmZmZ e d¡Z dgZ!dgZ"da#e j$Z$dd„ Z%dd„ Z&dDdd„Z'ddœdd„Z(dEdd„Z)e
dd„ ƒZ*dd„ Z+e�r<d dl,Z,e,j-Z.n8zd dl,Z,W n e/k
�rf   dZ,e0Z.Y nX e1e,de0ƒZ.dZ2dZ3dFdd „Z4e5d!œd"d#„Z6dedddfd$d%„Z7d&d'd(d)d*d+d,d-d.d/d0d1d2d3d4d5d6d7d8gZ8G d9d:„ d:eƒZ9d;d<„ Z:G d=d>„ d>e;ƒZ<d?d?e<dfd@dA„Z=G dBdC„ dCeƒZ>dS )Gé    N)Úcontextmanager)Ú	has_magic)ÚTYPE_CHECKINGÚIterableé   )ÚDEFAULT_CALLBACK)ÚFSTimeoutError)ÚLocalFileSystemÚmake_path_posixÚtrailing_sep)ÚAbstractBufferedFileÚAbstractFileSystem)Úglob_translateÚis_exceptionÚother_pathsz_[^_]c                   C   s   t st ¡ a t S )zAllocate or return a threading lock.

    The lock is allocated on first use to allow setting one lock per forked process.
    )Ú_lockÚ	threadingÚLock© r   r   úJ/var/www/html/bwave/python/venv/lib/python3.8/site-packages/fsspec/asyn.pyÚget_lock   s    r   c                   C   s   dt d< dtd< dadS )z­Reset the global lock.

    This should be called only on the init of a forked process to reset the lock to
    None, enabling the new forked process to get a new lock.
    Nr   )ÚiothreadÚloopr   r   r   r   r   Ú
reset_lock&   s    r   c              
   Ã   st   |r|nd }|d k	r"t j||d�}zBz|I d H |d< W n* tk
r` } z||d< W 5 d }~X Y nX W 5 |  ¡  X d S )N©Útimeoutr   )ÚasyncioÚwait_forÚsetÚ	Exception)ÚeventÚcoroÚresultr   Úexr   r   r   Ú_runner3   s    r$   r   c          
      O   sø   |r|nd}| dks|   ¡ r$tdƒ‚ztj ¡ }|| kr@tdƒ‚W n( tk
rX   ‚ Y n tk
rj   Y nX |||Ž}dg}t ¡ }t t	||||ƒ| ¡ | 
d¡r¦qÄ|dk	rš|d8 }|dk ršt‚qš|d }	t|	tjƒràt|	‚nt|	tƒrð|	‚n|	S dS )zÕ
    Make loop run coroutine until it returns. Runs in other thread

    Examples
    --------
    >>> fsspec.asyn.sync(fsspec.asyn.get_loop(), func, *args,
                         timeout=timeout, **kwargs)
    NzLoop is not runningz)Calling sync() from within a running loopr   r   )Z	is_closedÚRuntimeErrorr   ÚeventsÚget_running_loopÚNotImplementedErrorr   ÚEventZrun_coroutine_threadsafer$   Úwaitr   Ú
isinstanceÚTimeoutErrorÚBaseException)
r   Úfuncr   ÚargsÚkwargsZloop0r!   r"   r    Zreturn_resultr   r   r   Úsync?   s6    	



r1   c                    s   t  ˆ ¡‡ ‡fdd„ƒ}|S )zµGiven a function, make so can be called in blocking contexts

    Leave obj=None if defining within a class. Pass the instance if attaching
    as an attribute of the instance.
    c                     s    ˆp
| d }t |jˆ f| ž|ŽS )Nr   )r1   r   )r/   r0   Úself©r.   Úobjr   r   Úwrappers   s    zsync_wrapper.<locals>.wrapper)Ú	functoolsÚwraps)r.   r4   r5   r   r3   r   Úsync_wrapperl   s    r8   c               	   c   sF   t  ¡ } z,tjdkr,tt dƒr,t  t  ¡ ¡ d V  W 5 t  | ¡ X d S )NÚntÚWindowsSelectorEventLoopPolicy)r   Zget_event_loop_policyZset_event_loop_policyÚosÚnameÚhasattrr:   )Zoriginal_policyr   r   r   Ú_selector_policy{   s    
r>   c               
   C   sz   t d dkrrtƒ �Z t d dkrhtƒ � t ¡ t d< W 5 Q R X tjt d jdd�} d| _|  	¡  | t
d< W 5 Q R X t d S )zdCreate or return the default fsspec IO loop

    The loop will be running on a separate thread.
    r   NZfsspecIO)Útargetr<   T)r   r   r>   r   Znew_event_loopr   ÚThreadZrun_foreverÚdaemonÚstartr   )Úthr   r   r   Úget_loop‡   s    rD   Úerroré€   i   Fc              
   C   s”   ddl m} | r"d|kr2|d S nd|kr2|d S | r:tS td krFtS zt tj¡\}}W n ttt	fk
rx   t Y S X |tj
krˆdS |d S d S )Nr   )ÚconfZnofiles_gather_batch_sizeZgather_batch_sizeéÿÿÿÿé   )Zfsspec.configrG   Ú_NOFILES_DEFAULT_BATCH_SIZEÚresourceÚ_DEFAULT_BATCH_SIZEZ	getrlimitZRLIMIT_NOFILEÚImportErrorÚ
ValueErrorÚResourceErrorZRLIM_INFINITY)ÚnofilesrG   Z
soft_limitÚ_r   r   r   Ú_get_batch_size«   s"    


rR   )Úreturnc                   C   s*   zt  ¡  W dS  tk
r$   Y dS X dS )z Being executed by an event loop?TFN)r   r'   r%   r   r   r   r   Úrunning_asyncÄ   s
    rT   c                 ƒ   sä   |dkrt |d�}|dkr"t| ƒ}|dks.t‚‡ ‡‡fdd„}d}t| ƒ}dg| }	tƒ }
|
sf||k ràt|
ƒ|k rž||k rž|
 t || | |ƒ¡¡ |d7 }qf|
s¤qàtj|
tjd�I dH \}}
|rZ| 	¡ I dH \}}||	|< q¾qZ|	S )	aS  Run the given coroutines in  chunks.

    Parameters
    ----------
    coros: list of coroutines to run
    batch_size: int or None
        Number of coroutines to submit/wait on simultaneously.
        If -1, then it will not be any throttling. If
        None, it will be inferred from _get_batch_size()
    callback: fsspec.callbacks.Callback instance
        Gets a relative_update when each coroutine completes
    timeout: number or None
        If given, each coroutine times out after this time. Note that, since
        there are multiple batches, the total run time of this function will in
        general be longer
    return_exceptions: bool
        Same meaning as in asyncio.gather
    nofiles: bool
        If inferring the batch_size, does this operation involve local files?
        If yes, you normally expect smaller batches.
    N)rP   rH   r   c              
   “   sn   z\ztj| ˆd�I d H |fW W ¢>S  tk
rX } zˆs8‚ ||f W Y ¢W ¢S d }~X Y nX W 5 ˆ   d¡ X d S )Nr   r   )Zrelative_updater   r   r   )r!   ÚiÚe©ÚcallbackÚreturn_exceptionsr   r   r   Ú	_run_coroó   s    &z'_run_coros_in_chunks.<locals>._run_coror   )Zreturn_when)
rR   ÚlenÚAssertionErrorr   Úaddr   Zensure_futurer*   ZFIRST_COMPLETEDÚpop)ÚcorosÚ
batch_sizerX   r   rY   rP   rZ   rU   ÚnÚresultsÚpendingÚdoner"   Úkr   rW   r   Ú_run_coros_in_chunksÍ   s*    



rf   Ú_lsÚ	_cat_fileÚ	_get_fileÚ	_put_fileÚ_rm_fileÚ_cp_fileÚ
_pipe_fileÚ_expand_pathÚ_infoÚ_isfileÚ_isdirÚ_existsÚ_walkÚ_globÚ_findÚ_duÚ_sizeÚ_mkdirÚ	_makedirsc                       sh  e Zd ZdZdZdZdZddddœ‡ fdd„
Zedd	„ ƒZ	d
d„ Z
dKdd„Zdd„ Zdd„ ZdLdd„ZdMdd„ZdNdd„Zdd„ ZdOdd„ZdPdd„ZdQd!d"„ZdRd#d$„Zdeddfd%d&„Zd'd(„ Zdedfd)d*„Zd+d,„ Zd-d.„ Zd/d0„ ZdSd1d2„Zd3d4„ Zd5d6„ ZdTd7d8„Z dUd:d;„Z!dVd<d=„Z"dWd>d?„Z#dXd@dA„Z$dYdBdC„Z%dZdDdE„Z&d[dFdG„Z'd\dIdJ„Z(‡  Z)S )]ÚAsyncFileSystemak  Async file operations, default implementations

    Passes bulk operations to asyncio.gather for concurrent operation.

    Implementations that have concurrent batch operations and/or async methods
    should inherit from this class instead of AbstractFileSystem. Docstrings are
    copied from the un-underscored method in AbstractFileSystem, if not given.
    TFN)Úasynchronousr   r`   c                   s@   || _ t ¡ | _|s"|ptƒ | _nd | _|| _tƒ j||Ž d S ©N)	r{   r;   ÚgetpidÚ_pidrD   Ú_loopr`   ÚsuperÚ__init__)r2   r{   r   r`   r/   r0   ©Ú	__class__r   r   r�   ;  s    
zAsyncFileSystem.__init__c                 C   s   | j t ¡ krtdƒ‚| jS )NzThis class is not fork-safe)r~   r;   r}   r%   r   ©r2   r   r   r   r   E  s    zAsyncFileSystem.loopc                 Ë   s   t ‚d S r|   ©r(   ©r2   Úpathr0   r   r   r   rk   K  s    zAsyncFileSystem._rm_filec                 ‹   sF   |pˆj }ˆj||d�I d H }t‡ ‡fdd„t|ƒD ƒ|dd�I d H S )N©Ú	recursivec                    s   g | ]}ˆj |fˆ Ž‘qS r   )rk   ©Ú.0Úp©r0   r2   r   r   Ú
<listcomp>S  s     z'AsyncFileSystem._rm.<locals>.<listcomp>T©r`   rP   )r`   rn   rf   Úreversed)r2   r‡   r‰   r`   r0   r   r�   r   Ú_rmN  s    
ýzAsyncFileSystem._rmc                 Ë   s   t ‚d S r|   r…   )r2   Úpath1Úpath2r0   r   r   r   rl   X  s    zAsyncFileSystem._cp_filec                 Ã   s&   |   ||¡I d H  |  |¡I d H  d S r|   )rl   rk   )r2   r’   r“   r   r   r   Ú_mv_file[  s    zAsyncFileSystem._mv_filec                 ‹   sb  |d kr|rd}n|d krd}t |tƒr<t |tƒr<|}|}	n¸t |tƒ}
ˆj|||d�I d H }|
rŒ|rl|d k	rŒ‡fdd„|D ƒI d H }|sŒd S t|ƒdk}t |tƒo¸t|ƒp¸ˆ |¡I d H }|
oàt|ƒrÊ|pàt|ƒ oà|oàt|ƒ }t||||
 d�}	|püˆj	}‡ ‡fdd„t
||	ƒD ƒ}t||d	d	d
�I d H }tt|ƒD ]$}|dk�rVt |tƒ�rV�q8|‚�q8d S )NÚignoreÚraise)Úmaxdepthr‰   c                 “   s(   g | ] }t |ƒsˆ  |¡I d H s|‘qS r|   ©r   rq   rŠ   r„   r   r   rŽ   z  s      z)AsyncFileSystem._copy.<locals>.<listcomp>r   ©ÚexistsÚflattenc                    s    g | ]\}}ˆj ||fˆ Ž‘qS r   )rl   )r‹   Úp1Zp2r�   r   r   rŽ   ‘  s     T)r`   rY   rP   )r+   ÚlistÚstrrn   r[   r   rq   r   r   r`   Úziprf   Úfilterr   ÚFileNotFoundError)r2   r’   r“   r‰   Úon_errorr—   r`   r0   Zpaths1Zpaths2Úsource_is_strÚsource_is_fileÚdest_is_dirrš   r_   r"   r#   r   r�   r   Ú_copy_  sZ    

  ÿ
ÿ
ÿþü
   ÿzAsyncFileSystem._copyÚ	overwritec                 Ë   s   t ‚d S r|   r…   )r2   r‡   ÚvalueÚmoder0   r   r   r   rm   ›  s    zAsyncFileSystem._pipe_filec                 ‹   sD   t |tƒr||i}|pˆj}t‡ ‡fdd„| ¡ D ƒ|dd�I d H S )Nc                    s    g | ]\}}ˆj ||fˆ Ž‘qS r   )rm   ©r‹   re   Úvr�   r   r   rŽ   £  s     z)AsyncFileSystem._pipe.<locals>.<listcomp>Tr�   )r+   rž   r`   rf   Úitems)r2   r‡   r¨   r`   r0   r   r�   r   Ú_pipež  s    

ýzAsyncFileSystem._pipec                 Ã   sÌ   d}d}|dk	rR|dk rR|dkr0| }d}d}q^|pF|   |¡I dH d }|| }n|dkr^d}|s¼|dk	rœ|dk rœ|dk	r¨|p�|   |¡I dH d }|| }n|dkr¨d}t|tjƒr¼|d8 }d|› d	|› �S )
z"Helper for "Range"-based _cat_fileNFr   Ú TÚsizer   zbytes=ú-)ro   r+   ÚnumbersÚIntegral)r2   ÚurlrB   Úendr¯   Úsuffr   r   r   Ú_process_limits¨  s*    

zAsyncFileSystem._process_limitsc                 Ë   s   t ‚d S r|   r…   )r2   r‡   rB   r´   r0   r   r   r   rh   Â  s    zAsyncFileSystem._cat_filer–   c           
      ‹   s´   ˆj ||d�I d H }‡ ‡fdd„|D ƒ}|p0ˆj}t||ddd�I d H }ˆdkrhttt|ƒdƒ}	|	rh|	‚t|ƒdks�t|tƒs�|d	 ˆ 	|¡kr¨‡fd
d„t
||ƒD ƒS |d	 S d S )Nrˆ   c                    s   g | ]}ˆj |fˆ Ž‘qS r   ©rh   )r‹   r‡   r�   r   r   rŽ   É  s     z(AsyncFileSystem._cat.<locals>.<listcomp>T©r`   rP   rY   r–   Fr   r   c                    s&   i | ]\}}ˆ d kst |ƒs||“qS )Úomit)r   rª   )r¢   r   r   Ú
<dictcomp>×  s
    þ z(AsyncFileSystem._cat.<locals>.<dictcomp>)rn   r`   rf   Únextr    r   r[   r+   r�   Ú_strip_protocolrŸ   )
r2   r‡   r‰   r¢   r`   r0   Úpathsr_   Úoutr#   r   )r0   r¢   r2   r   Ú_catÅ  s.    
   ÿ
ÿþý
þzAsyncFileSystem._catrS   c           	      ‹   sª   |dk	rt ‚t|tƒst‚t|tƒs2|gt|ƒ }t|tƒsJ|gt|ƒ }t|ƒt|ƒksjt|ƒt|ƒkrnt‚‡ ‡fdd„t|||ƒD ƒ}|p’ˆj}t	||ddd�I dH S )aY  Get the contents of byte ranges from one or more files

        Parameters
        ----------
        paths: list
            A list of of filepaths on this filesystems
        starts, ends: int or list
            Bytes limits of the read. If using a single int, the same value will be
            used to read all the specified files.
        Nc                    s*   g | ]"\}}}ˆj |f||d œˆ —Ž‘qS ))rB   r´   r·   )r‹   rŒ   ÚsrV   r�   r   r   rŽ   ÿ  s   ÿz/AsyncFileSystem._cat_ranges.<locals>.<listcomp>Tr¸   )
r(   r+   r�   Ú	TypeErrorr   r[   rN   rŸ   r`   rf   )	r2   r½   ZstartsZendsZmax_gapr`   r¢   r0   r_   r   r�   r   Ú_cat_rangesß  s(    


 
þ
   ÿzAsyncFileSystem._cat_rangesc                 Ë   s   t ‚d S r|   r…   )r2   ÚlpathÚrpathr©   r0   r   r   r   rj     s    zAsyncFileSystem._put_filec                 ‹   s¤  t |tƒrt |tƒr|}|}	nÈt |tƒ}
|
r4t|ƒ}tƒ ‰ ˆ j|||d�}	|
rt|rZ|dk	rt‡ fdd„|	D ƒ}	|	stdS t|	ƒdk}t |tƒo t|ƒp ˆ |¡I dH }ˆ 	|¡}|
oÒt
|ƒr¼|pÒt
|ƒ oÒ|oÒt|ƒ }t|	|||
 d�}dd„ |	D ƒ‰‡fd	d„t|	|ƒD ƒ}‡fd
d„t|	|ƒD ƒ}tj‡fdd„|D ƒŽ I dH  |�pLˆj}g }| t|ƒ¡ |D ]*\}}| ˆj¡}| |||f|Ž¡ �qdt|||d�I dH S )aÆ  Copy file(s) from local.

        Copies a specific file or tree of files (if recursive=True). If rpath
        ends with a "/", it will be assumed to be a directory, and target files
        will go within.

        The put_file method will be called concurrently on a batch of files. The
        batch_size option can configure the amount of futures that can be executed
        at the same time. If it is -1, then all the files will be uploaded concurrently.
        The default can be set for this instance by passing "batch_size" in the
        constructor, or for all instances by setting the "gather_batch_size" key
        in ``fsspec.config.conf``, falling back to 1/8th of the system limit .
        ©r‰   r—   Nc                    s"   g | ]}t |ƒsˆ  |¡s|‘qS r   )r   ÚisdirrŠ   )Úfsr   r   rŽ   /  s      
 z(AsyncFileSystem._put.<locals>.<listcomp>r   r™   c                 S   s   i | ]}|t j |¡“qS r   )r;   r‡   rÆ   )r‹   Úlr   r   r   rº   D  s      z(AsyncFileSystem._put.<locals>.<dictcomp>c                    s   g | ]\}}ˆ | r|‘qS r   r   ©r‹   rÈ   Úr©Úis_dirr   r   rŽ   E  s      c                    s    g | ]\}}ˆ | s||f‘qS r   r   rÉ   rË   r   r   rŽ   F  s      c                    s   g | ]}ˆ j |d d�‘qS ©T)Úexist_ok)ry   )r‹   Údr„   r   r   rŽ   H  s     ©r`   rX   )r+   r�   rž   r
   r	   Zexpand_pathr[   r   rq   r¼   r   r   rŸ   r   Zgatherr`   Úset_sizeÚbranch_cororj   Úappendrf   )r2   rÃ   rÄ   r‰   rX   r`   r—   r0   ÚrpathsÚlpathsr£   r¤   r¥   rš   ZrdirsZ
file_pairsr_   ZlfileÚrfileZput_filer   )rÇ   rÌ   r2   r   Ú_put  sT    

ÿ
þü  ÿzAsyncFileSystem._putc                 Ë   s   t ‚d S r|   r…   )r2   rÄ   rÃ   r0   r   r   r   ri   U  s    zAsyncFileSystem._get_filec                 ‹   sd  t |tƒrt |tƒr|}|}nÎt |tƒ}	|	o4t|ƒ }
ˆ  |¡}ˆ j|||d�I dH }|	r†|rf|dk	r†‡ fdd„|D ƒI dH }|s†dS t|ƒ}t|ƒdk}t |tƒo¶t|ƒp¶tƒ  	|¡}|	oØt
|ƒrÈ|pØt
|ƒ oØ|oØ|
}t||||	 d�}dd„ |D ƒ | dˆ j¡}g }| t|ƒ¡ t||ƒD ]*\}}| ˆ j¡}| |||f|Ž¡ �q$t|||d	�I dH S )
a  Copy file(s) to local.

        Copies a specific file or tree of files (if recursive=True). If lpath
        ends with a "/", it will be assumed to be a directory, and target files
        will go within. Can submit a list of paths, which may be glob-patterns
        and will be expanded.

        The get_file method will be called concurrently on a batch of files. The
        batch_size option can configure the amount of futures that can be executed
        at the same time. If it is -1, then all the files will be uploaded concurrently.
        The default can be set for this instance by passing "batch_size" in the
        constructor, or for all instances by setting the "gather_batch_size" key
        in ``fsspec.config.conf``, falling back to 1/8th of the system limit .
        rÅ   Nc                 “   s(   g | ] }t |ƒsˆ  |¡I d H s|‘qS r|   r˜   rŠ   r„   r   r   rŽ   ~  s      z(AsyncFileSystem._get.<locals>.<listcomp>r   r™   c                 S   s"   g | ]}t jt j |¡d d�‘qS rÍ   )r;   Úmakedirsr‡   Údirname)r‹   Zlpr   r   r   rŽ   •  s     r`   rÐ   )r+   r�   rž   r   r¼   rn   r
   r[   r	   rÆ   r   r   r^   r`   rÑ   rŸ   rÒ   ri   rÓ   rf   )r2   rÄ   rÃ   r‰   rX   r—   r0   rÔ   rÕ   r£   Zsource_not_trailing_sepr¤   r¥   rš   r`   r_   Zget_filer   r„   r   Ú_getX  sX    

  ÿ
ÿ
ÿþü  ÿzAsyncFileSystem._getc                 Ã   s.   z|   |¡I d H d dkW S    Y dS X d S )NÚtypeÚfileF)ro   ©r2   r‡   r   r   r   rp   ¡  s    zAsyncFileSystem._isfilec                 Ã   s6   z|   |¡I d H d dkW S  tk
r0   Y dS X d S )NrÛ   Ú	directoryF)ro   ÚOSErrorrÝ   r   r   r   rq   §  s    zAsyncFileSystem._isdirc                 Ã   s   |   |¡I d H  dd ¡S )Nr¯   )ro   ÚgetrÝ   r   r   r   rw   ­  s    zAsyncFileSystem._sizec                 ƒ   s*   |pˆ j }t‡ fdd„|D ƒ|d�I d H S )Nc                    s   g | ]}ˆ   |¡‘qS r   )rw   rŠ   r„   r   r   rŽ   ³  s     z*AsyncFileSystem._sizes.<locals>.<listcomp>)r`   )r`   rf   )r2   r½   r`   r   r„   r   Ú_sizes°  s
    
 ÿzAsyncFileSystem._sizesc                 Ë   s6   z| j |f|ŽI d H  W dS  tk
r0   Y dS X d S )NTF)ro   r¡   r†   r   r   r   rr   ¶  s
    zAsyncFileSystem._existsc                 Ë   s   t ‚d S r|   r…   r†   r   r   r   ro   ½  s    zAsyncFileSystem._infoc                 Ë   s   t ‚d S r|   r…   )r2   r‡   Údetailr0   r   r   r   rg   À  s    zAsyncFileSystem._lsr¹   c              
   K  s¼  |d k	r|dk rt dƒ‚|  |¡}i }i }i }| dd¡}z | j|fddi|—ŽI d H }	W nf ttfk
rÀ }
 zD|dkr|‚ nt|ƒrŒ||
ƒ |rž|i i fV  n|g g fV  W Y ¢d S d }
~
X Y nX |	D ]h}|d  d¡}| dd¡d	 }|d
 dk�r||k�r|||< |||< qÆ||k�r&||d< qÆ|||< qÆ|�rD|||fV  n|t	|ƒt	|ƒfV  |d k	�rx|d8 }|dk �rxd S |D ]:}| j
|| f||dœ|—Ž2 z3 d H W }|V  �qœ6 �q|d S )Nr   úmaxdepth must be at least 1râ   FTr–   r<   ú/rH   rÛ   rÞ   r®   )r—   râ   )rN   r¼   r^   rg   r¡   rß   ÚcallableÚrstripÚrsplitr�   rs   )r2   r‡   r—   r¢   r0   Z	full_dirsÚdirsÚfilesrâ   ZlistingrV   ÚinfoÚpathnamer<   rÏ   rQ   r   r   r   rs   Ã  sX    
 





ÿ ÿÿzAsyncFileSystem._walkc                 ‹   sf  |d k	r|dk rt dƒ‚dd l}tjjr8tjjtjjfntjjf}| |¡}|  |¡}|pp| tdd„ |D ƒƒ¡‰ | 	d¡dkrŠ| 	d¡nt
|ƒ}| 	d¡dkrª| 	d¡nt
|ƒ}| 	d¡dkrÊ| 	d¡nt
|ƒ}	t|||	ƒ}
| d	d
¡}t|ƒ�s@| j|f|ŽI d H �r0|�s|gS || j|f|ŽI d H iS n|�s:g S i S nnd|d |
… k�r�|d |
…  d¡}
|d |
d … }||
d d …  d¡d }nd}||
d d …  d¡d }d|k�rô|d k	�rð| 	d¡}||d …  d¡d }|| | }nd }| j|f|dddœ|—ŽI d H }t||�r"dnd ƒ‰| ˆ¡‰‡ ‡fdd„t| ¡ ƒD ƒ}|�rZ|S t|ƒS d S )Nr   rã   r   c                 s   s   | ]}|d  V  qdS )ú**Nr   )r‹   Úsepr   r   r   Ú	<genexpr>  s     z(AsyncFileSystem._glob.<locals>.<genexpr>Ú*ú?ú[râ   Frä   r®   rì   T)r—   Úwithdirsrâ   c                    s8   i | ]0\}}ˆ  ˆ r(|d  dkr(|d n|¡r||“qS )rÛ   rÞ   rä   )Úmatch)r‹   rŒ   rê   ©Zappend_slash_to_dirnameÚpatternr   r   rº   /  s   ÿ
ÿ
ýþ z)AsyncFileSystem._glob.<locals>.<dictcomp>)rN   Úrer;   r‡   Úaltseprí   Úendswithr¼   ÚtupleÚfindr[   Úminr^   r   rr   ro   ÚrindexÚcountru   r   ÚcompileÚsortedr¬   r�   )r2   r‡   r—   r0   rö   ÚsepsZends_with_sepZidx_starZ	idx_qmarkZ	idx_braceÚmin_idxrâ   ÚrootÚdepthZidx_double_starsZdepth_double_starsZallpathsr¾   r   rô   r   rt   ù  sf    "

ÿ   



ÿ  ÿÿ

þ
zAsyncFileSystem._globc                 Ë   s^   i }| j |fd|i|—ŽI d H D ]$}|  |¡I d H }|d ||d < q |rVt| ¡ ƒS |S d S )Nr—   r¯   r<   )ru   ro   ÚsumÚvalues)r2   r‡   Útotalr—   r0   ÚsizesÚfrê   r   r   r   rv   >  s     zAsyncFileSystem._duc           
      ‹   sà   |   |¡}i ‰ | dd¡}|rJ|dkrJ|  |¡I d H rJ|  |¡I d H ˆ |< | j||fddi|—Ž2 z83 d H W \}}}|r‚| |¡ ˆ  dd„ | ¡ D ƒ¡ qb6 ˆ sº|  |¡I d H rºi ˆ |< tˆ ƒ}	|sÊ|	S ‡ fdd„|	D ƒS d S )Nrâ   Fr®   Tc                 S   s   i | ]\}}|d  |“qS )r<   r   )r‹   r<   rê   r   r   r   rº   W  s      z)AsyncFileSystem._find.<locals>.<dictcomp>c                    s   i | ]}|ˆ | “qS r   r   )r‹   r<   ©r¾   r   r   rº   `  s      )	r¼   r^   rq   ro   rs   Úupdater¬   rp   rÿ   )
r2   r‡   r—   rò   r0   râ   rQ   rè   ré   Únamesr   r	  r   ru   I  s    
*
zAsyncFileSystem._findc                 ƒ   s8  |d k	r|dk rt dƒ‚t|tƒr:ˆ  |g||¡I d H }nètƒ }‡ fdd„|D ƒ}|D ]Ê}t|ƒrÎtˆ j||d�I d H ƒ}||O }|rV|d k	r˜|dkr˜qV|tˆ jt|ƒ||d k	r¸|d nd d�I d H ƒO }qVn&|rôtˆ j||dd�I d H ƒ}||O }||krV|d	k�sˆ  	|¡I d H rV| 
|¡ qV|�s0t|ƒ‚t|ƒS )
Nr   rã   c                    s   g | ]}ˆ   |¡‘qS r   )r¼   rŠ   r„   r   r   rŽ   j  s     z0AsyncFileSystem._expand_path.<locals>.<listcomp>)r—   rÅ   T)r—   rò   F)rN   r+   rž   rn   r   r   rt   r�   ru   rr   r]   r¡   rÿ   )r2   r‡   r‰   r—   r¾   rŒ   ÚbitZrecr   r„   r   rn   b  s:    
ý
ÿ"zAsyncFileSystem._expand_pathc                 Ë   s   d S r|   r   )r2   r‡   Zcreate_parentsr0   r   r   r   rx   ‡  s    zAsyncFileSystem._mkdirc                 Ã   s   d S r|   r   )r2   r‡   rÎ   r   r   r   ry   Š  s    zAsyncFileSystem._makedirsÚrbc                 Ë   s   d|ks|  d¡rt‚t‚d S )NÚbÚcompression)rà   rN   r(   )r2   r‡   r©   r0   r   r   r   Ú
open_async�  s    zAsyncFileSystem.open_async)FN)FNNN)r§   )NN)NN)Fr–   N)NNrS   )r§   )N)T)Nr¹   )N)TN)NF)FN)T)F)r  )*Ú__name__Ú
__module__Ú__qualname__Ú__doc__Z
async_implÚmirror_sync_methodsZdisable_throttlingr�   Úpropertyr   rk   r‘   rl   r”   r¦   rm   r­   r¶   rh   r¿   rÂ   rj   r   r×   ri   rÚ   rp   rq   rw   rá   rr   ro   rg   rs   rt   rv   ru   rn   rx   ry   r  Ú__classcell__r   r   r‚   r   rz   *  sl   



    ù
<



     ÿ
   ù
)
ù
Jú
I


6
E


%

rz   c                 C   s¼   ddl m} tttƒ D ]ž}| d¡s(q|dd… }t |¡rt 	t
| |dƒ¡}t
t
| |dƒddƒ}|t
||dƒk}|r|rtt
| |ƒ| d	�}t| ||ƒ |jst
t
||dƒd
dƒ|_qdS )a,  Populate sync and async methods for obj

    For each method will create a sync version if the name refers to an async method
    (coroutine) and there is no override in the child class; will create an async
    method for the corresponding sync method if there is no implementation.

    Uses the methods specified in
    - async_methods: the set that an implementation is expected to provide
    - default_async_methods: that can be derived from their sync version in
      AbstractFileSystem
    - AsyncFileSystem: async-specific default coroutines
    r   )r   rQ   r   NFÚ__func__r®   )r4   r  )Zfsspecr   Úasync_methodsÚdirrz   Ú
startswithÚprivateró   ÚinspectÚiscoroutinefunctionÚgetattrr8   Úsetattrr  )r4   r   ÚmethodZsmethodZiscoZunsyncÚ
is_defaultZmthr   r   r   r  “  s$    


  ÿr  c                   @   s   e Zd ZdS )ÚFSSpecCoroutineCancelN)r  r  r  r   r   r   r   r#  ³  s   r#  Tc              	      s¾   dd l ‰dd„ tj td ¡D ƒ}| r4dd„ |D ƒ ‡‡fdd„|D ƒ}|rº|D ]h‰ ˆ j}ˆ  ¡  tjj 	ˆ |¡ tjj ˆ ¡ ‡ fdd„|D ƒ zˆ j
 |¡ W qP |k
r¶   Y qPX qP|S )Nr   c                 S   s   g | ]}|  ¡ s|‘qS r   )rd   )r‹   Útr   r   r   rŽ   ¼  s      z'_dump_running_tasks.<locals>.<listcomp>c                 S   s   g | ]}|  ¡ ‘qS r   )Úprint_stack©r‹   Útaskr   r   r   rŽ   ¾  s     c              	      sL   g | ]D}|j jj|j jjj|j jjj|j jjˆ  |j j¡ˆr@|nd dœ‘qS )N)ÚlocalsrÜ   Ú	firstlineZlineloÚstackr'  )Ú_coroÚcr_frameÚf_localsÚf_codeÚco_filenameÚco_firstlinenoÚf_linenoÚformat_stackr&  )Ú	tracebackÚ	with_taskr   r   rŽ   ¿  s   	ù


úc                    s   g | ]}|d  ˆ ƒ‘qS )r   r   )r‹   Úcb)r$  r   r   rŽ   Ð  s     )r3  r   ÚtasksZ	all_tasksr   Z
_callbacksÚcancelZfuturesÚFutureZset_exceptionr+  Úthrow)Zprintoutr7  Úexcr4  r6  r¾   Zcbsr   )r$  r3  r4  r   Ú_dump_running_tasks·  s&    	÷r;  c                   @   sZ   e Zd Zddd„Zdd„ Zdd„ Zdd	d
„Zdd„ Zdd„ Zdd„ Z	dd„ Z
ddd„ZdS )ÚAbstractAsyncStreamedFilerH   c                 Ã   s„   |dkrdnt |ƒ}| jdkr&tdƒ‚|dk r:| j| j }| jrHtdƒ‚|dkrTdS |  | j| j| ¡I dH }|  jt|ƒ7  _|S )zÇ
        Return data from cache, or fetch pieces as necessary

        Parameters
        ----------
        length: int (-1)
            Number of bytes to read; if <0, all remaining bytes.
        NrH   r  zFile not in read moder   úI/O operation on closed file.ó    )Úintr©   rN   r¯   ÚlocÚclosedÚ_fetch_ranger[   )r2   Úlengthr¾   r   r   r   ÚreadÜ  s    	
zAbstractAsyncStreamedFile.readc                 Ã   sj   | j dkrtdƒ‚| jr tdƒ‚| jr.tdƒ‚| j |¡}|  j|7  _| j ¡ | jkrf|  	¡ I dH  |S )zë
        Write data to buffer.

        Buffer only sent on flush() or if buffer is greater than
        or equal to blocksize.

        Parameters
        ----------
        data: bytes
            Set of bytes to be written.
        >   ÚwbÚabzFile not in write moder=  z0This file has been force-flushed, can only closeN)
r©   rN   rA  ÚforcedÚbufferÚwriter@  ÚtellÚ	blocksizeÚflush)r2   Údatar¾   r   r   r   rI  ó  s    
zAbstractAsyncStreamedFile.writec                 Ã   s|   t | ddƒrdS | jrdS | jdkr,d| _nF| jsD| jdd�I dH  | jdk	rr| j | j¡ | j | j 	| j¡¡ d| _dS )z=Close file

        Finalizes writes, discards cache
        Z_unclosableFNr  T)Úforce)
r  rA  r©   ÚcacherG  rL  rÇ   Zinvalidate_cacher‡   Z_parentr„   r   r   r   Úclose  s    

zAbstractAsyncStreamedFile.closeFc                 Ã   sÄ   | j rtdƒ‚|r | jr tdƒ‚|r*d| _| jdkr8d S |sP| j ¡ | jk rPd S | jd krˆd| _z|  ¡ I d H  W n   d| _ ‚ Y nX | j	|d�I d H dk	rÀ|  j| j 
dd¡7  _t ¡ | _d S )	NzFlush on closed filez+Force flush cannot be called more than onceT>   rE  rF  r   )ÚfinalFé   )rA  rN   rG  r©   rH  rJ  rK  ÚoffsetÚ_initiate_uploadÚ_upload_chunkÚseekÚioÚBytesIO)r2   rN  r   r   r   rL     s(    


zAbstractAsyncStreamedFile.flushc                 Ã   s   | S r|   r   r„   r   r   r   Ú
__aenter__=  s    z$AbstractAsyncStreamedFile.__aenter__c                 Ã   s   |   ¡ I d H  d S r|   )rP  )r2   Úexc_typeÚexc_valÚexc_tbr   r   r   Ú	__aexit__@  s    z#AbstractAsyncStreamedFile.__aexit__c                 Ã   s   t ‚d S r|   r…   )r2   rB   r´   r   r   r   rB  C  s    z&AbstractAsyncStreamedFile._fetch_rangec                 Ã   s   d S r|   r   r„   r   r   r   rT  F  s    z*AbstractAsyncStreamedFile._initiate_uploadc                 Ã   s   t ‚d S r|   r…   )r2   rQ  r   r   r   rU  I  s    z'AbstractAsyncStreamedFile._upload_chunkN)rH   )F)F)r  r  r  rD  rI  rP  rL  rY  r]  rB  rT  rU  r   r   r   r   r<  Ø  s   

r<  )N)N)F)?r   Zasyncio.eventsr6   r  rW  r±   r;   rö   r   Ú
contextlibr   Úglobr   Útypingr   r   Ú	callbacksr   Ú
exceptionsr   Zimplementations.localr	   r
   r   Úspecr   r   Úutilsr   r   r   rþ   r  r   r   r   r'   r   r   r$   r1   r8   r>   rD   rK   rE   rO   rM   rß   r  rL   rJ   rR   ÚboolrT   rf   r  rz   r  r   r#  r;  r<  r   r   r   r   Ú<module>   s¢   

-



ú
Gí    m    ÿ
!