eive-obsw/generators/objects/objects.py

105 lines
3.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
2021-08-02 11:49:00 +02:00
from fsfwgen.core import get_console_logger
from fsfwgen.objects.objects import sql_object_exporter, ObjectDefinitionParser, \
write_translation_file, \
2021-05-17 19:12:25 +02:00
export_object_file, write_translation_header_file
2021-06-08 17:10:47 +02:00
from fsfwgen.utility.printer import PrettyPrinter
from fsfwgen.utility.file_management import copy_file, move_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
2021-08-02 11:49:00 +02:00
LOGGER = get_console_logger()
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
2021-05-17 18:56:38 +02:00
BSP_SELECT = BspType.BSP_Q7S
BSP_DIR_NAME = BSP_SELECT.value
2021-05-17 19:03:37 +02:00
if BSP_SELECT == BspType.BSP_Q7S or BSP_SELECT == BspType.BSP_LINUX_BOARD:
2021-08-02 11:49:00 +02:00
FSFW_CONFIG_ROOT = f"{OBSW_ROOT_DIR}/linux/fsfwconfig"
2021-05-17 19:03:37 +02:00
else:
2021-08-02 11:49:00 +02:00
FSFW_CONFIG_ROOT = f"{OBSW_ROOT_DIR}/{BSP_DIR_NAME}/fsfwconfig"
2021-05-17 19:03:37 +02:00
2021-05-17 20:03:56 +02:00
EXPORT_TO_SQL = True
CPP_COPY_DESTINATION = f"{FSFW_CONFIG_ROOT}/objects/"
2021-08-02 11:49:00 +02:00
CSV_MOVE_DESTINATION = f"{ROOT_DIR}"
CPP_FILENAME = f'{__package__}/translateObjects.cpp'
CPP_H_FILENAME = f'{__package__}/translateObjects.h'
2021-05-18 16:16:02 +02:00
CSV_OBJECT_FILENAME = f"{BSP_SELECT.value}_objects.csv"
2021-05-17 18:56:38 +02:00
FILE_SEPARATOR = ";"
2021-05-18 16:49:24 +02:00
OBJECTS_PATH = f"{FSFW_CONFIG_ROOT}/objects/systemObjectList.h"
2021-08-02 11:49:00 +02:00
FRAMEWORK_OBJECT_PATH = f'{OBSW_ROOT_DIR}/fsfw/src/fsfw/objectmanager/frameworkObjects.h'
COMMON_OBJECTS_PATH = f'{OBSW_ROOT_DIR}/common/config/commonObjects.h'
2021-05-18 16:49:24 +02:00
OBJECTS_DEFINITIONS = [OBJECTS_PATH, FRAMEWORK_OBJECT_PATH, COMMON_OBJECTS_PATH]
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(?,?)
"""
2021-08-02 11:49:00 +02:00
def parse_objects(print_object_list: bool = True):
2021-05-17 18:56:38 +02:00
# fetch objects
object_parser = ObjectDefinitionParser(OBJECTS_DEFINITIONS)
subsystem_definitions = object_parser.parse_files()
# id_subsystem_definitions.update(framework_subsystem_definitions)
list_items = sorted(subsystem_definitions.items())
2021-08-02 11:49:00 +02:00
LOGGER.info(f'ObjectParser: Number of objects: {len(list_items)}')
if print_object_list:
PrettyPrinter.pprint(list_items)
handle_file_export(list_items)
if EXPORT_TO_SQL:
LOGGER.info('ObjectParser: Exporting to SQL')
sql_object_exporter(
object_table=list_items, delete_cmd=SQL_DELETE_OBJECTS_CMD,
insert_cmd=SQL_INSERT_INTO_OBJECTS_CMD,
create_cmd=SQL_CREATE_OBJECTS_CMD, db_filename=f"{ROOT_DIR}/{DATABASE_NAME}"
)
2021-05-17 18:56:38 +02:00
def handle_file_export(list_items):
if GENERATE_CPP:
2021-08-02 11:49:00 +02:00
LOGGER.info('ObjectParser: Generating translation C++ file')
write_translation_file(
filename=CPP_FILENAME, list_of_entries=list_items, date_string_full=DATE_STRING_FULL
)
2021-05-17 18:56:38 +02:00
if COPY_CPP:
print("ObjectParser: Copying object file to " + CPP_COPY_DESTINATION)
copy_file(CPP_FILENAME, CPP_COPY_DESTINATION)
2021-05-17 19:12:25 +02:00
if GENERATE_HEADER:
write_translation_header_file(filename=CPP_H_FILENAME)
copy_file(filename=CPP_H_FILENAME, destination=CPP_COPY_DESTINATION)
2021-05-17 18:56:38 +02:00
if GENERATE_CSV:
print("ObjectParser: Generating text export.")
2021-08-02 11:49:00 +02:00
export_object_file(
filename=CSV_OBJECT_FILENAME, object_list=list_items, file_separator=FILE_SEPARATOR
)