-
Notifications
You must be signed in to change notification settings - Fork 244
Expand file tree
/
Copy pathplugin.py
More file actions
360 lines (287 loc) · 11.9 KB
/
Copy pathplugin.py
File metadata and controls
360 lines (287 loc) · 11.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
"""Plugin system for tmuxp."""
from __future__ import annotations
import logging
import typing as t
import libtmux
from libtmux._compat import LegacyVersion as Version
from libtmux.common import get_version
from .__about__ import __version__
from .exc import TmuxpPluginException
logger = logging.getLogger(__name__)
#: Minimum version of tmux required to run tmuxp
TMUX_MIN_VERSION = "3.2"
#: Most recent version of tmux supported
TMUX_MAX_VERSION = None
#: Minimum version of libtmux required to run libtmux
LIBTMUX_MIN_VERSION = "0.8.3"
#: Most recent version of libtmux supported
LIBTMUX_MAX_VERSION = None
#: Minimum version of tmuxp required to use plugins
TMUXP_MIN_VERSION = "1.6.0"
#: Most recent version of tmuxp
TMUXP_MAX_VERSION = None
if t.TYPE_CHECKING:
from typing import TypeGuard
from libtmux.session import Session
from libtmux.window import Window
from typing_extensions import TypedDict, Unpack
from ._internal.types import PluginConfigSchema
class VersionConstraints(TypedDict):
"""Version constraints mapping for a tmuxp plugin.
Unpacked into :meth:`TmuxpPlugin._pass_version_check` to decide whether
one dependency satisfies the plugin's requirements.
Attributes
----------
version : Version | str
Installed version of the dependency being checked.
vmin : str
Oldest version accepted. An empty string skips the lower bound.
vmax : str | None
Newest version accepted. ``None`` leaves the upper bound open.
incompatible : list[t.Any | str]
Versions rejected outright, even when inside the ``vmin`` /
``vmax`` range.
"""
version: Version | str
vmin: str
vmax: str | None
incompatible: list[t.Any | str]
class TmuxpPluginVersionConstraints(TypedDict):
"""Version constraints for a tmuxp plugin.
Held on :attr:`TmuxpPlugin.version_constraints` and walked by
:meth:`TmuxpPlugin._version_check`, one entry per dependency.
Attributes
----------
tmux : VersionConstraints
Bounds checked against the running tmux binary.
tmuxp : VersionConstraints
Bounds checked against the installed tmuxp.
libtmux : VersionConstraints
Bounds checked against the installed libtmux.
"""
tmux: VersionConstraints
tmuxp: VersionConstraints
libtmux: VersionConstraints
class Config(t.TypedDict):
"""tmuxp plugin configuration mapping.
The fully populated form of the plugin keyword arguments: every key is
present once :func:`setup_plugin_config` has merged the caller's arguments
over ``DEFAULT_CONFIG``.
Attributes
----------
plugin_name : str
Name of the plugin, used to identify it in version incompatibility
messages.
tmux_min_version : str
Oldest tmux version the plugin supports.
tmux_max_version : str | None
Newest tmux version the plugin supports. ``None`` leaves the upper
bound open.
tmux_version_incompatible : list[str] | None
Individual tmux versions the plugin rejects even though they fall
inside the min / max range. ``None`` means no such exclusions.
libtmux_min_version : str
Oldest libtmux version the plugin supports.
libtmux_max_version : str | None
Newest libtmux version the plugin supports. ``None`` leaves the upper
bound open.
libtmux_version_incompatible : list[str] | None
Individual libtmux versions the plugin rejects even though they fall
inside the min / max range. ``None`` means no such exclusions.
tmuxp_min_version : str
Oldest tmuxp version the plugin supports.
tmuxp_max_version : str | None
Newest tmuxp version the plugin supports. ``None`` leaves the upper
bound open.
tmuxp_version_incompatible : list[str] | None
Individual tmuxp versions the plugin rejects even though they fall
inside the min / max range. ``None`` means no such exclusions.
"""
plugin_name: str
tmux_min_version: str
tmux_max_version: str | None
tmux_version_incompatible: list[str] | None
libtmux_min_version: str
libtmux_max_version: str | None
libtmux_version_incompatible: list[str] | None
tmuxp_min_version: str
tmuxp_max_version: str | None
tmuxp_version_incompatible: list[str] | None
DEFAULT_CONFIG: Config = {
"plugin_name": "tmuxp-plugin",
"tmux_min_version": TMUX_MIN_VERSION,
"tmux_max_version": TMUX_MAX_VERSION,
"tmux_version_incompatible": None,
"libtmux_min_version": LIBTMUX_MIN_VERSION,
"libtmux_max_version": LIBTMUX_MAX_VERSION,
"libtmux_version_incompatible": None,
"tmuxp_min_version": TMUXP_MIN_VERSION,
"tmuxp_max_version": TMUXP_MAX_VERSION,
"tmuxp_version_incompatible": None,
}
def validate_plugin_config(config: PluginConfigSchema) -> TypeGuard[Config]:
"""Return True if tmuxp plugin configuration valid, also upcasts."""
return isinstance(config, dict)
def setup_plugin_config(
config: PluginConfigSchema,
default_config: Config = DEFAULT_CONFIG,
) -> Config:
"""Initialize tmuxp plugin configuration."""
new_config = config.copy()
for default_key, default_value in default_config.items():
if default_key not in new_config:
new_config[default_key] = default_value # type:ignore
assert validate_plugin_config(new_config)
return new_config
class TmuxpPlugin:
"""Base class for a tmuxp plugin."""
def __init__(self, **kwargs: Unpack[PluginConfigSchema]) -> None:
"""
Initialize plugin.
The default version values are set to the versions that the plugin
system requires.
Parameters
----------
plugin_name : str
Name of the child plugin. Used in error message plugin fails to
load
tmux_min_version : str
Min version of tmux that the plugin supports
tmux_max_version : str
Min version of tmux that the plugin supports
tmux_version_incompatible : list
Versions of tmux that are incompatible with the plugin
libtmux_min_version : str
Min version of libtmux that the plugin supports
libtmux_max_version : str
Max version of libtmux that the plugin supports
libtmux_version_incompatible : list
Versions of libtmux that are incompatible with the plugin
tmuxp_min_version : str
Min version of tmuxp that the plugin supports
tmuxp_max_version : str
Max version of tmuxp that the plugin supports
tmuxp_version_incompatible : list
Versions of tmuxp that are incompatible with the plugin
"""
config = setup_plugin_config(config=kwargs)
self.plugin_name = config["plugin_name"]
# Dependency versions
self.tmux_version = get_version()
self.libtmux_version = libtmux.__about__.__version__
self.tmuxp_version = Version(__version__)
self.version_constraints: TmuxpPluginVersionConstraints = {
"tmux": {
"version": self.tmux_version,
"vmin": config["tmux_min_version"],
"vmax": config["tmux_max_version"],
"incompatible": config["tmux_version_incompatible"] or [],
},
"libtmux": {
"version": self.libtmux_version,
"vmin": config["libtmux_min_version"],
"vmax": config["libtmux_max_version"],
"incompatible": config["libtmux_version_incompatible"] or [],
},
"tmuxp": {
"version": self.tmuxp_version,
"vmin": config["tmuxp_min_version"],
"vmax": config["tmuxp_max_version"],
"incompatible": config["tmuxp_version_incompatible"] or [],
},
}
self._version_check()
def _version_check(self) -> None:
"""Check all dependency versions for compatibility."""
logger.debug("checking version constraints for %s", self.plugin_name)
for dep, constraints in self.version_constraints.items():
assert isinstance(constraints, dict)
try:
assert self._pass_version_check(**constraints)
except AssertionError as e:
msg = (
"Incompatible {dep} version: {version}\n{plugin_name} "
"requirements:\nmin: {vmin} | max: {vmax} | "
"incompatible: {incompatible}\n".format(
dep=dep,
plugin_name=self.plugin_name,
**constraints,
)
)
raise TmuxpPluginException(
msg,
) from e
def _pass_version_check(
self,
version: str | Version,
vmin: str,
vmax: str | None,
incompatible: list[t.Any | str],
) -> bool:
"""Provide affirmative if version compatibility is correct."""
if vmin and version < Version(vmin):
return False
if vmax and version > Version(vmax):
return False
return version not in incompatible
def before_workspace_builder(self, session: Session) -> None:
"""
Provide a session hook previous to creating the workspace.
This runs after the session has been created but before any of
the windows/panes/commands are entered.
Parameters
----------
session : :class:`libtmux.Session`
session to hook into
"""
def on_window_create(self, window: Window) -> None:
"""
Provide a window hook previous to doing anything with a window.
This runs runs before anything is created in the windows, like panes.
Parameters
----------
window: :class:`libtmux.Window`
window to hook into
"""
def after_window_finished(self, window: Window) -> None:
"""
Provide a window hook after creating the window.
This runs after everything has been created in the window, including
the panes and all of the commands for the panes. It also runs after the
``options_after`` has been applied to the window.
Parameters
----------
window: :class:`libtmux.Window`
window to hook into
"""
def before_script(self, session: Session) -> None:
"""
Provide a session hook after the workspace has been built.
This runs after the workspace has been loaded with ``tmuxp load``. It
augments instead of replaces the ``before_script`` section of the
workspace data.
This hook provides access to the LibTmux.session object for any
behavior that would be used in the ``before_script`` section of the
workspace file that needs access directly to the session object.
This runs after the workspace has been loaded with ``tmuxp load``.
The hook augments, rather than replaces, the ``before_script`` section
of the workspace. While it is possible to do all of the
``before_script`` workspace in this function, if a shell script
is currently being used for the workspace, it would be cleaner to
continue using the script in the ``before_section``.
If changes to the session need to be made prior to anything being
built, please use
:meth:`~tmuxp.plugin.TmuxpPlugin.before_workspace_builder` instead.
Parameters
----------
session : :class:`libtmux.Session`
session to hook into
"""
def reattach(self, session: Session) -> None:
"""
Provide a session hook before reattaching to the session.
Parameters
----------
session : :class:`libtmux.Session`
session to hook into
"""