Skip to content

Commit 74145bf

Browse files
committed
refactor(plugin): Add typings
1 parent 74a01fc commit 74145bf

File tree

1 file changed

+69
-32
lines changed

1 file changed

+69
-32
lines changed

src/tmuxp/plugin.py

Lines changed: 69 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,11 @@
2727

2828

2929
if t.TYPE_CHECKING:
30-
from typing_extensions import TypedDict
30+
from libtmux.session import Session
31+
from libtmux.window import Window
32+
from typing_extensions import TypedDict, Unpack
33+
34+
from ._types import PluginConfigSchema
3135

3236
class VersionConstraints(TypedDict):
3337
version: t.Union[Version, str]
@@ -41,20 +45,52 @@ class TmuxpPluginVersionConstraints(TypedDict):
4145
libtmux: VersionConstraints
4246

4347

48+
class Config(t.TypedDict):
49+
plugin_name: str
50+
tmux_min_version: str
51+
tmux_max_version: t.Optional[str]
52+
tmux_version_incompatible: t.Optional[t.List[str]]
53+
libtmux_min_version: str
54+
libtmux_max_version: t.Optional[str]
55+
libtmux_version_incompatible: t.Optional[t.List[str]]
56+
tmuxp_min_version: str
57+
tmuxp_max_version: t.Optional[str]
58+
tmuxp_version_incompatible: t.Optional[t.List[str]]
59+
60+
61+
DEFAULT_CONFIG: "Config" = {
62+
"plugin_name": "tmuxp-plugin",
63+
"tmux_min_version": TMUX_MIN_VERSION,
64+
"tmux_max_version": TMUX_MAX_VERSION,
65+
"tmux_version_incompatible": None,
66+
"libtmux_min_version": LIBTMUX_MIN_VERSION,
67+
"libtmux_max_version": LIBTMUX_MAX_VERSION,
68+
"libtmux_version_incompatible": None,
69+
"tmuxp_min_version": TMUXP_MIN_VERSION,
70+
"tmuxp_max_version": TMUXP_MAX_VERSION,
71+
"tmuxp_version_incompatible": None,
72+
}
73+
74+
75+
def validate_plugin_config(config: "PluginConfigSchema") -> t.TypeGuard["Config"]:
76+
return isinstance(config, dict)
77+
78+
79+
def setup_plugin_config(
80+
config: "PluginConfigSchema", default_config: "Config" = DEFAULT_CONFIG
81+
) -> "Config":
82+
new_config = config.copy()
83+
for default_key, default_value in default_config.items():
84+
if default_key not in new_config:
85+
new_config[default_key] = default_value
86+
87+
assert validate_plugin_config(new_config)
88+
89+
return new_config
90+
91+
4492
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:
93+
def __init__(self, **kwargs: "Unpack[PluginConfigSchema]") -> None:
5894
"""
5995
Initialize plugin.
6096
@@ -95,7 +131,8 @@ def __init__(
95131
Versions of tmuxp that are incompatible with the plugin
96132
97133
"""
98-
self.plugin_name = plugin_name
134+
config = setup_plugin_config(config=kwargs)
135+
self.plugin_name = config["plugin_name"]
99136

100137
# Dependency versions
101138
self.tmux_version = get_version()
@@ -105,26 +142,26 @@ def __init__(
105142
self.version_constraints: "TmuxpPluginVersionConstraints" = {
106143
"tmux": {
107144
"version": self.tmux_version,
108-
"vmin": tmux_min_version,
109-
"vmax": tmux_max_version,
110-
"incompatible": tmux_version_incompatible
111-
if tmux_version_incompatible
145+
"vmin": config["tmux_min_version"],
146+
"vmax": config["tmux_max_version"],
147+
"incompatible": config["tmux_version_incompatible"]
148+
if config["tmux_version_incompatible"]
112149
else [],
113150
},
114151
"libtmux": {
115152
"version": self.libtmux_version,
116-
"vmin": libtmux_min_version,
117-
"vmax": libtmux_max_version,
118-
"incompatible": libtmux_version_incompatible
119-
if libtmux_version_incompatible
153+
"vmin": config["libtmux_min_version"],
154+
"vmax": config["libtmux_max_version"],
155+
"incompatible": config["libtmux_version_incompatible"]
156+
if config["libtmux_version_incompatible"]
120157
else [],
121158
},
122159
"tmuxp": {
123160
"version": self.tmuxp_version,
124-
"vmin": tmuxp_min_version,
125-
"vmax": tmuxp_max_version,
126-
"incompatible": tmuxp_version_incompatible
127-
if tmuxp_version_incompatible
161+
"vmin": config["tmuxp_min_version"],
162+
"vmax": config["tmuxp_max_version"],
163+
"incompatible": config["tmuxp_version_incompatible"]
164+
if config["tmuxp_version_incompatible"]
128165
else [],
129166
},
130167
}
@@ -167,7 +204,7 @@ def _pass_version_check(
167204

168205
return True
169206

170-
def before_workspace_builder(self, session):
207+
def before_workspace_builder(self, session: "Session"):
171208
"""
172209
Provide a session hook previous to creating the workspace.
173210
@@ -180,7 +217,7 @@ def before_workspace_builder(self, session):
180217
session to hook into
181218
"""
182219

183-
def on_window_create(self, window):
220+
def on_window_create(self, window: "Window"):
184221
"""
185222
Provide a window hook previous to doing anything with a window.
186223
@@ -192,7 +229,7 @@ def on_window_create(self, window):
192229
window to hook into
193230
"""
194231

195-
def after_window_finished(self, window):
232+
def after_window_finished(self, window: "Window"):
196233
"""
197234
Provide a window hook after creating the window.
198235
@@ -206,7 +243,7 @@ def after_window_finished(self, window):
206243
window to hook into
207244
"""
208245

209-
def before_script(self, session):
246+
def before_script(self, session: "Session"):
210247
"""
211248
Provide a session hook after the workspace has been built.
212249
@@ -234,7 +271,7 @@ def before_script(self, session):
234271
session to hook into
235272
"""
236273

237-
def reattach(self, session):
274+
def reattach(self, session: "Session"):
238275
"""
239276
Provide a session hook before reattaching to the session.
240277

0 commit comments

Comments
 (0)