Debian Codegraph @

src/libtmux/exc.py

raw symbols SCIP
1 """libtmux exceptions.
2
3 libtmux.exc
4 ~~~~~~~~~~~
5
6 """
7
8 from __future__ import annotations
9
10 import typing as t
11
12 from libtmux._internal.query_list import ObjectDoesNotExist
13
14 if t.TYPE_CHECKING:
15 from libtmux.neo import ListExtraArgs
16
17
18 class LibTmuxException(Exception):
19 """Base Exception for libtmux Errors.
20
21 Parameters
22 ----------
23 *args : object
24 Forwarded to :class:`Exception`.
25 subcommand : str, optional
26 The tmux subcommand that produced this error (e.g. ``"last-window"``).
27 When set, :meth:`__str__` formats as ``"<subcommand>: <stderr>"`` so
28 downstream consumers see which tmux command failed.
29
30 .. versionadded:: 0.57
31 """
32
33 def __init__(
34 self,
35 *args: object,
36 subcommand: str | None = None,
37 ) -> None:
38 super().__init__(*args)
39 self.subcommand = subcommand
40
41 def __str__(self) -> str:
42 """Render with optional ``"<subcommand>: …"`` prefix."""
43 base = super().__str__()
44 if self.subcommand is None:
45 return base
46 return f"{self.subcommand}: {base}"
47
48
49 class DeprecatedError(LibTmuxException):
50 """Raised when a deprecated function, method, or parameter is used.
51
52 This exception provides clear guidance on what to use instead.
53
54 Parameters
55 ----------
56 deprecated : str
57 The name of the deprecated API (e.g., "Pane.resize_pane()")
58 replacement : str
59 The recommended replacement API to use instead
60 version : str
61 The version when the API was deprecated (e.g., "0.28.0")
62 """
63
64 def __init__(
65 self,
66 *,
67 deprecated: str,
68 replacement: str,
69 version: str,
70 ) -> None:
71 msg = (
72 f"{deprecated} was deprecated in {version} and has been removed. "
73 f"Use {replacement} instead."
74 )
75 super().__init__(msg)
76
77
78 class TmuxSessionExists(LibTmuxException):
79 """Session does not exist in the server."""
80
81
82 class TmuxCommandNotFound(LibTmuxException):
83 """Application binary for tmux not found."""
84
85
86 class TmuxObjectDoesNotExist(ObjectDoesNotExist):
87 """The query returned multiple objects when only one was expected."""
88
89 def __init__(
90 self,
91 obj_key: str | None = None,
92 obj_id: str | None = None,
93 list_cmd: str | None = None,
94 list_extra_args: ListExtraArgs | None = None,
95 *args: object,
96 ) -> None:
97 if all(arg is not None for arg in [obj_key, obj_id, list_cmd, list_extra_args]):
98 return super().__init__(
99 f"Could not find {obj_key}={obj_id} for {list_cmd} "
100 f"{list_extra_args if list_extra_args is not None else ''}",
101 )
102 return super().__init__("Could not find object")
103
104
105 class VersionTooLow(LibTmuxException):
106 """Raised if tmux below the minimum version to use libtmux."""
107
108
109 class BadSessionName(LibTmuxException):
110 """Disallowed session name for tmux (empty, contains periods or colons)."""
111
112 def __init__(
113 self,
114 reason: str,
115 session_name: str | None = None,
116 *args: object,
117 ) -> None:
118 msg = f"Bad session name: {reason}"
119 if session_name is not None:
120 msg += f" (session name: {session_name})"
121 return super().__init__(msg)
122
123
124 class OptionError(LibTmuxException):
125 """Root error for any error involving invalid, ambiguous or bad options."""
126
127
128 class UnknownOption(OptionError):
129 """Option unknown to tmux show-option(s) or show-window-option(s)."""
130
131
132 class UnknownColorOption(UnknownOption):
133 """Unknown color option."""
134
135 def __init__(self, *args: object) -> None:
136 return super().__init__("Server.colors must equal 88 or 256")
137
138
139 class InvalidOption(OptionError):
140 """Option invalid to tmux."""
141
142
143 class AmbiguousOption(OptionError):
144 """Option that could potentially match more than one."""
145
146
147 class WaitTimeout(LibTmuxException):
148 """Function timed out without meeting condition."""
149
150
151 class VariableUnpackingError(LibTmuxException):
152 """Error unpacking variable."""
153
154 def __init__(self, variable: t.Any | None = None, *args: object) -> None:
155 return super().__init__(f"Unexpected variable: {variable!s}")
156
157
158 class PaneError(LibTmuxException):
159 """Any type of pane related error."""
160
161
162 class PaneNotFound(PaneError):
163 """Pane not found."""
164
165 def __init__(self, pane_id: str | None = None, *args: object) -> None:
166 if pane_id is not None:
167 return super().__init__(f"Pane not found: {pane_id}")
168 return super().__init__("Pane not found")
169
170
171 class WindowError(LibTmuxException):
172 """Any type of window related error."""
173
174
175 class MultipleActiveWindows(WindowError):
176 """Multiple active windows."""
177
178 def __init__(self, count: int, *args: object) -> None:
179 return super().__init__(f"Multiple active windows: {count} found")
180
181
182 class NoActiveWindow(WindowError):
183 """No active window found."""
184
185 def __init__(self, *args: object) -> None:
186 return super().__init__("No active windows found")
187
188
189 class NoWindowsExist(WindowError):
190 """No windows exist for object."""
191
192 def __init__(self, *args: object) -> None:
193 return super().__init__("No windows exist for object")
194
195
196 class AdjustmentDirectionRequiresAdjustment(LibTmuxException, ValueError):
197 """If *adjustment_direction* is set, *adjustment* must be set."""
198
199 def __init__(self) -> None:
200 super().__init__("adjustment_direction requires adjustment")
201
202
203 class WindowAdjustmentDirectionRequiresAdjustment(
204 WindowError,
205 AdjustmentDirectionRequiresAdjustment,
206 ):
207 """ValueError for :meth:`libtmux.Window.resize_window`."""
208
209
210 class PaneAdjustmentDirectionRequiresAdjustment(
211 WindowError,
212 AdjustmentDirectionRequiresAdjustment,
213 ):
214 """ValueError for :meth:`libtmux.Pane.resize_pane`."""
215
216
217 class RequiresDigitOrPercentage(LibTmuxException, ValueError):
218 """Requires digit (int or str digit) or a percentage."""
219
220 def __init__(self) -> None:
221 super().__init__("Requires digit (int or str digit) or a percentage.")