From 313619cc08ce711a6deafdf8cd563c6c8779f8c4 Mon Sep 17 00:00:00 2001 From: AndrewI26 <105824138+AndrewI26@users.noreply.github.com> Date: Wed, 6 Nov 2024 13:58:24 -0500 Subject: [PATCH 01/12] changed jninja generation function --- docs/includes/gen_glossary.py | 1 - scripts/cangen/cangen/can_generator.py | 61 ++++++++++++++++---------- scripts/cangen/cangen/main.py | 4 +- 3 files changed, 39 insertions(+), 27 deletions(-) diff --git a/docs/includes/gen_glossary.py b/docs/includes/gen_glossary.py index ccd3f5c1..205b8556 100644 --- a/docs/includes/gen_glossary.py +++ b/docs/includes/gen_glossary.py @@ -22,7 +22,6 @@ glossary: list[dict] = [] with open(HERE / file_glossary_input, "r") as f: - for idx, line in enumerate(f.readlines()): match = re.match(term_pattern, line) if match: diff --git a/scripts/cangen/cangen/can_generator.py b/scripts/cangen/cangen/can_generator.py index 2b8348ed..29ec73ae 100644 --- a/scripts/cangen/cangen/can_generator.py +++ b/scripts/cangen/cangen/can_generator.py @@ -9,7 +9,7 @@ import re import shutil import time -from typing import Dict, List, Tuple +from typing import Dict, List, Tuple, Callable, Any import numpy as np from cantools.database import Database, Message, Signal @@ -171,26 +171,34 @@ def _camel_to_snake(text): return re.sub("([a-z0-9])([A-Z])", r"\1_\2", s1).lower() -def _generate_from_jinja2_template( - template_path: str, output_path: str, context_dict: dict +def _create_jninja_environment( + package_name: str, filters: dict[str, Callable[..., Any]] ): - # Create the environment with trim_blocks and lstrip_blocks settings env = Environment( - loader=PackageLoader("cangen"), trim_blocks=True, lstrip_blocks=True + loader=PackageLoader(package_name), trim_blocks=True, lstrip_blocks=True ) + for filter_name, filter_function in filters: + env.filters[filter_name] = filter_function - # Register the camel_to_snake filter - env.filters["camel_to_snake"] = _camel_to_snake - env.filters["decimal_to_hex"] = hex + return env - # Load and render template - template = env.get_template(template_path) - rendered_code = template.render(**context_dict) - with open(output_path, "w") as output_file: - output_file.write(rendered_code) +def _generate_from_jinja2_templates( + template_paths: List[str], output_paths: List[str], context: dict +): + env = _create_jninja_environment( + "cangen", {"camel_to_snake": _camel_to_snake, "decimal_to_hex": hex} + ) + + # Load and render templates + for template_path, output_path in zip(template_paths, output_paths): + template = env.get_template(template_path) + rendered_code = template.render(**context) - logger.info(f"Rendered code written to '{os.path.abspath(output_path)}'") + with open(output_path, "w") as output_file: + output_file.write(rendered_code) + + logger.info(f"Rendered code written to '{os.path.abspath(output_path)}'") def generate_code(bus: Bus, config: Config): @@ -220,17 +228,17 @@ def generate_code(bus: Bus, config: Config): "bus_name": bus.bus_name, } - logger.debug("Generating code for can messages") - _generate_from_jinja2_template( + logger.debug("Generating code for can messages and msg registry.") + template_file_names = [ CAN_MESSAGES_TEMPLATE_FILENAME, - os.path.join(config.output_dir, bus.bus_name.lower() + CAN_MESSAGES_FILE_NAME), - context, - ) - - logger.debug("Generating code for msg registry") - _generate_from_jinja2_template( MSG_REGISTRY_TEMPLATE_FILENAME, - os.path.join(config.output_dir, bus.bus_name.lower() + MSG_REGISTRY_FILE_NAME), + ] + _generate_from_jinja2_templates( + template_file_names, + [ + os.path.join(config.output_dir, bus.bus_name.lower() + file_name) + for file_name in template_file_names + ], context, ) @@ -240,7 +248,10 @@ def generate_code(bus: Bus, config: Config): def _prepare_output_directory(output_dir): """Deletes previously generated files and creates a gitignore for the directory""" if os.path.exists(output_dir): + logger.info("Deleting previously generated code") shutil.rmtree(output_dir) + + logger.info("Creating generated/can folder") os.makedirs(output_dir, exist_ok=True) gitignore_path = os.path.join(output_dir, ".gitignore") @@ -248,7 +259,9 @@ def _prepare_output_directory(output_dir): f.write("*") -def generate_can_from_dbc(project_folder_name: str): +def generate_can_for_project(project_folder_name: str): + """Generates C code for a given project. + Ensure the project contains a config.yaml file which specifies the relative path to its corresponding dbc file.""" os.chdir(project_folder_name) config = Config.from_yaml("config.yaml") diff --git a/scripts/cangen/cangen/main.py b/scripts/cangen/cangen/main.py index f2912e7f..efe1ddbf 100644 --- a/scripts/cangen/cangen/main.py +++ b/scripts/cangen/cangen/main.py @@ -6,7 +6,7 @@ import argparse import logging -from .can_generator import generate_can_from_dbc +from .can_generator import generate_can_for_project logging.basicConfig(level="INFO", format="%(levelname)-8s| (%(name)s) %(message)s") logger = logging.getLogger(__name__) @@ -39,4 +39,4 @@ def main(): } logger.setLevel(cmake_to_python_level[args.level]) - generate_can_from_dbc(args.project) + generate_can_for_project(args.project) From 0698c8a82b3350cb9388de6a94a4442469505727 Mon Sep 17 00:00:00 2001 From: AndrewI26 <105824138+AndrewI26@users.noreply.github.com> Date: Wed, 6 Nov 2024 14:30:26 -0500 Subject: [PATCH 02/12] changes jninja functions --- scripts/cangen/cangen/can_generator.py | 18 ++++++++---------- 1 file changed, 8 insertions(+), 10 deletions(-) diff --git a/scripts/cangen/cangen/can_generator.py b/scripts/cangen/cangen/can_generator.py index 29ec73ae..e80e9b68 100644 --- a/scripts/cangen/cangen/can_generator.py +++ b/scripts/cangen/cangen/can_generator.py @@ -25,8 +25,8 @@ MSG_REGISTRY_FILE_NAME = "_msg_registry.h" CAN_MESSAGES_FILE_NAME = "_can_messages.h" -CAN_MESSAGES_TEMPLATE_FILENAME = "can_messages.h.jinja2" -MSG_REGISTRY_TEMPLATE_FILENAME = "msg_registry.h.jinja2" +TEMPLATE_FILE_NAMES = ["can_messages.h.jinja2", "msg_registry.h.jinja2"] + def _parse_dbc_files(dbc_file: str) -> Database: @@ -177,11 +177,13 @@ def _create_jninja_environment( env = Environment( loader=PackageLoader(package_name), trim_blocks=True, lstrip_blocks=True ) - for filter_name, filter_function in filters: + for filter_name, filter_function in filters.items(): env.filters[filter_name] = filter_function return env +def _create_output_file_name(output_dir, bus_name, template_filename): + return os.path.join(output_dir, bus_name.lower() + "_" + template_filename[:-7]) def _generate_from_jinja2_templates( template_paths: List[str], output_paths: List[str], context: dict @@ -229,15 +231,11 @@ def generate_code(bus: Bus, config: Config): } logger.debug("Generating code for can messages and msg registry.") - template_file_names = [ - CAN_MESSAGES_TEMPLATE_FILENAME, - MSG_REGISTRY_TEMPLATE_FILENAME, - ] _generate_from_jinja2_templates( - template_file_names, + TEMPLATE_FILE_NAMES, [ - os.path.join(config.output_dir, bus.bus_name.lower() + file_name) - for file_name in template_file_names + _create_output_file_name(config.output_dir, bus.bus_name, file_name) + for file_name in TEMPLATE_FILE_NAMES ], context, ) From ef1195b3b9fb0146cc3d472990c5353406567a13 Mon Sep 17 00:00:00 2001 From: AndrewI26 <105824138+AndrewI26@users.noreply.github.com> Date: Thu, 7 Nov 2024 14:45:41 -0500 Subject: [PATCH 03/12] inline jninja generation --- scripts/cangen/cangen/can_generator.py | 39 ++++++++------------------ 1 file changed, 12 insertions(+), 27 deletions(-) diff --git a/scripts/cangen/cangen/can_generator.py b/scripts/cangen/cangen/can_generator.py index e80e9b68..2b9fd480 100644 --- a/scripts/cangen/cangen/can_generator.py +++ b/scripts/cangen/cangen/can_generator.py @@ -182,26 +182,8 @@ def _create_jninja_environment( return env -def _create_output_file_name(output_dir, bus_name, template_filename): - return os.path.join(output_dir, bus_name.lower() + "_" + template_filename[:-7]) - -def _generate_from_jinja2_templates( - template_paths: List[str], output_paths: List[str], context: dict -): - env = _create_jninja_environment( - "cangen", {"camel_to_snake": _camel_to_snake, "decimal_to_hex": hex} - ) - - # Load and render templates - for template_path, output_path in zip(template_paths, output_paths): - template = env.get_template(template_path) - rendered_code = template.render(**context) - - with open(output_path, "w") as output_file: - output_file.write(rendered_code) - - logger.info(f"Rendered code written to '{os.path.abspath(output_path)}'") - +def _create_output_file_name(output_dir: str, bus_name: str, template_file_name: str): + return os.path.join(output_dir, bus_name.lower() + "_" + template_file_name[:-7]) def generate_code(bus: Bus, config: Config): """ @@ -231,14 +213,17 @@ def generate_code(bus: Bus, config: Config): } logger.debug("Generating code for can messages and msg registry.") - _generate_from_jinja2_templates( - TEMPLATE_FILE_NAMES, - [ - _create_output_file_name(config.output_dir, bus.bus_name, file_name) - for file_name in TEMPLATE_FILE_NAMES - ], - context, + + env = _create_jninja_environment( + "cangen", {"camel_to_snake": _camel_to_snake, "decimal_to_hex": hex} ) + for template_file_name in TEMPLATE_FILE_NAMES: + template = env.get_template(template_file_name) + rendered_code = template.render(**context) + output_file_name = _create_output_file_name(config.output_dir, bus.bus_name, template_file_name) + with open(output_file_name, "w") as output_file: + output_file.write(rendered_code) + logger.info(f"Rendered code written to '{os.path.abspath(output_file_name)}'") logger.info("Code generation complete") From 4737c8383069ec748de6c70ec1f74d9a31435c0a Mon Sep 17 00:00:00 2001 From: AndrewI26 <105824138+AndrewI26@users.noreply.github.com> Date: Thu, 7 Nov 2024 15:06:28 -0500 Subject: [PATCH 04/12] changed function names cangen --- scripts/cangen/cangen/can_generator.py | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/scripts/cangen/cangen/can_generator.py b/scripts/cangen/cangen/can_generator.py index 2b9fd480..1ff86557 100644 --- a/scripts/cangen/cangen/can_generator.py +++ b/scripts/cangen/cangen/can_generator.py @@ -28,7 +28,6 @@ TEMPLATE_FILE_NAMES = ["can_messages.h.jinja2", "msg_registry.h.jinja2"] - def _parse_dbc_files(dbc_file: str) -> Database: logger.info(f"Parsing DBC files: {dbc_file}") can_db = Database() @@ -185,7 +184,7 @@ def _create_jninja_environment( def _create_output_file_name(output_dir: str, bus_name: str, template_file_name: str): return os.path.join(output_dir, bus_name.lower() + "_" + template_file_name[:-7]) -def generate_code(bus: Bus, config: Config): +def _generate_code(bus: Bus, config: Config): """ Parses DBC files, extracts information, and generates code using Jinja2 templates. @@ -228,7 +227,7 @@ def generate_code(bus: Bus, config: Config): logger.info("Code generation complete") -def _prepare_output_directory(output_dir): +def _prepare_output_directory(output_dir: str): """Deletes previously generated files and creates a gitignore for the directory""" if os.path.exists(output_dir): logger.info("Deleting previously generated code") @@ -244,11 +243,11 @@ def _prepare_output_directory(output_dir): def generate_can_for_project(project_folder_name: str): """Generates C code for a given project. - Ensure the project contains a config.yaml file which specifies the relative path to its corresponding dbc file.""" + Ensure the project folder contains a config.yaml file which specifies the relative path to its corresponding dbc file.""" os.chdir(project_folder_name) config = Config.from_yaml("config.yaml") _prepare_output_directory(config.output_dir) for bus in config.busses: - generate_code(bus, config) + _generate_code(bus, config) From 84dd4c662636bae934b837a07490b6313b76cb04 Mon Sep 17 00:00:00 2001 From: AndrewI26 <105824138+AndrewI26@users.noreply.github.com> Date: Sun, 10 Nov 2024 19:24:55 -0500 Subject: [PATCH 05/12] moved jninja generation into its own function --- scripts/cangen/cangen/can_generator.py | 24 +++++++++++++++++------- 1 file changed, 17 insertions(+), 7 deletions(-) diff --git a/scripts/cangen/cangen/can_generator.py b/scripts/cangen/cangen/can_generator.py index 1ff86557..c5b468f1 100644 --- a/scripts/cangen/cangen/can_generator.py +++ b/scripts/cangen/cangen/can_generator.py @@ -27,6 +27,8 @@ TEMPLATE_FILE_NAMES = ["can_messages.h.jinja2", "msg_registry.h.jinja2"] +PACKAGE_NAME = "cangen" + def _parse_dbc_files(dbc_file: str) -> Database: logger.info(f"Parsing DBC files: {dbc_file}") @@ -184,6 +186,15 @@ def _create_jninja_environment( def _create_output_file_name(output_dir: str, bus_name: str, template_file_name: str): return os.path.join(output_dir, bus_name.lower() + "_" + template_file_name[:-7]) +def _generate_jninja_template(template_file_name, output_path, context, env): + template = env.get_template(template_file_name) + rendered_code = template.render(**context) + + output_file_name = output_path + with open(output_file_name, "w") as output_file: + output_file.write(rendered_code) + logger.info(f"Rendered code written to '{os.path.abspath(output_file_name)}'") + def _generate_code(bus: Bus, config: Config): """ Parses DBC files, extracts information, and generates code using Jinja2 @@ -214,15 +225,14 @@ def _generate_code(bus: Bus, config: Config): logger.debug("Generating code for can messages and msg registry.") env = _create_jninja_environment( - "cangen", {"camel_to_snake": _camel_to_snake, "decimal_to_hex": hex} + PACKAGE_NAME, {"camel_to_snake": _camel_to_snake, "decimal_to_hex": hex} ) for template_file_name in TEMPLATE_FILE_NAMES: - template = env.get_template(template_file_name) - rendered_code = template.render(**context) - output_file_name = _create_output_file_name(config.output_dir, bus.bus_name, template_file_name) - with open(output_file_name, "w") as output_file: - output_file.write(rendered_code) - logger.info(f"Rendered code written to '{os.path.abspath(output_file_name)}'") + _generate_jninja_template( + template_file_name, + _create_output_file_name(config.output_dir, bus.bus_name, template_file_name), + context, + env) logger.info("Code generation complete") From 2b1d480df14950da0a1b179e6a37bbd9b23cb328 Mon Sep 17 00:00:00 2001 From: AndrewI26 <105824138+AndrewI26@users.noreply.github.com> Date: Sun, 10 Nov 2024 20:15:09 -0500 Subject: [PATCH 06/12] added -v flag and changed logging levels --- scripts/cangen/cangen/main.py | 28 +++++++++++++++++++--------- 1 file changed, 19 insertions(+), 9 deletions(-) diff --git a/scripts/cangen/cangen/main.py b/scripts/cangen/cangen/main.py index efe1ddbf..ff4dd0e5 100644 --- a/scripts/cangen/cangen/main.py +++ b/scripts/cangen/cangen/main.py @@ -16,20 +16,22 @@ def parse(): parser = argparse.ArgumentParser(description="DBC to C code generator") parser.add_argument("project", type=str, help="Name of the project") parser.add_argument( - "--log-level", + "-v", + "--verbose", + dest="is_verbose", + action="store_true", + help="Enable verbose output", + ) + parser.add_argument( + "--cmake-log-level", dest="level", choices=["STATUS", "INFO", "VERBOSE", "DEBUG"], default="INFO", help="Log verbosity threshold", ) - # If parsing fails (ex incorrect or no arguments provided) then this exits with - # code 2. - return parser.parse_args() - - -def main(): - args = parse() + # If parsing fails (ex incorrect or no arguments provided) then this code will be 2 + args = parser.parse_args() cmake_to_python_level = { "STATUS": "INFO", @@ -37,6 +39,14 @@ def main(): "VERBOSE": "DEBUG", "DEBUG": "DEBUG", } - logger.setLevel(cmake_to_python_level[args.level]) + + args.level = cmake_to_python_level['VERBOSE'] if args.is_verbose else cmake_to_python_level[args.level] + return args + + +def main(): + args = parse() + + logger.setLevel(args.level) generate_can_for_project(args.project) From b0a859ef40de58d150b606442c5f413520fe73df Mon Sep 17 00:00:00 2001 From: AndrewI26 <105824138+AndrewI26@users.noreply.github.com> Date: Sun, 10 Nov 2024 20:18:10 -0500 Subject: [PATCH 07/12] ruff formated --- scripts/cangen/cangen/can_generator.py | 12 +++++++++--- scripts/cangen/cangen/main.py | 6 +++++- 2 files changed, 14 insertions(+), 4 deletions(-) diff --git a/scripts/cangen/cangen/can_generator.py b/scripts/cangen/cangen/can_generator.py index c5b468f1..6e29c03d 100644 --- a/scripts/cangen/cangen/can_generator.py +++ b/scripts/cangen/cangen/can_generator.py @@ -183,9 +183,11 @@ def _create_jninja_environment( return env + def _create_output_file_name(output_dir: str, bus_name: str, template_file_name: str): return os.path.join(output_dir, bus_name.lower() + "_" + template_file_name[:-7]) + def _generate_jninja_template(template_file_name, output_path, context, env): template = env.get_template(template_file_name) rendered_code = template.render(**context) @@ -195,6 +197,7 @@ def _generate_jninja_template(template_file_name, output_path, context, env): output_file.write(rendered_code) logger.info(f"Rendered code written to '{os.path.abspath(output_file_name)}'") + def _generate_code(bus: Bus, config: Config): """ Parses DBC files, extracts information, and generates code using Jinja2 @@ -229,10 +232,13 @@ def _generate_code(bus: Bus, config: Config): ) for template_file_name in TEMPLATE_FILE_NAMES: _generate_jninja_template( - template_file_name, - _create_output_file_name(config.output_dir, bus.bus_name, template_file_name), + template_file_name, + _create_output_file_name( + config.output_dir, bus.bus_name, template_file_name + ), context, - env) + env, + ) logger.info("Code generation complete") diff --git a/scripts/cangen/cangen/main.py b/scripts/cangen/cangen/main.py index ff4dd0e5..8aff353d 100644 --- a/scripts/cangen/cangen/main.py +++ b/scripts/cangen/cangen/main.py @@ -40,7 +40,11 @@ def parse(): "DEBUG": "DEBUG", } - args.level = cmake_to_python_level['VERBOSE'] if args.is_verbose else cmake_to_python_level[args.level] + args.level = ( + cmake_to_python_level["VERBOSE"] + if args.is_verbose + else cmake_to_python_level[args.level] + ) return args From 250c4de164b127a4f4bf92c63ffa8282f2cf14ec Mon Sep 17 00:00:00 2001 From: AndrewI26 <105824138+AndrewI26@users.noreply.github.com> Date: Mon, 11 Nov 2024 15:52:13 -0500 Subject: [PATCH 08/12] refactored jninja environment setup --- firmware/cmake/cangen.cmake | 2 +- scripts/cangen/cangen/can_generator.py | 61 +++++++++----------------- scripts/cangen/cangen/main.py | 5 ++- 3 files changed, 24 insertions(+), 44 deletions(-) diff --git a/firmware/cmake/cangen.cmake b/firmware/cmake/cangen.cmake index 6f308158..c881fdcf 100644 --- a/firmware/cmake/cangen.cmake +++ b/firmware/cmake/cangen.cmake @@ -5,7 +5,7 @@ cmake_language(GET_MESSAGE_LOG_LEVEL LOG_LEVEL) # cangen is provided by racecar/scripts/cangen add_custom_target( generated_can - COMMAND "cangen" ${CMAKE_CURRENT_SOURCE_DIR} "--log-level=${LOG_LEVEL}" + COMMAND "cangen" ${CMAKE_CURRENT_SOURCE_DIR} "--cmake-log-level=${LOG_LEVEL}" DEPENDS ${CAN_DEPENDENCIES} WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR} COMMENT Generating CAN code from DBCs diff --git a/scripts/cangen/cangen/can_generator.py b/scripts/cangen/cangen/can_generator.py index 6e29c03d..2c060a42 100644 --- a/scripts/cangen/cangen/can_generator.py +++ b/scripts/cangen/cangen/can_generator.py @@ -9,7 +9,7 @@ import re import shutil import time -from typing import Dict, List, Tuple, Callable, Any +from typing import Dict, List, Tuple import numpy as np from cantools.database import Database, Message, Signal @@ -22,13 +22,9 @@ EIGHT_BITS = 8 EIGHT_BYTES = 8 TOTAL_BITS = EIGHT_BITS * EIGHT_BYTES -MSG_REGISTRY_FILE_NAME = "_msg_registry.h" -CAN_MESSAGES_FILE_NAME = "_can_messages.h" TEMPLATE_FILE_NAMES = ["can_messages.h.jinja2", "msg_registry.h.jinja2"] -PACKAGE_NAME = "cangen" - def _parse_dbc_files(dbc_file: str) -> Database: logger.info(f"Parsing DBC files: {dbc_file}") @@ -172,33 +168,11 @@ def _camel_to_snake(text): return re.sub("([a-z0-9])([A-Z])", r"\1_\2", s1).lower() -def _create_jninja_environment( - package_name: str, filters: dict[str, Callable[..., Any]] -): - env = Environment( - loader=PackageLoader(package_name), trim_blocks=True, lstrip_blocks=True - ) - for filter_name, filter_function in filters.items(): - env.filters[filter_name] = filter_function - - return env - - -def _create_output_file_name(output_dir: str, bus_name: str, template_file_name: str): - return os.path.join(output_dir, bus_name.lower() + "_" + template_file_name[:-7]) - +def _create_output_file_name(bus_name: str, template_file_name: str) -> str: + return os.path.join(bus_name.lower() + "_" + template_file_name.removesuffix(".jninja2")) -def _generate_jninja_template(template_file_name, output_path, context, env): - template = env.get_template(template_file_name) - rendered_code = template.render(**context) - output_file_name = output_path - with open(output_file_name, "w") as output_file: - output_file.write(rendered_code) - logger.info(f"Rendered code written to '{os.path.abspath(output_file_name)}'") - - -def _generate_code(bus: Bus, config: Config): +def _generate_code(bus: Bus): """ Parses DBC files, extracts information, and generates code using Jinja2 templates. @@ -227,20 +201,24 @@ def _generate_code(bus: Bus, config: Config): logger.debug("Generating code for can messages and msg registry.") - env = _create_jninja_environment( - PACKAGE_NAME, {"camel_to_snake": _camel_to_snake, "decimal_to_hex": hex} + env = Environment( + loader=PackageLoader(__package__), trim_blocks=True, lstrip_blocks=True ) + env.filters["decimal_to_hex"] = hex + env.filters["camel_to_snake"] = _camel_to_snake + for template_file_name in TEMPLATE_FILE_NAMES: - _generate_jninja_template( - template_file_name, - _create_output_file_name( - config.output_dir, bus.bus_name, template_file_name - ), - context, - env, - ) + template = env.get_template(template_file_name) + rendered_code = template.render(**context) + + + + output_file_name = _create_output_file_name(bus.bus_name, template_file_name) + with open(_create_output_file_name, "w") as output_file: + output_file.write(rendered_code) + logger.info(f"Rendered code written to '{os.path.abspath(output_file_name)}'") - logger.info("Code generation complete") + logger.info("Code generation complete") def _prepare_output_directory(output_dir: str): @@ -265,5 +243,6 @@ def generate_can_for_project(project_folder_name: str): _prepare_output_directory(config.output_dir) + os.chdir(config.output_dir) for bus in config.busses: _generate_code(bus, config) diff --git a/scripts/cangen/cangen/main.py b/scripts/cangen/cangen/main.py index 8aff353d..c867ac56 100644 --- a/scripts/cangen/cangen/main.py +++ b/scripts/cangen/cangen/main.py @@ -15,14 +15,15 @@ def parse(): parser = argparse.ArgumentParser(description="DBC to C code generator") parser.add_argument("project", type=str, help="Name of the project") - parser.add_argument( + group = parser.add_mutually_exclusive_group() + group.add_argument( "-v", "--verbose", dest="is_verbose", action="store_true", help="Enable verbose output", ) - parser.add_argument( + group.add_argument( "--cmake-log-level", dest="level", choices=["STATUS", "INFO", "VERBOSE", "DEBUG"], From e6fffaac98fb472e1e0bd4bc16879889fde655a1 Mon Sep 17 00:00:00 2001 From: AndrewI26 <105824138+AndrewI26@users.noreply.github.com> Date: Mon, 11 Nov 2024 15:52:58 -0500 Subject: [PATCH 09/12] formatted ruff --- scripts/cangen/cangen/can_generator.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/scripts/cangen/cangen/can_generator.py b/scripts/cangen/cangen/can_generator.py index 2c060a42..ee594cff 100644 --- a/scripts/cangen/cangen/can_generator.py +++ b/scripts/cangen/cangen/can_generator.py @@ -169,7 +169,9 @@ def _camel_to_snake(text): def _create_output_file_name(bus_name: str, template_file_name: str) -> str: - return os.path.join(bus_name.lower() + "_" + template_file_name.removesuffix(".jninja2")) + return os.path.join( + bus_name.lower() + "_" + template_file_name.removesuffix(".jninja2") + ) def _generate_code(bus: Bus): @@ -211,8 +213,6 @@ def _generate_code(bus: Bus): template = env.get_template(template_file_name) rendered_code = template.render(**context) - - output_file_name = _create_output_file_name(bus.bus_name, template_file_name) with open(_create_output_file_name, "w") as output_file: output_file.write(rendered_code) From 37459d05eeacc329db777033f5396debafe7a581 Mon Sep 17 00:00:00 2001 From: AndrewI26 <105824138+AndrewI26@users.noreply.github.com> Date: Mon, 11 Nov 2024 15:53:46 -0500 Subject: [PATCH 10/12] changed generate code --- scripts/cangen/cangen/can_generator.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/scripts/cangen/cangen/can_generator.py b/scripts/cangen/cangen/can_generator.py index ee594cff..11649fb4 100644 --- a/scripts/cangen/cangen/can_generator.py +++ b/scripts/cangen/cangen/can_generator.py @@ -245,4 +245,4 @@ def generate_can_for_project(project_folder_name: str): os.chdir(config.output_dir) for bus in config.busses: - _generate_code(bus, config) + _generate_code(bus) From ed9d002434cc5647c8b08ebb09c1256b02254d0e Mon Sep 17 00:00:00 2001 From: AndrewI26 <105824138+AndrewI26@users.noreply.github.com> Date: Mon, 11 Nov 2024 18:06:12 -0500 Subject: [PATCH 11/12] changed output_dir function --- scripts/cangen/cangen/can_generator.py | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) diff --git a/scripts/cangen/cangen/can_generator.py b/scripts/cangen/cangen/can_generator.py index 11649fb4..1530ec70 100644 --- a/scripts/cangen/cangen/can_generator.py +++ b/scripts/cangen/cangen/can_generator.py @@ -168,13 +168,12 @@ def _camel_to_snake(text): return re.sub("([a-z0-9])([A-Z])", r"\1_\2", s1).lower() -def _create_output_file_name(bus_name: str, template_file_name: str) -> str: +def _create_output_file_name(output_dir: str, bus_name: str, template_file_name: str) -> str: return os.path.join( - bus_name.lower() + "_" + template_file_name.removesuffix(".jninja2") + output_dir, bus_name.lower() + "_" + template_file_name.removesuffix(".jninja2") ) - -def _generate_code(bus: Bus): +def _generate_code(bus: Bus, output_dir: str): """ Parses DBC files, extracts information, and generates code using Jinja2 templates. @@ -213,8 +212,8 @@ def _generate_code(bus: Bus): template = env.get_template(template_file_name) rendered_code = template.render(**context) - output_file_name = _create_output_file_name(bus.bus_name, template_file_name) - with open(_create_output_file_name, "w") as output_file: + output_file_name = _create_output_file_name(output_dir, bus.bus_name, template_file_name) + with open(output_file_name, "w") as output_file: output_file.write(rendered_code) logger.info(f"Rendered code written to '{os.path.abspath(output_file_name)}'") @@ -243,6 +242,5 @@ def generate_can_for_project(project_folder_name: str): _prepare_output_directory(config.output_dir) - os.chdir(config.output_dir) for bus in config.busses: - _generate_code(bus) + _generate_code(bus, config.output_dir) From 3c042863cec4aa246e0a8990ed5805bed243f9d6 Mon Sep 17 00:00:00 2001 From: AndrewI26 <105824138+AndrewI26@users.noreply.github.com> Date: Mon, 11 Nov 2024 20:55:10 -0500 Subject: [PATCH 12/12] fixed typo --- firmware/projects/FrontController/vehicle_control_system | 1 + .../projects/debug/FrontControllerSimple/vehicle_control_system | 1 + scripts/cangen/cangen/can_generator.py | 2 +- 3 files changed, 3 insertions(+), 1 deletion(-) create mode 160000 firmware/projects/FrontController/vehicle_control_system create mode 160000 firmware/projects/debug/FrontControllerSimple/vehicle_control_system diff --git a/firmware/projects/FrontController/vehicle_control_system b/firmware/projects/FrontController/vehicle_control_system new file mode 160000 index 00000000..4fddb936 --- /dev/null +++ b/firmware/projects/FrontController/vehicle_control_system @@ -0,0 +1 @@ +Subproject commit 4fddb9369730a8cee4e74f83d71d3b1e33b8d5ea diff --git a/firmware/projects/debug/FrontControllerSimple/vehicle_control_system b/firmware/projects/debug/FrontControllerSimple/vehicle_control_system new file mode 160000 index 00000000..a04e3a93 --- /dev/null +++ b/firmware/projects/debug/FrontControllerSimple/vehicle_control_system @@ -0,0 +1 @@ +Subproject commit a04e3a93f0def3d748ca7c636cb0f226328c0a10 diff --git a/scripts/cangen/cangen/can_generator.py b/scripts/cangen/cangen/can_generator.py index 1530ec70..a84e36f0 100644 --- a/scripts/cangen/cangen/can_generator.py +++ b/scripts/cangen/cangen/can_generator.py @@ -170,7 +170,7 @@ def _camel_to_snake(text): def _create_output_file_name(output_dir: str, bus_name: str, template_file_name: str) -> str: return os.path.join( - output_dir, bus_name.lower() + "_" + template_file_name.removesuffix(".jninja2") + output_dir, bus_name.lower() + "_" + template_file_name.removesuffix(".jinja2") ) def _generate_code(bus: Bus, output_dir: str):