forked from Schrolli91/BOSWatch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pluginLoader.py
110 lines (91 loc) · 3.02 KB
/
pluginLoader.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
#!/usr/bin/python
# -*- coding: UTF-8 -*-
"""
Functions to Load and import the Plugins
@author: Bastian Schroll
@requires: Configuration has to be set in the config.ini
"""
import logging # Global logger
import imp
import os
from ConfigParser import NoOptionError # we need this exception
from includes import globalVars # Global variables
def loadPlugins():
"""
Load all plugins into globalVars.pluginList
@return: nothing
@exception: Exception if insert into globalVars.pluginList failed
"""
try:
logging.debug("loading plugins")
# go to all Plugins from getPlugins()
for i in getPlugins():
try:
# call for each Plugin the loadPlugin() Methode
plugin = loadPlugin(i)
except:
# call next plugin, if one has thrown an exception
logging.error("error loading plugin: %s", i["name"])
logging.debug("error loading plugin: %s", i["name"], exc_info=True)
else: # only call onLoad() and insert into pluginList[] if import is succesfull
try:
# Try to call the .onLoad() routine for all active plugins
logging.debug("call %s.onLoad()", i["name"])
plugin.onLoad()
# Add it to globalVars.pluginList
globalVars.pluginList[i["name"]] = plugin
except:
# call next plugin, if one has thrown an exception
logging.error("error calling %s.onLoad()", i["name"])
logging.debug("error calling %s.onLoad()", exc_info=True)
except:
logging.error("cannot load plugins")
logging.debug("cannot load plugins", exc_info=True)
raise
def getPlugins():
"""
get a Python Dict of all activeated plugins
@return: plugins as Python Dict
@exception: Exception if plugin search failed
"""
try:
logging.debug("Search in plugin folder")
PluginFolder = globalVars.script_path+"/plugins"
plugins = []
# Go to all Folders in the Plugin-Dir
for i in os.listdir(PluginFolder):
location = os.path.join(PluginFolder, i)
# Skip if Path.isdir() or no File DIR_NAME.py is found
if not os.path.isdir(location) or not i + ".py" in os.listdir(location):
continue
# is the plugin enabled in the config-file?
try:
if globalVars.config.getint("Plugins", i):
info = imp.find_module(i, [location])
plugins.append({"name": i, "info": info})
logging.debug("Plugin [ENABLED ] %s", i)
else:
logging.debug("Plugin [DISABLED] %s ", i)
# no entry for plugin found in config-file
except NoOptionError:
logging.warning("Plugin [NO CONF ] %s", i)
except:
logging.error("Error during plugin search")
logging.debug("Error during plugin search", exc_info=True)
raise
return plugins
def loadPlugin(plugin):
"""
Imports a single plugin
@type plugin: plugin Data
@param plugin: Contains the information to import a plugin
@return: nothing
@exception: Exception if plugin import failed
"""
try:
logging.debug("load plugin: %s", plugin["name"])
return imp.load_module(plugin["name"], *plugin["info"])
except:
logging.error("cannot load plugin: %s", plugin["name"])
logging.debug("cannot load plugin: %s", plugin["name"], exc_info=True)
raise