Commit 3d6ed226 authored by Robin Mueller's avatar Robin Mueller
Browse files

added moving window implementation to core

parent 7d662323
......@@ -45,6 +45,9 @@ class FileParser:
self.__parser_mode = FileParserModes.REGULAR
self.__parser_args = 0
self.__debug_moving_window = False
self.__debug_moving_window_filename = ""
def set_regular_parser_mode(self):
"""
Set regular parsing mode. This will be the default, so it is not strictly necessary to call
......@@ -62,6 +65,10 @@ class FileParser:
self.__parser_mode = FileParserModes.MOVING_WINDOW
self.__parser_args = moving_window_size
def enable_moving_window_debugging(self, file_name: str):
self.__debug_moving_window = True
self.__debug_moving_window_filename = file_name
def parse_files(self, *args: any, **kwargs) -> Dict:
"""
Core method which is called to parse the files
......@@ -82,8 +89,8 @@ class FileParser:
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)
self.__parse_file_with_moving_window(file_name=file_name, *args, **kwargs)
self._post_parsing_operation()
return self.mib_table
@abstractmethod
......@@ -121,5 +128,66 @@ class FileParser:
:return:
"""
def __parse_file_with_moving_window(self, file_name: str):
def __parse_file_with_moving_window(self, file_name: str, *args, **kwargs):
all_lines = self._open_file(file_name=file_name)
moving_window_size = self.__parser_args
if moving_window_size == 0:
print("Moving window size is 0!")
return
moving_window = [""] * moving_window_size
for line_idx, line in enumerate(all_lines):
if self.__debug_moving_window and self.__debug_moving_window_filename in file_name:
print(f"Moving window pre line anaylsis line {line_idx}")
print(moving_window)
# The moving window will start with only the bottom being in the file
if line_idx == 0:
moving_window[self.__parser_args - 1] = line
# More and more of the window is inside the file now
elif line_idx < moving_window_size:
for idx in range(line_idx, 0, -1):
moving_window[moving_window_size - 1 - idx] = \
moving_window[moving_window_size - idx]
moving_window[moving_window_size - 1] = line
# The full window is inside the file now.
elif line_idx >= moving_window_size:
for idx in range(moving_window_size - 1):
moving_window[idx] = moving_window[idx + 1]
moving_window[moving_window_size - 1] = line
if self.__debug_moving_window and self.__debug_moving_window_filename in file_name:
print(f"Moving window post line anaylsis line {line_idx}")
print(moving_window)
self._handle_file_parsing_moving_window(
file_name=file_name, current_line=line_idx, moving_window=moving_window,
moving_window_size=moving_window_size, *args, **kwargs
)
# Now the moving window moved past the end of the file. Sections which are outside
# the file are assigned an empty string until the window has moved out of file completely
for remaining_windows_idx in range(moving_window_size):
if self.__debug_moving_window and self.__debug_moving_window_filename in file_name:
print(f"Moving window pre line analysis post EOF")
print(moving_window)
num_entries_to_clear = remaining_windows_idx + 1
for idx_to_clear in range(num_entries_to_clear):
moving_window[moving_window_size - 1 - idx_to_clear] = ""
for idx_to_reassign in range(moving_window_size - 1 - num_entries_to_clear):
moving_window[idx_to_reassign] = moving_window[idx_to_reassign + 1]
if self.__debug_moving_window and self.__debug_moving_window_filename in file_name:
print(f"Moving window post line anaylsis post EOF")
print(moving_window)
pass
@staticmethod
def _open_file(file_name: str) -> list:
"""
Open a file, attempting common encodings utf-8 and cp1252
:param file_name:
:return:
"""
try:
file = open(file_name, 'r', encoding='utf-8')
all_lines = file.readlines()
except UnicodeDecodeError:
print("ReturnValueParser: Decoding error with file " + file_name)
file = open(file_name, 'r', encoding='cp1252')
all_lines = file.readlines()
return all_lines
......@@ -84,13 +84,7 @@ class ReturnValueParser(FileParser):
'HasReturnvaluesIF')})
def _handle_file_parsing(self, file_name: str, *args, **kwargs):
try:
file = open(file_name, 'r', encoding='utf-8')
all_lines = file.readlines()
except UnicodeDecodeError:
print("ReturnValueParser: Decoding error with file " + file_name)
file = open(file_name, 'r', encoding='cp1252')
all_lines = file.readlines()
all_lines = self._open_file(file_name=file_name)
if len(args) == 1:
print_truncated_entries = args[0]
else:
......
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