pvl/syslog/filter.py
changeset 74 952ee07efd7a
parent 68 bea41de5cc98
child 99 8d60eb5604e4
--- a/pvl/syslog/filter.py	Thu Jan 10 17:50:10 2013 +0200
+++ b/pvl/syslog/filter.py	Thu Jan 10 17:51:53 2013 +0200
@@ -16,38 +16,46 @@
         # drop None's
         self.filters = dict((attr, regex) for attr, regex in filters.iteritems() if regex is not None)
 
-    def match_prog (self, attr, glob, prog=None) :
+    def match_glob (self, attr, glob, value=None) :
         """
-            XXX: Match given prog as glob?
+            Match prog as glob.
         """
 
-        if not glob :
-            # ignore
-            return None
-
-        if not prog :
+        if not value :
             # require
             return False
 
         # normalize
-        prog = prog.strip().lower()
-        _, prog = os.path.split(prog)
+        value = value.strip()
 
         # match
-        if fnmatch.fnmatch(prog, glob) :
-            return { attr: prog }
+        if fnmatch.fnmatch(value, glob) :
+            return { attr: value }
         else :
             return False
+ 
+    match_facility = match_glob
 
-    def match (self, attr, regex, value=None) :
+    def match_prog (self, attr, glob, prog=None) :
+        """
+            Match prog as glob.
+        """
+
+        if prog :
+            # normalize
+            prog = prog.strip().lower()
+            _, prog = os.path.split(prog)
+
+        # match
+        return self.match_glob(attr, glob, prog)
+
+    REGEX_TYPE = type(re.compile(''))
+
+    def match_regex (self, attr, regex, value=None) :
         """
             Match given value against given pattern.
         """
 
-        if not regex :
-            # ignore
-            return None
-
         if not value :
             # XXX: optional = match empty string?
             value = ''
@@ -78,12 +86,25 @@
         matches = {}
 
         for attr in self.filters :
-            # lookup match-func
-            match = getattr(self, 'match_{attr}'.format(attr=attr), self.match)
-
             # filter
             filter = self.filters[attr]
+
+            if not filter :
+                # ignore
+                continue
             
+            # lookup match-func
+            match = getattr(self, 'match_{attr}'.format(attr=attr), None)
+
+            if match :
+                pass
+
+            elif isinstance(filter, self.REGEX_TYPE) :
+                match = self.match_regex
+
+            else :
+                match = self.match_glob
+
             # apply match
             if attr in item :
                 match = match(attr, filter, item[attr])
@@ -96,17 +117,13 @@
                 # match
                 matches.update(match)
             
-            elif match is None :
-                # ignore
-                continue
-
             else :
                 # reject
                 return
         
-        # XXX: test last match, in case they were all None
+        # test last match
         if match is None :
-            # XXX: empty filter!?
+            # empty filter -> all None
             return True
         else :
             return matches