forked from EDCD/EDMarketConnector
-
Notifications
You must be signed in to change notification settings - Fork 0
/
plug.py
185 lines (161 loc) · 5.55 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
"""
Plugin hooks for EDMC - Ian Norton
"""
import os
import imp
import sys
if __debug__:
from traceback import print_exc
from config import config
"""
Dictionary of loaded plugin modules.
"""
PLUGINS = dict()
def find_plugins():
"""
Look for plugin entry points.
:return:
"""
found = dict()
disabled = list()
plug_folders = os.listdir(config.plugin_dir)
for name in plug_folders:
if name.endswith(".disabled"):
name, discard = name.rsplit(".", 1)
disabled.append(name)
continue
loadfile = os.path.join(config.plugin_dir, name, "load.py")
if os.path.isfile(loadfile):
found[name] = loadfile
return found, disabled
def load_plugins():
"""
Load all found plugins
:return:
"""
found, disabled = find_plugins()
imp.acquire_lock()
for plugname in disabled:
sys.stdout.write("plugin {} disabled\n".format(plugname))
for plugname in found:
try:
sys.stdout.write("loading plugin {}\n".format(plugname))
with open(found[plugname], "rb") as plugfile:
plugmod = imp.load_module(plugname, plugfile, found[plugname].encode(sys.getfilesystemencoding()),
(".py", "r", imp.PY_SOURCE))
if hasattr(plugmod, "plugin_start"):
newname = plugmod.plugin_start()
PLUGINS[newname and unicode(newname) or plugname] = plugmod
except Exception as plugerr:
if __debug__:
print_exc()
else:
sys.stderr.write('%s: %s\n' % (plugname, plugerr)) # appears in %TMP%/EDMarketConnector.log in packaged Windows app
imp.release_lock()
def _get_plugin_func(plugname, funcname):
"""
Get a function from a plugin, else return None if it isn't implemented.
:param plugname:
:param funcname:
:return:
"""
return getattr(PLUGINS[plugname], funcname, None)
def get_plugin_app(plugname, parent):
"""
If the plugin provides mainwindow content create and return it.
:param plugname: name of the plugin
:param parent: the parent frame for this entry.
:return:
"""
plugin_app = _get_plugin_func(plugname, "plugin_app")
if plugin_app:
return plugin_app(parent)
return None
def get_plugin_prefs(plugname, parent):
"""
If the plugin provides a prefs frame, create and return it.
:param plugname: name of the plugin
:param parent: the parent frame for this preference tab.
:return:
"""
plugin_prefs = _get_plugin_func(plugname, "plugin_prefs")
if plugin_prefs:
return plugin_prefs(parent)
return None
def notify_prefs_changed():
"""
Notify each plugin that the settings dialog has been closed.
:return:
"""
for plugname in PLUGINS:
prefs_changed = _get_plugin_func(plugname, "prefs_changed")
if prefs_changed:
try:
prefs_changed()
except Exception as plugerr:
if __debug__:
print_exc()
else:
sys.stderr.write('%s: %s\n' % (plugname, plugerr))
def notify_journal_entry(cmdr, system, station, entry, state):
"""
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
:return:
"""
for plugname in PLUGINS:
journal_entry = _get_plugin_func(plugname, "journal_entry")
if journal_entry:
try:
# Pass a copy of the journal entry in case the callee modifies it
if journal_entry.func_code.co_argcount == 4:
journal_entry(cmdr, system, station, dict(entry))
else:
journal_entry(cmdr, system, station, dict(entry), dict(state))
except Exception as plugerr:
if __debug__:
print_exc()
else:
sys.stderr.write('%s: %s\n' % (plugname, plugerr))
def notify_system_changed(timestamp, system, coordinates):
"""
Send notification data to each plugin when we arrive at a new system.
:param timestamp:
:param system:
:return:
deprecated:: 2.2
Use :func:`journal_entry` with the 'FSDJump' event.
"""
for plugname in PLUGINS:
system_changed = _get_plugin_func(plugname, "system_changed")
if system_changed:
try:
if system_changed.func_code.co_argcount == 2:
system_changed(timestamp, system)
else:
system_changed(timestamp, system, coordinates)
except Exception as plugerr:
if __debug__:
print_exc()
else:
sys.stderr.write('%s: %s\n' % (plugname, plugerr))
def notify_newdata(data):
"""
Send the latest EDMC data from the FD servers to each plugin
:param data:
:return:
"""
for plugname in PLUGINS:
cmdr_data = _get_plugin_func(plugname, "cmdr_data")
if cmdr_data:
try:
cmdr_data(data)
except Exception as plugerr:
if __debug__:
print_exc()
else:
sys.stderr.write('%s: %s\n' % (plugname, plugerr))