B
    4[~                 @   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 )zTemporaryDirectory class, copied from Python 3

NamedFileInTemporaryDirectory and TemporaryWorkingDirectory from IPython, which
uses the 3-clause BSD license.
    )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   aG  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 everything contained
        in it are removed.
         Nc             C   s   t |||| _d| _d S )NF)r   name_closed)selfsuffixprefixdir r   /lib/python3.7/site-packages/testpath/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__9   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   M   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               @   s6   e Zd ZdZdddZdd ZeZdd	 Zd
d ZdS )NamedFileInTemporaryDirectorya  Open a file named `filename` in a temporary directory.
    
    This context manager is preferred over :class:`tempfile.NamedTemporaryFile`
    when one needs to reopen the file, because on Windows only one handle on a
    file can be open at a time. You can close the returned handle explicitly
    inside the context without deleting the file, and the context manager will
    delete the whole directory when it exits.

    Arguments `mode` and `bufsize` are passed to `open`.
    Rest of the arguments are passed to `TemporaryDirectory`.
    
    Usage example::
    
        with NamedFileInTemporaryDirectory('myfile', 'wb') as f:
            f.write('stuff')
            f.close()
            # You can now pass f.name to things that will re-open the file
    w+bc             K   s0   t f || _tj| jj|}t|||| _d S )N)r   _tmpdirr2   r*   r4   r   openr   )r	   filenamemodebufsizekwdsr*   r   r   r   r   v   s    z&NamedFileInTemporaryDirectory.__init__c             C   s   | j   | j  d S )N)r   closer=   r   )r	   r   r   r   r   {   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/   r0   r   r   r#   r   r"   r   r   r   r   r:   c   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   Z
__future__r   osr2   warningsr8   sysr   Ztempfiler   ImportErrorr   r   objectr:   rF   r   r   r   r   <module>   s   O%