created simple LogParser
authorChristian Herdtweck <christian.herdtweck@intra2net.com>
Wed, 29 Jun 2016 15:06:59 +0000 (17:06 +0200)
committerChristian Herdtweck <christian.herdtweck@intra2net.com>
Wed, 29 Jun 2016 15:06:59 +0000 (17:06 +0200)
src/log_read.py

index 4e52560..475dfa2 100644 (file)
@@ -37,6 +37,7 @@ like date, time, module name, urgency and message.
 """
 
 import os
+import re
 from warnings import warn
 import os.path
 from iter_helpers import zip_longest
@@ -310,6 +311,31 @@ class LineReader(IterativeReader):
         return result
 
 
-class LogParser:
-    """ takes lines from LineReader and parses their contents """
-    pass
+class LogParser(LineReader):
+    """ takes lines from LineReader and parses their contents
+
+    requires a pattern for log lines, auto-detection is not implemented yet
+
+    Iteration returns re.match result or -- if matching failed -- the original
+    raw line
+    """
+
+    def __init__(self, log_file, pattern=None):
+        """ create a LogParser
+
+        :param str log_file: name of log file to parse (required!)
+        :param pattern: regexp to split log lines; None (default) to return
+                        line as they are
+        """
+        super(LogParser, self).__init__(log_file)
+
+        self.pattern = pattern
+
+    def prepare_result(self, *args):
+        # let super class split data into lines
+        for _, raw_line in super(LogParser, self).prepare_result(*args):
+            result = re.match(self.pattern, raw_line)
+            if result:
+                yield result
+            else:
+                yield raw_line