eive-obsw/generators/objects/objects.py

143 lines
4.5 KiB
Python
Raw Normal View History

2021-08-02 11:49:00 +02:00
"""Part of the Mission Information Base Exporter for the SOURCE project by KSat.
2021-05-17 18:56:38 +02:00
Object exporter.
"""
import datetime
2023-02-09 15:14:21 +01:00
import logging
2022-03-01 18:03:50 +01:00
import os
2022-06-20 18:05:05 +02:00
from pathlib import Path
2021-05-17 18:56:38 +02:00
2022-03-01 18:03:50 +01:00
from fsfwgen.objects.objects import (
sql_object_exporter,
ObjectDefinitionParser,
write_translation_file,
export_object_file,
write_translation_header_file,
)
2021-06-08 17:10:47 +02:00
from fsfwgen.utility.printer import PrettyPrinter
2022-03-01 18:03:50 +01:00
from fsfwgen.utility.file_management import copy_file
2021-05-17 18:56:38 +02:00
2021-08-02 11:49:00 +02:00
from definitions import BspType, DATABASE_NAME, OBSW_ROOT_DIR, ROOT_DIR
2021-05-17 18:56:38 +02:00
2023-02-09 15:14:21 +01:00
_LOGGER = logging.getLogger(__name__)
2021-05-17 18:56:38 +02:00
DATE_TODAY = datetime.datetime.now()
DATE_STRING_FULL = DATE_TODAY.strftime("%Y-%m-%d %H:%M:%S")
GENERATE_CSV = True
MOVE_CSV = True
GENERATE_CPP = True
COPY_CPP = True
2021-05-17 19:12:25 +02:00
GENERATE_HEADER = True
2023-02-09 15:51:36 +01:00
PRINT_OBJECTS = False
2021-05-17 19:12:25 +02:00
2023-02-09 15:14:21 +01:00
class BspConfig:
def __init__(self, bsp_select: BspType):
self.bsp_select = bsp_select
self.bsp_dir_name = bsp_select.value
if (
self.bsp_select == BspType.BSP_Q7S
or self.bsp_select == BspType.BSP_LINUX_BOARD
):
self.fsfw_config_root = f"{OBSW_ROOT_DIR}/linux/fsfwconfig"
else:
self.fsfw_config_root = f"{OBSW_ROOT_DIR}/{self.bsp_dir_name}/fsfwconfig"
self.cpp_copy_dest = Path(f"{self.fsfw_config_root}/objects/")
self.csv_obj_filename = f"{ROOT_DIR}/{self.bsp_dir_name}_objects.csv"
self.objects_path = Path(f"{self.fsfw_config_root}/objects/systemObjectList.h")
self.objects_defs = [
self.objects_path,
FRAMEWORK_OBJECT_PATH,
COMMON_OBJECTS_PATH,
]
2021-05-17 19:03:37 +02:00
2021-05-17 20:03:56 +02:00
EXPORT_TO_SQL = True
2022-03-01 18:03:50 +01:00
CPP_FILENAME = f"{os.path.dirname(os.path.realpath(__file__))}//translateObjects.cpp"
CPP_H_FILENAME = f"{os.path.dirname(os.path.realpath(__file__))}//translateObjects.h"
2022-11-29 16:55:55 +01:00
CSV_COPY_DEST = f"{OBSW_ROOT_DIR}/tmtc/eive_tmtc/config/objects.csv"
2021-05-17 18:56:38 +02:00
FILE_SEPARATOR = ";"
2022-06-20 18:05:05 +02:00
FRAMEWORK_OBJECT_PATH = Path(
2022-03-01 18:03:50 +01:00
f"{OBSW_ROOT_DIR}/fsfw/src/fsfw/objectmanager/frameworkObjects.h"
)
2022-09-27 09:33:34 +02:00
COMMON_OBJECTS_PATH = Path(f"{OBSW_ROOT_DIR}/common/config/eive/objects.h")
2021-05-17 18:56:38 +02:00
SQL_DELETE_OBJECTS_CMD = """
DROP TABLE IF EXISTS Objects
"""
SQL_CREATE_OBJECTS_CMD = """
CREATE TABLE IF NOT EXISTS Objects(
id INTEGER PRIMARY KEY,
objectid TEXT,
name TEXT
)
"""
SQL_INSERT_INTO_OBJECTS_CMD = """
INSERT INTO Objects(objectid, name)
VALUES(?,?)
"""
2023-02-09 15:51:36 +01:00
def parse_objects(bsp_select: BspType, copy_to_eive_tmtc: bool):
2023-02-09 15:14:21 +01:00
cfg = BspConfig(bsp_select)
2021-05-17 18:56:38 +02:00
# fetch objects
2023-02-09 15:14:21 +01:00
object_parser = ObjectDefinitionParser(cfg.objects_defs)
2021-05-17 18:56:38 +02:00
subsystem_definitions = object_parser.parse_files()
# id_subsystem_definitions.update(framework_subsystem_definitions)
list_items = sorted(subsystem_definitions.items())
2023-02-09 15:14:21 +01:00
_LOGGER.info(f"ObjectParser: Number of objects: {len(list_items)}")
2021-08-02 11:49:00 +02:00
2023-02-09 15:51:36 +01:00
if PRINT_OBJECTS:
2021-08-02 11:49:00 +02:00
PrettyPrinter.pprint(list_items)
2023-02-09 15:51:36 +01:00
handle_file_export(cfg, list_items, copy_to_eive_tmtc)
2021-08-02 11:49:00 +02:00
if EXPORT_TO_SQL:
2023-02-09 15:14:21 +01:00
_LOGGER.info("ObjectParser: Exporting to SQL")
2021-08-02 11:49:00 +02:00
sql_object_exporter(
2022-03-01 18:03:50 +01:00
object_table=list_items,
delete_cmd=SQL_DELETE_OBJECTS_CMD,
2021-08-02 11:49:00 +02:00
insert_cmd=SQL_INSERT_INTO_OBJECTS_CMD,
2022-03-01 18:03:50 +01:00
create_cmd=SQL_CREATE_OBJECTS_CMD,
db_filename=f"{ROOT_DIR}/{DATABASE_NAME}",
2021-08-02 11:49:00 +02:00
)
2021-05-17 18:56:38 +02:00
2023-02-09 15:51:36 +01:00
def handle_file_export(cfg: BspConfig, list_items, copy_to_eive_tmtc: bool):
2021-05-17 18:56:38 +02:00
if GENERATE_CPP:
2023-02-09 15:14:21 +01:00
_LOGGER.info("ObjectParser: Generating C++ translation file")
2021-08-02 11:49:00 +02:00
write_translation_file(
2022-03-01 18:03:50 +01:00
filename=CPP_FILENAME,
list_of_entries=list_items,
date_string_full=DATE_STRING_FULL,
2021-08-02 11:49:00 +02:00
)
2021-05-17 18:56:38 +02:00
if COPY_CPP:
2023-02-09 15:14:21 +01:00
_LOGGER.info(
"ObjectParser: Copying object file to " + str(cfg.cpp_copy_dest)
)
copy_file(Path(CPP_FILENAME), cfg.cpp_copy_dest)
2021-05-17 19:12:25 +02:00
if GENERATE_HEADER:
write_translation_header_file(filename=CPP_H_FILENAME)
2023-02-09 15:14:21 +01:00
copy_file(filename=Path(CPP_H_FILENAME), destination=cfg.cpp_copy_dest)
2021-05-17 18:56:38 +02:00
if GENERATE_CSV:
2023-02-09 15:14:21 +01:00
_LOGGER.info("ObjectParser: Generating text export")
2021-08-02 11:49:00 +02:00
export_object_file(
2023-02-09 15:14:21 +01:00
filename=cfg.csv_obj_filename,
2022-03-01 18:03:50 +01:00
object_list=list_items,
file_separator=FILE_SEPARATOR,
2021-08-02 11:49:00 +02:00
)
2023-02-09 15:51:36 +01:00
if copy_to_eive_tmtc:
_LOGGER.info(
f"ObjectParser: Copying CSV file to {CSV_COPY_DEST}"
)
copy_file(
filename=Path(cfg.csv_obj_filename),
destination=Path(CSV_COPY_DEST),
delete_existing_file=True,
)