Skip to content

Commit 2e5d723

Browse files
committed
refactor!(options): Move handle_options_error -> options
1 parent b53c82e commit 2e5d723

File tree

4 files changed

+47
-39
lines changed

4 files changed

+47
-39
lines changed

src/libtmux/common.py

Lines changed: 0 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -446,43 +446,6 @@ def session_check_name(session_name: t.Optional[str]) -> None:
446446
raise exc.BadSessionName(reason="contains colons", session_name=session_name)
447447

448448

449-
def handle_option_error(error: str) -> t.Type[exc.OptionError]:
450-
"""Raise exception if error in option command found.
451-
452-
In tmux 3.0, show-option and show-window-option return invalid option instead of
453-
unknown option. See https://github.com/tmux/tmux/blob/3.0/cmd-show-options.c.
454-
455-
In tmux >2.4, there are 3 different types of option errors:
456-
457-
- unknown option
458-
- invalid option
459-
- ambiguous option
460-
461-
In tmux <2.4, unknown option was the only option.
462-
463-
All errors raised will have the base error of :exc:`exc.OptionError`. So to
464-
catch any option error, use ``except exc.OptionError``.
465-
466-
Parameters
467-
----------
468-
error : str
469-
Error response from subprocess call.
470-
471-
Raises
472-
------
473-
:exc:`exc.OptionError`, :exc:`exc.UnknownOption`, :exc:`exc.InvalidOption`,
474-
:exc:`exc.AmbiguousOption`
475-
"""
476-
if "unknown option" in error:
477-
raise exc.UnknownOption(error)
478-
elif "invalid option" in error:
479-
raise exc.InvalidOption(error)
480-
elif "ambiguous option" in error:
481-
raise exc.AmbiguousOption(error)
482-
else:
483-
raise exc.OptionError(error) # Raise generic option error
484-
485-
486449
def get_libtmux_version() -> LooseVersion:
487450
"""Return libtmux version is a PEP386 compliant format.
488451

src/libtmux/options.py

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
"""Helpers for tmux options."""
2+
import logging
3+
import typing as t
4+
5+
from . import exc
6+
7+
logger = logging.getLogger(__name__)
8+
9+
10+
def handle_option_error(error: str) -> t.Type[exc.OptionError]:
11+
"""Raise exception if error in option command found.
12+
13+
In tmux 3.0, show-option and show-window-option return invalid option instead of
14+
unknown option. See https://github.com/tmux/tmux/blob/3.0/cmd-show-options.c.
15+
16+
In tmux >2.4, there are 3 different types of option errors:
17+
18+
- unknown option
19+
- invalid option
20+
- ambiguous option
21+
22+
In tmux <2.4, unknown option was the only option.
23+
24+
All errors raised will have the base error of :exc:`exc.OptionError`. So to
25+
catch any option error, use ``except exc.OptionError``.
26+
27+
Parameters
28+
----------
29+
error : str
30+
Error response from subprocess call.
31+
32+
Raises
33+
------
34+
:exc:`exc.OptionError`, :exc:`exc.UnknownOption`, :exc:`exc.InvalidOption`,
35+
:exc:`exc.AmbiguousOption`
36+
"""
37+
if "unknown option" in error:
38+
raise exc.UnknownOption(error)
39+
elif "invalid option" in error:
40+
raise exc.InvalidOption(error)
41+
elif "ambiguous option" in error:
42+
raise exc.AmbiguousOption(error)
43+
else:
44+
raise exc.OptionError(error) # Raise generic option error

src/libtmux/session.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,11 @@
2121
from .common import (
2222
EnvironmentMixin,
2323
WindowDict,
24-
handle_option_error,
2524
has_gte_version,
2625
has_version,
2726
session_check_name,
2827
)
28+
from .options import handle_option_error
2929

3030
if t.TYPE_CHECKING:
3131
from .server import Server

src/libtmux/window.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,9 @@
1818
from libtmux.pane import Pane
1919

2020
from . import exc
21-
from .common import PaneDict, WindowOptionDict, handle_option_error
21+
from .common import PaneDict, WindowOptionDict
2222
from .formats import FORMAT_SEPARATOR
23+
from .options import handle_option_error
2324

2425
if t.TYPE_CHECKING:
2526
from .server import Server

0 commit comments

Comments
 (0)