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

@ -1,4 +1,5 @@
import re
from typing import List
from fsfwgen.parserbase.parser import FileParser
@ -96,21 +97,28 @@ class EventParser(FileParser):
file_name=file_name
)
def __handle_event_match(self, event_match, macro_api_match: bool, moving_window: list, file_name: str):
event_full_match = False
def __handle_event_match(
self, event_match, macro_api_match: bool, moving_window: list, file_name: str
):
if ";" in event_match.group(0):
event_full_match = self.__handle_one_line_event_match(
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 = " "
if event_full_match:
name = event_match.group(EVENT_NAME_IDX)
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)
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)
self.mib_table.update({full_id: (name, severity, description, file_name)})
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])
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):
pass
@ -208,14 +223,17 @@ def export_to_file(filename: str, event_list: list, file_separator: str):
event_value = entry[1]
file.write(
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] +
file_separator + event_value[EVENT_SOURCE_FILE_IDX] + '\n'
event_value[EVENT_ENTRY_SEVERITY_IDX] + file_separator +
event_value[EVENT_ENTRY_INFO_IDX] + file_separator + event_value[EVENT_SOURCE_FILE_IDX]
+ '\n'
)
file.close()
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")
definitions = ""
@ -224,11 +242,13 @@ def write_translation_source_file(event_list: list, date_string: str, filename:
event_id = entry[0]
event_value = entry[1]
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 += '\tdefault:\n\t\treturn "UNKNOWN_EVENT";\n'
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" * 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(
event_list: list, date_string: str, file_name: str = "translateEvents.cpp", generate_header: bool = True,
header_file_name: str = "translateEvents.h"
event_list: list, date_string: str, file_name: str = "translateEvents.cpp",
generate_header: bool = True, header_file_name: str = "translateEvents.h"
):
print("EventParser: Generating translation cpp file.")
write_translation_source_file(event_list=event_list, date_string=date_string, filename=file_name)