diff --git a/plugin_test/plugin_test.py b/plugin_test/plugin_test.py index fe8ac5d..a002b6d 100755 --- a/plugin_test/plugin_test.py +++ b/plugin_test/plugin_test.py @@ -6,7 +6,7 @@ import time import pluginloader import os #for absolute path: os.path.dirname(os.path.abspath(__file__)) -import ConfigParser #for parse the config file +import configparser #for parse the config file import logging @@ -15,7 +15,7 @@ logger = logging.getLogger() logger.setLevel(logging.DEBUG) #set log string format -formatter = logging.Formatter('%(asctime)s - %(levelname)s: %(message)s', '%d.%m.%Y %I:%M:%S') +formatter = logging.Formatter('%(asctime)s - %(module)s - %(levelname)s: %(message)s', '%d.%m.%Y %I:%M:%S') #create a file loger fh = logging.FileHandler('boswatch.log', 'w') @@ -39,11 +39,11 @@ logger.addHandler(ch) #exception - error with exception message in log #critical - critical error, program exit -#ConfigParser +#configparser try: logging.debug("reading config file") script_path = os.path.dirname(os.path.abspath(__file__)) - globals.config = ConfigParser.ConfigParser() + globals.config = configparser.ConfigParser() globals.config.read(script_path+"/config/config.ini") except: logging.exception("cannot read config file") @@ -52,9 +52,13 @@ except: data = {"ric":"1234567", "function":"1", "msg":"Hello World!"} while True: - time.sleep(1) - logging.info("Alarm!") + try: + time.sleep(1) + logging.info("Alarm!") for i in pluginloader.getPlugins(): - plugin = pluginloader.loadPlugin(i) - logging.debug(i["name"] + " Plugin called") - plugin.run("POC","80000000",data) \ No newline at end of file + plugin = pluginloader.loadPlugin(i) + logging.debug(i["name"] + " Plugin called") + plugin.run("POC","80000000",data) + except: + logging.exception("Cannot Throw Modules") + exit() \ No newline at end of file diff --git a/plugin_test/pluginloader.py b/plugin_test/pluginloader.py index cb353e4..e5f45ae 100644 --- a/plugin_test/pluginloader.py +++ b/plugin_test/pluginloader.py @@ -10,26 +10,27 @@ PluginFolder = "./plugins" MainModule = "__init__" def getPlugins(): - plugins = [] - possibleplugins = os.listdir(PluginFolder) - for i in possibleplugins: - location = os.path.join(PluginFolder, i) - # plugins have to be a subdir with MainModule, if not skip - if not os.path.isdir(location) or not MainModule + ".py" in os.listdir(location): - continue - logging.debug("found plugin: "+i) + plugins = [] + possibleplugins = os.listdir(PluginFolder) + for i in possibleplugins: + location = os.path.join(PluginFolder, i) + # plugins have to be a subdir with MainModule, if not skip + if not os.path.isdir(location) or not MainModule + ".py" in os.listdir(location): + continue + logging.debug("found plugin: "+i) - # is the plugin enabled in the config-file? - try: - usePlugin = int(globals.config.get("Module", i)) - except: #no entry for plugin found in config-file, skip - continue - logging.debug("use Plugin: "+str(usePlugin)) - if usePlugin: - info = imp.find_module(MainModule, [location]) - plugins.append({"name": i, "info": info}) - logging.debug("append Plugin: "+i) - return plugins + # is the plugin enabled in the config-file? + try: + usePlugin = int(globals.config.get("Module", i)) + except: #no entry for plugin found in config-file, skip + logging.warning("Plugin not in config: "+i) + + logging.debug("use Plugin: "+str(usePlugin)) + if usePlugin: + info = imp.find_module(MainModule, [location]) + plugins.append({"name": i, "info": info}) + logging.debug("append Plugin: "+i) + return plugins def loadPlugin(plugin): - return imp.load_module(MainModule, *plugin["info"]) \ No newline at end of file + return imp.load_module(MainModule, *plugin["info"]) \ No newline at end of file diff --git a/plugin_test/plugins/none/__init__.py b/plugin_test/plugins/none/__init__.py new file mode 100644 index 0000000..dffa895 --- /dev/null +++ b/plugin_test/plugins/none/__init__.py @@ -0,0 +1,9 @@ +#!/usr/bin/python +# -*- coding: cp1252 -*- + +import logging # Global logger +import globals # Global variables + +def run(typ,freq,data): + logging.info("Nothing to do") + \ No newline at end of file diff --git a/plugin_test/plugins/template/__init__.py b/plugin_test/plugins/template/__init__.py index a82d6b1..d0f6005 100644 --- a/plugin_test/plugins/template/__init__.py +++ b/plugin_test/plugins/template/__init__.py @@ -8,5 +8,7 @@ def run(typ,freq,data): try: if typ == "ZVEI": logging.info("ZVEI: %s wurde auf %s empfangen!", data["zvei"],freq) + else: + logging.warning(typ + " not supportet") except: logging.exception("Error in Template Plugin") \ No newline at end of file