-
Notifications
You must be signed in to change notification settings - Fork 156
/
plug.py
461 lines (373 loc) · 16 KB
/
plug.py
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
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
"""
plug.py - Plugin API.
Copyright (c) EDCD, All Rights Reserved
Licensed under the GNU General Public License.
See LICENSE file.
"""
from __future__ import annotations
import copy
import importlib
import logging
import operator
import os
import sys
import tkinter as tk
from pathlib import Path
from tkinter import ttk
from typing import Any, Mapping, MutableMapping
import companion
import myNotebook as nb # noqa: N813
from config import config
from EDMCLogging import get_main_logger
logger = get_main_logger()
# List of loaded Plugins
PLUGINS = []
PLUGINS_not_py3 = []
PLUGINS_broken = []
# For asynchronous error display
class LastError:
"""Holds the last plugin error."""
msg: str | None
root: tk.Tk
def __init__(self) -> None:
self.msg = None
last_error = LastError()
class Plugin:
"""An EDMC plugin."""
def __init__(self, name: str, loadfile: Path | None, plugin_logger: logging.Logger | None): # noqa: CCR001
"""
Load a single plugin.
:param name: Base name of the file being loaded from.
:param loadfile: Full path/filename of the plugin.
:param plugin_logger: The logging instance for this plugin to use.
:raises Exception: Typically, ImportError or OSError
"""
self.name: str = name # Display name.
self.folder: str | None = name # basename of plugin folder. None for internal plugins.
self.module = None # None for disabled plugins.
self.logger: logging.Logger | None = plugin_logger
if loadfile:
logger.info(f'loading plugin "{name.replace(".", "_")}" from "{loadfile}"')
try:
filename = 'plugin_'
filename += name.encode(encoding='ascii', errors='replace').decode('utf-8').replace('.', '_')
spec = importlib.util.spec_from_file_location(filename, loadfile)
# Replaces older load_module() code. Includes a safety check that the module name is set.
if spec is not None and spec.loader is not None:
module = importlib.util.module_from_spec(spec)
sys.modules[module.__name__] = module
spec.loader.exec_module(module)
if getattr(module, 'plugin_start3', None):
newname = module.plugin_start3(Path(loadfile).resolve().parent)
self.name = str(newname) if newname else self.name
self.module = module
elif getattr(module, 'plugin_start', None):
logger.warning(f'plugin {name} needs migrating\n')
PLUGINS_not_py3.append(self)
else:
logger.error(f'plugin {name} has no plugin_start3() function')
else:
logger.error(f'Failed to load Plugin "{name}" from file "{loadfile}"')
except Exception:
logger.exception(f': Failed for Plugin "{name}"')
raise
else:
logger.info(f'plugin {name} disabled')
def _get_func(self, funcname: str):
"""
Get a function from a plugin.
:param funcname:
:returns: The function, or None if it isn't implemented.
"""
return getattr(self.module, funcname, None)
def get_app(self, parent: tk.Frame) -> tk.Frame | None:
"""
If the plugin provides mainwindow content create and return it.
:param parent: the parent frame for this entry.
:returns: None, a tk Widget, or a pair of tk.Widgets
"""
plugin_app = self._get_func('plugin_app')
if plugin_app:
try:
appitem = plugin_app(parent)
if appitem is None:
return None
if isinstance(appitem, tuple):
if (
len(appitem) != 2
or not isinstance(appitem[0], tk.Widget)
or not isinstance(appitem[1], tk.Widget)
):
raise AssertionError
elif not isinstance(appitem, tk.Widget):
raise AssertionError
return appitem
except Exception:
logger.exception(f'Failed for Plugin "{self.name}"')
return None
def get_prefs(self, parent: ttk.Notebook, cmdr: str | None, is_beta: bool) -> nb.Frame | None:
"""
If the plugin provides a prefs frame, create and return it.
:param parent: the parent frame for this preference tab.
:param cmdr: current Cmdr name (or None). Relevant if you want to have
different settings for different user accounts.
:param is_beta: whether the player is in a Beta universe.
:returns: a myNotebook Frame
"""
plugin_prefs = self._get_func('plugin_prefs')
if plugin_prefs:
try:
frame = plugin_prefs(parent, cmdr, is_beta)
if isinstance(frame, nb.Frame):
return frame
raise AssertionError
except Exception:
logger.exception(f'Failed for Plugin "{self.name}"')
return None
def load_plugins(master: tk.Tk) -> None:
"""Find and load all plugins."""
last_error.root = master
internal = _load_internal_plugins()
PLUGINS.extend(sorted(internal, key=lambda p: operator.attrgetter('name')(p).lower()))
# Add plugin folder to load path so packages can be loaded from plugin folder
sys.path.append(config.plugin_dir)
found = _load_found_plugins()
PLUGINS.extend(sorted(found, key=lambda p: operator.attrgetter('name')(p).lower()))
def _load_internal_plugins():
internal = []
for name in sorted(os.listdir(config.internal_plugin_dir_path)):
if name.endswith('.py') and name[0] not in ('.', '_'):
try:
plugin_name = name[:-3]
plugin_path = config.internal_plugin_dir_path / name
plugin = Plugin(plugin_name, plugin_path, logger)
plugin.folder = None
internal.append(plugin)
except Exception:
logger.exception(f'Failure loading internal Plugin "{name}"')
return internal
def _load_found_plugins():
found = []
# Load any plugins that are also packages first, but note it's *still*
# 100% relying on there being a `load.py`, as only that will be loaded.
# The intent here is to e.g. have EDMC-Overlay load before any plugins
# that depend on it.
plugin_files = sorted(config.plugin_dir_path.iterdir(), key=lambda p: (
not (p / '__init__.py').is_file(), p.name.lower()))
for plugin_file in plugin_files:
name = plugin_file.name
if not (config.plugin_dir_path / name).is_dir() or name.startswith(('.', '_')):
pass
elif name.endswith('.disabled'):
name, discard = name.rsplit('.', 1)
found.append(Plugin(name, None, logger))
else:
try:
# Add plugin's folder to load path in case plugin has internal package dependencies
sys.path.append(str(config.plugin_dir_path / name))
import EDMCLogging
# Create a logger for this 'found' plugin. Must be before the load.py is loaded.
plugin_logger = EDMCLogging.get_plugin_logger(name)
found.append(Plugin(name, config.plugin_dir_path / name / 'load.py', plugin_logger))
except Exception:
PLUGINS_broken.append(Plugin(name, None, logger))
logger.exception(f'Failure loading found Plugin "{name}"')
pass
return found
def provides(fn_name: str) -> list[str]:
"""
Find plugins that provide a function.
:param fn_name:
:returns: list of names of plugins that provide this function
.. versionadded:: 3.0.2
"""
return [p.name for p in PLUGINS if p._get_func(fn_name)]
def invoke(
plugin_name: str, fallback: str | None, fn_name: str, *args: Any
) -> str | None:
"""
Invoke a function on a named plugin.
:param plugin_name: preferred plugin on which to invoke the function
:param fallback: fallback plugin on which to invoke the function, or None
:param fn_name:
:param *args: arguments passed to the function
:returns: return value from the function, or None if the function was not found
.. versionadded:: 3.0.2
"""
for plugin in PLUGINS:
if plugin.name == plugin_name:
plugin_func = plugin._get_func(fn_name)
if plugin_func is not None:
return plugin_func(*args)
for plugin in PLUGINS:
if plugin.name == fallback:
plugin_func = plugin._get_func(fn_name)
assert plugin_func, plugin.name # fallback plugin should provide the function
return plugin_func(*args)
return None
def notify_stop() -> str | None:
"""
Notify each plugin that the program is closing.
If your plugin uses threads then stop and join() them before returning.
.. versionadded:: 2.3.7
"""
error = None
for plugin in PLUGINS:
plugin_stop = plugin._get_func('plugin_stop')
if plugin_stop:
try:
logger.info(f'Asking plugin "{plugin.name}" to stop...')
newerror = plugin_stop()
error = error or newerror
except Exception:
logger.exception(f'Plugin "{plugin.name}" failed')
logger.info('Done')
return error
def _notify_prefs_plugins(fn_name: str, cmdr: str | None, is_beta: bool) -> None:
for plugin in PLUGINS:
prefs_callback = plugin._get_func(fn_name)
if prefs_callback:
try:
prefs_callback(cmdr, is_beta)
except Exception:
logger.exception(f'Plugin "{plugin.name}" failed')
def notify_prefs_cmdr_changed(cmdr: str | None, is_beta: bool) -> None:
"""
Notify plugins that the Cmdr was changed while the settings dialog is open.
Relevant if you want to have different settings for different user accounts.
:param cmdr: current Cmdr name (or None).
:param is_beta: whether the player is in a Beta universe.
"""
_notify_prefs_plugins("prefs_cmdr_changed", cmdr, is_beta)
def notify_prefs_changed(cmdr: str | None, is_beta: bool) -> None:
"""
Notify plugins that the settings dialog has been closed.
The prefs frame and any widgets you created in your `get_prefs()` callback
will be destroyed on return from this function, so take a copy of any
values that you want to save.
:param cmdr: current Cmdr name (or None).
:param is_beta: whether the player is in a Beta universe.
"""
_notify_prefs_plugins("prefs_changed", cmdr, is_beta)
def notify_journal_entry(
cmdr: str, is_beta: bool, system: str | None, station: str | None,
entry: MutableMapping[str, Any],
state: Mapping[str, Any]
) -> str | None:
"""
Send a journal entry to each plugin.
:param cmdr: The Cmdr name, or None if not yet known
:param system: The current system, or None if not yet known
:param station: The current station, or None if not docked or not yet known
:param entry: The journal entry as a dictionary
:param state: A dictionary containing info about the Cmdr, current ship and cargo
:param is_beta: whether the player is in a Beta universe.
:returns: Error message from the first plugin that returns one (if any)
"""
if entry['event'] in 'Location':
logger.trace_if('journal.locations', 'Notifying plugins of "Location" event')
error = None
for plugin in PLUGINS:
journal_entry = plugin._get_func('journal_entry')
if journal_entry:
try:
# Pass a copy of the journal entry in case the callee modifies it
newerror = journal_entry(cmdr, is_beta, system, station, dict(entry), dict(state))
error = error or newerror
except Exception:
logger.exception(f'Plugin "{plugin.name}" failed')
return error
def notify_journal_entry_cqc(
cmdr: str, is_beta: bool,
entry: MutableMapping[str, Any],
state: Mapping[str, Any]
) -> str | None:
"""
Send an in-CQC journal entry to each plugin.
:param cmdr: The Cmdr name, or None if not yet known
:param entry: The journal entry as a dictionary
:param state: A dictionary containing info about the Cmdr, current ship and cargo
:param is_beta: whether the player is in a Beta universe.
:returns: Error message from the first plugin that returns one (if any)
"""
error = None
for plugin in PLUGINS:
cqc_callback = plugin._get_func('journal_entry_cqc')
if cqc_callback is not None and callable(cqc_callback):
try:
# Pass a copy of the journal entry in case the callee modifies it
newerror = cqc_callback(cmdr, is_beta, copy.deepcopy(entry), copy.deepcopy(state))
error = error or newerror
except Exception:
logger.exception(f'Plugin "{plugin.name}" failed while handling CQC mode journal entry')
return error
def notify_dashboard_entry(cmdr: str, is_beta: bool, entry: MutableMapping[str, Any],) -> str | None:
"""
Send a status entry to each plugin.
:param cmdr: The piloting Cmdr name
:param is_beta: whether the player is in a Beta universe.
:param entry: The status entry as a dictionary
:returns: Error message from the first plugin that returns one (if any)
"""
error = None
for plugin in PLUGINS:
status = plugin._get_func('dashboard_entry')
if status:
try:
# Pass a copy of the status entry in case the callee modifies it
newerror = status(cmdr, is_beta, dict(entry))
error = error or newerror
except Exception:
logger.exception(f'Plugin "{plugin.name}" failed')
return error
def notify_capidata(data: companion.CAPIData, is_beta: bool) -> str | None:
"""
Send the latest EDMC data from the FD servers to each plugin.
:param data:
:param is_beta: whether the player is in a Beta universe.
:returns: Error message from the first plugin that returns one (if any)
"""
error = None
for plugin in PLUGINS:
# TODO: Handle it being Legacy data
if data.source_host == companion.SERVER_LEGACY:
cmdr_data = plugin._get_func('cmdr_data_legacy')
else:
cmdr_data = plugin._get_func('cmdr_data')
if cmdr_data:
try:
newerror = cmdr_data(data, is_beta)
error = error or newerror
except Exception:
logger.exception(f'Plugin "{plugin.name}" failed')
return error
def notify_capi_fleetcarrierdata(data: companion.CAPIData) -> str | None:
"""
Send the latest CAPI Fleet Carrier data from the FD servers to each plugin.
:param data: The CAPIData returned in the CAPI response
:returns: Error message from the first plugin that returns one (if any)
"""
error = None
for plugin in PLUGINS:
fc_callback = plugin._get_func('capi_fleetcarrier')
if fc_callback is not None and callable(fc_callback):
try:
# Pass a copy of the CAPIData in case the callee modifies it
newerror = fc_callback(copy.deepcopy(data))
error = error if error else newerror
except Exception:
logger.exception(f'Plugin "{plugin.name}" failed on receiving Fleet Carrier data')
return error
def show_error(err: str) -> None:
"""
Display an error message in the status line of the main window.
Will be NOP during shutdown to avoid Tk hang.
:param err:
.. versionadded:: 2.3.7
"""
if config.shutting_down:
logger.info(f'Called during shutdown: "{str(err)}"')
return
if err and last_error.root:
last_error.msg = str(err)
last_error.root.event_generate('<<PluginError>>', when="tail")