made some more code generic

This commit is contained in:
Robin Müller 2021-06-21 12:47:03 +02:00
parent f75892776e
commit 798efe2048
No known key found for this signature in database
GPG Key ID: FC76078F520434A5
3 changed files with 113 additions and 79 deletions

View File

@ -1,4 +1,5 @@
import re import re
from typing import List
from fsfwgen.parserbase.parser import FileParser from fsfwgen.parserbase.parser import FileParser
@ -96,21 +97,28 @@ class EventParser(FileParser):
file_name=file_name file_name=file_name
) )
def __handle_event_match(self, event_match, macro_api_match: bool, moving_window: list, file_name: str): def __handle_event_match(
event_full_match = False self, event_match, macro_api_match: bool, moving_window: list, file_name: str
):
if ";" in event_match.group(0): if ";" in event_match.group(0):
event_full_match = self.__handle_one_line_event_match( event_full_match = self.__handle_one_line_event_match(
macro_api_match=macro_api_match, moving_window=moving_window macro_api_match=macro_api_match, moving_window=moving_window
) )
else:
event_full_match = self.__build_multi_line_event_string(
first_line=event_match.group(0), moving_window=moving_window
)
# Description will be parsed separately later # Description will be parsed separately later
description = " " description = " "
if event_full_match: if event_full_match:
name = event_match.group(EVENT_NAME_IDX) name = event_match.group(EVENT_NAME_IDX)
if macro_api_match: if macro_api_match:
full_id = (self.my_id * 100) + self.return_number_from_string(event_full_match.group(2)) full_id = (self.my_id * 100) + self.return_number_from_string(
event_full_match.group(2))
severity = event_full_match.group(3) severity = event_full_match.group(3)
else: else:
full_id = (self.my_id * 100) + self.return_number_from_string(event_full_match.group(3)) full_id = (self.my_id * 100) + self.return_number_from_string(
event_full_match.group(3))
severity = event_full_match.group(4) severity = event_full_match.group(4)
self.mib_table.update({full_id: (name, severity, description, file_name)}) self.mib_table.update({full_id: (name, severity, description, file_name)})
self.count = self.count + 1 self.count = self.count + 1
@ -128,6 +136,13 @@ class EventParser(FileParser):
event_full_match = re.search(regex_string, moving_window[self.moving_window_center_idx]) event_full_match = re.search(regex_string, moving_window[self.moving_window_center_idx])
return event_full_match return event_full_match
def __build_multi_line_event_string(
self, first_line: str, moving_window: List[str]
) -> str:
return self._build_multi_line_string_generic(
first_line=first_line, moving_window=moving_window
)
def _post_parsing_operation(self): def _post_parsing_operation(self):
pass pass
@ -208,14 +223,17 @@ def export_to_file(filename: str, event_list: list, file_separator: str):
event_value = entry[1] event_value = entry[1]
file.write( file.write(
str(event_id) + file_separator + event_value[EVENT_ENTRY_NAME_IDX] + file_separator + str(event_id) + file_separator + event_value[EVENT_ENTRY_NAME_IDX] + file_separator +
event_value[EVENT_ENTRY_SEVERITY_IDX] + file_separator + event_value[EVENT_ENTRY_INFO_IDX] + event_value[EVENT_ENTRY_SEVERITY_IDX] + file_separator +
file_separator + event_value[EVENT_SOURCE_FILE_IDX] + '\n' event_value[EVENT_ENTRY_INFO_IDX] + file_separator + event_value[EVENT_SOURCE_FILE_IDX]
+ '\n'
) )
file.close() file.close()
return return
def write_translation_source_file(event_list: list, date_string: str, filename: str = "translateEvents.cpp"): def write_translation_source_file(
event_list: list, date_string: str, filename: str = "translateEvents.cpp"
):
outputfile = open(filename, "w") outputfile = open(filename, "w")
definitions = "" definitions = ""
@ -224,11 +242,13 @@ def write_translation_source_file(event_list: list, date_string: str, filename:
event_id = entry[0] event_id = entry[0]
event_value = entry[1] event_value = entry[1]
definitions += \ definitions += \
f"const char *{event_value[EVENT_ENTRY_NAME_IDX]}_STRING = \"{event_value[EVENT_ENTRY_NAME_IDX]}\";\n" f"const char *{event_value[EVENT_ENTRY_NAME_IDX]}_STRING " \
f"= \"{event_value[EVENT_ENTRY_NAME_IDX]}\";\n"
function += f"\tcase({event_id}):\n\t\treturn {event_value[EVENT_ENTRY_NAME_IDX]}_STRING;\n" function += f"\tcase({event_id}):\n\t\treturn {event_value[EVENT_ENTRY_NAME_IDX]}_STRING;\n"
function += '\tdefault:\n\t\treturn "UNKNOWN_EVENT";\n' function += '\tdefault:\n\t\treturn "UNKNOWN_EVENT";\n'
outputfile.write( outputfile.write(
f"/**\n * @brief Auto-generated event translation file. Contains {len(event_list)} translations.\n" f"/**\n * @brief Auto-generated event translation file. "
f"Contains {len(event_list)} translations.\n"
f" * @details\n" f" * @details\n"
f" * Generated on: {date_string}\n */\n" f" * Generated on: {date_string}\n */\n"
) )
@ -258,8 +278,8 @@ def handle_csv_export(file_name: str, event_list: list, file_separator: str):
def handle_cpp_export( def handle_cpp_export(
event_list: list, date_string: str, file_name: str = "translateEvents.cpp", generate_header: bool = True, event_list: list, date_string: str, file_name: str = "translateEvents.cpp",
header_file_name: str = "translateEvents.h" generate_header: bool = True, header_file_name: str = "translateEvents.h"
): ):
print("EventParser: Generating translation cpp file.") print("EventParser: Generating translation cpp file.")
write_translation_source_file(event_list=event_list, date_string=date_string, filename=file_name) write_translation_source_file(event_list=event_list, date_string=date_string, filename=file_name)

View File

@ -12,8 +12,9 @@ Child classes fill out the MIB table (self.mib_table)
@date 14.11.2019 @date 14.11.2019
""" """
import enum import enum
import re
from abc import abstractmethod from abc import abstractmethod
from typing import Dict from typing import Dict, List
class FileParserModes(enum.Enum): class FileParserModes(enum.Enum):
@ -47,6 +48,7 @@ class FileParser:
self.__debug_moving_window = False self.__debug_moving_window = False
self.__debug_moving_window_filename = "" self.__debug_moving_window_filename = ""
self._moving_window_center_idx = 3
self._verbose_level = 1 self._verbose_level = 1
@ -195,3 +197,56 @@ class FileParser:
file = open(file_name, 'r', encoding='cp1252') file = open(file_name, 'r', encoding='cp1252')
all_lines = file.readlines() all_lines = file.readlines()
return all_lines 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)

