a
    W%bs                     @   s\   d dl Z d dl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d	 Z
dS )
    Nwraps)contextmanagerc                   @   sB   e Zd ZdZdddZdd Zd	d
 Zdd Zdd Zdd Z	dS )TempDirz class for temporary directories
creates a (named) directory which is deleted after use.
All files created within the directory are destroyed
Might not work on windows when the files are still opened
 tmpNc                 C   s   t j|||d| _d S )N)suffixprefixdir)tempfilemkdtempname)selfr   r	   Zbasedir r   W/Users/vegardjervell/Documents/master/model/venv/lib/python3.9/site-packages/tempdir.py__init__   s    zTempDir.__init__c                 C   s*   z| j r|   W n ty$   Y n0 d S N)r   dissolveAttributeErrorr   r   r   r   __del__   s
    zTempDir.__del__c                 C   s   | j S r   r   r   r   r   r   	__enter__   s    zTempDir.__enter__c                 G   s   |    d S r   )r   r   Zerrstuffr   r   r   __exit__   s    zTempDir.__exit__c                 C   s   | j rt| j  d| _ dS )z;remove all files and directories created within the tempdirr   N)r   shutilrmtreer   r   r   r   r      s    zTempDir.dissolvec                 C   s   | j rd| j f S dS d S )Nztemporary directory at: %szdissolved temporary directoryr   r   r   r   r   __str__#   s    zTempDir.__str__)r   r   N)
__name__
__module____qualname____doc__r   r   r   r   r   r   r   r   r   r   r      s   
r   c                   @   s(   e Zd ZdZdd Zdd Zdd ZdS )	
in_tempdirz0Create a temporary directory and change to it.  c                 O   s   t |i || _d S r   )r   tmpdir)r   argskwargsr   r   r   r   -   s    zin_tempdir.__init__c                 C   s    t  | _t | jj | jjS r   )osgetcwdold_pathchdirr#   r   r   r   r   r   r   0   s    
zin_tempdir.__enter__c                 G   s   t | j | j  d S r   )r&   r)   r(   r#   r   r   r   r   r   r   5   s    zin_tempdir.__exit__N)r   r   r    r!   r   r   r   r   r   r   r   r"   *   s   r"   c                     s    fdd}|S )z{Make a function execute in a new tempdir.
    Any time the function is called, a new tempdir is created and destroyed.
    c                    s   t   fdd}|S )Nc                     s@   t  i  | i |W  d    S 1 s20    Y  d S r   )r"   )ZfuncargsZ
funckwargs)r$   fncr%   r   r   wrapper>   s    z4run_in_tempdir.<locals>.change_dird.<locals>.wrapperr   )r*   r+   r$   r%   )r*   r   change_dird=   s    z#run_in_tempdir.<locals>.change_dirdr   )r$   r%   r-   r   r,   r   run_in_tempdir9   s    r.   )r&   r   r   	functoolsr   
contextlibr   objectr   r"   r.   r   r   r   r   <module>   s   #