U
    ,jg0                     @   sB   d dl Z d dlZd dlZd dlmZ dddZG dd deZdS )    N)AsyncFileSystemc                    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 G/tmp/pip-unpacked-wheel-ojcr9xjz/fsspec/implementations/asyn_wrapper.pywrapper   s    zasync_wrapper.<locals>.wrapper)	functoolswraps)r   objr   r	   r   r
   async_wrapper   s    r   c                       s@   e Zd ZdZ 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.
    c                    s0   t  j|| d| _|| _| jj| _|   d S )NT)super__init__Zasynchronoussync_fsprotocol_wrap_all_sync_methods)selfr   r   r   	__class__r	   r
   r   -   s
    
zAsyncFileSystemWrapper.__init__c                 C   s   d| j j S )NZasync_)r   fsid)r   r	   r	   r
   r   4   s    zAsyncFileSystemWrapper.fsidc                 C   sv   t | jD ]f}|drq
t| j|}t|tr4q
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.
        _)r   N)dirr   
startswithinspectgetattr_static
isinstancepropertygetattrcallabler   iscoroutinefunctionr   setattr)r   method_nameattrmethodZasync_methodr	   r	   r
   r   8   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
   GeneratedAsyncFileSystemWrapperZ   s   r.   ZAsyncZWrapper)r)   )clsr(   r.   r	   r-   r
   
wrap_classI   s    z!AsyncFileSystemWrapper.wrap_class)r)   r*   r+   __doc__r   r    r   r   classmethodr0   r,   r	   r	   r   r
   r       s   
r   )N)r   r   r   Zfsspec.asynr   r   r   r	   r	   r	   r
   <module>   s
   
