o
    \~#g8                     @  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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mZ d dlmZ dd	lmZ erfd dlZd d
lmZ ejdkr`d 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	Z"G dd dej#e"dZ$ddgZ%dS )    )annotationsN)ABCMetaabstractmethod)	dataclass)local)TYPE_CHECKINGAnycast)WeakValueDictionary   )Timeout)TracebackType)      )Self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   r/var/www/static.ux5.de/https/Moving-Object-Detection-with-OpenCV/env/lib/python3.10/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                   @  sJ   e Zd ZU dZded< 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modeboolblockingNz
int | Nonelock_file_fdr   lock_counter)r*   r+   r,   r-   __annotations__r7   r8   r   r   r   r   r.   3   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   r:   M   s    r:   c                      s.   e Zd Z			ddddd fddZ  ZS )FileLockMeta  TFr6   is_singletonr0   str | os.PathLike[str]r2   r1   r4   r3   thread_localr5   r6   r?   kwargsr   r   r   c             	     s  |rS| j t|}|rS|| f||jf||jf||jfd}	dd |	 D }
|
s0tt	|S d}|d7 }|
 D ]\}\}}|d| d| d| d	7 }q:t
||||||d
|}t| jj  fdd| D }t j|fi |}|r|| j t|< tt	|S )N)rA   r2   r4   r6   c                 S  s&   i | ]\}\}}||kr|||fqS r   r   ).0namepassed_param	set_paramr   r   r   
<dictcomp>g   s
    
z)FileLockMeta.__call__.<locals>.<dictcomp>zGSingleton lock instances cannot be initialized with differing argumentsz
Non-matching arguments: z
	z (existing lock has z but z was passed))r2   r4   rA   r6   r?   c                   s   i | ]\}}| v r||qS r   r   )rC   keyvaluepresent_paramsr   r   rG      s    )
_instancesgetr/   is_thread_localr2   r4   r6   itemsr	   r   
ValueErrorinspect	signaturer   
parameterssuper__call__)clsr0   r2   r4   rA   r6   r?   rB   instanceparams_to_checknon_matching_paramsmsg
param_namerE   rF   
all_paramsinit_params	__class__rJ   r   rU   R   s@   

	
zFileLockMeta.__call__r<   r=   T)r0   r@   r2   r1   r4   r3   rA   r5   r6   r5   r?   r5   rB   r   r   r   )r*   r+   r,   rU   __classcell__r   r   r^   r   r;   Q   s    r;   c                      s0  e Zd ZU dZded< dN fdd	Z	
		dOddddPddZdQddZedQddZ	edRd d!Z
edSd"d#ZejdTd&d#ZedQd'd(ZejdUd)d(ZedVd*d+ZedWd,d-ZedWd.d/ZedQd0d1ZedVd2d3Z	4	5dXd4d4d6dYd<d=ZdZd[d?d@Zd\dBdCZd]dJdKZdWdLdMZ  ZS )^r   z+Abstract base class for a file lock object.z&WeakValueDictionary[str, BaseFileLock]rL   rB   dict[str, Any]r   r   c                   s   t  jdi | t | _dS )z'Setup unique state for lock subclasses.Nr   )rT   __init_subclass__r
   rL   )rV   rB   r^   r   r   rc      s   zBaseFileLock.__init_subclass__r<   r=   TFr>   r0   r@   r2   r1   r4   r3   rA   r5   r6   r?   c                C  s<   || _ || _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.
        :param blocking: whether the lock should be blocking or not
        :param is_singleton: If this is set to ``True`` then only one instance of this class will be created             per lock file. This is useful if you want to use the lock object for reentrant locking without needing             to pass the same object around.

        )r0   r2   r4   r6   Nr   )_is_thread_local_is_singletonosfspathr:   r.   _context)r   r0   r2   r4   rA   r6   r?   rB   r   r   r   r      s   zBaseFileLock.__init__c                 C  r   )z>:return: a flag indicating if this lock is thread local or not)rd   r   r   r   r   rN      s   zBaseFileLock.is_thread_localc                 C  r   )z;:return: a flag indicating if this lock is singleton or not)re   r   r   r   r   r?      s   zBaseFileLock.is_singletonr/   c                 C     | j jS )z:return: path to the lock file)rh   r0   r   r   r   r   r0         zBaseFileLock.lock_filec                 C  ri   )za
        :return: the default timeout value, in seconds

        .. versionadded:: 2.0.0
        )rh   r2   r   r   r   r   r2      s   zBaseFileLock.timeoutrI   float | strc                 C  s   t || j_dS )ze
        Change the default timeout value.

        :param value: the new value, in seconds

        N)r1   rh   r2   r   rI   r   r   r   r2      s   c                 C  ri   )z/:return: whether the locking is blocking or notrh   r6   r   r   r   r   r6      rj   zBaseFileLock.blockingc                 C  s   || j _dS )zb
        Change the default blocking value.

        :param value: the new value as bool

        Nrm   rl   r   r   r   r6      s   c                 C  ri   )z.:return: the file permissions for the lockfile)rh   r4   r   r   r   r   r4      rj   zBaseFileLock.modec                 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  rn   )z>Releases the lock and sets self._context.lock_file_fd to None.ro   r   r   r   r   _release   rr   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)rh   r7   r   r   r   r   	is_locked   s   
zBaseFileLock.is_lockedc                 C  ri   )zP:return: The number of times this lock has been acquired (but not yet released).)rh   r8   r   r   r   r   r8   
  rj   zBaseFileLock.lock_counterN皙?)poll_intervallr6   float | Nonepoll_intervalrv   bool | Noner   c          	      C  sD  |du r| j j}|du r| j j}|dur d}tj|tdd |}| j  jd7  _t| }| j}t	
 }zV	 | jsCtd|| |   | jrNtd|| n:|d	u r]td
|| t|d|  krkt	
 | k rxn ntd|| t|d}t|||| t	| q5W 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   )rh   r2   r6   warningswarnDeprecationWarningr8   idr0   timeperf_counterrt   _LOGGERdebugrq   r   sleepBaseExceptionmaxr   )	r   r2   rx   rv   r6   rZ   lock_idlock_filename
start_timer   r   r   acquire  sH   ( 


zBaseFileLock.acquire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)rt   rh   r8   r   r0   r   r   rs   )r   r   r   r   r   r   r   r&   ^  s   zBaseFileLock.releaser   c                 C  s   |    | S )zF
        Acquire the lock.

        :return: the lock object

        )r   r   r   r   r   r   q  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)r   Nr   r   r   r   r   __del__  s   zBaseFileLock.__del__)rB   rb   r   r   r`   )r0   r@   r2   r1   r4   r3   rA   r5   r6   r5   r?   r5   r   r   )r   r5   )r   r/   )r   r1   )rI   rk   r   r   )rI   r5   r   r   )r   r3   )r   r   )Nru   )
r2   rw   rx   r1   rv   rw   r6   ry   r   r   )F)r   r5   r   r   )r   r   r)   )r*   r+   r,   r-   r9   rc   r   rN   propertyr?   r0   r2   setterr6   r4   r   rq   rs   rt   r8   r   r&   r   r(   r   ra   r   r   r^   r   r      sX   
 
'		O


r   )	metaclass)&
__future__r   
contextlibrQ   loggingrf   r   r|   abcr   r   dataclassesr   	threadingr   typingr   r   r	   weakrefr
   _errorr   systypesr   version_infor   typing_extensions	getLoggerr   r   r.   r:   r;   ContextDecoratorr   __all__r   r   r   r   <module>   s>    

=  