
    XA                         d Z ddlmZ ddlZddlZddlZ	 ddl	m
Z
  G d d	e      Z G d
 de
      Zy# e$ r ddl	mZmZ  G d de      Z
Y 1w xY w)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                   J   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j4                  Zd	 Zy)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                 6    t        |||      | _        d| _        y NF)r   name_closed)selfsuffixprefixdirs       :/usr/lib/python3/dist-packages/ipython_genutils/tempdir.py__init__zTemporaryDirectory.__init__#   s    4DI DL    c                     | j                   S N)r   r   s    r   	__enter__zTemporaryDirectory.__enter__'   s    99r   c                 x   | j                   rY| j                  sL	 | j                  | j                          d| _        |r&| j                  dj                  |       t               y y y y # t        t        f$ rC}dt        |      vr t        dj                  ||       t        j                         Y d }~y d }~ww xY w)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    yy
LL+  $JJ<CCDI&(  ".y ">2  SW,>EEb$P#{{,s   A' 'B969B44B9c                 $    | j                          y r   r'   )r   excvaluetbs       r   __exit__zTemporaryDirectory.__exit__<   s    LLNr   c                 (    | j                  d       y )NT)r$   r)   r   s    r   __del__zTemporaryDirectory.__del__?   s    LLtL$r   c                 j   | j                  |      D ]L  }| j                  ||      }	 | j                  |      }|r| j	                  |       ;	 | j                  |       N 	 | j                  |       y # | j                  $ r d}Y Nw xY w# | j                  $ r Y w xY w# | j                  $ r Y y 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" KK1E LL*X.D! ~~ "!E"  >>  >> s5   A4B"B  4BBBB B21B2)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
chhnn-szz*cii(II		r   r   c                   *    e Zd ZddZd ZeZd Zd Zy)NamedFileInTemporaryDirectoryc                     t        di || _        t        j                  j	                  | j                  j
                  |      }t        |||      | _        y)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   sA     *1D1xx}}T\\..9tW-	r   c                 l    | j                   j                          | j                  j                          y r   )r   closerK   r'   r   s    r   r'   z%NamedFileInTemporaryDirectory.cleanupx   s     		r   c                     | j                   S r   r   r   s    r   r   z'NamedFileInTemporaryDirectory.__enter__~   s    yyr   c                 $    | j                          y r   r)   )r   typer+   	tracebacks       r   r-   z&NamedFileInTemporaryDirectory.__exit__   s    r   N)zw+b)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   superrY   r   )r   	__class__s    r   r   z#TemporaryWorkingDirectory.__enter__   s1    jjl		$)).?AAr   c                 l    t        j                  | j                         t        t        |   |||      S r   )r?   r]   r\   r^   rY   r-   )r   r*   r+   r,   r_   s       r   r-   z"TemporaryWorkingDirectory.__exit__   s*    		$++.>sE2NNr   )r:   r;   r<   r=   r   r-   __classcell__)r_   s   @r   rY   rY      s    B
O Or   rY   )r=   
__future__r   osr?   warningsrE   sysr"   tempfiler   ImportErrorr   r   objectrI   rY   rG   r   r   <module>ri      sb    &   R+jF <O 2 Oc  O*LV LOs   3 AA