U
    Ha3                     @   sp  d 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
rH   dZY nX zddlZW n ek
rn   dZY nX zddlZW n ek
r   dZY nX ze	 W n e
k
r   eZ	Y nX ddddddgZd	Zdad
d ZG dd de	ZG dd deZG dd deZG dd deZG dd deZG dd deZdZerHeZn$erTeZneZedk	rled dS )zD
A platform independent file lock that supports the with-statement.
    NTimeoutBaseFileLockWindowsFileLockUnixFileLockSoftFileLockFileLockz3.0.12c                   C   s   t ptta t S )z0Returns the logger instance used in this module.)_loggerlogging	getLogger__name__ r   r   ,/tmp/pip-unpacked-wheel-nk9nrq51/filelock.pyloggerL   s    r   c                   @   s    e Zd ZdZdd Zdd ZdS )r   zN
    Raised when the lock could not be acquired in *timeout*
    seconds.
    c                 C   s
   || _ dS 	
        N)	lock_file)selfr   r   r   r   __init__[   s    zTimeout.__init__c                 C   s   d | j}|S )Nz)The file lock '{}' could not be acquired.)formatr   )r   tempr   r   r   __str__b   s    zTimeout.__str__N)r   
__module____qualname____doc__r   r   r   r   r   r   r   U   s   c                   @   s$   e Zd Zdd Zdd Zdd ZdS )_Acquire_ReturnProxyc                 C   s
   || _ d S Nlock)r   r   r   r   r   r   u   s    z_Acquire_ReturnProxy.__init__c                 C   s   | j S r   r   r   r   r   r   	__enter__y   s    z_Acquire_ReturnProxy.__enter__c                 C   s   | j   d S r   )r   releaser   exc_type	exc_value	tracebackr   r   r   __exit__|   s    
z_Acquire_ReturnProxy.__exit__N)r   r   r   r   r   r%   r   r   r   r   r   s   s   r   c                   @   s   e Zd ZdZdddZedd Zedd Zejd	d Zd
d Z	dd Z
edd ZdddZdddZdd Zdd Zdd ZdS ) r   z3
    Implements the base class of a file lock.
    c                 C   s&   || _ d| _|| _t | _d| _dS )r   Nr   )
_lock_file_lock_file_fdtimeout	threadingLock_thread_lock_lock_counter)r   r   r)   r   r   r   r      s    
zBaseFileLock.__init__c                 C   s   | j S )z,
        The path to the lock file.
        )r'   r   r   r   r   r      s    zBaseFileLock.lock_filec                 C   s   | j S )a~  
        You can set a default timeout for the filelock. It will be used as
        fallback value in the acquire method, if no timeout value (*None*) is
        given.

        If you want to disable the timeout, set it to a negative value.

        A timeout of 0 means, that there is exactly one attempt to acquire the
        file lock.

        .. versionadded:: 2.0.0
        )_timeoutr   r   r   r   r)      s    zBaseFileLock.timeoutc                 C   s   t || _dS r   )floatr.   )r   valuer   r   r   r)      s    
c                 C   s
   t  dS )z
        Platform dependent. If the file lock could be
        acquired, self._lock_file_fd holds the file descriptor
        of the lock file.
        NNotImplementedErrorr   r   r   r   _acquire   s    zBaseFileLock._acquirec                 C   s
   t  dS )zH
        Releases the lock and sets self._lock_file_fd to None.
        Nr1   r   r   r   r   _release   s    zBaseFileLock._releasec                 C   s
   | j dk	S )z
        True, if the object holds the file lock.

        .. versionchanged:: 2.0.0

            This was previously a method and is now a property.
        N)r(   r   r   r   r   	is_locked   s    	zBaseFileLock.is_lockedN皙?c                 C   s"  |dkr| j }| j |  jd7  _W 5 Q R X t| }| j}t }z| j$ | jslt d|| | 	  W 5 Q R X | jrt 
d|| qqF|dkrt | |krt d|| t| jqFt d||| t| qFW n2   | j td| jd | _W 5 Q R X  Y nX t| dS )	aY  
        Acquires the file lock or fails with a :exc:`Timeout` error.

        .. code-block:: python

            # You can use this method in the context manager (recommended)
            with lock.acquire():
                pass

            # Or use an equivalent try-finally construct:
            lock.acquire()
            try:
                pass
            finally:
                lock.release()

        :arg float timeout:
            The maximum time waited for the file lock.
            If ``timeout < 0``, there is no timeout and this method will
            block until the lock could be acquired.
            If ``timeout`` is None, the default :attr:`~timeout` is used.

        :arg float poll_intervall:
            We check once in *poll_intervall* seconds if we can acquire the
            file lock.

        :raises Timeout:
            if the lock could not be acquired in *timeout* seconds.

        .. versionchanged:: 2.0.0

            This method returns now a *proxy* object instead of *self*,
            so that it can be used in a with statement without side effects.
        N   z#Attempting to acquire lock %s on %szLock %s acquired on %sr   z"Timeout on acquiring lock %s on %sz2Lock %s not acquired on %s, waiting %s seconds ...r   )r)   r,   r-   idr'   timer5   r   debugr3   infor   sleepmaxr   )r   r)   Zpoll_intervalllock_idlock_filename
