o
    ~eI(                     @  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mZ ddlmZ erZd dlZd d	lmZ ejd
krTd dlmZ nd dlmZ edZG dd dZeG dd dZG dd deeZG dd deejZddgZ dS )    )annotationsN)ABCabstractmethod)	dataclass)local)TYPE_CHECKINGAny   )Timeout)TracebackType)      )SelfZ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   =D:\Projects\ConvertPro\env\Lib\site-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                   @  sB   e Zd ZU dZded< ded< ded< dZd	ed
< dZded< dS )FileLockContextzBA dataclass which holds the context for a ``BaseFileLock`` object.str	lock_filefloattimeoutintmodeNz
int | Nonelock_file_fdr   lock_counter)r'   r(   r)   r*   __annotations__r2   r3   r   r   r   r   r+   1   s   
 r+   c                   @  s   e Zd ZdZdS )ThreadLocalFileContextz8A thread local version of the ``FileLockContext`` class.N)r'   r(   r)   r*   r   r   r   r   r5   H   s    r5   c                   @  s   e Zd ZdZ			d>d?ddZd@ddZedAddZedBddZej	dCddZe
dDddZe
dDddZed@dd ZedEd!d"Z	#	$dFd#dd%dGd+d,ZdHdId/d0ZdJd2d3ZdKd:d;ZdDd<d=Zd#S )Lr   z+Abstract base class for a file lock object.  Tr-   str | os.PathLike[str]r/   r.   r1   r0   thread_localboolr   r   c                 C  s4   || _ t|||d}|rtntdi || _dS )a  
        Create a new lock object.

        :param lock_file: path to the file
        :param timeout: default timeout when acquiring the lock, in seconds. 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.
        :param mode: file permissions for the lockfile.
        :param thread_local: Whether this object's internal context should be thread local or not.
        If this is set to ``False`` then the lock will be reentrant across threads.
        )r-   r/   r1   Nr   )_is_thread_localosfspathr5   r+   _context)r   r-   r/   r1   r9   kwargsr   r   r   r   O   s   zBaseFileLock.__init__c                 C  r   )z>:return: a flag indicating if this lock is thread local or not)r;   r   r   r   r   is_thread_locall   s   zBaseFileLock.is_thread_localr,   c                 C     | j jS )z:return: path to the lock file)r>   r-   r   r   r   r   r-   p      zBaseFileLock.lock_filec                 C  rA   )za
        :return: the default timeout value, in seconds

        .. versionadded:: 2.0.0
        )r>   r/   r   r   r   r   r/   u   s   zBaseFileLock.timeoutvaluefloat | strc                 C  s   t || j_dS )zd
        Change the default timeout value.

        :param value: the new value, in seconds
        N)r.   r>   r/   )r   rC   r   r   r   r/   ~   s   c                 C     t )zjIf the file lock could be acquired, self._context.lock_file_fd holds the file descriptor of the lock file.NotImplementedErrorr   r   r   r   _acquire      zBaseFileLock._acquirec                 C  rE   )z>Releases the lock and sets self._context.lock_file_fd to None.rF   r   r   r   r   _release   rI   zBaseFileLock._releasec                 C  s   | j 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)r>   r2   r   r   r   r   	is_locked   s   
zBaseFileLock.is_lockedc                 C  rA   )zP:return: The number of times this lock has been acquired (but not yet released).)r>   r3   r   r   r   r   r3      rB   zBaseFileLock.lock_counterN皙?)poll_intervallblockingfloat | Nonepoll_intervalrM   rN   r   c          	      C  s4  |du r| j j}|durd}tj|tdd |}| j  jd7  _t| }| j}t	 }zV	 | j
s;td|| |   | j
rFtd|| n:|d	u rUtd
|| t|d|  krct	 | k rpn ntd|| t|d}t|||| t| q-W n ty   td| j jd | j _ w t| dS )a_  
        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
        :param blocking: defaults to True. If False, function will return immediately if it cannot obtain a lock on the
         first attempt. Otherwise, this method will block until the timeout expires or the lock is acquired.
        :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 %sFz+Failed to immediately acquire lock %s on %sr   z"Timeout on acquiring lock %s on %sz2Lock %s not acquired on %s, waiting %s seconds ...r   )r>   r/   warningswarnDeprecationWarningr3   idr-   timeperf_counterrK   _LOGGERdebugrH   r
   sleepBaseExceptionmaxr   )	r   r/   rP   rM   rN   msglock_idlock_filename
start_timer   r   r   acquire   sD   ( 


zBaseFileLock.acquireFforcec                 C  sn   | j r3| j jd8  _| jjdks|r5t| | j}}td|| |   d| j_td|| dS dS 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)rK   r>   r3   rV   r-   rY   rZ   rJ   )r   rc   r_   r`   r   r   r   r#      s   zBaseFileLock.releaser   c                 C  s   |    | S )zE
        Acquire the lock.

        :return: the lock object
        )rb   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)rc   Nrd   r   r   r   r   __del__  s   zBaseFileLock.__del__)r6   r7   T)
r-   r8   r/   r.   r1   r0   r9   r:   r   r   )r   r:   )r   r,   )r   r.   )rC   rD   r   r   )r   r   )r   r0   )NrL   )
r/   rO   rP   r.   rM   rO   rN   r:   r   r   )F)rc   r:   r   r   )r   r   r&   )r'   r(   r)   r*   r   r@   propertyr-   r/   setterr   rH   rJ   rK   r3   rb   r#   r   r%   re   r   r   r   r   r   L   s>    
L

	r   )!
__future__r   
contextlibloggingr<   rW   rS   abcr   r   dataclassesr   	threadingr   typingr   r   _errorr
   systypesr   version_infor   Ztyping_extensions	getLoggerrY   r   r+   r5   ContextDecoratorr   __all__r   r   r   r   <module>   s6    

 S