U
    /e                     @  s   d dl mZ d dlmZmZ d dlmZmZmZm	Z	 d dl
mZ d dlmZ d dlmZ erxd dlmZ edZe	d	ZG d
d dZedddd Ze	dddZG dd dee ZdS )    )annotations)	lru_cachewraps)TYPE_CHECKINGCallableGenericTypeVar)configentry_points)funcname)	ParamSpecBackendFuncParamsBackendFuncReturnc                   @  s   e Zd ZdZdS )DaskBackendEntrypointa  Base Collection-Backend Entrypoint Class

    Most methods in this class correspond to collection-creation
    for a specific library backend. Once a collection is created,
    the existing data will be used to dispatch compute operations
    within individual tasks. The backend is responsible for
    ensuring that these data-directed dispatch functions are
    registered when ``__init__`` is called.
    N)__name__
__module____qualname____doc__ r   r   1/tmp/pip-unpacked-wheel-dbjnr7gq/dask/backends.pyr      s   
r      )maxsizec                 C  s   t | }dd |D S )Nc                 S  s   i | ]}|j |qS r   )name).0epr   r   r   
<dictcomp>"   s      z&detect_entrypoints.<locals>.<dictcomp>r
   )Zentry_point_nameentrypointsr   r   r   detect_entrypoints   s    r   BackendEntrypointType)boundc                   @  s   e Zd ZU dZded< ded< ded< ded< ded	< d"dddddddZddddddZddddZeddddZ	e	j
ddddZ	d#ddddddZddd d!Zd
S )$CreationDispatchz9Simple backend dispatch for collection-creation functionsz dict[str, BackendEntrypointType]_lookupstr_module_name_config_field_defaultztype[BackendEntrypointType]_entrypoint_classNz
str | None)module_namedefaultentrypoint_classr   c                 C  s2   i | _ || _| d| _|| _|| _|r.|| _d S )Nz.backend)r"   r$   r%   r&   r'   r   )selfr(   r)   r*   r   r   r   r   __init__4   s    zCreationDispatch.__init__r   )r   backendreturnc                 C  s4   t || js&td| j dt| || j|< |S )z:Register a target class for a specific array-backend labelz$This CreationDispatch only supports z registration. Got )
isinstancer'   
ValueErrortyper"   )r+   r   r-   r   r   r   register_backendC   s    
z!CreationDispatch.register_backend)r-   c                 C  sn   z| j | }W nH tk
rV   td| j d}||krR| |||    Y S Y nX |S td| dS )z%Return the desired backend entrypointzdask.z	.backendsz#No backend dispatch registered for N)r"   KeyErrorr   r$   r2   loadr0   )r+   r-   implr   r   r   r   dispatchP   s     zCreationDispatch.dispatch)r.   c                 C  s   t | j| jp| jS )z%Return the desired collection backend)r	   getr%   r&   )r+   r   r   r   r-   ]   s    zCreationDispatch.backend)valuec                 C  s   t d| j dd S )Nz#Set the backend by configuring the z option)RuntimeErrorr%   )r+   r8   r   r   r   r-   b   s    zjCallable[[Callable[BackendFuncParams, BackendFuncReturn]], Callable[BackendFuncParams, BackendFuncReturn]])r-   r   r.   c                   s   ddd fdd}|S )zRegister dispatchable functionz.Callable[BackendFuncParams, BackendFuncReturn])fnr.   c                   s@   p| j  }| |  t|  fdd} |_ |S )Nc               
     sd   t  }z|| |W S  tk
r^ } z*t|dt| dj d| |W 5 d }~X Y nX d S )Nz$An error occurred while calling the z method registered to the z backend.
Original Message: )getattr	Exceptionr1   r   r-   )argskwargsfunce)dispatch_namer+   r   r   wrappery   s    
zECreationDispatch.register_inplace.<locals>.decorator.<locals>.wrapper)r   r6   __setattr__r   )r:   
dispatcherrB   r-   r   r+   )rA   r   	decoratorr   s    

z4CreationDispatch.register_inplace.<locals>.decoratorr   )r+   r-   r   rF   r   rE   r   register_inplaceh   s    
z!CreationDispatch.register_inplace)itemc                 C  s   |  | j}t||S )zJ
        Return the appropriate attribute for the current backend
        )r6   r-   r;   )r+   rH   r-   r   r   r   __getattr__   s    zCreationDispatch.__getattr__)N)N)r   r   r   r   __annotations__r,   r2   r6   propertyr-   setterrG   rI   r   r   r   r   r!   +   s"   
  "r!   N)
__future__r   	functoolsr   r   typingr   r   r   r   Zdaskr	   Zdask.compatibilityr   Z
dask.utilsr   Ztyping_extensionsr   r   r   r   r   r   r!   r   r   r   r   <module>   s"   
