a
    ø£BeK  ã                   @   sH   d Z ddlZddlmZ ddlmZ G dd„ deƒZG dd„ deƒZ	dS )	a   This module contains classes - NamedFileInTemporaryDirectory, TemporaryWorkingDirectory.

These classes add extra features such as creating a named file in temporary directory and
creating a context manager for the working directory which is also temporary.
é    N)ÚPath)ÚTemporaryDirectoryc                   @   s2   e Zd Zddd„Zdd„ ZeZdd	„ Zd
d„ ZdS )ÚNamedFileInTemporaryDirectoryúw+béÿÿÿÿFc                 K   sF   t f i |¤Ž| _t| jjƒ| }d|v r,dnd}t||||d�| _dS )aN  
        Open a file named `filename` in a temporary directory.

        This context manager is preferred over `NamedTemporaryFile` in
        stdlib `tempfile` when one needs to reopen the file.

        Arguments `mode` and `bufsize` are passed to `open`.
        Rest of the arguments are passed to `TemporaryDirectory`.

        ÚbNzutf-8)Úencoding)r   Ú_tmpdirr   ÚnameÚopenÚfile)ÚselfÚfilenameÚmodeÚbufsizeZadd_to_syspathÚkwdsÚpathr   © r   úX/home/yuhw/wc/wire-cell-python/wcpy/lib/python3.9/site-packages/IPython/utils/tempdir.pyÚ__init__   s    z&NamedFileInTemporaryDirectory.__init__c                 C   s   | j  ¡  | j ¡  d S ©N)r   Úcloser	   Úcleanup©r   r   r   r   r      s    
z%NamedFileInTemporaryDirectory.cleanupc                 C   s   | j S r   )r   r   r   r   r   Ú	__enter__#   s    z'NamedFileInTemporaryDirectory.__enter__c                 C   s   |   ¡  d S r   )r   )r   ÚtypeÚvalueÚ	tracebackr   r   r   Ú__exit__&   s    z&NamedFileInTemporaryDirectory.__exit__N)r   r   F)Ú__name__Ú
__module__Ú__qualname__r   r   Ú__del__r   r   r   r   r   r   r      s
   
r   c                       s,   e Zd ZdZ‡ fdd„Z‡ fdd„Z‡  ZS )ÚTemporaryWorkingDirectoryzÛ
    Creates a temporary directory and sets the cwd to that directory.
    Automatically reverts to previous cwd upon cleanup.
    Usage example:

        with TemporaryWorkingDirectory() as tmpdir:
            ...
    c                    s$   t  ¡ | _t | j¡ tt| ƒ ¡ S r   )	r   ÚcwdÚold_wdÚ_osÚchdirr
   Úsuperr#   r   r   ©Ú	__class__r   r   r   4   s    
z#TemporaryWorkingDirectory.__enter__c                    s    t  | j¡ tt| ƒ |||¡S r   )r&   r'   r%   r(   r#   r   )r   Úexcr   Útbr)   r   r   r   9   s    z"TemporaryWorkingDirectory.__exit__)r   r    r!   Ú__doc__r   r   Ú__classcell__r   r   r)   r   r#   *   s   	r#   )
r-   Úosr&   Úpathlibr   Útempfiler   Úobjectr   r#   r   r   r   r   Ú<module>   s
   