|
@@ -0,0 +1,87 @@
|
|
1
|
+#-*- coding: utf-8 -*-
|
|
2
|
+
|
|
3
|
+import inspect
|
|
4
|
+import os, os.path
|
|
5
|
+import warnings
|
|
6
|
+
|
|
7
|
+from Lodel.settings import Settings
|
|
8
|
+from Lodel.hooks import LodelHook
|
|
9
|
+from Lodel.user import authentication_method, identification_method
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+## @brief Returns a list of human readable registered hooks
|
|
13
|
+# @param names list | None : optionnal filter on name
|
|
14
|
+# @param plugins list | None : optionnal filter on plugin name
|
|
15
|
+# @return A str representing registered hooks
|
|
16
|
+def list_hooks(names = None, plugins = None):
|
|
17
|
+ res = ""
|
|
18
|
+ # Hooks registered and handled by Lodel.usera
|
|
19
|
+ for decorator in [authentication_method, identification_method]:
|
|
20
|
+ if names is None or decorator.__name__ in names:
|
|
21
|
+ res += "%s :\n" % decorator.__name__
|
|
22
|
+ for hook in decorator.list_methods():
|
|
23
|
+ module = inspect.getmodule(hook).__name__
|
|
24
|
+ if plugins is not None: # Filter by plugin
|
|
25
|
+ spl = module.split('.')
|
|
26
|
+ if spl[-1] not in plugins:
|
|
27
|
+ continue
|
|
28
|
+ res += "\t- %s.%s\n" % (module, hook.__name__)
|
|
29
|
+ # Hooks registered and handled by Lodel.hooks
|
|
30
|
+ registered_hooks = LodelHook.hook_list(names)
|
|
31
|
+ for hook_name, hooks in registered_hooks.items():
|
|
32
|
+ if names is None or hook_name in names:
|
|
33
|
+ res += "%s :\n" % hook_name
|
|
34
|
+ for hook, priority in hooks:
|
|
35
|
+ module = inspect.getmodule(hook).__name__
|
|
36
|
+ if plugins is not None: # Filter by plugin
|
|
37
|
+ spl = module.split('.')
|
|
38
|
+ if spl[-1] not in plugins:
|
|
39
|
+ continue
|
|
40
|
+ res += "\t- %s.%s ( priority %d )\n" % (module, hook.__name__, priority)
|
|
41
|
+ return res
|
|
42
|
+
|
|
43
|
+## @brief Return a human readable list of plugins
|
|
44
|
+# @param activated bool | None : Optionnal filter on activated or not plugin
|
|
45
|
+# @return a str
|
|
46
|
+def list_plugins(activated = None):
|
|
47
|
+ res = ""
|
|
48
|
+ # Activated plugins
|
|
49
|
+ if activated is None or activated:
|
|
50
|
+ res += "Activated plugins :\n"
|
|
51
|
+ for plugin_name in Settings.plugins:
|
|
52
|
+ res += "\t- %s\n" % plugin_name
|
|
53
|
+ # Deactivated plugins
|
|
54
|
+ if activated is None or not activated:
|
|
55
|
+ plugin_dir = os.path.join(Settings.lodel2_lib_path, 'plugins')
|
|
56
|
+ res += "Not activated plugins :\n"
|
|
57
|
+ all_plugins = [fname for fname in os.listdir(plugin_dir) if fname != '.' and fname != '..' and fname != '__init__.py']
|
|
58
|
+ for plugin_name in all_plugins:
|
|
59
|
+ if os.path.isfile(os.path.join(plugin_dir, plugin_name)) and plugin_name.endswith('.py'):
|
|
60
|
+ plugin_name = ''.join(plugin_name.split('.')[:-1])
|
|
61
|
+ elif not os.path.isdir(os.path.join(plugin_dir, plugin_name)):
|
|
62
|
+ warnings.warn("Dropped file in plugins directory : '%s'" % (os.path.join(plugin_dir, plugin_name)))
|
|
63
|
+ continue
|
|
64
|
+ elif plugin_name == '__pycache__':
|
|
65
|
+ continue
|
|
66
|
+
|
|
67
|
+ if plugin_name not in Settings.plugins:
|
|
68
|
+ res += "\t- %s\n" % plugin_name
|
|
69
|
+ return res
|
|
70
|
+
|
|
71
|
+## @brief Utility function that generate the __all__ list of the plugins/__init__.py file
|
|
72
|
+# @return A list of module name to import
|
|
73
|
+def _all_plugins():
|
|
74
|
+ plugin_dir = os.path.join(Settings.lodel2_lib_path, 'plugins')
|
|
75
|
+ res = list()
|
|
76
|
+ for plugin_name in Settings.plugins:
|
|
77
|
+ if os.path.isdir(os.path.join(plugin_dir, plugin_name)):
|
|
78
|
+ # plugin is a module
|
|
79
|
+ res.append('%s' % plugin_name)
|
|
80
|
+ #res.append('%s.loader' % plugin_name)
|
|
81
|
+ elif os.path.isfile(os.path.join(plugin_dir, '%s.py' % plugin_name)):
|
|
82
|
+ # plugin is a simple python sourcefile
|
|
83
|
+ res.append('%s' % plugin_name)
|
|
84
|
+ return res
|
|
85
|
+
|
|
86
|
+
|
|
87
|
+
|