Commit 7d662323 authored by Robin Mueller's avatar Robin Mueller
Browse files

extending generic file parser

parent 4e08d014
#! /usr/bin/python3.7
#! /usr/bin/python3
"""
@file
mib_packet_content_parser.py
@brief
Generic File Parser class
@file mib_packet_content_parser.py
@brief Generic File Parser class
@details
Used by the MIB Exporter. There are two functions which can be implemented by child class.
Default implementations are empty
Used by the MIB Exporter. There are multiple functions which are abstract and should
be implemented by a custom parser implementation.
1. handleFileParsing(...) which handles the parsing of one file
2. updateTableEntries(...) After all files have been parsed, the table can
be updated by imlementing this function
A file list to parse must be supplied.
Child classes fill out the MIB table (self.mib_table)
@author
R. Mueller
@date
14.11.2019
A file list to parse must be supplied.
Child classes fill out the MIB table (self.mib_table)
@author R. Mueller
@date 14.11.2019
"""
import enum
from abc import abstractmethod
from typing import Dict
class FileParserModes(enum.Enum):
REGULAR = enum.auto(),
MOVING_WINDOW = enum.auto()
class FileParser:
"""
This parent class gathers common file parser operations into a super class.
Everything else needs to be implemented by child classes
(e.g. which strings to parse for or operations to take after file parsing has finished)
The user should do the following to use this base class:
1. Create a custom parser class which implements this class and implement the abstract
functions
2. Set the parser mode
3. Call parse_files. Additional arguments and keyword arguments can be supplied as well and
will be passed through to the abstract function implementations.
"""
def __init__(self, file_list):
if len(file_list) == 0:
......@@ -40,26 +42,75 @@ class FileParser:
self.index = 0
# Initialize empty MIB table which will be filled by specific parser implementation
self.mib_table = dict()
self.__parser_mode = FileParserModes.REGULAR
self.__parser_args = 0
def set_regular_parser_mode(self):
"""
Set regular parsing mode. This will be the default, so it is not strictly necessary to call
this.
:return:
"""
self.__parser_mode = FileParserModes.REGULAR
def set_moving_window_mode(self, moving_window_size: int):
"""
Set moving window parsing mode
:param moving_window_size:
:return:
"""
self.__parser_mode = FileParserModes.MOVING_WINDOW
self.__parser_args = moving_window_size
def parse_files(self, *args: any, **kwargs) -> Dict:
"""
Core method which is called to parse the files
:param args: Optional positional arguments. Passed on the file parser
:param kwargs: Optional keyword arguments. Passed on to file parser
:return:
:return: Returns the mib table dictionary.
"""
if not self.file_list_empty:
if self.file_list_empty:
print(f"Nothing to parse, supplied file list is empty!")
return self.mib_table
if self.__parser_mode == FileParserModes.REGULAR:
for file_name in self.file_list:
# Implemented by child class ! Fill out info table (self.mib_table) in this routine
self._handle_file_parsing(file_name, *args, **kwargs)
self._handle_file_parsing(file_name=file_name, *args, **kwargs)
# Can be implemented by child class to edit the table after it is finished.
# default implementation is empty
self._post_parsing_operation()
elif self.__parser_mode == FileParserModes.MOVING_WINDOW:
for file_name in self.file_list:
self.__parse_file_with_moving_window(file_name=file_name)
return self.mib_table
# Implemented by child class ! Fill out info table (self.mib_table) in this routine
@abstractmethod
def _handle_file_parsing(self, file_name: str, *args, **kwargs):
"""
Implemented by child class. The developer should fill the info table (self.mib_table)
in this routine
:param file_name:
:param args: Additional arguments passed through the parse_files method.
:param kwargs: Additional keyword arguments passed through the parse_files method.
:return: Nothing. Fill out the member dictionary self.mib_table in the function instead.
"""
pass
@abstractmethod
def _handle_file_parsing_moving_window(
self, file_name: str, current_line: int, moving_window_size: int, moving_window: list,
*args, **kwargs):
"""
This will be called for the MOVING_WINDOW parser mode.
:param file_name: Current file name
:param current_line: Current line number.
:param moving_window_size: Size of the moving window
:param moving_window: Current moving window. The last entry of the moving window
is the current line number
:return: Nothing. Fill out the member dictionary self.mib_table in the function instead.
"""
pass
@abstractmethod
......@@ -69,3 +120,6 @@ class FileParser:
flag or editting MIB table entries)
:return:
"""
def __parse_file_with_moving_window(self, file_name: str):
pass
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment