made some more code generic

This commit is contained in:
2021-06-21 12:47:03 +02:00
parent f75892776e
commit 798efe2048
3 changed files with 113 additions and 79 deletions

View File

@ -12,8 +12,9 @@ Child classes fill out the MIB table (self.mib_table)
@date 14.11.2019
"""
import enum
import re
from abc import abstractmethod
from typing import Dict
from typing import Dict, List
class FileParserModes(enum.Enum):
@ -47,6 +48,7 @@ class FileParser:
self.__debug_moving_window = False
self.__debug_moving_window_filename = ""
self._moving_window_center_idx = 3
self._verbose_level = 1
@ -195,3 +197,56 @@ class FileParser:
file = open(file_name, 'r', encoding='cp1252')
all_lines = file.readlines()
return all_lines
def _build_multi_line_string_generic(
self, first_line: str, moving_window: List[str]
) -> str:
all_lines = first_line.rstrip()
end_found = False
current_idx = self._moving_window_center_idx
while not end_found and current_idx < len(moving_window) - 1:
current_idx += 1
string_to_add = moving_window[current_idx].lstrip()
if ";" in moving_window[current_idx]:
all_lines += string_to_add
break
else:
string_to_add.rstrip()
all_lines += string_to_add
return all_lines
def _search_for_descrip_string_generic(
self, moving_window: List[str], break_pattern: str
) -> str:
current_idx = self._moving_window_center_idx - 1
# Look at the line above first
descrip_match = re.search(
r"\[EXPORT][\s]*:[\s]*\[COMMENT]", moving_window[current_idx]
)
if not descrip_match:
while current_idx > 0:
current_idx -= 1
if re.search(
break_pattern, moving_window[current_idx]
):
break
descrip_match = re.search(
r"\[EXPORT][\s]*:[\s]*\[COMMENT]", moving_window[current_idx]
)
if descrip_match:
break
if descrip_match:
current_build_idx = current_idx
descrip_string = ""
while current_build_idx < self._moving_window_center_idx:
string_to_add = moving_window[current_build_idx].lstrip()
string_to_add = string_to_add.lstrip("//!<>")
string_to_add = string_to_add.rstrip()
descrip_string += string_to_add
current_build_idx += 1
else:
return ""
resulting_description = re.search(
r"\[EXPORT][\s]*:[\s]*\[COMMENT](.*)", descrip_string
)
return resulting_description.group(1)