From 41a77965497d0a40f7a85d737f979a18c4e70a51 Mon Sep 17 00:00:00 2001 From: Pelagic Date: Fri, 8 Dec 2023 14:46:41 +0200 Subject: [PATCH] Added the library file. --- starfields_logparser/base_parsers.py | 72 ++++++++++++++++++++++++++++ 1 file changed, 72 insertions(+) create mode 100644 starfields_logparser/base_parsers.py diff --git a/starfields_logparser/base_parsers.py b/starfields_logparser/base_parsers.py new file mode 100644 index 0000000..9189593 --- /dev/null +++ b/starfields_logparser/base_parsers.py @@ -0,0 +1,72 @@ +#!/usr/bin/python + + +class BaseOrderedLogParser: + """ + This parser is to be inherited from. It goes through log entries + until it finds an entry it hasn't gone through, in which case it + parses until the end of the entries. + """ + + def execute(self): + self.retrieve_status() + self.parser_init() + + context_manager = self.get_context_manager() + + with context_manager(*self.context_manager_args) as file: + self.aggregators_init(file) + line = file.readline() + new_flag = False + + while (line): + # Only check for a new line if we're not at new entries yet + if not new_flag: + new_flag = self.new_flag_condition(line) + + # Ignore old entries and only consider new ones + if new_flag: + break_condition = self.parse_line(line) + + if not break_condition: + break + + line = file.readline() + + self.aggregators_deinit(file) + + self.update_status() + self.parser_deinit() + + def get_context_manager(self): + """ + Hook to customize the context manager. Defaults to the file context + manager. The context manager file should have a readline() method + that reads the next "line". + """ + return open + + def parser_init(self): + """ + Extra hook that runs after the status has been retrieved but before + the parser starts going through the file + """ + pass + + def aggregators_deinit(self, file): + """ + Hook that runs right before the context manager closes the file. + """ + pass + + def update_status(self): + """ + Updates the saved status of the parser + """ + pass + + def parser_deinit(self): + """ + Extra hook that runs after the parser updated the saved status + """ + pass