From 763d003f8392373cd21b3d88edb7ef4e79cddd78 Mon Sep 17 00:00:00 2001 From: Andreas Lauser Date: Fri, 26 Jan 2024 15:35:43 +0100 Subject: [PATCH 1/2] CLI: introduce a non-strict mode to all tools MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit using the non-strict mode is more robust regarding incorrect input files, bugs and unimplemented features in odxtools, but it might cause undefined behaviour. Signed-off-by: Andreas Lauser Signed-off-by: Katja Köhler --- odxtools/cli/browse.py | 2 +- odxtools/cli/main.py | 19 ++++++++++++++++++- tests/test_cli.py | 12 +++++++++--- 3 files changed, 28 insertions(+), 5 deletions(-) diff --git a/odxtools/cli/browse.py b/odxtools/cli/browse.py index cf302ec4..1c5602f2 100644 --- a/odxtools/cli/browse.py +++ b/odxtools/cli/browse.py @@ -3,9 +3,9 @@ import logging import sys from typing import List, Optional, Union -from tabulate import tabulate # TODO: switch to rich tables import PyInquirer.prompt as PI_prompt +from tabulate import tabulate # TODO: switch to rich tables from ..database import Database from ..dataobjectproperty import DataObjectProperty diff --git a/odxtools/cli/main.py b/odxtools/cli/main.py index 09adc0bb..35e3e3d2 100644 --- a/odxtools/cli/main.py +++ b/odxtools/cli/main.py @@ -3,6 +3,8 @@ import importlib from typing import Any, List +import odxtools + from ..version import __version__ as odxtools_version from .dummy_sub_parser import DummyTool @@ -31,6 +33,14 @@ def start_cli() -> None: formatter_class=argparse.RawTextHelpFormatter, ) + argparser.add_argument( + "--no-strict", + action="store_true", + default=False, + required=False, + help="Load the dataset in non-strict mode (which is more robust but might lead to undefined behavior)", + ) + argparser.add_argument( "--version", required=False, action="store_true", help="Print the odxtools version") @@ -50,4 +60,11 @@ def start_cli() -> None: for tool in tool_modules: if tool._odxtools_tool_name_ == args.subparser_name: - tool.run(args) + odxtools.exceptions.strict_mode = not args.no_strict + try: + tool.run(args) + except: + raise + finally: + odxtools.exceptions.strict_mode = False + return diff --git a/tests/test_cli.py b/tests/test_cli.py index a9a4b786..f745a57c 100644 --- a/tests/test_cli.py +++ b/tests/test_cli.py @@ -21,6 +21,7 @@ class UtilFunctions: @staticmethod def run_list_tool(path_to_pdx_file: str = "./examples/somersault.pdx", + no_strict: bool = False, ecu_variants: Optional[List[str]] = None, print_neg_responses: bool = False, ecu_services: Optional[List[str]] = None, @@ -30,6 +31,7 @@ def run_list_tool(path_to_pdx_file: str = "./examples/somersault.pdx", dump_database: bool = False) -> None: list_args = Namespace( pdx_file=path_to_pdx_file, + no_strict=no_strict, variants=ecu_variants, global_negative_responses=print_neg_responses, services=ecu_services, @@ -71,12 +73,14 @@ def run_find_tool(service_names: List[str], @staticmethod def run_compare_tool(path_to_pdx_file: str = "./examples/somersault.pdx", + no_strict: bool = False, ecu_variants: Optional[List[str]] = None, database: Optional[List[str]] = None, no_details: bool = True) -> None: compare_args = Namespace( pdx_file=path_to_pdx_file, + no_strict=no_strict, variants=ecu_variants, database=database, no_details=no_details) @@ -89,6 +93,7 @@ class TestCommandLineTools(unittest.TestCase): def test_list_tool(self) -> None: UtilFunctions.run_list_tool() + UtilFunctions.run_list_tool(no_strict=True) UtilFunctions.run_list_tool(ecu_variants=["somersault"]) UtilFunctions.run_list_tool(print_neg_responses=True) UtilFunctions.run_list_tool(print_params=True) @@ -114,11 +119,12 @@ def test_find_tool(self) -> None: def test_compare_tool(self) -> None: UtilFunctions.run_compare_tool() - UtilFunctions.run_compare_tool(database=[r"./examples/somersault_modified.pdx"]) + UtilFunctions.run_compare_tool(database=["./examples/somersault_modified.pdx"]) + UtilFunctions.run_compare_tool(no_strict=True) UtilFunctions.run_compare_tool( - database=[r"./examples/somersault_modified.pdx"], no_details=False) + database=["./examples/somersault_modified.pdx"], no_details=False) UtilFunctions.run_compare_tool( - database=[r"./examples/somersault_modified.pdx"], ecu_variants=["somersault_lazy"]) + database=["./examples/somersault_modified.pdx"], ecu_variants=["somersault_lazy"]) UtilFunctions.run_compare_tool(ecu_variants=[ "somersault_lazy", "somersault_assiduous", "somersault_young", "somersault_old" ]) From 09af3f979aece64cea47c367bbdc6d0db75a9af2 Mon Sep 17 00:00:00 2001 From: Andreas Lauser Date: Mon, 29 Jan 2024 12:30:40 +0100 Subject: [PATCH 2/2] consider review comments MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit as usual, the thanks go to [at]kayoub5. Signed-off-by: Andreas Lauser Signed-off-by: Katja Köhler --- odxtools/cli/main.py | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/odxtools/cli/main.py b/odxtools/cli/main.py index 35e3e3d2..786c18fb 100644 --- a/odxtools/cli/main.py +++ b/odxtools/cli/main.py @@ -60,11 +60,10 @@ def start_cli() -> None: for tool in tool_modules: if tool._odxtools_tool_name_ == args.subparser_name: + orig_strict = odxtools.exceptions.strict_mode odxtools.exceptions.strict_mode = not args.no_strict try: tool.run(args) - except: - raise finally: - odxtools.exceptions.strict_mode = False + odxtools.exceptions.strict_mode = orig_strict return