fix bugs in interface & module loading
[logparse.git] / logparse / config.py
index ac8cc9f7a668437a89ae086e9fb1fa37ae04a734..13d6d07099759e82c168304acb92aca437096404 100644 (file)
@@ -9,6 +9,7 @@ import yaml
 import types
 import os
 from pkg_resources import Requirement, resource_filename
+from types import SimpleNamespace
 
 import logparse
 import logging
@@ -20,7 +21,23 @@ def locate(filename):
     logger.debug("Found {0}".format(loc))
     return loc
 
-prefs = {
+class Configuration(dict):
+
+    def __init__(self, *arg, **kw):
+        super(Configuration, self).__init__(*arg, **kw)
+    
+    def _str2bool(x):
+        positives = ["yes", "true", "1", "y"]
+        negatives = ["no", "false", "0", "n"]
+        x = x.lower()
+        if x in positives:
+            return True
+        elif x in negatives:
+            return False
+        else:
+            raise ValueError("Unknown option %s" % x)
+
+defaults = Configuration({
     'output': '/var/www/logparse/summary.html',
     'header':  '/etc/logparse/header.html',
     'css': '/etc/logparse/main.css',
@@ -38,7 +55,7 @@ prefs = {
     'verbose': 'n',
     'hddtemp': {
         'drives': ['/dev/sda'],
-        'host': 'localhost',
+        'host': '127.0.0.1',
         'separator': '|',
         'timeout': 10,
         'port': 7634,
@@ -71,25 +88,40 @@ prefs = {
         'postfix': '/var/log/mail.log',
         'httpd': '/var/log/apache2'
     }
-}
+})
+
+def verify(raw_dict, defaults):
+    for key, value in raw_dict.items():
+        if key in defaults: # valid key
+            logger.debug("Found valid key {0} with value {1}".format(key, value))
+            if (isinstance(value, dict)):
+                verify(value, defaults[key])   # recurse nested dictionaries
+
+        else:               # invalid key
+            logger.warning("Invalid key {0} with value {1}".format(key, value))
 
 def loadconf(argparser, configfile = "/etc/logparse/logparse.conf"):
-    logger.debug("getting config from {0}".format(configfile))
+    logger.debug("Getting config from {0}".format(configfile))
     try:
-        data = yaml.safe_load(open(configfile))
-        for value in data:
-            if(isinstance(data[value], dict)):
-                for key in data[value].items():
+        raw_dict = yaml.safe_load(open(configfile))
+        # verify fields
+        verify(raw_dict, defaults)
+        prefs = defaults
+        for value in raw_dict:
+            if(isinstance(raw_dict[value], dict)):
+                for key in raw_dict[value].items():
+                    logger.debug("Inserting key {0} with value {1}".format(key[0], key[1]))
+                    if not value in prefs:
+                        prefs[value] = {}
                     prefs[value][key[0]] = key[1]
             else:
-                prefs[value] = data[value]
-#        config['dest'] = paths.dirname(config['output'])
+                prefs[value] = raw_dict[value]
         if argparser.parse_args().to is not None:
             prefs['mail']['to'] = argparser.parse_args().to
         if not prefs['mail']['to']:
-            logger.info("no recipient address provided, outputting to stdout")
+            logger.info("No recipient address provided, outputting to stdout")
         else:
-            logger.info("email will be sent to " + prefs['mail']['to'])
+            logger.info("Email will be sent to " + prefs['mail']['to'])
         return prefs 
     except Exception as e:
-        logger.warning("error processing config: " + str(e))
+        logger.warning("Error processing config: " + str(e))