Skip to content

Commit fc97469

Browse files
committed
refactor(plugin): Add typings
1 parent 883bb28 commit fc97469

File tree

1 file changed

+72
-27
lines changed

1 file changed

+72
-27
lines changed

src/tmuxp/plugin.py

+72-27
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727

2828

2929
if t.TYPE_CHECKING:
30-
from typing_extensions import TypedDict
30+
from typing_extensions import NotRequired, TypedDict, Unpack
3131

3232
class VersionConstraints(TypedDict):
3333
version: t.Union[Version, str]
@@ -40,21 +40,65 @@ class TmuxpPluginVersionConstraints(TypedDict):
4040
tmuxp: VersionConstraints
4141
libtmux: VersionConstraints
4242

43+
class ConfigSchema(TypedDict):
44+
plugin_name: NotRequired[str]
45+
tmux_min_version: NotRequired[str]
46+
tmux_max_version: NotRequired[str]
47+
tmux_version_incompatible: NotRequired[t.List[str]]
48+
libtmux_min_version: NotRequired[str]
49+
libtmux_max_version: NotRequired[str]
50+
libtmux_version_incompatible: NotRequired[t.List[str]]
51+
tmuxp_min_version: NotRequired[str]
52+
tmuxp_max_version: NotRequired[str]
53+
tmuxp_version_incompatible: NotRequired[t.List[str]]
54+
55+
56+
class Config(t.TypedDict):
57+
plugin_name: str
58+
tmux_min_version: str
59+
tmux_max_version: t.Optional[str]
60+
tmux_version_incompatible: t.Optional[t.List[str]]
61+
libtmux_min_version: str
62+
libtmux_max_version: t.Optional[str]
63+
libtmux_version_incompatible: t.Optional[t.List[str]]
64+
tmuxp_min_version: str
65+
tmuxp_max_version: t.Optional[str]
66+
tmuxp_version_incompatible: t.Optional[t.List[str]]
67+
68+
69+
DEFAULT_CONFIG: "Config" = {
70+
"plugin_name": "tmuxp-plugin",
71+
"tmux_min_version": TMUX_MIN_VERSION,
72+
"tmux_max_version": TMUX_MAX_VERSION,
73+
"tmux_version_incompatible": None,
74+
"libtmux_min_version": LIBTMUX_MIN_VERSION,
75+
"libtmux_max_version": LIBTMUX_MAX_VERSION,
76+
"libtmux_version_incompatible": None,
77+
"tmuxp_min_version": TMUXP_MIN_VERSION,
78+
"tmuxp_max_version": TMUXP_MAX_VERSION,
79+
"tmuxp_version_incompatible": None,
80+
}
81+
82+
83+
def validate_plugin_config(config: "ConfigSchema") -> t.TypeGuard["Config"]:
84+
return isinstance(config, dict)
85+
86+
87+
def setup_plugin_config(
88+
config: "ConfigSchema", default_config: "Config" = DEFAULT_CONFIG
89+
) -> "Config":
90+
new_config = config.copy()
91+
for default_key, default_value in default_config.items():
92+
if default_key not in new_config:
93+
new_config[default_key] = default_value
94+
95+
assert validate_plugin_config(new_config)
96+
97+
return new_config
98+
4399

44100
class TmuxpPlugin:
45-
def __init__(
46-
self,
47-
plugin_name: str = "tmuxp-plugin",
48-
tmux_min_version: str = TMUX_MIN_VERSION,
49-
tmux_max_version: t.Optional[str] = TMUX_MAX_VERSION,
50-
tmux_version_incompatible: t.Optional[t.List[str]] = None,
51-
libtmux_min_version: str = LIBTMUX_MIN_VERSION,
52-
libtmux_max_version: t.Optional[str] = LIBTMUX_MAX_VERSION,
53-
libtmux_version_incompatible: t.Optional[t.List[str]] = None,
54-
tmuxp_min_version: str = TMUXP_MIN_VERSION,
55-
tmuxp_max_version: t.Optional[str] = TMUXP_MAX_VERSION,
56-
tmuxp_version_incompatible: t.Optional[t.List[str]] = None,
57-
) -> None:
101+
def __init__(self, **kwargs: "Unpack[ConfigSchema]") -> None:
58102
"""
59103
Initialize plugin.
60104
@@ -95,7 +139,8 @@ def __init__(
95139
Versions of tmuxp that are incompatible with the plugin
96140
97141
"""
98-
self.plugin_name = plugin_name
142+
config = setup_plugin_config(config=kwargs)
143+
self.plugin_name = config["plugin_name"]
99144

100145
# Dependency versions
101146
self.tmux_version = get_version()
@@ -105,26 +150,26 @@ def __init__(
105150
self.version_constraints: "TmuxpPluginVersionConstraints" = {
106151
"tmux": {
107152
"version": self.tmux_version,
108-
"vmin": tmux_min_version,
109-
"vmax": tmux_max_version,
110-
"incompatible": tmux_version_incompatible
111-
if tmux_version_incompatible
153+
"vmin": config["tmux_min_version"],
154+
"vmax": config["tmux_max_version"],
155+
"incompatible": config["tmux_version_incompatible"]
156+
if config["tmux_version_incompatible"]
112157
else [],
113158
},
114159
"libtmux": {
115160
"version": self.libtmux_version,
116-
"vmin": libtmux_min_version,
117-
"vmax": libtmux_max_version,
118-
"incompatible": libtmux_version_incompatible
119-
if libtmux_version_incompatible
161+
"vmin": config["libtmux_min_version"],
162+
"vmax": config["libtmux_max_version"],
163+
"incompatible": config["libtmux_version_incompatible"]
164+
if config["libtmux_version_incompatible"]
120165
else [],
121166
},
122167
"tmuxp": {
123168
"version": self.tmuxp_version,
124-
"vmin": tmuxp_min_version,
125-
"vmax": tmuxp_max_version,
126-
"incompatible": tmuxp_version_incompatible
127-
if tmuxp_version_incompatible
169+
"vmin": config["tmuxp_min_version"],
170+
"vmax": config["tmuxp_max_version"],
171+
"incompatible": config["tmuxp_version_incompatible"]
172+
if config["tmuxp_version_incompatible"]
128173
else [],
129174
},
130175
}

0 commit comments

Comments
 (0)