o
    ȷeA                     @   s   d Z ddlmZ ddlZddlZddlZzddl	m
Z
 W n ey5   ddl	mZmZ G dd deZ
Y nw 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   O/var/www/ideatree/venv/lib/python3.10/site-packages/ipython_genutils/tempdir.py__init__#   s   
zTemporaryDirectory.__init__c                 C      | j S N)r   r
   r   r   r   	__enter__'      zTemporaryDirectory.__enter__Fc              
   C   s   | j rF| jsHz| | j  W n' ttfy5 } zdt|vr td|| tj	d W Y d }~d S d }~ww d| _|rJ| 
d| t d S d S d S 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      |    d S r   r$   r
   excvaluetbr   r   r   __exit__<      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   |  |D ]5}| ||}z| |}W n | jy    d}Y nw |r)| | qz| | W q | jy:   Y qw z| | W d S  | jyN   Y d S w r   )_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.   r4   joinr/   r6   r0   remover2   rmdirr3   errorr1   	_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   s4   t di || _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`.

        Nr   )r   _tmpdirr<   r4   r>   r   openr   )r
   filenamemodebufsizekwdsr4   r   r   r   r   i   s   z&NamedFileInTemporaryDirectory.__init__c                 C   s   | j   | j  d S r   )r   closerG   r$   r   r   r   r   r$   x   s   
z%NamedFileInTemporaryDirectory.cleanupc                 C   r   r   r   r   r   r   r   r   ~   r   z'NamedFileInTemporaryDirectory.__enter__c                 C   r%   r   r&   )r
   typer)   	tracebackr   r   r   r+      r,   z&NamedFileInTemporaryDirectory.__exit__N)rE   rF   )r7   r8   r9   r   r$   r-   r   r+   r   r   r   r   rD   g   s    
rD   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<   getcwdold_wdchdirr   superrP   r   r   	__class__r   r   r      s   
z#TemporaryWorkingDirectory.__enter__c                    s    t | j tt| |||S r   )r<   rS   rR   rT   rP   r+   r'   rU   r   r   r+      s   z"TemporaryWorkingDirectory.__exit__)r7   r8   r9   r:   r   r+   __classcell__r   r   rU   r   rP      s    rP   )r:   
__future__r   osr<   warningsrB   sysr   tempfiler   ImportErrorr   r   objectrD   rP   r   r   r   r   <module>   s    S