o
    ;“bÕ   ã                   @  s¬   d dl m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m	Z	 d dl
mZ d dlmZ d dlmZ ddlmZ e d	¡ZG d
d„ dƒZG dd„ deejƒZddgZdS )é    )ÚannotationsN)ÚABCÚabstractmethod)ÚLock)ÚTracebackType)ÚAnyé   )ÚTimeoutÚfilelockc                   @  s.   e Zd ZdZddd„Zddd	„Zddd„ZdS )ÚAcquireReturnProxyzDA context aware object that will release the lock file when exiting.ÚlockÚBaseFileLockÚreturnÚNonec                 C  s
   || _ d S ©N©r   )Úselfr   © r   ú//usr/lib/python3/dist-packages/filelock/_api.pyÚ__init__   s   
zAcquireReturnProxy.__init__c                 C  ó   | j S r   r   ©r   r   r   r   Ú	__enter__   s   zAcquireReturnProxy.__enter__Úexc_typeútype[BaseException] | NoneÚ	exc_valueúBaseException | NoneÚ	tracebackúTracebackType | Nonec                 C  s   | j  ¡  d S r   )r   Úrelease©r   r   r   r   r   r   r   Ú__exit__   s   zAcquireReturnProxy.__exit__N)r   r   r   r   ©r   r   ©r   r   r   r   r   r   r   r   )Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r   r!   r   r   r   r   r      s
    

r   c                   @  s²   e Zd ZdZd3d4d	d
„Zed5dd„ƒZed6dd„ƒZejd7dd„ƒZe	d8dd„ƒZ
e	d8dd„ƒZed9dd„ƒZ		d:ddœd;d!d"„Zd<d=d%d&„Zd>d'd(„Zd?d/d0„Zd8d1d2„ZdS )@r   z+Abstract base class for a file lock object.éÿÿÿÿÚ	lock_fileústr | os.PathLike[Any]ÚtimeoutÚfloatr   r   c                 C  s*   t  |¡| _d| _|| _tƒ | _d| _dS )a�  
        Create a new lock object.

        :param lock_file: path to the file
        :param timeout: default timeout when acquiring the lock. 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.
        Nr   )ÚosÚfspathÚ
_lock_fileÚ_lock_file_fdr+   r   Ú_thread_lockÚ_lock_counter)r   r)   r+   r   r   r   r   *   s
   

zBaseFileLock.__init__Ústrc                 C  r   )z:return: path to the lock file)r/   r   r   r   r   r)   D   s   zBaseFileLock.lock_filec                 C  r   )zU
        :return: the default timeout value

        .. versionadded:: 2.0.0
        )Ú_timeoutr   r   r   r   r+   I   s   zBaseFileLock.timeoutÚvalueúfloat | strc                 C  s   t |ƒ| _dS )zX
        Change the default timeout value.

        :param value: the new value
        N)r,   r4   )r   r5   r   r   r   r+   R   s   c                 C  ó   t ‚)zbIf the file lock could be acquired, self._lock_file_fd holds the file descriptor of the lock file.©ÚNotImplementedErrorr   r   r   r   Ú_acquire[   ó   zBaseFileLock._acquirec                 C  r7   )z6Releases the lock and sets self._lock_file_fd to None.r8   r   r   r   r   Ú_release`   r;   zBaseFileLock._releaseÚboolc                 C  s
   | j duS )zÄ

        :return: A boolean indicating if the lock file is holding the lock currently.

        .. versionchanged:: 2.0.0

            This was previously a method and is now a property.
        N)r0   r   r   r   r   Ú	is_lockede   s   

zBaseFileLock.is_lockedNçš™™™™™©?)Úpoll_intervallúfloat | NoneÚpoll_intervalr@   r   c                C  s‚  |du r| j }|durd}tj|tdd� |}| j� |  jd7  _W d  ƒ n1 s,w   Y  t| ƒ}| j}t 	¡ }z[	 | j� | j
sPt d||¡ |  ¡  W d  ƒ n1 sZw   Y  | j
rjt d||¡ n,d	|  krxt 	¡ | k r†n nt d
||¡ t| jƒ‚d}t ||||¡ t |¡ q>W n$ ty»   | j� td	| jd ƒ| _W d  ƒ ‚ 1 sµw   Y  ‚ w t| d�S )az  
        Try to acquire the file lock.

        :param timeout: maximum wait time for acquiring the lock, ``None`` means use the default :attr:`~timeout` is and
         if ``timeout < 0``, there is no timeout and this method will block until the lock could be acquired
        :param poll_interval: interval of trying to acquire the lock file
        :param poll_intervall: deprecated, kept for backwards compatibility, use ``poll_interval`` instead
        :raises Timeout: if fails to acquire lock within the timeout period
        :return: a context object that will unlock the file when the context is exited

        .. 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()

        .. 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.

        Nz+use poll_interval instead of poll_intervallé   )Ú
stacklevelr   Tz#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+   ÚwarningsÚwarnÚDeprecationWarningr1   r2   Úidr/   ÚtimeÚ	monotonicr>   Ú_LOGGERÚdebugr:   r	   ÚsleepÚBaseExceptionÚmaxr   )r   r+   rB   r@   ÚmsgÚlock_idÚlock_filenameÚ
start_timer   r   r   Úacquireq   sP   %ÿ€ý 

ñ
ÿþý
zBaseFileLock.acquireFÚforcec                 C  s¨   | j �G | jr:|  jd8  _| jdks|rBt| ƒ| j}}t d||¡ |  ¡  d| _t d||¡ W d  ƒ dS W d  ƒ dS W d  ƒ dS 1 sMw   Y  dS )a*  
        Releases the file lock. Please note, that the lock is only completely released, if the lock counter is 0. Also
        note, that the lock file itself is not automatically deleted.

        :param force: If true, the lock counter is ignored and the lock is released in every case/
        r   r   z#Attempting to release lock %s on %szLock %s released on %sN)r1   r>   r2   rH   r/   rK   rL   r<   )r   rU   rQ   rR   r   r   r   r   ¼   s   õþ"ûzBaseFileLock.releasec                 C  s   |   ¡  | S )zE
        Acquire the lock.

        :return: the lock object
        )rT   r   r   r   r   r   Ð   s   zBaseFileLock.__enter__r   r   r   r   r   r   c                 C  s   |   ¡  dS )zÎ
        Release the lock.

        :param exc_type: the exception type if raised
        :param exc_value: the exception value if raised
        :param traceback: the exception traceback if raised
        N©r   r    r   r   r   r!   Ù   s   zBaseFileLock.__exit__c                 C  s   | j dd� dS )z'Called when the lock object is deleted.T)rU   NrV   r   r   r   r   Ú__del__è   s   zBaseFileLock.__del__)r(   )r)   r*   r+   r,   r   r   )r   r3   )r   r,   )r5   r6   r   r   )r   r   )r   r=   )Nr?   )r+   rA   rB   r,   r@   rA   r   r   )F)rU   r=   r   r   r"   r#   )r$   r%   r&   r'   r   Úpropertyr)   r+   Úsetterr   r:   r<   r>   rT   r   r   r!   rW   r   r   r   r   r   '   s0    ýûK

	r   )Ú
__future__r   Ú
contextlibÚloggingr-   rI   rE   Úabcr   r   Ú	threadingr   Útypesr   Útypingr   Ú_errorr	   Ú	getLoggerrK   r   ÚContextDecoratorr   Ú__all__r   r   r   r   Ú<module>   s$    
 Hþ