fixbot/logwatch/message.py
changeset 53 21ab25ffa1e8
parent 50 edbc337b7c29
child 65 1de3c0ca2baf
--- a/fixbot/logwatch/message.py	Fri Feb 05 20:38:44 2010 +0200
+++ b/fixbot/logwatch/message.py	Fri Feb 05 21:16:19 2010 +0200
@@ -71,7 +71,7 @@
         +   r"(?P<timestamp>\w{3} [0-9 ]\d \d{2}:\d{2}:\d{2}) (?P<hostname>\S+)"
 
             # the message, including possible tag/pid
-        +   r" (?P<message>(?P<tag>(?P<program>[^:\]]+)(?:\[(?P<pid>\d+)\])?: )?(?P<text>.+))\n?"
+        +   r" (?P<message>(?P<tag>(?P<program>[^:\]]+)(?:\[(?P<pid>\d+)\])?: )?(?P<text>.*))\n?"
     )
 
     # strptime format of timestamp
@@ -158,7 +158,65 @@
         
         # the raw line as matched
         self.raw = match.group(0)
+    
+    def properties (self) :
+        """
+            Return a dict containing the attributes specified for this message
+        """
+        
+        # XXX: ugh... doesn't do @property
+        return dict(
+            facility        = self.facility,
+            priority        = self.priority,
+            timestamp       = self.timestamp,
+            hostname        = self.hostname,
+            tag             = self.tag,
+            program         = self.program,
+            pid             = self.pid,
+            text            = self.text,
+            message         = self.message,
+        )
 
+    def match (self, regexp=None, program=None) :
+        """
+            Evaluate a match against this message using the given criteria
+                
+                regexp          - match regexp against the message's contents
+                program         - match program component of tag against given value, may also be False
+        """
+
+        params = {}
+        
+        ## program
+        if program is False :
+            # only match if no tag
+            if self.program is not None :
+                return False
+
+        elif program is not None :
+            # case-insensitive match
+            if self.program.lower() != program.lower() :
+                return False
+        
+        else :
+            # params
+            params['program'] = self.program
+
+        ## pattern
+        if regexp is not None :
+            # apply regexp
+            match = regexp.match(self.text)
+
+            if not match :
+                # no match
+                return False
+            
+            # good, params
+            params.update(match.groupdict())
+        
+        # we appear to have a match
+        return params
+        
     def __str__ (self) :
         """
             Format to default format