U
    xÊ¹j
  ã                   @   sF   d dl Z d dlZd dlZd dlmZmZ ddd„ZG dd„ deƒZdS )é    N)ÚAsyncFileSystemÚrunning_asyncc                    s   t  ˆ ¡‡ fdd„ƒ}|S )aF  
    Wraps a synchronous function to make it awaitable.

    Parameters
    ----------
    func : callable
        The synchronous function to wrap.
    obj : object, optional
        The instance to bind the function to, if applicable.

    Returns
    -------
    coroutine
        An awaitable version of the function.
    c                  Ÿ   s   t jˆ f| ž|ŽI d H S ©N)ÚasyncioZ	to_thread)ÚargsÚkwargs©Úfunc© úb/var/www/html/bwave/python/venv/lib/python3.8/site-packages/fsspec/implementations/asyn_wrapper.pyÚwrapper   s    zasync_wrapper.<locals>.wrapper)Ú	functoolsÚwraps)r	   Úobjr   r
   r   r   Úasync_wrapper   s    r   c                       sN   e Zd ZdZdZdZddœ‡ fdd„
Zedd	„ ƒZd
d„ Z	e
dd„ ƒZ‡  ZS )ÚAsyncFileSystemWrapperaX  
    A wrapper class to convert a synchronous filesystem into an asynchronous one.

    This class takes an existing synchronous filesystem implementation and wraps all
    its methods to provide an asynchronous interface.

    Parameters
    ----------
    sync_fs : AbstractFileSystem
        The synchronous filesystem instance to wrap.
    r   FN)Úasynchronousc                   s@   |d krt ƒ }tƒ j|d|i|—Ž || _| jj| _|  ¡  d S )Nr   )r   ÚsuperÚ__init__Úsync_fsÚprotocolÚ_wrap_all_sync_methods)ÚselfÚfsr   r   r   ©Ú	__class__r
   r   r   0   s    
zAsyncFileSystemWrapper.__init__c                 C   s   d| j j› �S )NZasync_)r   Úfsid)r   r
   r
   r   r   8   s    zAsyncFileSystemWrapper.fsidc                 C   s„   dh}t | jƒD ]n}| d¡s||kr(qt | j|¡}t|tƒrBqt| j|ƒ}t|ƒrt	 
|¡st|| d�}t| d|› �|ƒ qdS )zg
        Wrap all synchronous methods of the underlying filesystem with asynchronous versions.
        ÚopenÚ_)r   N)Údirr   Ú
startswithÚinspectÚgetattr_staticÚ
isinstanceÚpropertyÚgetattrÚcallabler   Úiscoroutinefunctionr   Úsetattr)r   Zexcluded_methodsÚmethod_nameÚattrÚmethodZasync_methodr
   r
   r   r   <   s    
z-AsyncFileSystemWrapper._wrap_all_sync_methodsc                    s(   G ‡ fdd„d| ƒ}dˆ j › d�|_ |S )a¥  
        Create a new class that can be used to instantiate an AsyncFileSystemWrapper
        with lazy instantiation of the underlying synchronous filesystem.

        Parameters
        ----------
        sync_fs_class : type
            The class of the synchronous filesystem to wrap.

        Returns
        -------
        type
            A new class that wraps the provided synchronous filesystem class.
        c                       s   e Zd Z‡ ‡fdd„Z‡  ZS )zJAsyncFileSystemWrapper.wrap_class.<locals>.GeneratedAsyncFileSystemWrapperc                    s   ˆ||Ž}t ƒ  |¡ d S r   )r   r   )r   r   r   r   )r   Úsync_fs_classr
   r   r   `   s    
zSAsyncFileSystemWrapper.wrap_class.<locals>.GeneratedAsyncFileSystemWrapper.__init__)Ú__name__Ú
__module__Ú__qualname__r   Ú__classcell__r
   ©r,   r   r   ÚGeneratedAsyncFileSystemWrapper_   s   r2   ZAsyncZWrapper)r-   )Úclsr,   r2   r
   r1   r   Ú
wrap_classN   s    ÿz!AsyncFileSystemWrapper.wrap_class)r-   r.   r/   Ú__doc__r   Zcachabler   r$   r   r   Úclassmethodr4   r0   r
   r
   r   r   r       s   
r   )N)r   r   r!   Zfsspec.asynr   r   r   r   r
   r
   r
   r   Ú<module>   s
   
