Debian Codegraph @

src/filelock/_error.py

raw
1 from __future__ import annotations
2
3
4 class Timeout(TimeoutError): # ruff:ignore[error-suffix-on-exception-name] # public exception name; renaming breaks the API
5 """Raised when the lock could not be acquired in *timeout* seconds."""
6
7 def __init__(self, lock_file: str) -> None:
8 super().__init__()
9 self._lock_file = lock_file
10
11 def __reduce__(self) -> tuple[type[Timeout], tuple[str], dict[str, object]]:
12 # __init__ needs lock_file, so pickle must restore it as a constructor arg
13 return self.__class__, (self._lock_file,), self.__dict__
14
15 def __str__(self) -> str: # pragma: needs hard-link
16 return f"The file lock '{self._lock_file}' could not be acquired."
17
18 def __repr__(self) -> str:
19 return f"{self.__class__.__name__}({self.lock_file!r})"
20
21 @property
22 def lock_file(self) -> str:
23 """The path of the file lock."""
24 return self._lock_file
25
26
27 class SoftFileLockLifetimeWarning(DeprecationWarning):
28 """The configured soft-lock lifetime permits overlapping live holders after expiry."""
29
30
31 class LeaseSettingsMismatch(ValueError): # ruff:ignore[error-suffix-on-exception-name] # public exception name; renaming breaks the API
32 """A lease contender disagrees with the published claim about how long the lease lasts."""
33
34
35 class SoftFileLockProtocolError(OSError):
36 """Raised when strict soft-lock state cannot be interpreted without risking overlap."""
37
38 def __init__(self, lock_file: str, claim_name: str | None, reason: str) -> None:
39 self._lock_file = lock_file
40 self._claim_name = claim_name
41 self._reason = reason
42 super().__init__(self.__str__())
43
44 def __reduce__(
45 self,
46 ) -> tuple[
47 type[SoftFileLockProtocolError], tuple[str, str | None, str], dict[str, object]
48 ]: # pragma: needs hard-link
49 return self.__class__, (self._lock_file, self._claim_name, self._reason), self.__dict__
50
51 def __str__(self) -> str:
52 location = self._lock_file if self._claim_name is None else f"{self._lock_file}: claim {self._claim_name!r}"
53 return f"Invalid strict soft-lock state at {location}: {self._reason}"
54
55 @property
56 def lock_file(self) -> str: # pragma: needs hard-link
57 """The requested lock path."""
58 return self._lock_file
59
60 @property
61 def claim_name(self) -> str | None: # pragma: needs hard-link
62 """The claim that caused the error, if scanning identified one."""
63 return self._claim_name
64
65 @property
66 def reason(self) -> str: # pragma: needs hard-link
67 """The protocol validation failure."""
68 return self._reason
69
70
71 __all__ = [
72 "LeaseSettingsMismatch",
73 "SoftFileLockLifetimeWarning",
74 "SoftFileLockProtocolError",
75 "Timeout",
76 ]