B
    &dA                 @   s   d Z ddlmZ ddlZddlZddlZyddl	m
Z
 W n4 ek
rl   ddl	mZmZ G dd deZ
Y nX G dd	 d	eZG d
d de
ZdS )zTemporaryDirectory class, copied from Python 3

This is copied from the stdlib and will be standard in Python 3.2 and onwards.
    )print_functionN)TemporaryDirectory)mkdtemptemplatec               @   s   e Zd ZdZdedfddZdd Zdd	d
Zdd Zdd Z	e
ejZe
ejjZe
ejjZe
ejZe
ejZejZejZdd ZdS )r   aF  Create and return a temporary directory.  This has the same
        behavior as mkdtemp but can be used as a context manager.  For
        example:

            with TemporaryDirectory() as tmpdir:
                ...

        Upon exiting the context, the directory and everthing contained
        in it are removed.
         Nc             C   s   t |||| _d| _d S )NF)r   name_closed)selfsuffixprefixdir r   e/work/yifan.wang/ringdown/master-ringdown-env/lib/python3.7/site-packages/ipython_genutils/tempdir.py__init__#   s    zTemporaryDirectory.__init__c             C   s   | j S )N)r   )r	   r   r   r   	__enter__'   s    zTemporaryDirectory.__enter__Fc          
   C   s   | j r| jsy| | j  W nJ ttfk
rf } z(dt|kr@ td|| tj	d d S d }~X Y nX d| _|r| 
d| t d S )NNonez"ERROR: {!r} while cleaning up {!r})fileTzImplicitly cleaning up {!r})r   r   _rmtree	TypeErrorAttributeErrorstrprintformat_sysstderr_warnWarning)r	   r   exr   r   r   cleanup*   s    
zTemporaryDirectory.cleanupc             C   s   |    d S )N)r   )r	   excvaluetbr   r   r   __exit__<   s    zTemporaryDirectory.__exit__c             C   s   | j dd d S )NT)r   )r   )r	   r   r   r   __del__?   s    zTemporaryDirectory.__del__c          	   C   s   x~|  |D ]p}| ||}y| |}W n | jk
rD   d}Y nX |rV| | qy| | W q | jk
rz   Y qX qW y| | W n | jk
r   Y nX d S )NF)_listdir
_path_join_isdir	_os_errorr   _remove_rmdir)r	   pathr   fullnameisdirr   r   r   r   P   s     

zTemporaryDirectory._rmtree)F)__name__
__module____qualname____doc__r   r   r   r   r"   r#   staticmethod_oslistdirr$   r*   joinr%   r,   r&   remover(   rmdirr)   errorr'   	_warningswarnr   r   r   r   r   r   r      s   

	


r   c               @   s2   e Zd ZdddZdd ZeZdd Zd	d
 ZdS )NamedFileInTemporaryDirectoryw+bc             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   _tmpdirr2   r*   r4   r   openr   )r	   filenamemodebufsizekwdsr*   r   r   r   r   i   s    z&NamedFileInTemporaryDirectory.__init__c             C   s   | j   | j  d S )N)r   closer=   r   )r	   r   r   r   r   x   s    
z%NamedFileInTemporaryDirectory.cleanupc             C   s   | j S )N)r   )r	   r   r   r   r   ~   s    z'NamedFileInTemporaryDirectory.__enter__c             C   s   |    d S )N)r   )r	   typer    	tracebackr   r   r   r"      s    z&NamedFileInTemporaryDirectory.__exit__N)r;   r<   )r-   r.   r/   r   r   r#   r   r"   r   r   r   r   r:   g   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)r2   getcwdold_wdchdirr   superrF   r   )r	   )	__class__r   r   r      s    
z#TemporaryWorkingDirectory.__enter__c                s    t | j tt| |||S )N)r2   rI   rH   rJ   rF   r"   )r	   r   r    r!   )rK   r   r   r"      s    z"TemporaryWorkingDirectory.__exit__)r-   r.   r/   r0   r   r"   __classcell__r   r   )rK   r   rF      s   rF   )r0   
__future__r   osr2   warningsr8   sysr   tempfiler   ImportErrorr   r   objectr:   rF   r   r   r   r   <module>   s   P