From a5dee6e41749508a85842a931e6f1d210aee2031 Mon Sep 17 00:00:00 2001 From: Robin Mueller Date: Tue, 21 Jun 2022 01:21:01 +0200 Subject: [PATCH] improved csv format --- fsfwgen/events/event_parser.py | 31 +++++++++++---------- fsfwgen/returnvalues/returnvalues_parser.py | 24 ++++++++++------ 2 files changed, 32 insertions(+), 23 deletions(-) diff --git a/fsfwgen/events/event_parser.py b/fsfwgen/events/event_parser.py index 4ff15b2..67361cd 100644 --- a/fsfwgen/events/event_parser.py +++ b/fsfwgen/events/event_parser.py @@ -239,19 +239,22 @@ class EventParser(FileParser): return description -def export_to_file(filename: Path, event_list: EventDictT, file_separator: str): - file = open(filename, "w") - fsep = file_separator - for entry in event_list.items(): - event_id = int(entry[0]) - event_value = entry[1] - event_id_as_hex = f"{event_id:#06x}" - file.write( - f"{event_id}{fsep}{event_id_as_hex}{fsep}{event_value.name}{fsep}" - f"{event_value.severity}{fsep}{event_value.description}" - f"{fsep}{event_value.file_name.as_posix()}\n" +def export_to_csv(filename: Path, event_list: EventDictT, col_sep: str): + with open(filename, "w") as out: + fsep = col_sep + out.write( + f"Event ID (dec){col_sep} Event ID (hex){col_sep} Name{col_sep} " + f"Severity{col_sep} Description{col_sep} File Path\n" ) - file.close() + for entry in event_list.items(): + event_id = int(entry[0]) + event_value = entry[1] + event_id_as_hex = f"{event_id:#06x}" + out.write( + f"{event_id}{fsep}{event_id_as_hex}{fsep}{event_value.name}{fsep}" + f"{event_value.severity}{fsep}{event_value.description}" + f"{fsep}{event_value.file_name.as_posix()}\n" + ) def write_translation_source_file( @@ -297,9 +300,7 @@ def handle_csv_export(file_name: Path, event_list: EventDictT, file_separator: s Generates the CSV in the same directory as the .py file and copes the CSV to another directory if specified. """ - export_to_file( - filename=file_name, event_list=event_list, file_separator=file_separator - ) + export_to_csv(filename=file_name, event_list=event_list, col_sep=file_separator) def handle_cpp_export( diff --git a/fsfwgen/returnvalues/returnvalues_parser.py b/fsfwgen/returnvalues/returnvalues_parser.py index 0147bbf..8e4323a 100644 --- a/fsfwgen/returnvalues/returnvalues_parser.py +++ b/fsfwgen/returnvalues/returnvalues_parser.py @@ -405,15 +405,23 @@ class ReturnValueParser(FileParser): self.mib_table = self.return_value_dict @staticmethod - def export_to_file(filename: Path, list_of_entries: RetvalDictT, file_sep: str): - file = open(filename, "w") - for entry in list_of_entries.items(): - file.write( - f"{entry[0]:#06x}{file_sep}{entry[1].name}{file_sep}{entry[1].description}" - f"{file_sep}{entry[1].unique_id}{file_sep}{entry[1].subsystem_name}{file_sep}" - f"{entry[1].file_name.as_posix()}\n" + def export_to_csv(filename: Path, list_of_entries: RetvalDictT, column_sep: str): + with open(filename, "w") as out: + out.write( + f"Full ID (hex){column_sep} Name{column_sep} Description{column_sep} " + f"Unique ID{column_sep} Subsytem Name{column_sep} File Path\n" ) - file.close() + for entry in list_of_entries.items(): + if column_sep == ";": + entry[1].description = entry[1].description.replace(";", ",") + elif column_sep == ",": + # Quote the description + entry[1].description = f'"{entry[1].description}"' + out.write( + f"{entry[0]:#06x}{column_sep}{entry[1].name}{column_sep}{entry[1].description}" + f"{column_sep}{entry[1].unique_id}{column_sep}{entry[1].subsystem_name}" + f"{column_sep}{entry[1].file_name.as_posix()}\n" + ) def build_checked_string( self,