-
Notifications
You must be signed in to change notification settings - Fork 0
/
plugin_control.py
328 lines (254 loc) · 7.67 KB
/
plugin_control.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
# coding: UTF-8
"""
Copyright (c) 2009 Marian Tietz
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:
1. Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
SUCH DAMAGE.
"""
"""
plugin interface
load(filename)
-> imp(filename)
-> plugins[filename] = <filename>.filename(pluginInterface)
unload(filename)
-> plugins[filename].unload()
-> del plugins[filename]
"""
import gobject
import os
import imp
import sys
import logging
import traceback
import config
from messages import print_error
_module_prefix = "nigiriplugin_"
_plugins = {}
(
PLUGIN_MODNAME,
PLUGIN_MODULE,
PLUGIN_INSTANCE,
PLUGIN_INFO_LENGTH
) = range(4)
def strip_suffix(filename):
""" foo.py -> foo """
return os.path.split(filename)[-1].split(".")[0]
def get_plugin_config_section(filename):
return "plugin_"+strip_suffix(filename)
def is_loaded(filename):
""" returns True if the plugin is loaded, otherwise False """
return _plugins.has_key(filename)
def _register_plugin(filename, modname, module, instance):
""" add plugin to local plugin dict, key is filename """
global _plugins
if _plugins.has_key(filename):
return False
_plugins[filename] = (modname, module, instance)
return True
def _unregister_plugin(filename):
""" remove plugin from local plugin dict """
global _plugins
if not _plugins.has_key(filename):
return False
del _plugins[filename]
return True
def _find_module(filename):
""" wrapper for imp.find_module.
Searches for the module named after
filename in the configured search
path (tekka, plugin_dirs).
"""
# get the name of the module to search for
name = strip_suffix(filename)
mod_info = None
try:
mod_info = imp.find_module(
name, config.get_list("nigiri","plugin_dirs",[]))
except ImportError, e:
print_error("Error while finding module for '%s'" % (filename))
return None
if not mod_info:
print_error("No plugin found for filename '%s'" % (filename))
return None
return mod_info
def _load_module(filename, mod_info):
""" wrapper for imp.load_module.
Returns a tuple with the identifying
name of the loaded module and the
module itself if loading was successful.
Otherwise the function returns (None,None).
"""
name = strip_suffix(filename)
plugin = None
modname = _module_prefix + name
try:
plugin = imp.load_module(modname, *mod_info)
except ImportError,e:
print_error("Failure while loading plugin '%s': %s" % (name, e))
try:
mod_info[0].close()
except (IndexError,AttributeError):
pass
return modname, plugin
def _unload_module(name):
""" unload the module loaded by imp.load_module
by deleting it's reference from sys.modules
"""
del sys.modules[name]
def load(filename):
""" load a module named after filename in the
configured search path (tekka, plugin_dirs),
instance the class named after the module
(np for np.py) and register the bunch.
On error this function calls print_error()
with an specific error message and returns
False. Otherwise this function returns True.
"""
global _plugins
if _plugins.has_key(filename):
print_error("A plugin with that name is already loaded.")
return False
mod_info = _find_module(filename)
if not mod_info:
return False
modname, plugin = _load_module(filename, mod_info)
classname = strip_suffix(filename)
if not plugin:
_unload_module(modname)
return False
try:
instance = eval ("plugin.%s()" % (classname))
except BaseException,e:
logging.debug("plugin %s: %s" % (filename, traceback.format_exc(e)))
print_error("Error while instancing plugin: %s" % (e))
_unload_module(modname)
return False
if not _register_plugin(filename, modname, plugin, instance):
_unload_module(modname)
return False
return True
def unload(filename):
""" Call unload() in the plugin instance,
unload the module and unregister it.
On success this function returns True,
otherwise it returns False.
"""
global _plugins
try:
entry = _plugins[filename]
except KeyError:
print_error("Failed to unload plugin '%s', does not exist." % (filename))
return False
# tell the instance, it's time to go
try:
entry[PLUGIN_INSTANCE].unload()
except:
pass
_unload_module(entry[PLUGIN_MODNAME])
# unregister locally
return _unregister_plugin(filename)
def _check_info_tuple(info):
if (type(info) != tuple
or len(info) != PLUGIN_INFO_LENGTH
or len([n for n in info if type(n) != str])):
return False
return True
def get_info(filename):
""" return the plugin info from the plugin.
If any error occurs, this function
returns None, else it returns a tuple.
tuple returned:
(<description>, <version>, <author>)
"""
global _plugins
if _plugins.has_key(filename):
try:
info = _plugins[filename][PLUGIN_MODULE].plugin_info
except:
return None
if not _check_info_tuple(info):
return None
return info
else:
name = strip_suffix(filename)
mod_info = _find_module(filename)
if not mod_info:
return None
modname, plugin = _load_module(filename, mod_info)
if not plugin:
return None
try:
info = plugin.plugin_info
except AttributeError:
_unload_module(modname)
return None
# return None if the attribute is not a tuple
# or any item in the tuple is not a string
if not _check_info_tuple(info):
_unload_module(modname)
return None
_unload_module(modname)
return info
return None
def _check_options_tuple(options):
if type(options) != tuple:
return False
for x in options:
# name, label and type are required
if type(x) != tuple or len(x) < 3:
return False
return True
def get_options(filename):
""" Return the configurable options and the input
types associated with every option.
If no options are found, None is returned.
e.g. ( ("pass", "some label", sushi.TYPE_PASSWORD, ""),
("name", "another label", sushi.TYPE_ENTRY, "default"))
"""
global _plugins
def retrieve_options(plugin):
try:
options = plugin.plugin_options
except AttributeError:
return None
if not _check_options_tuple(options):
return None
return options
if _plugins.has_key(filename):
return retrieve_options(_plugins[filename][PLUGIN_MODULE])
else:
mod_info = _find_module(filename)
if not mod_info:
return None
modname, plugin = _load_module(filename, mod_info)
if not plugin:
return None
options = retrieve_options(plugin)
_unload_module(modname)
return options
return None
def load_autoloads():
autoloads = config.get("autoload_plugins").items()
for opt,filename in autoloads:
print "autoloading '%s'" % (filename)
if not load(filename):
print_error("Failed to load plugin '%s' automatically, "\
"removing from list." % (filename))
config.unset("autoload_plugins",opt)