U eT.@s ddlmZddlZddlZddlZddlZddlZddlmZm Z ddl m Z ddl m Z ddlmZmZmZddlmZdd lmZerddlZdd lmZejd krdd lmZn dd lmZed ZGdddZe GdddZGdddee Z Gdddeej!Z"ddgZ#dS)) annotationsN)ABCabstractmethod) dataclass)local) TYPE_CHECKINGAnyClassVar)WeakValueDictionary)Timeout) TracebackType) )Selffilelockc@sBeZdZdZdddddZdddd Zd d d dd ddZdS)AcquireReturnProxyzDA context aware object that will release the lock file when exiting. BaseFileLockNone)lockreturncCs ||_dSNr)selfrr;/opt/hc_python/lib/python3.8/site-packages/filelock/_api.py__init__#szAcquireReturnProxy.__init__rcCs|jSrrrrrr __enter__&szAcquireReturnProxy.__enter__type[BaseException] | NoneBaseException | NoneTracebackType | Noneexc_type exc_value tracebackrcCs|jdSr)rreleaserr$r%r&rrr__exit__)szAcquireReturnProxy.__exit__N)__name__ __module__ __qualname____doc__rrr)rrrrr src@sBeZdZUdZded<ded<ded<dZd ed <d Zded <dS) FileLockContextzBA dataclass which holds the context for a ``BaseFileLock`` object.str lock_filefloattimeoutintmodeNz int | None lock_file_fdr lock_counter)r*r+r,r-__annotations__r5r6rrrrr.2s  r.c@seZdZdZdS)ThreadLocalFileContextz8A thread local version of the ``FileLockContext`` class.N)r*r+r,r-rrrrr8Isr8c sneZdZUdZeZded<d@ddd d d d d d ddfddZdAddd d d d d ddddZd dddZ e d dddZ e ddddZ e d dddZ e jd dd!d"dZ eddd#d$Zeddd%d&Ze d dd'd(Ze d dd)d*ZdBd+dd-d.d d.d d/d0d1d2ZdCd dd3d4d5Zddd6d7Zd8d9d:dd;dd?ZZS)Drz+Abstract base class for a file lock object.z0ClassVar[WeakValueDictionary[str, BaseFileLock]] _instancesTF) is_singletonzstr | os.PathLike[str]r1r3boolzdict[str, Any]r)r0r2r4 thread_localr<kwargsrc sB|st|S|jt|}|s>t|}||jt|<|S)zYCreate a new lock object or if specified return the singleton instance for the lock file.)super__new__r9getr/)clsr0r2r4r>r<r?instance __class__rrrARs   zBaseFileLock.__new__r)r0r2r4r>r<rcCs6||_||_t|||d}|r&tntf||_dS)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 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. )r0r2r4N)_is_thread_local _is_singletonosfspathr8r._context)rr0r2r4r>r<r?rrrrgszBaseFileLock.__init__rcCs|jS)z>:return: a flag indicating if this lock is thread local or not)rGrrrris_thread_localszBaseFileLock.is_thread_localcCs|jS)z;:return: a flag indicating if this lock is singleton or not)rHrrrrr<szBaseFileLock.is_singletonr/cCs|jjS)z:return: path to the lock file)rKr0rrrrr0szBaseFileLock.lock_filecCs|jjS)za :return: the default timeout value, in seconds .. versionadded:: 2.0.0 )rKr2rrrrr2szBaseFileLock.timeoutz float | str)valuercCst||j_dS)zd Change the default timeout value. :param value: the new value, in seconds N)r1rKr2)rrMrrrr2scCstdS)zjIf the file lock could be acquired, self._context.lock_file_fd holds the file descriptor of the lock file.NNotImplementedErrorrrrr_acquireszBaseFileLock._acquirecCstdS)z>Releases the lock and sets self._context.lock_file_fd to None.NrNrrrr_releaseszBaseFileLock._releasecCs |jjdk 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)rKr5rrrr is_lockeds zBaseFileLock.is_lockedcCs|jjS)zP:return: The number of times this lock has been acquired (but not yet released).)rKr6rrrrr6szBaseFileLock.lock_counterN皙?)poll_intervallblockingz float | Noner)r2 poll_intervalrTrUrc Cs:|dkr|jj}|dk r0d}tj|tdd|}|jjd7_t|}|j}t }z|j stt d||| |j rt d||q|dkrt d ||t|d |krt |krnnt d ||t|d }t ||||t|qXWn.tk r.td |jjd|j_YnXt|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 z#Attempting to acquire lock %s on %szLock %s acquired on %sFz+Failed to immediately acquire lock %s on %srz"Timeout on acquiring lock %s on %sz2Lock %s not acquired on %s, waiting %s seconds ...r)rKr2warningswarnDeprecationWarningr6idr0time perf_counterrR_LOGGERdebugrPr sleep BaseExceptionmaxr) rr2rVrTrUmsglock_id lock_filename start_timerrracquires<( zBaseFileLock.acquire)forcercCsf|jrb|jjd8_|jjdks&|rbt||j}}td|||d|j_td||dS)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 rz#Attempting to release lock %s on %szLock %s released on %sN)rRrKr6r\r0r_r`rQ)rrirerfrrrr'szBaseFileLock.releasecCs ||S)zE Acquire the lock. :return: the lock object )rhrrrrr#szBaseFileLock.__enter__r r!r"r#cCs |dS)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 Nr'r(rrrr),s zBaseFileLock.__exit__cCs|jdddS)z'Called when the lock object is deleted.T)riNrjrrrr__del__;szBaseFileLock.__del__)r:r;T)r:r;T)NrS)F)r*r+r,r-r r9r7rArrLpropertyr<r0r2setterrrPrQrRr6rhr'rr)rk __classcell__rrrErrMsR "# L r)$ __future__r contextlibloggingrIr]rYabcrr dataclassesr threadingrtypingrrr weakrefr _errorr systypesr version_inforZtyping_extensions getLoggerr_rr.r8ContextDecoratorr__all__rrrrs6         u