pvl.syslog.rule: implement better filters handling, and handle ValueErrors from e.g. regexp syntax
authorTero Marttila <terom@paivola.fi>
Sat, 12 Jan 2013 21:42:03 +0200
changeset 100 0f34cf81b9f1
parent 99 8d60eb5604e4
child 101 6b27d7010bd4
pvl.syslog.rule: implement better filters handling, and handle ValueErrors from e.g. regexp syntax
pvl/syslog/rule.py
--- a/pvl/syslog/rule.py	Sat Jan 12 21:41:32 2013 +0200
+++ b/pvl/syslog/rule.py	Sat Jan 12 21:42:03 2013 +0200
@@ -58,10 +58,39 @@
         rules = [cls.config_section(subsection, section[subsection]) for subsection in section.sections]
         attrs = dict((name, section[name]) for name in section.scalars)
          
-        return cls.config(name, rules, **attrs)
+        try :
+            return cls.config(name, rules, **attrs)
+
+        except ValueError as ex :
+            raise ValueError("[%s] %s" % (name, ex))
 
     @classmethod
-    def config (cls, name, rules=None, format=None, irk=None, program=None, facility=None, pattern=None, **filters) :
+    def config_filters (cls, program=None, facility=None, pattern=None, **filters) :
+        """
+            Return filter expression from given attr/value in config.
+        """
+
+        # XXX: get rid of these special cases
+        if facility :
+            yield 'facility', facility # glob
+
+        if program :
+            yield 'prog', program # glob
+
+        if pattern :
+            filters['msg'] = pattern
+        
+        # generic
+        for attr, value in filters.iteritems() :
+            try :
+                # regex
+                yield attr, re.compile(value)
+            
+            except re.error as ex :
+                raise ValueError("%s: %s" % (attr, ex))
+
+    @classmethod
+    def config (cls, name, rules=None, format=None, irk=None, **filters) :
         """
             Build SyslogRule from config options
         """
@@ -73,19 +102,8 @@
 
         if irk :
             format['irk'] = irk
-
-        filters = dict(
-            (attr, re.compile(regex)) for attr, regex in filters.iteritems()
-        )
-
-        if facility :
-            filters['facility'] = facility # glob
-
-        if program :
-            filters['prog'] = program # glob
-
-        if pattern :
-            filters['msg'] = re.compile(pattern)
+        
+        filters = dict(cls.config_filters(**filters))
 
         filter = SyslogFilter(**filters)