a
    ij',                     @  s  d Z 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Zddl	m
Z
mZmZ ddlmZ g dZejddG d	d
 d
ZG dd dejZeZ[g dZddddddZG dd dZd"ddddddZejddG dd dZdddd d!ZdS )#zlazy imports implementation.    )annotationsN)AnyIteratorOptional)epy)	__builtins__
__cached____doc____file__
__loader____name____package____path____spec__F)eqc                   @  s   e Zd ZU dZded< ded< ejdddZded< ejdd	dZd
ed< eje	dZ
ded< d	Zded< ejd	ddZded< eddddZeddddZeddddZddddZd	S )LazyModuleStatea  State of the lazy module.

  We store the state in a separate object to:

  1) Reduce the risk of collision
  2) Avoid infinite recursion error when typo on a attribute
  3) `@property`, `epy.cached_property` fail when the class is changed

  Attributes:
    module_name: E.g. `jax.numpy`
    alias: E.g. `jnp`
    is_std: Whether the module is part of the standard library (to order
      imports)
    host: `LazyModule` attached to the state
    extra_imports: Additional extra imports to trigger (e.g. `concurrent`
      trigger `concurrent.futures` import)
    _module: Cached original imported module
    trace_repr: Track the trace which trigger the import (Helpful to debug) E.g.
      `Colab` call '.getdoc' on the background, which trigger import.
  strmodule_namealiasF)reprdefaultboolis_stdN
LazyModulehostdefault_factory	list[str]extra_importszOptional[types.ModuleType]_module)r   r   zOptional[str]
trace_reprtypes.ModuleTypereturnc                 C  s6   | j s0dt | _t| j| jd| _| 	  | jS )zReturns the module. )r   )
module_loadedjoin	tracebackformat_stackr    _load_moduler   r   r   _mutate_hostself r-   U/home/ghrups/robot-bench/.venv/lib/python3.9/site-packages/etils/ecolab/lazy_utils.pymoduleN   s    zLazyModuleState.modulec                 C  s
   | j duS )z'Returns `True` if the module is loaded.N)r   r+   r-   r-   r.   r%   a   s    zLazyModuleState.module_loadedc                 C  sd   | j | jkrd| j  S d| j v rP| j jddd\}}|| jkrPd| d| S d| j  d| j S )z#Returns the `import xyz` statement.zimport .   )maxsplitzfrom z import z as )r   r   rsplit)r,   Zleft_importZright_importr-   r-   r.   import_statementf   s    

z LazyModuleState.import_statementNonec                 C  sD   | j s
J t }tD ]*}t| j||}||urt| j|| qdS )zAWhen the module is first loaded, update `__doc__`, `__file__`,...N)r%   object_MODULE_ATTR_NAMESgetattrr/   __setattr__r   )r,   missing	attr_nameZ
attr_valuer-   r-   r.   r*   y   s    
zLazyModuleState._mutate_host)r   
__module____qualname__r	   __annotations__dataclassesfieldr   r   listr   r   r    propertyr/   r%   r4   r*   r-   r-   r-   r.   r   ,   s   
r   c                   @  sf   e Zd ZU dZded< ddddZddd	d
dZddddddZddddZddddZ	dS )r/   z5Lazy module which auto-loads on first attribute call.r   _etils_state)statec                 C  s4   t | dd  t | d| |jd u s*J | |_d S )Nr
   rC   )r6   r9   r   )r,   rD   r-   r-   r.   __init__   s    zmodule.__init__r   r   )namer#   c                 C  s"   | j js|dv rtt| j j|S )N>   getdoc__wrapped__)rC   r%   AttributeErrorr8   r/   r,   rF   r-   r-   r.   __getattr__   s    	zmodule.__getattr__r5   )rF   valuer#   c                 C  s   t | jj|| d S N)setattrrC   r/   )r,   rF   rL   r-   r-   r.   r9      s    zmodule.__setattr__r   r"   c                 C  s   t | jjS rM   )dirrC   r/   r+   r-   r-   r.   __dir__   s    zmodule.__dir__c                 C  sN   | j jsd| j jdS | j j}t|dr2|j}nd}d|jd|dS d S )NzLazyModule()r
   z
(built-in)z<lazy_module z from >)rC   r%   r   r/   hasattrr
   r   )r,   Zmodule_filer-   r-   r.   __repr__   s    
