prelude-correlator/master: Implement our own Config class

[email protected] Thu, 18 Jun 2009 16:39:25 +0200 (CEST)
Newsgroups gmane.comp.security.ids.prelude.cvs
Message-ID <[email protected]>
commit 37945571d4674342ef2b5f2d9583654c6c79d2a2
Author: Yoann Vandoorselaere <[email protected]>
Date:   Thu Jun 18 16:36:18 2009 +0200

    Implement our own Config class
    
    Simplify the code as much as possible, implement our own Configuration
    class, including some helpers. Remove globals.


========================================

 PreludeCorrelator/config.py             |   46 +++++++++++++++++++++++++++++++
 PreludeCorrelator/main.py               |    4 ++-
 PreludeCorrelator/pluginmanager.py      |   43 ++++++++---------------------
 prelude_correlator.egg-info/SOURCES.txt |    1 +
 4 files changed, 62 insertions(+), 32 deletions(-)

========================================

diff --git a/PreludeCorrelator/config.py b/PreludeCorrelator/config.py
new file mode 100644
index 0000000..4beea52
--- /dev/null
+++ b/PreludeCorrelator/config.py
@@ -0,0 +1,46 @@
+# Copyright (C) 2009 PreludeIDS Technologies. All Rights Reserved.
+# Author: Yoann Vandoorselaere <[email protected]>
+#
+# This file is part of the Prelude-Correlator program.
+#
+# This program is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 2, or (at your option)
+# any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program; see the file COPYING.  If not, write to
+# the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
+
+import ConfigParser
+
+class Config(ConfigParser.ConfigParser):
+        def __init__(self, filename):
+                ConfigParser.ConfigParser.__init__(self)
+                self.read(filename)
+
+        def get(self, section, option, raw=None, vars=None, default=None):
+                try:
+                        return ConfigParser.ConfigParser.get(self, section, option, raw, vars)
+
+                except ConfigParser.NoSectionError:
+                        return default
+
+                except ConfigParser.NoOptionError:
+                        return default
+
+        def getAsBool(self, section, option, raw=None, vars=None, default=None):
+                b = self.get(section, option, raw, vars, default)
+                if type(b) is bool:
+                        return b
+
+                b = b.strip().lower()
+                if b == "true" or b == "yes":
+                        return True
+
+                return False
diff --git a/PreludeCorrelator/main.py b/PreludeCorrelator/main.py
index d337a8b..717daa2 100644
--- a/PreludeCorrelator/main.py
+++ b/PreludeCorrelator/main.py
@@ -24,11 +24,13 @@ import sys, os, time, signal
 from optparse import OptionParser
 from PreludeEasy import ClientEasy, CheckVersion
 from PreludeCorrelator import __version__ as VERSION
-from PreludeCorrelator import idmef, pluginmanager, context, siteconfig, log
+from PreludeCorrelator import idmef, pluginmanager, context, siteconfig, log, config
+
 
 class Env:
         def __init__(self):
                 self.logger = log.Log()
+                self.config = config.Config(siteconfig.conf_dir + '/prelude-correlator.conf')
 
 
 class SignalHandler:
diff --git a/PreludeCorrelator/pluginmanager.py b/PreludeCorrelator/pluginmanager.py
index 1582f29..1ac5749 100644
--- a/PreludeCorrelator/pluginmanager.py
+++ b/PreludeCorrelator/pluginmanager.py
@@ -17,61 +17,42 @@
 # along with this program; see the file COPYING.  If not, write to
 # the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
 
-import pkg_resources
-import ConfigParser, sys, os, traceback
+import pkg_resources, sys, os, traceback, ConfigParser
 from PreludeCorrelator import siteconfig
 
 
-config = ConfigParser.ConfigParser()
-config.read(siteconfig.conf_dir + '/prelude-correlator.conf')
-
-ENTRYPOINT = 'PreludeCorrelator.plugins'
-
 class Plugin(object):
     enable = True
 
+    def getConfigValue(self, option, default=None):
+        return self.env.config.get(self.__class__.__name__, option, default=default)
+
     def __init__(self, env):
         self.env = env
 
-    def getConfigValue(self, key, replacement=None):
-        if not config.has_section(self.__class__.__name__):
-            return replacement
-
-        try:
-            return config.get(self.__class__.__name__, key)
-        except ConfigParser.NoOptionError:
-            return replacement
-
     def run(self, idmef):
         pass
 
 
 class PluginManager:
-    def _parseBoolean(self, b):
-        if type(b) is bool:
-                return b
-
-        b = b.strip().lower()
-        if b == "true" or b == "yes":
-                return True
-
-        return False
+    __ENTRYPOINT = 'PreludeCorrelator.plugins'
 
     def __init__(self, env):
         self._count = 0
         self.__instances = []
 
-        for entrypoint in pkg_resources.iter_entry_points(ENTRYPOINT):
+        for entrypoint in pkg_resources.iter_entry_points(self.__ENTRYPOINT):
             plugin_class = entrypoint.load()
+            pname = plugin_class.__name__
+
+            if env.config.getAsBool(pname, "disable", default=False) is True:
+                env.logger.info("%s disabled on user request" % (pname))
+                continue
 
             try:
                 pi = plugin_class(env)
             except Exception, e:
-                env.logger.warning("Exception occurred while loading %s: %s" % (plugin_class.__name__, e))
-                continue
-
-            if self._parseBoolean(pi.getConfigValue("disable", False)) is True:
-                env.logger.info("%s disabled on user request" % (plugin_class.__name__))
+                env.logger.warning("Exception occurred while loading %s: %s" % (pname, e))
                 continue
 
             self.__instances.append(pi)
diff --git a/prelude_correlator.egg-info/SOURCES.txt b/prelude_correlator.egg-info/SOURCES.txt
index ededff5..ee88cac 100644
--- a/prelude_correlator.egg-info/SOURCES.txt
+++ b/prelude_correlator.egg-info/SOURCES.txt
@@ -7,6 +7,7 @@ README
 ez_setup.py
 setup.py
 PreludeCorrelator/__init__.py
+PreludeCorrelator/config.py
 PreludeCorrelator/context.py
 PreludeCorrelator/idmef.py
 PreludeCorrelator/log.py
_______________________________________________
Prelude-cvslog site list
[email protected]
http://lists.prelude-ids.org/mailman/listinfo/prelude-cvslog