Skip to content

Commit 9073aa9

Browse files
committed
refactor!(options): Move handle_options_error -> options
1 parent 1cb86f9 commit 9073aa9

File tree

4 files changed

+48
-38
lines changed

4 files changed

+48
-38
lines changed

src/libtmux/common.py

Lines changed: 0 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -464,42 +464,6 @@ def session_check_name(session_name: str | None) -> None:
464464
raise exc.BadSessionName(reason="contains colons", session_name=session_name)
465465

466466

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

src/libtmux/options.py

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

3333
if t.TYPE_CHECKING:
3434
import sys

src/libtmux/window.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,13 @@
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:
3333
import sys
3434
import types
3535

36+
from .common import PaneDict, WindowOptionDict
3637
from .server import Server
3738
from .session import Session
3839

0 commit comments

Comments
 (0)