zmodule.__repr__N)
r   r<   r=   r	   r>   rE   rK   r9   rP   rU   r-   r-   r-   r.   r/      s   
r/   )Zdataclass_arrayetilsZlarkZsundsZvisu3dZimageioZmediapyZpycolmapr   r   r!   )r   r   r#   c                C  s`   t  }|@ |D ] }|dkr(t s(qt| qt| W  d   S 1 sR0    Y  dS )z/Load the module, eventually using adhoc-import.ztqdm.notebookN)
contextlibsuppressr   Zis_notebook	importlibimport_module)r   r   Zadhoc_cmZextra_importr-   r-   r.   r)      s    r)   c                   @  s.   e Zd ZdZdd ZejdddddZd	S )
LazyImportsBuilderzGCapture import statements and replace them by lazy-import equivalement.c                 C  s   || _ i | _d S rM   )_globalslazy_modules)r,   globals_r-   r-   r.   rE      s    zLazyImportsBuilder.__init__r   zIterator[None])r   r#   c                c  s|   t j}ztt _dV  W |t _n|t _0 t| j D ]B\}}t|tr4t|j	||j
|d}t|}|| j|< || j|< q4dS )z2Replace import statement by their lazy equivalent.N)r   r   r   r   )builtins
__import___lazy_importrA   r\   items
isinstance_ModuleImportProxyr   qualnameleaves_qualnamesr   r]   )r,   r   Zoriginal_importkvrD   Zlazy_moduler-   r-   r.   replace_imports   s     	

z"LazyImportsBuilder.replace_importsN)r   r<   r=   r	   rE   rW   contextmanagerri   r-   r-   r-   r.   r[      s   r[   r-   ztuple[str, ...]int)rF   fromlistlevelc           	      C  sX   ~~|rt d|  d| d^}}t|d}|}|D ]} t|| }q8|rP|S |S dS )zMock of `builtins.__import__`.z*Relative import statements not supported (z).r0   rF   N)
ValueErrorsplitrd   r8   )	rF   r^   Zlocals_rl   rm   Z	root_namepartsrootchildr-   r-   r.   ra     s    
ra   c                   @  sz   e Zd ZU dZded< dZded< ejedZ	ded	< e
dd
ddZe
dd
ddZdd
ddZddddZdS )rd   z`_ModuleImportProxy` replace all modules during import statement.

  ```python
  with LazyImportsBuilder().replace_imports():
    import abc.def
    assert isinstance(abc.def, _ModuleImportProxy)
  ```
  r   rF   NzOptional[_ModuleImportProxy]parentr   zdict[str, _ModuleImportProxy]childrenr"   c                 C  s$   | j s| jS | j j d| j S d S )Nr0   )rt   rF   re   r+   r-   r-   r.   re   /  s    z_ModuleImportProxy.qualnamer   c                 C  s4   g }| j  D ] }||j }r$|n|jg q|S )z)Extract all qualnames of leaves children.)ru   valuesextendrf   re   )r,   Zall_childrenru   rf   r-   r-   r.   rf   6  s    z#_ModuleImportProxy.leaves_qualnamesc                 C  s4   | j rd| j  }nd}t| j d| j | dS )Nz, children=r$   (rQ   )rf   typer   re   )r,   Z	child_argr-   r-   r.   rU   B  s    z_ModuleImportProxy.__repr__rn   c                 C  s*   || j vr t| || d| j |< | j | S )N)rF   rt   )ru   ry   rJ   r-   r-   r.   rK   I  s    
z_ModuleImportProxy.__getattr__)r   r<   r=   r	   r>   rt   r?   r@   dictru   rB   re   rf   rU   rK   r-   r-   r-   r.   rd     s   
	rd   zdict[str, LazyModule])r]   r#   c                 C  sp   g }dd |   D } dd | D }dd |D }dd |D }|| |r\|r\|d || d|S )z)Returns the lazy import statement string.c                 S  s   g | ]
}|j qS r-   )rC   .0mr-   r-   r.   
<listcomp>V      z-current_import_statements.<locals>.<listcomp>c                 S  s    g | ]}|j r|jd kr|qS )Zlazy_imports)r%   r   r{   r-   r-   r.   r~   W  s   c                 S  s   g | ]}|j r|jqS r-   r   r4   r{   r-   r-   r.   r~   ]  r   c                 S  s   g | ]}|j s|jqS r-   r   r{   r-   r-   r.   r~   ^  s   r$   
)rv   rw   appendr&   )r]   linesZused_lazy_modulesZstd_modulesZnon_std_modulesr-   r-   r.   current_import_statementsR  s    


r   )NNr-   r   )r	   
__future__r   r_   rW   r?   rY   r'   typestypingr   r   r   rV   r   r7   	dataclassr   
ModuleTyper/   r   Z_PACKAGE_RESTRICTr)   r[   ra   rd   r   r-   r-   r-   r.   <module>   s4   
X.+    
3