#!/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