Skip to content

Commit 5225bfe

Browse files
committed
refactor!(options): Move handle_options_error -> options
1 parent 9901b83 commit 5225bfe

File tree

4 files changed

+47
-38
lines changed

4 files changed

+47
-38
lines changed

src/libtmux/common.py

Lines changed: 0 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -460,42 +460,6 @@ def session_check_name(session_name: t.Optional[str]) -> None:
460460
raise exc.BadSessionName(reason="contains colons", session_name=session_name)
461461

462462

463-
def handle_option_error(error: str) -> t.Type[exc.OptionError]:
464-
"""Raise exception if error in option command found.
465-
466-
In tmux 3.0, show-option and show-window-option return invalid option instead of
467-
unknown option. See https://github.com/tmux/tmux/blob/3.0/cmd-show-options.c.
468-
469-
In tmux >2.4, there are 3 different types of option errors:
470-
471-
- unknown option
472-
- invalid option
473-
- ambiguous option
474-
475-
In tmux <2.4, unknown option was the only option.
476-
477-
All errors raised will have the base error of :exc:`exc.OptionError`. So to
478-
catch any option error, use ``except exc.OptionError``.
479-
480-
Parameters
481-
----------
482-
error : str
483-
Error response from subprocess call.
484-
485-
Raises
486-
------
487-
:exc:`exc.OptionError`, :exc:`exc.UnknownOption`, :exc:`exc.InvalidOption`,
488-
:exc:`exc.AmbiguousOption`
489-
"""
490-
if "unknown option" in error:
491-
raise exc.UnknownOption(error)
492-
if "invalid option" in error:
493-
raise exc.InvalidOption(error)
494-
if "ambiguous option" in error:
495-
raise exc.AmbiguousOption(error)
496-
raise exc.OptionError(error) # Raise generic option error
497-
498-
499463
def get_libtmux_version() -> LooseVersion:
500464
"""Return libtmux version is a PEP386 compliant format.
501465

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+
3+
import logging
4+
import typing as t
5+
6+
from . import exc
7+
8+
logger = logging.getLogger(__name__)
9+
10+
11+
def handle_option_error(error: str) -> t.Type[exc.OptionError]:
12+
"""Raise exception if error in option command found.
13+
14+
In tmux 3.0, show-option and show-window-option return invalid option instead of
15+
unknown option. See https://github.com/tmux/tmux/blob/3.0/cmd-show-options.c.
16+
17+
In tmux >2.4, there are 3 different types of option errors:
18+
19+
- unknown option
20+
- invalid option
21+
- ambiguous option
22+
23+
In tmux <2.4, unknown option was the only option.
24+
25+
All errors raised will have the base error of :exc:`exc.OptionError`. So to
26+
catch any option error, use ``except exc.OptionError``.
27+
28+
Parameters
29+
----------
30+
error : str
31+
Error response from subprocess call.
32+
33+
Raises
34+
------
35+
:exc:`exc.OptionError`, :exc:`exc.UnknownOption`, :exc:`exc.InvalidOption`,
36+
:exc:`exc.AmbiguousOption`
37+
"""
38+
if "unknown option" in error:
39+
raise exc.UnknownOption(error)
40+
if "invalid option" in error:
41+
raise exc.InvalidOption(error)
42+
if "ambiguous option" in error:
43+
raise exc.AmbiguousOption(error)
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
@@ -23,11 +23,11 @@
2323
from .common import (
2424
EnvironmentMixin,
2525
WindowDict,
26-
handle_option_error,
2726
has_gte_version,
2827
has_version,
2928
session_check_name,
3029
)
30+
from .options import handle_option_error
3131

3232
if t.TYPE_CHECKING:
3333
from .server import Server

src/libtmux/window.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,10 @@
2727
from libtmux.pane import Pane
2828

2929
from . import exc
30-
from .common import PaneDict, WindowOptionDict, handle_option_error
30+
from .options import handle_option_error
3131

3232
if t.TYPE_CHECKING:
33+
from .common import PaneDict, WindowOptionDict
3334
from .server import Server
3435
from .session import Session
3536

0 commit comments

Comments
 (0)