start_timer   r   r   acquire   s<    $  zBaseFileLock.acquireFc              	   C   st   | j d | jrf|  jd8  _| jdks*|rft| }| j}t d|| |   d| _t d|| W 5 Q R X dS )aV  
        Releases the file lock.

        Please note, that the lock is only completly released, if the lock
        counter is 0.

        Also note, that the lock file itself is not automatically deleted.

        :arg bool force:
            If true, the lock counter is ignored and the lock is released in
            every case.
        r7   r   z#Attempting to release lock %s on %szLock %s released on %sN)	r,   r5   r-   r8   r'   r   r:   r4   r;   )r   forcer>   r?   r   r   r   r    %  s    zBaseFileLock.releasec                 C   s   |    | S r   )rA   r   r   r   r   r   B  s    zBaseFileLock.__enter__c                 C   s   |    d S r   r    r!   r   r   r   r%   F  s    zBaseFileLock.__exit__c                 C   s   | j dd d S )NT)rB   rC   r   r   r   r   __del__J  s    zBaseFileLock.__del__)r&   )Nr6   )F)r   r   r   r   r   propertyr   r)   setterr3   r4   r5   rA   r    r   r%   rD   r   r   r   r   r      s"   



		


J
c                   @   s    e Zd ZdZdd Zdd ZdS )r   ze
    Uses the :func:`msvcrt.locking` function to hard lock the lock file on
    windows systems.
    c              	   C   s|   t jt jB t jB }zt | j|}W n tk
r8   Y n@X zt|tj	d W n" t
tfk
rp   t | Y nX || _d S Nr7   )osO_RDWRO_CREATO_TRUNCopenr'   OSErrormsvcrtlockingZLK_NBLCKIOErrorcloser(   r   Z	open_modefdr   r   r   r3   X  s    zWindowsFileLock._acquirec                 C   sP   | j }d | _ t|tjd t| zt| j W n tk
rJ   Y nX d S rG   )	r(   rN   rO   ZLK_UNLCKrH   rQ   remover'   rM   r   rS   r   r   r   r4   h  s    
zWindowsFileLock._releaseNr   r   r   r   r3   r4   r   r   r   r   r   R  s   c                   @   s    e Zd ZdZdd Zdd ZdS )r   zR
    Uses the :func:`fcntl.flock` to hard lock the lock file on unix systems.
    c              	   C   sf   t jt jB t jB }t | j|}zt|tjtj	B  W n" t
tfk
rZ   t | Y nX || _d S r   )rH   rI   rJ   rK   rL   r'   fcntlflockZLOCK_EXZLOCK_NBrP   rM   rQ   r(   rR   r   r   r   r3   ~  s    zUnixFileLock._acquirec                 C   s(   | j }d | _ t|tj t| d S r   )r(   rW   rX   ZLOCK_UNrH   rQ   rU   r   r   r   r4     s
    
zUnixFileLock._releaseNrV   r   r   r   r   r   y  s   c                   @   s    e Zd ZdZdd Zdd ZdS )r   z8
    Simply watches the existence of the lock file.
    c              	   C   sN   t jt jB t jB t jB }zt | j|}W n ttfk
rB   Y nX || _	d S r   )
rH   O_WRONLYrJ   O_EXCLrK   rL   r'   rP   rM   r(   rR   r   r   r   r3     s    zSoftFileLock._acquirec                 C   s<   t | j d | _zt | j W n tk
r6   Y nX d S r   )rH   rQ   r(   rT   r'   rM   r   r   r   r   r4     s    zSoftFileLock._releaseNrV   r   r   r   r   r     s   
z only soft file lock is available)r   r	   rH   r*   r9   warningsImportErrorrN   rW   TimeoutError	NameErrorrM   __all____version__r   r   r   objectr   r   r   r   r   r   warnr   r   r   r   <module>   sZ   



		 R'!
