a
    i‚j}  ã                   @  sX   d Z ddlmZ ddlZddlZddlmZmZmZ edƒZ	G dd„ dej
ee	 ƒZdS )zContextmanager utils.é    )ÚannotationsN)ÚGenericÚIterableÚTypeVarÚ_Tc                      sV   e Zd ZdZ‡ fdd„Zejddœdd„ƒZde_d	dœd
d„Z	ddœdd„Z
‡  ZS )ÚContextManageraƒ  ContextManager allows to define contextmanager class using yield-syntax.

  Example:

  ```python
  class A(epy.ContextManager):

    def __contextmanager__(self) -> Iterable[A]:
      yield self


  with A() as a:
    pass
  ```

  One the code is more mature, this could be merged to `contextlib` directly.
  https://discuss.python.org/t/yield-based-contextmanager-for-classes/8453

  c                   s8   t ƒ jf i |¤Ž t| jdƒs4t | j¡| _d| j_d S )NÚ	_cm_addedT)ÚsuperÚ__init_subclass__ÚhasattrÚ__contextmanager__Ú
contextlibÚcontextmanagerr   )ÚclsÚkwargs©Ú	__class__© úR/home/ghrups/robot-bench/.venv/lib/python3.9/site-packages/etils/epy/contextlib.pyr
   7   s
    ÿz ContextManager.__init_subclass__zIterable[_T])Úreturnc                 C  s   d S ©Nr   ©Úselfr   r   r   r   A   s    z!ContextManager.__contextmanager__Tr   c                 C  s   |   ¡ | _| j ¡ S r   )r   Ú_epy_cmÚ	__enter__r   r   r   r   r   G   s    
zContextManager.__enter__ÚNonec                 C  s   | j  |||¡S r   )r   Ú__exit__)r   Úexc_typeÚ	exc_valueÚ	tracebackr   r   r   r   K   s    zContextManager.__exit__)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r
   ÚabcÚabstractmethodr   r   r   r   Ú__classcell__r   r   r   r   r   "   s   
r   )r#   Ú
__future__r   r$   r   Útypingr   r   r   r   ÚABCr   r   r   r   r   Ú<module>   s   