B
    Çä˜\ß  ã               @   s<   d Z ddl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)Ú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éÿÿÿÿc             K   s0   t f |Ž| _tj | jj|¡}t|||ƒ| _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`.

        N)r   Ú_tmpdirÚ_osÚpathÚjoinÚnameÚopenÚfile)ÚselfÚfilenameÚmodeÚbufsizeÚkwdsr   © r   ú4lib/python3.7/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 )N)r   )r   r   r   r   Ú	__enter__"   s    z'NamedFileInTemporaryDirectory.__enter__c             C   s   |   ¡  d S )N)r   )r   ÚtypeÚvalueÚ	tracebackr   r   r   Ú__exit__%   s    z&NamedFileInTemporaryDirectory.__exit__N)r   r   )Ú__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 )N)r   ÚgetcwdÚold_wdÚchdirr
   Úsuperr    r   )r   )Ú	__class__r   r   r   2   s    
z#TemporaryWorkingDirectory.__enter__c                s    t  | j¡ tt| ƒ |||¡S )N)r   r#   r"   r$   r    r   )r   Úexcr   Útb)r%   r   r   r   7   s    z"TemporaryWorkingDirectory.__exit__)r   r   r   Ú__doc__r   r   Ú__classcell__r   r   )r%   r   r    )   s   r    )r(   Úosr   Ztempfiler   Úobjectr   r    r   r   r   r   Ú<module>   s   