
    j_A                         d Z ddlmZ ddlZddlZddlZ	 ddl	m
Z
 n!# e$ r ddl	mZmZ  G d de          Z
Y nw xY w G d d	e          Z G 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                      e Zd ZdZdedfdZd ZddZd Z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
 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                 @    t          |||          | _        d| _        d S NF)r   name_closed)selfsuffixprefixdirs       8lib/python3.11/site-packages/ipython_genutils/tempdir.py__init__zTemporaryDirectory.__init__#   s     44DI DLLL    c                     | j         S N)r   r   s    r   	__enter__zTemporaryDirectory.__enter__'   s
    9r   Fc                    | j         r| j        s	 |                     | j                    n`# t          t          f$ rL}dt          |          vr t          d                    ||           t          j	                   Y d }~d S d }~ww xY wd| _        |r4| 
                    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$   exs      r   cleanupzTemporaryDirectory.cleanup*   s
   y ( (
LL++++!>2    SWW,,>EEb$PP#{, , , ,FFFFF  $ (JJ<CCDII&( ( ( ( (( ( ( (( (s   + BABBc                 .    |                                   d S r   r'   )r   excvaluetbs       r   __exit__zTemporaryDirectory.__exit__<   s    LLNNNNNr   c                 2    |                      d           d S )NT)r$   r)   r   s    r   __del__zTemporaryDirectory.__del__?   s    LLtL$$$$$r   c                 ~   |                      |          D ]}|                     ||          }	 |                     |          }n# | j        $ r d}Y nw xY w|r|                     |           Y	 |                     |           p# | j        $ r Y |w xY w	 |                     |           d S # | j        $ r Y d S w xY wr
   )_listdir
_path_join_isdir	_os_errorr   _remove_rmdir)r   pathr   fullnameisdirs        r   r   zTemporaryDirectory._rmtreeP   s
    d++  ??466" KK11EE~ " " "!EEE" LL****X....>   D!!!!!>   s5   AAA0B
BBB. .
B<;B<)F)__name__
__module____qualname____doc__r   r   r   r'   r-   r/   staticmethod_oslistdirr1   r7   joinr2   r9   r3   remover5   rmdirr6   errorr4   	_warningswarnr$   r    r   r   r   r      s        		 		 #%X4 	! 	! 	! 	!	 	 		( 	( 	( 	($	 	 		% 	% 	%  <,,!\#(-00
chn--,sz**ci((I		 	 	 	 	r   r   c                   ,    e Zd ZddZd ZeZd Zd ZdS )	NamedFileInTemporaryDirectoryw+bc                     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`.

        NrG   )r   _tmpdirr?   r7   rA   r   openr   )r   filenamemodebufsizekwdsr7   s         r   r   z&NamedFileInTemporaryDirectory.__init__i   sJ     *11D11x}}T\.99tW--			r   c                 j    | j                                          | j                                         d S r   )r   closerM   r'   r   s    r   r'   z%NamedFileInTemporaryDirectory.cleanupx   s.    	r   c                     | j         S r   r   r   s    r   r   z'NamedFileInTemporaryDirectory.__enter__~   s
    yr   c                 .    |                                   d S r   r)   )r   typer+   	tracebacks       r   r-   z&NamedFileInTemporaryDirectory.__exit__   s    r   N)rJ   rK   )r:   r;   r<   r   r'   r/   r   r-   rG   r   r   rI   rI   g   s\        . . . .   G      r   rI   c                   ,     e Zd ZdZ fdZ fdZ x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                     t          j                    | _        t          j        | j                   t          t          |                                           S r   )r?   getcwdold_wdchdirr   superrZ   r   )r   	__class__s    r   r   z#TemporaryWorkingDirectory.__enter__   s?    jll	$).55??AAAr   c                     t          j        | j                   t          t          |                               |||          S r   )r?   r^   r]   r_   rZ   r-   )r   r*   r+   r,   r`   s       r   r-   z"TemporaryWorkingDirectory.__exit__   s8    	$+.55>>sE2NNNr   )r:   r;   r<   r=   r   r-   __classcell__)r`   s   @r   rZ   rZ      si         B B B B B
O O O O O O O O Or   rZ   )r=   
__future__r   osr?   warningsrE   sysr"   tempfiler   ImportErrorr   r   objectrI   rZ   rG   r   r   <module>rj      sA    & % % % % %            R+++++++ O O O********L L L L LV L L L L LOf    F   <O O O O O 2 O O O O Os    ;;