a
    iDf                     @  st  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mZ d dlmZ e
rd dlZd dlmZ ejdkrd d	lmZ nee Zd d
lmZ d dlmZ g dZdddddZdddddZdddddZdddddZdddddZdd dd!d"Z ej!d#d$d d%d&d'Z"dd%d(d)Z#d*d+d,d-d.Z$d+d/d0d1d2Z%dS )3    )annotationsN)entry_points)TYPE_CHECKINGAnyCallable)BACKEND_ENTRYPOINTSBackendEntrypoint)module_available)
EntryPoint   
   )EntryPoints)BufferedIOBase)AbstractDataStore)Znetcdf4Zh5netcdfZscipyr   zlist[EntryPoint])entrypointsreturnc           
      C  s   t | dd d}tj|dd d}g }|D ]n\}}tt|}||d  t|}|dkr*dd |D }|d }	td	| d
| d| d|	 d	t	 q*|S )Nc                 S  s   | j S Nnameep r   d/nfs/NAS7/SABIOD/METHODE/ermites/ermites_venv/lib/python3.9/site-packages/xarray/backends/plugins.py<lambda>       z#remove_duplicates.<locals>.<lambda>)keyc                 S  s   | j S r   r   r   r   r   r   r       r   r      c                 S  s   g | ]}|j d d qS ):r   )valuesplit).0er   r   r   
<listcomp>)   r   z%remove_duplicates.<locals>.<listcomp>zFound z! entrypoints for the engine name z:
 z.
 The entrypoint z will be used.)
sorted	itertoolsgroupbylistsetappendlenwarningswarnRuntimeWarning)
r   Zentrypoints_sortedZentrypoints_groupedZunique_entrypointsr   Z_matchesmatchesZmatches_lenZall_module_namesZselected_module_namer   r   r   remove_duplicates   s&    r/   r   ztuple[str, ...])open_datasetr   c                 C  sf   t | }|j}g }| D ]@\}}|jt jjt jjfv rJtd| d|dkr|	| qt
|S )NzAll the parameters in zB signature should be explicit. *args and **kwargs is not supportedself)inspect	signature
parametersitemskind	ParameterVAR_KEYWORDVAR_POSITIONAL	TypeErrorr)   tuple)r0   r3   r4   Zparameters_listr   paramr   r   r   detect_parameters4   s    

r=   z"dict[str, type[BackendEntrypoint]]c                 C  sj   i }| D ]\}|j }z| }|||< W q tyb } z$td|d| t W Y d }~qd }~0 0 q|S )NzEngine z loading failed:
)r   load	Exceptionr+   r,   r-   )r   backend_entrypointsZ
entrypointr   backendexr   r   r   backends_dict_from_pkgF   s    0rC   None)r@   r   c                 C  s0   |   D ]"\}}|jd u r|j}t||_qd S r   )r5   Zopen_dataset_parametersr0   r=   )r@   _rA   r0   r   r   r   set_missing_parametersT   s    
rF   c                   sL   i }t D ]"} |d d ur |||< q| fddt D  |S )Nc                   s   i | ]}| | qS r   r   )r!   r   r@   r   r   
<dictcomp>e   r   z!sort_backends.<locals>.<dictcomp>)STANDARD_BACKENDS_ORDERgetpopupdater$   )r@   Zordered_backends_entrypointsZbe_namer   rG   r   sort_backends]   s    rM   zdict[str, BackendEntrypoint]c                 C  sn   i }t  D ]$\}\}}|d u s(t|r|||< qt| }t|}|| t|}t| dd | D S )Nc                 S  s   i | ]\}}|| qS r   r   )r!   r   rA   r   r   r   rH   t   r   z!build_engines.<locals>.<dictcomp>)r   r5   r	   r/   rC   rL   rM   rF   )r   r@   backend_namemodule_namerA   Zentrypoints_uniqueZexternal_backend_entrypointsr   r   r   build_enginesj   s    

rP   r   )maxsize)r   c                  C  s,   t jdkrtdd} nt dg } t| S )a  
    Return a dictionary of available engines and their BackendEntrypoint objects.

    Returns
    -------
    dictionary

    Notes
    -----
    This function lives in the backends namespace (``engs=xr.backends.list_engines()``).
    If available, more information is available about each backend via ``engs["eng_name"]``.

    # New selection mechanism introduced with Python 3.10. See GH6514.
    r   zxarray.backends)group)sysversion_infor   rJ   rP   )r   r   r   r   list_enginesw   s    
rU   c                   C  s   t   dS )z:Refreshes the backend engines based on installed packages.N)rU   cache_clearr   r   r   r   refresh_engines   s    rW   z;str | os.PathLike[Any] | BufferedIOBase | AbstractDataStorezstr | type[BackendEntrypoint])
store_specr   c           	   	   C  s  t  }| D ]X\}}z|| r,|W   S W q tyB    Y q tyd   t|dt Y q0 qg }t D ]P\}\}}z| }|| r|	| W qt ty   t|dt Y qt0 qtdd |D }|s|rd| d}qd}nd| d}t
|d S )	Nz fails while guessingc                 S  s   g | ]}|d kr|qS )storer   )r!   kr   r   r   r#      r   z guess_engine.<locals>.<listcomp>zHdid not find a match in any of xarray's currently installed IO backends a  . Consider explicitly selecting one of the installed engines via the ``engine`` parameter, or installing additional IO dependencies, see:
https://docs.xarray.dev/en/stable/getting-started-guide/installing.html
https://docs.xarray.dev/en/stable/user-guide/io.htmla  xarray is unable to open this file because it has no currently installed IO backends. Xarray's read/write support requires installing optional IO dependencies, see:
https://docs.xarray.dev/en/stable/getting-started-guide/installing.html
https://docs.xarray.dev/en/stable/user-guide/iozIfound the following matches with the input file in xarray's IO backends: z. But their dependencies may not be installed, see:
https://docs.xarray.dev/en/stable/user-guide/io.html 
https://docs.xarray.dev/en/stable/getting-started-guide/installing.html)rU   r5   Zguess_can_openPermissionErrorr?   r+   r,   r-   r   r)   
ValueError)	rX   enginesenginerA   Zcompatible_enginesrE   Zbackend_clsZinstalled_engines	error_msgr   r   r   guess_engine   s>    


	r`   r   )r^   r   c                 C  sh   t | tr:t }| |vr0td|  dt| ||  }n*t | trVt| trV|  }ntd|  |S )z3Select open_dataset method based on current engine.zunrecognized engine z must be one of: zLengine must be a string or a subclass of xarray.backends.BackendEntrypoint: )	
isinstancestrrU   r\   r'   type
issubclassr   r:   )r^   r]   rA   r   r   r   get_backend   s    

re   )&
__future__r   	functoolsr2   r%   rS   r+   importlib.metadatar   typingr   r   r   Zxarray.backends.commonr   r   Zxarray.core.utilsr	   osr
   rT   r   r'   ior   r   rI   r/   r=   rC   rF   rM   rP   	lru_cacherU   rW   r`   re   r   r   r   r   <module>   s:   
	
5