@@ -41,36 +41,32 @@ def __exit__(
41
41
class FileLockContext :
42
42
"""
43
43
A dataclass which holds the context for a ``BaseFileLock`` object.
44
-
45
- The context is held in a separate class to allow optional use of thread local storage
46
- via the ``ThreadLocalFileContext`` class.
47
44
"""
48
45
49
- # The path to the lock file.
46
+ # The context is held in a separate class to allow optional use of thread local storage via the
47
+ # ThreadLocalFileContext class.
48
+
49
+ #: The path to the lock file.
50
50
lock_file : str
51
51
52
- # The default timeout value.
52
+ #: The default timeout value.
53
53
timeout : float
54
54
55
- # The mode for the lock files
55
+ #: The mode for the lock files
56
56
mode : int
57
57
58
- # The file descriptor for the *_lock_file* as it is returned by the os.open() function.
59
- # This file lock is only NOT None, if the object currently holds the lock.
58
+ #: The file descriptor for the *_lock_file* as it is returned by the os.open() function, not None when lock held
60
59
lock_file_fd : int | None = None
61
60
62
- # The lock counter is used for implementing the nested locking mechanism. Whenever the lock is acquired, the
63
- # counter is increased and the lock is only released, when this value is 0 again.
64
- lock_counter : int = 0
61
+ #: The lock counter is used for implementing the nested locking mechanism.
62
+ lock_counter : int = 0 # When the lock is acquired is increased and the lock is only released, when this value is 0
65
63
66
64
67
65
class ThreadLocalFileContext (FileLockContext , local ):
68
66
"""
69
67
A thread local version of the ``FileLockContext`` class.
70
68
"""
71
69
72
- pass
73
-
74
70
75
71
class BaseFileLock (ABC , contextlib .ContextDecorator ):
76
72
"""Abstract base class for a file lock object."""
@@ -91,23 +87,22 @@ def __init__(
91
87
to a negative value. A timeout of 0 means, that there is exactly one attempt to acquire the file lock.
92
88
:param mode: file permissions for the lockfile.
93
89
:param thread_local: Whether this object's internal context should be thread local or not.
94
- If this is set to ``False`` then the lock will be rentrant across threads.
90
+ If this is set to ``False`` then the lock will be reentrant across threads.
95
91
"""
96
- # Whether or not this object is thread local or not
97
- self .thread_local = thread_local
92
+ self ._is_thread_local = thread_local
98
93
99
- # Create the context. Note that external code should not work with the context directly
100
- # and should instead use properties of this class.
101
- context_kwargs : dict [str , Any ] = {
94
+ # Create the context. Note that external code should not work with the context directly and should instead use
95
+ # properties of this class.
96
+ kwargs : dict [str , Any ] = {
102
97
"lock_file" : os .fspath (lock_file ),
103
98
"timeout" : timeout ,
104
99
"mode" : mode ,
105
100
}
106
- self ._context : FileLockContext | ThreadLocalFileContext
107
- if thread_local :
108
- self . _context = ThreadLocalFileContext ( ** context_kwargs )
109
- else :
110
- self ._context = FileLockContext ( ** context_kwargs )
101
+ self ._context : FileLockContext = ( ThreadLocalFileContext if thread_local else FileLockContext )( ** kwargs )
102
+
103
+ def is_thread_local ( self ) -> bool :
104
+ """:return: a flag indicating if this lock is thread local or not"""
105
+ return self ._is_thread_local
111
106
112
107
@property
113
108
def lock_file (self ) -> str :
@@ -177,7 +172,7 @@ def acquire(
177
172
:param poll_interval: interval of trying to acquire the lock file
178
173
:param poll_intervall: deprecated, kept for backwards compatibility, use ``poll_interval`` instead
179
174
:param blocking: defaults to True. If False, function will return immediately if it cannot obtain a lock on the
180
- first attempt. Otherwise this method will block until the timeout expires or the lock is acquired.
175
+ first attempt. Otherwise, this method will block until the timeout expires or the lock is acquired.
181
176
:raises Timeout: if fails to acquire lock within the timeout period
182
177
:return: a context object that will unlock the file when the context is exited
183
178
@@ -253,7 +248,7 @@ def release(self, force: bool = False) -> None:
253
248
254
249
_LOGGER .debug ("Attempting to release lock %s on %s" , lock_id , lock_filename )
255
250
self ._release ()
256
- self ._lock_counter = 0
251
+ self ._context . lock_counter = 0
257
252
_LOGGER .debug ("Lock %s released on %s" , lock_id , lock_filename )
258
253
259
254
def __enter__ (self ) -> BaseFileLock :
0 commit comments