From 445afb2390b6a7843bb7f6d53c740bf8099cd756 Mon Sep 17 00:00:00 2001 From: Christian Herdtweck Date: Wed, 29 Jun 2016 17:06:59 +0200 Subject: [PATCH] created simple LogParser --- src/log_read.py | 32 +++++++++++++++++++++++++++++--- 1 files changed, 29 insertions(+), 3 deletions(-) diff --git a/src/log_read.py b/src/log_read.py index 4e52560..475dfa2 100644 --- a/src/log_read.py +++ b/src/log_read.py @@ -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 -- 1.7.1