View File

@ -141,7 +141,6 @@ class ReturnValueParser(FileParser):
self.count = 0 self.count = 0
# Stores last three lines # Stores last three lines
self.last_lines = ["", "", ""] self.last_lines = ["", "", ""]
self.moving_window_center_idx = 3
self.current_interface_id_entries = { self.current_interface_id_entries = {
"Name": "", "Name": "",
"ID": 0, "ID": 0,
@ -153,17 +152,8 @@ class ReturnValueParser(FileParser):
'RETURN_FAILED', 'HasReturnvaluesIF.h', 'RETURN_FAILED', 'HasReturnvaluesIF.h',
'HasReturnvaluesIF')}) 'HasReturnvaluesIF')})
def set_moving_window_mode(self, moving_window_size: int):
"""
Set moving window parsing mode
:param moving_window_size:
:return:
"""
super().set_moving_window_mode(DEFAULT_MOVING_WINDOWS_SIZE)
def _handle_file_parsing(self, file_name: str, *args, **kwargs): def _handle_file_parsing(self, file_name: str, *args, **kwargs):
""" """Former way to parse returnvalues. Not recommended anymore.
Former way to parse returnvalues. Not recommended anymore.
:param file_name: :param file_name:
:param args: :param args:
:param kwargs: :param kwargs:
@ -177,18 +167,22 @@ class ReturnValueParser(FileParser):
for line in all_lines: for line in all_lines:
self.__handle_line_reading(line, file_name, print_truncated_entries) self.__handle_line_reading(line, file_name, print_truncated_entries)
def _handle_file_parsing_moving_window(self, file_name: str, current_line: int, def _handle_file_parsing_moving_window(
moving_window_size: int, moving_window: list, *args, self, file_name: str, current_line: int, moving_window_size: int, moving_window: list,
**kwargs): *args, **kwargs
):
"""Parse for returnvalues using a moving window"""
interface_id_match = re.search( interface_id_match = re.search(
rf"{CLASS_ID_NAMESPACE}::([a-zA-Z_0-9]*)", moving_window[self.moving_window_center_idx] rf"{CLASS_ID_NAMESPACE}::([a-zA-Z_0-9]*)", moving_window[self._moving_window_center_idx]
) )
if interface_id_match: if interface_id_match:
self.__handle_interfaceid_match(interface_id_match=interface_id_match, file_name=file_name) self.__handle_interfaceid_match(
interface_id_match=interface_id_match, file_name=file_name
)
returnvalue_match = re.search( returnvalue_match = re.search(
r"^[\s]*static const(?:expr)?[\s]*ReturnValue_t[\s]*([\w]*)[\s]*=[\s]*((?!;).*$)", r"^[\s]*static const(?:expr)?[\s]*ReturnValue_t[\s]*([\w]*)[\s]*=[\s]*((?!;).*$)",
moving_window[self.moving_window_center_idx], re.DOTALL moving_window[self._moving_window_center_idx], re.DOTALL
) )
full_returnvalue_string = "" full_returnvalue_string = ""
if returnvalue_match: if returnvalue_match:
@ -196,10 +190,12 @@ class ReturnValueParser(FileParser):
full_returnvalue_string = returnvalue_match.group(0) full_returnvalue_string = returnvalue_match.group(0)
else: else:
full_returnvalue_string = self.__build_multi_line_returnvalue_string( full_returnvalue_string = self.__build_multi_line_returnvalue_string(
moving_window=moving_window, first_line=moving_window[self.moving_window_center_idx] moving_window=moving_window,
first_line=moving_window[self._moving_window_center_idx]
) )
returnvalue_match = re.search( returnvalue_match = re.search(
r"^[\s]*static const(?:expr)? ReturnValue_t[\s]*([\w] *)[\s]*=[\s]*.*::[\w]*\(([\w]*),[\s]*([\d]*)\)", r"^[\s]*static const(?:expr)? ReturnValue_t[\s]*([\w] *)[\s]*"
r"=[\s]*.*::[\w]*\(([\w]*),[\s]*([\d]*)\)",
full_returnvalue_string full_returnvalue_string
) )
if not returnvalue_match: if not returnvalue_match:
@ -211,59 +207,21 @@ class ReturnValueParser(FileParser):
if returnvalue_match: if returnvalue_match:
description = self.__search_for_descrip_string(moving_window=moving_window) description = self.__search_for_descrip_string(moving_window=moving_window)
self.__handle_returnvalue_match( self.__handle_returnvalue_match(
name_match=returnvalue_match.group(1), file_name=file_name, number_match=returnvalue_match.group(2), name_match=returnvalue_match.group(1), file_name=file_name,
description=description number_match=returnvalue_match.group(2), description=description
) )
pass
def __build_multi_line_returnvalue_string( def __build_multi_line_returnvalue_string(
self, first_line: str, moving_window: List[str] self, first_line: str, moving_window: List[str]
) -> str: ) -> str:
all_lines = first_line.rstrip() return self._build_multi_line_string_generic(
end_found = False first_line=first_line, moving_window=moving_window
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(self, moving_window: List[str]) -> str: def __search_for_descrip_string(self, moving_window: List[str]) -> str:
current_idx = self.moving_window_center_idx - 1 return self._search_for_descrip_string_generic(
# Look at the line above first moving_window=moving_window, break_pattern=r"^[\s]*static const(?:expr)? ReturnValue_t"
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(r"^[\s]*static const(?:expr)? ReturnValue_t", 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)
def __handle_line_reading(self, line, file_name, print_truncated_entries: bool): def __handle_line_reading(self, line, file_name, print_truncated_entries: bool):
newline = line newline = line
@ -327,9 +285,10 @@ class ReturnValueParser(FileParser):
def export_to_file(filename: str, list_of_entries: dict, file_separator: str): def export_to_file(filename: str, list_of_entries: dict, file_separator: str):
file = open(filename, "w") file = open(filename, "w")
for entry in list_of_entries.items(): for entry in list_of_entries.items():
file.write(hex(entry[0]) + file_separator + entry[1][0] + file_separator + entry[1][1] + file.write(
file_separator + entry[1][2] + file_separator hex(entry[0]) + file_separator + entry[1][0] + file_separator + entry[1][1] +
+ entry[1][3] + file_separator + entry[1][4] + '\n') file_separator + entry[1][2] + file_separator + entry[1][3] + file_separator + entry[1][4] + '\n'
)
file.close() file.close()
def build_checked_string(self, first_part, second_part, max_string_len: int, def build_checked_string(self, first_part, second_part, max_string_len: int,