#!/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): status = self.retrieve_status() context_manager = self.get_context_manager() with context_manager(*self.context_manager_args) as file: aggregates = self.aggregators_init(file, status) line = file.readline() new_flag = False break_condition = True 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, status) # Ignore old entries and only consider new ones if new_flag: break_condition = self.parse_line(line, aggregates, status) if not break_condition: break line = file.readline() self.aggregators_deinit(file, aggregates, status) if not break_condition: self.reset_status(aggregates, status) else: self.update_status(aggregates, status) 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 aggregators_deinit(self, file, aggregates, status): """ Hook that runs right before the context manager closes the file. It comes with the aggregates and the status date so sometimes closing the status of the parser here makes more sense. """ pass def reset_status(self, aggregates, status): """ Resets the saved status of the parser. """ pass def update_status(self, aggregates, status): """ Updates the saved status of the parser. """ pass