-
Notifications
You must be signed in to change notification settings - Fork 236
Expand file tree
/
Copy pathplugin.py
More file actions
222 lines (177 loc) · 6.99 KB
/
plugin.py
File metadata and controls
222 lines (177 loc) · 6.99 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
from distutils.version import LooseVersion
import libtmux
from libtmux.common import get_version
from .__about__ import __version__
from .exc import TmuxpPluginException
#: Minimum version of tmux required to run libtmux
TMUX_MIN_VERSION = "1.8"
#: 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
class TmuxpPlugin:
def __init__(
self,
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,
):
"""
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
"""
self.plugin_name = plugin_name
# Dependency versions
self.tmux_version = get_version()
self.libtmux_version = libtmux.__version__
self.tmuxp_version = LooseVersion(__version__)
self.version_constraints = {
"tmux": {
"version": self.tmux_version,
"vmin": tmux_min_version,
"vmax": tmux_max_version,
"incompatible": tmux_version_incompatible
if tmux_version_incompatible
else [],
},
"libtmux": {
"version": self.libtmux_version,
"vmin": libtmux_min_version,
"vmax": libtmux_max_version,
"incompatible": libtmux_version_incompatible
if libtmux_version_incompatible
else [],
},
"tmuxp": {
"version": self.tmuxp_version,
"vmin": tmuxp_min_version,
"vmax": tmuxp_max_version,
"incompatible": tmuxp_version_incompatible
if tmuxp_version_incompatible
else [],
},
}
self._version_check()
def _version_check(self):
"""
Check all dependency versions for compatibility.
"""
for dep, constraints in self.version_constraints.items():
try:
assert self._pass_version_check(**constraints)
except AssertionError:
raise TmuxpPluginException(
"Incompatible {dep} version: {version}\n{plugin_name} "
"requirements:\nmin: {vmin} | max: {vmax} | "
"incompatible: {incompatible}\n".format(
dep=dep, plugin_name=self.plugin_name, **constraints
)
)
def _pass_version_check(self, version, vmin, vmax, incompatible):
"""
Provide affirmative if version compatibility is correct.
"""
if vmin and version < LooseVersion(vmin):
return False
if vmax and version > LooseVersion(vmax):
return False
if version in incompatible:
return False
return True
def before_workspace_builder(self, session):
"""
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):
"""
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):
"""
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):
"""
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 ``before_workspace_builder`` instead.
Parameters
----------
session : :class:`libtmux.Session`
session to hook into
"""
def reattach(self, session):
"""
Provide a session hook before reattaching to the session.
Parameters
----------
session : :class:`libtmux.Session`
session to hook into
"""