BOSWatch/includes/pluginLoader.py

90 lines
2.2 KiB
Python
Raw Normal View History

2015-05-22 22:40:44 +02:00
#!/usr/bin/python
# -*- coding: cp1252 -*-
2015-05-27 07:48:24 +02:00
"""
Functions to Load and import the Plugins
@author: Bastian Schroll
@requires: Configuration has to be set in the config.ini
"""
2015-05-22 22:40:44 +02:00
import logging # Global logger
import imp
import os
2015-06-14 20:35:04 +02:00
from ConfigParser import NoOptionError # we need this exception
2015-05-22 22:40:44 +02:00
from includes import globals # Global variables
def loadPlugins():
2015-05-27 07:48:24 +02:00
"""
Load all Plugins into globals.pluginList
@return: nothing
@exception: Exception if insert into globals.pluginList failed
"""
2015-05-22 22:40:44 +02:00
try:
logging.debug("loading plugins")
2015-06-14 20:21:21 +02:00
# go to all Plugins from getPlugins()
2015-05-22 22:40:44 +02:00
for i in getPlugins():
2015-06-14 20:21:21 +02:00
# call for each Plugin the loadPlugin() Methode
2015-05-22 22:40:44 +02:00
plugin = loadPlugin(i)
2015-06-14 20:21:21 +02:00
# Add it to globals.pluginList
2015-05-22 22:40:44 +02:00
globals.pluginList[i["name"]] = plugin
except:
logging.exception("cannot load Plugins")
def getPlugins():
2015-05-27 07:48:24 +02:00
"""
get a Python Dict of all activeated Plugins
@return: Plugins as Python Dict
@exception: Exception if Plugin search failed
"""
2015-05-22 22:40:44 +02:00
try:
logging.debug("Search in Plugin Folder")
PluginFolder = globals.script_path+"/plugins"
plugins = []
2015-06-14 20:21:21 +02:00
# Go to all Folders in the Plugin-Dir
2015-05-22 22:40:44 +02:00
for i in os.listdir(PluginFolder):
location = os.path.join(PluginFolder, i)
2015-05-26 11:41:05 +02:00
2015-06-14 20:21:21 +02:00
# Skip if Path.isdir() or no File DIR_NAME.py is found
2015-05-22 22:40:44 +02:00
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 globals.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)
2015-06-14 20:35:04 +02:00
# no entry for plugin found in config-file
except NoOptionError:
2015-05-22 22:40:44 +02:00
logging.warning("Plugin [NO CONF ] %s", i)
2015-06-14 20:35:04 +02:00
pass
2015-05-22 22:40:44 +02:00
except:
logging.exception("Error during Plugin search")
return plugins
def loadPlugin(plugin):
2015-05-27 07:48:24 +02:00
"""
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
"""
2015-05-22 22:40:44 +02:00
try:
logging.debug("load Plugin: %s", plugin["name"])
return imp.load_module(plugin["name"], *plugin["info"])
except:
logging.exception("cannot load Plugin: %s", plugin["name"])