U
    |Ha                     @   sJ  d dl Z d dlZd dlZd dlZd dlZzd dlZW n ek
rL   dZY nX zd dlmZ W n ek
rv   dZY nX dZej	d  dkZ
e
reneZdd Ze
rdndZejZejd	kreed
rdd Zdd Zdd Zdd Zn4d dlmZmZ dZdZeZdd Zdd Zdd Zdd Zdd ZG dd de Z!e!fd d!Z"dS )"    N)fspathz1.4.0   c                 C   s   t | ts| t S | S N)
isinstance	text_typedecodesysgetfilesystemencoding)x r   9/tmp/pip-unpacked-wheel-fm3mek5y/atomicwrites/__init__.py_path_to_unicode   s    
r   wbwwin32F_FULLFSYNCc                 C   s   t  | t j d S r   )fcntlr   )fdr   r   r   _proper_fsync(   s    r   c              	   C   s*   t | d}zt| W 5 t | X d S )Nr   )osopencloser   )	directoryr   r   r   r   _sync_directory.   s    r   c                 C   s(   t | | tt jt j| d S r   )r   renamer   pathnormpathdirnamesrcdstr   r   r   _replace_atomic6   s    r!   c                 C   sZ   t | | t |  t jt j| }t jt j|}t| ||krVt| d S r   )r   linkunlinkr   r   r   r   )r   r    src_dirZdst_dirr   r   r   _move_atomic:   s    
r%   )windllWinError      c                 C   s   | s
t  d S r   )r'   )rvr   r   r   _handle_errorsJ   s    r+   c                 C   s$   t tjt| t|ttB  d S r   )r+   r&   kernel32MoveFileExWr   _windows_default_flags_MOVEFILE_REPLACE_EXISTINGr   r   r   r   r!   N   s
     c                 C   s    t tjt| t|t d S r   )r+   r&   r,   r-   r   r.   r   r   r   r   r%   T   s
     c                 C   s
   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   r   r   r   replace_atomic[   s    r0   c                 C   s
   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   r   r   r   move_atomicf   s    	r1   c                   @   s`   e Zd ZdZedfddZdd Zejdd Z	d	e
 d
fddZdd Zdd Zdd Zd
S )AtomicWriteraY  
    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.

    If you need further control over the exact behavior, you are encouraged to
    subclass.
    Fc                 K   s\   d|krt dd|kr t dd|kr0t dtd k	r@t|}|| _|| _|| _|| _d S )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	overwriteZopen_kwargsr   r   r   __init__   s    zAtomicWriter.__init__c                 C   s   |  | jS )z*
        Open the temporary file.
        )_openget_fileobject)r9   r   r   r   r      s    zAtomicWriter.openc                 c   sr   d }z>d}|f | j}|V  | | W 5 Q R X | | d}W 5 |slz|  | W n tk
rj   Y nX X d S )NFT)rollback	Exceptionr8   synccommit)r9   r>   fsuccessr   r   r   r=      s    
zAtomicWriter._open Nc                 K   sZ   |dkrt jt j| j}tj|||d\}}t | | j|d< ||d< t	j
f |S )z!Return the temporary file to use.N)suffixprefixdirr:   file)r   r   r   r   r5   tempfilemkstempr   r6   ior   )r9   rF   rG   rH   kwargsZ
descriptornamer   r   r   r>      s    


zAtomicWriter.get_fileobjectc                 C   s   |   t|  dS )zNresponsible for clearing as many file caches as possible before
        commitN)flushr   filenor9   rC   r   r   r   rA      s    zAtomicWriter.syncc                 C   s(   | j rt|j| j nt|j| j dS )z/Move the temporary file to the target location.N)r7   r0   rN   r5   r1   rQ   r   r   r   rB      s    zAtomicWriter.commitc                 C   s   t |j dS )z!Clean up all temporary resources.N)r   r#   rN   rQ   r   r   r   r?      s    zAtomicWriter.rollback)__name__
__module____qualname____doc__DEFAULT_MODEr<   r   
contextlibcontextmanagerr=   rJ   gettempprefixr>   rA   rB   r?   r   r   r   r   r2   r   s   

r2   c                 K   s   || f|  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   Z
writer_clsZ
cls_kwargsr   r   r   atomic_write   s    rZ   )#rW   rL   r   r   rJ   r   ImportErrorr   __version__version_infoPY2unicodestrr   r   rV   fsyncr   platformhasattrr   r!   r%   ctypesr&   r'   r/   Z_MOVEFILE_WRITE_THROUGHr.   r+   r0   r1   objectr2   rZ   r   r   r   r   <module>   sF   





a