ó
Ð4¹[c           @  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
 Wn= e k
 r d d l	 m Z m Z d e f d „  ƒ  YZ
 n Xd e f d	 „  ƒ  YZ d
 e
 f d „  ƒ  YZ d S(   sŸ   TemporaryDirectory class, copied from Python 3

NamedFileInTemporaryDirectory and TemporaryWorkingDirectory from IPython, which
uses the 3-clause BSD license.
iÿÿÿÿ(   t   print_functionN(   t   TemporaryDirectory(   t   mkdtempt   templateR   c           B  s³   e  Z d  Z d e d d „ Z d „  Z e 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 RS(	   sG  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.
        t    c         C  s"   t  | | | ƒ |  _ t |  _ d  S(   N(   R   t   namet   Falset   _closed(   t   selft   suffixt   prefixt   dir(    (    s/   lib/python2.7/site-packages/testpath/tempdir.pyt   __init__    s    c         C  s   |  j  S(   N(   R   (   R   (    (    s/   lib/python2.7/site-packages/testpath/tempdir.pyt	   __enter__$   s    c         C  s­   |  j  r© |  j r© y |  j |  j  ƒ WnQ t t f k
 rz } d t | ƒ k rW ‚  n  t d j | |  ƒ d t j	 ƒd  SXt
 |  _ | r© |  j d j |  ƒ t ƒ q© n  d  S(   Nt   Nones"   ERROR: {!r} while cleaning up {!r}t   files   Implicitly cleaning up {!r}(   R   R   t   _rmtreet	   TypeErrort   AttributeErrort   strt   printt   formatt   _syst   stderrt   Truet   _warnt   Warning(   R   R   t   ex(    (    s/   lib/python2.7/site-packages/testpath/tempdir.pyt   cleanup'   s    
	c         C  s   |  j  ƒ  d  S(   N(   R   (   R   t   exct   valuet   tb(    (    s/   lib/python2.7/site-packages/testpath/tempdir.pyt   __exit__9   s    c         C  s   |  j  d t ƒ d  S(   NR   (   R   R   (   R   (    (    s/   lib/python2.7/site-packages/testpath/tempdir.pyt   __del__<   s    c         C  sÆ   x— |  j  | ƒ D]† } |  j | | ƒ } y |  j | ƒ } Wn |  j k
 rW t } n X| rn |  j | ƒ q y |  j | ƒ Wq |  j k
 r• q Xq Wy |  j | ƒ Wn |  j k
 rÁ n Xd  S(   N(   t   _listdirt
   _path_joint   _isdirt	   _os_errorR   R   t   _removet   _rmdir(   R   t   pathR   t   fullnamet   isdir(    (    s/   lib/python2.7/site-packages/testpath/tempdir.pyR   M   s     
N(   t   __name__t
   __module__t   __doc__R   R   R   R   R   R   R    R!   t   staticmethodt   _ost   listdirR"   R(   t   joinR#   R*   R$   t   removeR&   t   rmdirR'   t   errorR%   t	   _warningst   warnR   R   (    (    (    s/   lib/python2.7/site-packages/testpath/tempdir.pyR      s   
						t   NamedFileInTemporaryDirectoryc           B  s>   e  Z d  Z d d d „ Z d „  Z e Z d „  Z d „  Z RS(   sþ  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
    s   w+biÿÿÿÿc         K  sC   t  |   |  _ t j j |  j j | ƒ } t | | | ƒ |  _ d  S(   N(   R   t   _tmpdirR/   R(   R1   R   t   openR   (   R   t   filenamet   modet   bufsizet   kwdsR(   (    (    s/   lib/python2.7/site-packages/testpath/tempdir.pyR   v   s    c         C  s   |  j  j ƒ  |  j j ƒ  d  S(   N(   R   t   closeR8   R   (   R   (    (    s/   lib/python2.7/site-packages/testpath/tempdir.pyR   {   s    c         C  s   |  j  S(   N(   R   (   R   (    (    s/   lib/python2.7/site-packages/testpath/tempdir.pyR      s    c         C  s   |  j  ƒ  d  S(   N(   R   (   R   t   typeR   t	   traceback(    (    s/   lib/python2.7/site-packages/testpath/tempdir.pyR    „   s    (   R+   R,   R-   R   R   R!   R   R    (    (    (    s/   lib/python2.7/site-packages/testpath/tempdir.pyR7   c   s   		t   TemporaryWorkingDirectoryc           B  s    e  Z d  Z d „  Z d „  Z RS(   sÝ   
    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         C  s2   t  j ƒ  |  _ t  j |  j ƒ t t |  ƒ j ƒ  S(   N(   R/   t   getcwdt   old_wdt   chdirR   t   superRA   R   (   R   (    (    s/   lib/python2.7/site-packages/testpath/tempdir.pyR   ’   s    c         C  s,   t  j |  j ƒ t t |  ƒ j | | | ƒ S(   N(   R/   RD   RC   RE   RA   R    (   R   R   R   R   (    (    s/   lib/python2.7/site-packages/testpath/tempdir.pyR    —   s    (   R+   R,   R-   R   R    (    (    (    s/   lib/python2.7/site-packages/testpath/tempdir.pyRA   ˆ   s   		(   R-   t
   __future__R    t   osR/   t   warningsR5   t   sysR   t   tempfileR   t   ImportErrorR   R   t   objectR7   RA   (    (    (    s/   lib/python2.7/site-packages/testpath/tempdir.pyt   <module>   s   O%