
    xb:                     z   d dl Z d dlZd dlZd dlZd dlZ	 d dlZ	 d dlmZ dZej                  d    dk(  Z
e
reneZd Ze
rdndZej                   Zej$                  dk7  r eed	      rd
 Zd Zd Zd Znd dlmZmZ dZdZeZd Zd Zd Zd Zd Z G d de       Z!e!fdZ"y# e$ r dZY w xY w# e$ r dZY w xY w)    N)fspathz1.4.1   c                 l    t        | t              s#| j                  t        j                               S | S N)
isinstance	text_typedecodesysgetfilesystemencoding)xs    7/usr/lib/python3/dist-packages/atomicwrites/__init__.py_path_to_unicoder      s)    a#xx11344H    wbwwin32F_FULLFSYNCc                 L    t        j                   | t         j                         y r   )fcntlr   )fds    r   _proper_fsyncr   (   s     KKE--.r   c                     t        j                  | d      }	 t        |       t        j                  |       y # t        j                  |       w xY w)Nr   )osopenr   close)	directoryr   s     r   _sync_directoryr   .   s6    WWY"	"HHRLBHHRLs	   9 Ac                     t        j                  | |       t        t         j                  j	                  t         j                  j                  |                   y r   )r   renamer   pathnormpathdirnamesrcdsts     r   _replace_atomicr&   6   s4    
		#s(()=>?r   c                    t        j                  | |       t        j                  |        t         j                  j	                  t         j                  j                  |             }t         j                  j	                  t         j                  j                  |            }t        |       ||k7  rt        |       y y r   )r   linkunlinkr    r!   r"   r   )r$   r%   src_dirdst_dirs       r   _move_atomicr,   :   sz    
S
		#''""277??3#78''""277??3#78 gG$ r   )windllWinError      c                     | s
t               y r   )r.   )rvs    r   _handle_errorsr3   J   s    * r   c                     t        t        j                  j                  t	        |       t	        |      t
        t        z               y r   )r3   r-   kernel32MoveFileExWr   _windows_default_flags_MOVEFILE_REPLACE_EXISTINGr#   s     r   r&   r&   N   s4    v22S!#3C#8"%??
 	r   c                     t        t        j                  j                  t	        |       t	        |      t
                     y r   )r3   r-   r5   r6   r   r7   r#   s     r   r,   r,   T   s.    v22S!#3C#8"
 	r   c                     t        | |      S )z
    Move ``src`` to ``dst``. If ``dst`` exists, it will be silently
    overwritten.

    Both paths must reside on the same filesystem for the operation to be
    atomic.
    )r&   r#   s     r   replace_atomicr;   [   s     3$$r   c                     t        | |      S )a  
    Move ``src`` to ``dst``. There might a timewindow where both filesystem
    entries exist. If ``dst`` already exists, :py:exc:`FileExistsError` will be
    raised.

    Both paths must reside on the same filesystem for the operation to be
    atomic.
    )r,   r#   s     r   move_atomicr=   f   s     S!!r   c                       e Zd ZdZedfdZd Zej                  d        Z	d e
j                         dfdZd	 Zd
 Zd Zy)AtomicWritera	  
    A helper class for performing atomic writes. Usage::

        with AtomicWriter(path).open() as f:
            f.write(...)

    :param path: The destination filepath. May or may not exist.
    :param mode: The filemode for the temporary file. This defaults to `wb` in
        Python 2 and `w` in Python 3.
    :param overwrite: If set to false, an error is raised if ``path`` exists.
        Errors are only raised after the file has been written to.  Either way,
        the operation is atomic.
    :param open_kwargs: Keyword-arguments to pass to the underlying
        :py:func:`open` call. This can be used to set the encoding when opening
        files in text-mode.

    If you need further control over the exact behavior, you are encouraged to
    subclass.
    Fc                     d|v rt        d      d|v rt        d      d|vrt        d      t        t        |      }|| _        || _        || _        || _        y )NazAppending to an existing file is not supported, because that would involve an expensive `copy`-operation to a temporary file. Open the file in normal `w`-mode and copy explicitly if that's what you're after.r   z&Use the `overwrite`-parameter instead.r   z%AtomicWriters can only be written to.)
ValueErrorr   _path_mode
_overwrite_open_kwargs)selfr    mode	overwriteopen_kwargss        r   __init__zAtomicWriter.__init__   sq    $;1  $;EFFd?DEE $<D

#'r   c                 8    | j                  | j                        S )z*
        Open the temporary file.
        )_openget_fileobject)rG   s    r   r   zAtomicWriter.open   s     zz$--..r   c              #   N  K   d }	 d} |di | j                   5 }| | j                  |       d d d        | j                  |       d}|s	 | j                  |       y y # 1 sw Y   2xY w# t        $ r Y y w xY w# s"	 | j                  |       w # t        $ r Y w w xY ww xY ww)NFT )rF   synccommitrollback	Exception)rG   rN   fsuccesss       r   rM   zAtomicWriter._open   s     	G4$"3"34 		! KKNGMM!$   !  MM!$   sz   B%A< A!A< B%A- B%!A*&A< -	A96B%8A99B%<B" BB"	BB"BB""B% Nc                 D   |Ft         j                  j                  t         j                  j                  | j                              }t        j                  |||      \  }}t        j                  |       | j                  |d<   ||d<   t        j                  di |S )z!Return the temporary file to use.)suffixprefixdirrH   filerP   )r   r    r!   r"   rC   tempfilemkstempr   rD   ior   )rG   rY   rZ   r[   kwargs
descriptornames          r   rN   zAtomicWriter.get_fileobject   s     ;''""277??4::#>?C#++6&035
D
 	vvww   r   c                 V    |j                          t        |j                                y)zNresponsible for clearing as many file caches as possible before
        commitN)flushr   filenorG   rU   s     r   rQ   zAtomicWriter.sync   s     	
	ahhj!r   c                     | j                   r!t        |j                  | j                         yt	        |j                  | j                         y)z/Move the temporary file to the target location.N)rE   r;   rb   rC   r=   rf   s     r   rR   zAtomicWriter.commit   s-    ??1664::.

+r   c                 B    t        j                  |j                         y)z!Clean up all temporary resources.N)r   r)   rb   rf   s     r   rS   zAtomicWriter.rollback   s    
		!&&r   )__name__
__module____qualname____doc__DEFAULT_MODErK   r   
contextlibcontextmanagerrM   r]   gettempprefixrN   rQ   rR   rS   rP   r   r   r?   r?   r   s\    ( #/% (./    %'/Ex/E/E/G!",r   r?   c                 0     || fi |j                         S )a  
    Simple atomic writes. This wraps :py:class:`AtomicWriter`::

        with atomic_write(path) as f:
            f.write(...)

    :param path: The target path to write to.
    :param writer_cls: The writer class to use. This parameter is useful if you
        subclassed :py:class:`AtomicWriter` to change some behavior and want to
        use that new subclass.

    Additional keyword arguments are passed to the writer class. See
    :py:class:`AtomicWriter`.
    )r   )r    
writer_cls
cls_kwargss      r   atomic_writert      s     d)j)..00r   )#rn   r_   r   r
   r]   r   ImportErrorr   __version__version_infoPY2unicodestrr   r   rm   fsyncr   platformhasattrr   r&   r,   ctypesr-   r.   r8   _MOVEFILE_WRITE_THROUGHr7   r3   r;   r=   objectr?   rt   rP   r   r   <module>r      s    	 	 
 
  
qQG	 t  <<7um$	/@% (!$!4%	"a6 aH #/ 1[  E  Fs"   B# B0 #B-,B-0B:9B: