
    ob                         d Z ddlmZmZ ddlZddlZddlZddlZddlm	Z	 ddl
mZ ej                  rddlmZmZ ej                   G d d	e             Zy)
aL  Manage filesystems in temporary locations.

A temporary filesytem is stored in a location defined by your OS
(``/tmp`` on linux). The contents are deleted when the filesystem
is closed.

A `TempFS` is a good way of preparing a directory structure in advance,
that you can later copy. It can also be used as a temporary data store.

    )print_functionunicode_literalsN   )errors)OSFS)OptionalTextc                   H     e Zd ZdZ	 	 	 	 d fd	Zd Zd Z fdZd Z xZ	S )TempFSa  A temporary filesystem on the OS.

    Temporary filesystems are created using the `tempfile.mkdtemp`
    function to obtain a temporary folder in an OS-specific location.
    You can provide an alternative location with the ``temp_dir``
    argument of the constructor.

    Examples:
        Create with the constructor::

            >>> from fs.tempfs import TempFS
            >>> tmp_fs = TempFS()

        Or via an FS URL::

            >>> import fs
            >>> tmp_fs = fs.open_fs("temp://")

        Use a specific identifier for the temporary folder to better
        illustrate its purpose::

            >>> named_tmp_fs = fs.open_fs("temp://local_copy")
            >>> named_tmp_fs = TempFS(identifier="local_copy")

    c                     || _         || _        || _        d| _        |j	                  dd      | _         t        j                  |xs d|      | _        t        t        | +  | j                         y)ay  Create a new `TempFS` instance.

        Arguments:
            identifier (str): A string to distinguish the directory within
                the OS temp location, used as part of the directory name.
            temp_dir (str, optional): An OS path to your temp directory
                (leave as `None` to auto-detect).
            auto_clean (bool): If `True` (the default), the directory
                contents will be wiped on close.
            ignore_clean_errors (bool): If `True` (the default), any errors
                in the clean process will be suppressed. If `False`, they
                will be raised.

        F/-fsTempFS)dirN)
identifier_auto_clean_ignore_clean_errors_cleanedreplacetempfilemkdtemp	_temp_dirsuperr   __init__)selfr   temp_dir
auto_cleanignore_clean_errors	__class__s        +/usr/lib/python3/dist-packages/fs/tempfs.pyr   zTempFS.__init__7   sc    , %%$7!$,,S#6!))**B
Qfd$T^^4    c                      y)NzTempFS() r   s    r    __repr__zTempFS.__repr__W   s    r!   c                 8    dj                  | j                        S )Nz<tempfs '{}'>)formatr   r$   s    r    __str__zTempFS.__str__[   s    %%dnn55r!   c                 b    | j                   r| j                          t        t        |           y)a7  Close the filesystem and release any resources.

        It is important to call this method when you have finished
        working with the filesystem. Some filesystems may not finalize
        changes until they are closed (archives for example). You may
        call this method explicitly (it is safe to call close multiple
        times), or you can use the filesystem as a context manager to
        automatically close.

        Hint:
            Depending on the value of ``auto_clean`` passed when creating
            the `TempFS`, the underlying temporary folder may be removed
            or not.

        Example:
            >>> tmp_fs = TempFS(auto_clean=False)
            >>> syspath = tmp_fs.getsyspath("/")
            >>> tmp_fs.close()
            >>> os.path.exists(syspath)
            True

        N)r   cleanr   r   close)r   r   s    r    r+   zTempFS.close_   s$    0 JJLfd!#r!   c                 
   | j                   ry	 t        j                  | j                         d| _         y# t        $ rC}| j
                  s&t        j                  dj                  |      |      Y d}~d| _         yd}~ww xY w)z:Clean (delete) temporary files created by this filesystem.Nz(failed to remove temporary directory; {})msgexcT)	r   shutilrmtreer   	Exceptionr   r   OperationFailedr'   )r   errors     r    r*   zTempFS.clean{   sy     ==	MM$..)   	,,,,BII%P  -
 	s   6 	B2A==B)
__tempfs__NTT)
__name__
__module____qualname____doc__r   r%   r(   r+   r*   __classcell__)r   s   @r    r   r      s0    8   5@6$8r!   r   )r8   
__future__r   r   typingr/   sixr    r   osfsr   TYPE_CHECKINGr   r	   python_2_unicode_compatibler   r#   r!   r    <module>rA      sQ   	 8   
   	%   mT m !mr!   