a
    ijT                     @  s   d Z ddlmZ ddlZddlZddlZddlmZmZmZm	Z	 e	e
ee
 f Zddddd	Zd
dddd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S ) zAdhoc imports utils.    )annotationsN)SequenceNoReturnOptionalUnionStrOrStrListz	list[str])restrictreturnc                   s>   t |  tdd  D sJ t   fddtjD   S )z5Returns all `sys.modules` matching the restrict name.c                 s  s   | ]}d |vV  qdS )/N ).0moduler   r   W/home/ghrups/robot-bench/.venv/lib/python3.9/site-packages/etils/ecolab/module_utils.py	<genexpr>       z#get_module_names.<locals>.<genexpr>c                   s   g | ]}|  r|qS r   )
startswith)r   mmodulesr   r   
<listcomp>$   r   z$get_module_names.<locals>.<listcomp>)normalize_str_to_listalltuplesysr   )r   r   r   r   get_module_names   s
    r   FT)verbose
invalidateboolNone)r   r   r   r	   c                C  sn   t | }|sdS |D ]B}|r*td|  t| |rN|dsNttj|  tj|= qtjD ]
}|  q^dS )au  Clear the `sys.modules` cache.

  Helpful for interactive development to reload from Jupyter notebook the
  code we're currently editing (without having to restart the notebook kernel).

  Usage:

  ```python
  ecolab.clear_cached_modules(['visu3d', 'other_module.submodule'])

  import visu3d
  import other_module.submodule
  ```

  Args:
    modules: List of modules to clear (all submodules cleared too)
    verbose: Whether to display the list of modules cleared.
    invalidate: If `True` (default), the instances of the module will raise an
      error when used (to avoid using 2 versions of a module at the same time)
  Nz	Clearing Zetils)	r   print_clear_parent_module_attrr   _invalidate_moduler   r   typing	_cleanups)r   r   r   Zmodules_to_clearmodule_namecleanupr   r   r   clear_cached_modules(   s    

r&   str)r$   r	   c                 C  sH   d| vrdS |  dd\}}tj|}|s0dS t||rDt|| dS )z;Remove parent reference (e.g. `path.to.child` -> `path.to`..N   )rsplitr   r   gethasattrdelattr)r$   parent_module_nameZ
child_nameparent_moduler   r   r   r    \   s    
r    ztypes.ModuleType)r   r	   c                   s.   | j  ddd fdd}| j  || _dS )zInvalidate the module in-place.r'   r   )namer	   c                   s    t d  d|  d  ddS )z-All module attribute access will raise error.zCannot access r(   z on the old module instance.
z was reloaded, so the new module should be used instead.
You can pass `invalidate=False` to keep both old and reloaded modules.N)AttributeError)r0   r$   r   r   __getattr__m   s
    z'_invalidate_module.<locals>.__getattr__N)__name____dict__clearr3   )r   r3   r   r2   r   r!   h   s    	
r!   c                 C  s
   |  dS )z(Returns `True` if the module is a proto.Z_pb2)endswithr2   r   r   r   	_is_protoz   s    r8   zOptional[StrOrStrList])xr	   c                 C  sT   | d u rg S t | tr*dd | dD S t | ttfsHtd| nt| S d S )Nc                 S  s   g | ]}|  qS r   )strip)r   vr   r   r   r      r   z)normalize_str_to_list.<locals>.<listcomp>,zExpected list. Got: )
isinstancer'   splitlistr   	TypeError)r9   r   r   r   r      s    
r   )__doc__
__future__r   r   typesr"   r   r   r   r   r'   r   r   r&   r    r!   r8   r   r   r   r   r   <module>   s   4