|
@@ -16,7 +16,7 @@ import plugins
|
16
|
16
|
# - main.py containing hooks registration etc
|
17
|
17
|
# - confspec.py containing a configuration specification dictionary named CONFSPEC
|
18
|
18
|
|
19
|
|
-##@brief The package in wich we will load plugins modules
|
|
19
|
+##@brief The package in which we will load plugins modules
|
20
|
20
|
VIRTUAL_PACKAGE_NAME = 'lodel.plugins'
|
21
|
21
|
INIT_FILENAME = '__init__.py' # Loaded with settings
|
22
|
22
|
CONFSPEC_FILENAME_VARNAME = '__confspec__'
|
|
@@ -26,7 +26,26 @@ PLUGIN_DEPS_VARNAME = '__plugin_deps__'
|
26
|
26
|
ACTIVATE_METHOD_NAME = '_activate'
|
27
|
27
|
|
28
|
28
|
class PluginError(Exception):
|
29
|
|
- pass
|
|
29
|
+ def __init__(self, msg = "Unknow error", exceptions = None):
|
|
30
|
+ self._msg = msg
|
|
31
|
+ self._exceptions = dict() if exceptions is None else exceptions
|
|
32
|
+
|
|
33
|
+ def __repr__(self):
|
|
34
|
+ return self.__str__()
|
|
35
|
+
|
|
36
|
+ def __str__(self):
|
|
37
|
+ msg = self._msg
|
|
38
|
+ if isinstance(self._exceptions, dict):
|
|
39
|
+ for_iter = self._exceptions.items()
|
|
40
|
+ else:
|
|
41
|
+ for_iter = enumerate(self.__exceptions)
|
|
42
|
+ for obj, expt in for_iter:
|
|
43
|
+ msg += "\n\t{expt_obj} : ({expt_name}) {expt_msg}; ".format(
|
|
44
|
+ expt_obj = obj,
|
|
45
|
+ expt_name=expt.__class__.__name__,
|
|
46
|
+ expt_msg=str(expt)
|
|
47
|
+ )
|
|
48
|
+ return msg
|
30
|
49
|
|
31
|
50
|
##@brief Handle plugins
|
32
|
51
|
#
|
|
@@ -58,22 +77,23 @@ class Plugin(object):
|
58
|
77
|
# @throw PluginError
|
59
|
78
|
def __init__(self, plugin_name):
|
60
|
79
|
self.started()
|
61
|
|
-
|
62
|
80
|
self.name = plugin_name
|
63
|
81
|
self.path = self.plugin_path(plugin_name)
|
|
82
|
+ print(self.path)
|
64
|
83
|
self.module = None
|
65
|
84
|
self.__confspecs = dict()
|
66
|
85
|
self.loaded = False
|
67
|
86
|
|
68
|
87
|
# Importing __init__.py
|
69
|
|
- plugin_module = '%s.%s' % ( VIRTUAL_PACKAGE_NAME,
|
|
88
|
+ plugin_module = '%s.%s' % (VIRTUAL_PACKAGE_NAME,
|
70
|
89
|
plugin_name)
|
|
90
|
+
|
71
|
91
|
init_source = self.path + INIT_FILENAME
|
72
|
92
|
try:
|
73
|
93
|
loader = SourceFileLoader(plugin_module, init_source)
|
74
|
94
|
self.module = loader.load_module()
|
75
|
95
|
except ImportError as e:
|
76
|
|
- raise PluginError("Failed to load plugin '%s'. It seems that the plugin name is not valid" % plugin_name)
|
|
96
|
+ raise PluginError("Failed to load plugin '%s'. It seems that the plugin name is not valid" % plugin_name)
|
77
|
97
|
|
78
|
98
|
# loading confspecs
|
79
|
99
|
try:
|
|
@@ -169,7 +189,7 @@ class Plugin(object):
|
169
|
189
|
|
170
|
190
|
##@brief Load a plugin
|
171
|
191
|
#
|
172
|
|
- #Loading a plugin mean importing a file. The filename is defined in the
|
|
192
|
+ #Loading a plugin means importing a file. The filename is defined in the
|
173
|
193
|
#plugin's __init__.py file in a LOADER_FILENAME_VARNAME variable.
|
174
|
194
|
#
|
175
|
195
|
#The loading process has to take care of other things :
|
|
@@ -325,6 +345,15 @@ class Plugin(object):
|
325
|
345
|
res = cls._plugin_directories is not None
|
326
|
346
|
if raise_if_not and not res:
|
327
|
347
|
raise RuntimeError("Class Plugins is not initialized")
|
|
348
|
+
|
|
349
|
+ @classmethod
|
|
350
|
+ def clear(cls):
|
|
351
|
+ if cls._plugin_directories is not None:
|
|
352
|
+ cls._plugin_directories = None
|
|
353
|
+ if cls._plugin_instances != dict():
|
|
354
|
+ cls._plugin_instances = dict()
|
|
355
|
+ if cls._load_called != []:
|
|
356
|
+ cls._load_called = []
|
328
|
357
|
|
329
|
358
|
##@page lodel2_plugins Lodel2 plugins system
|
330
|
359
|
#
|