From bf581dd7178569dc20beb32f7f7c91937479d98d Mon Sep 17 00:00:00 2001 From: Haris Ali Date: Sun, 3 Nov 2024 21:18:42 +0500 Subject: [PATCH 01/10] change list command output from tabulate to rich tables --- odxtools/cli/_print_utils.py | 112 ++++++++++++++++++++++------------- 1 file changed, 71 insertions(+), 41 deletions(-) diff --git a/odxtools/cli/_print_utils.py b/odxtools/cli/_print_utils.py index fea6ad97..aba6f0f3 100644 --- a/odxtools/cli/_print_utils.py +++ b/odxtools/cli/_print_utils.py @@ -4,7 +4,8 @@ from typing import Any, Callable, Dict, List, Optional, Union import markdownify -from tabulate import tabulate # TODO: switch to rich tables +from rich.table import Table +from rich.console import Console from ..description import Description from ..diaglayers.diaglayer import DiagLayer @@ -82,9 +83,7 @@ def print_service_parameters(service: DiagService, f" Identifying Prefix: 0x{const_prefix.hex().upper()} ({bytes(const_prefix)!r})") print_fn(f" Parameters:") table = extract_parameter_tabulation_data(list(service.request.parameters)) - table_str = textwrap.indent(tabulate(table, headers='keys', tablefmt='presto'), " ") - print_fn() - print_fn(table_str) + print_fn(table) print_fn() else: print_fn(f" No Request!") @@ -97,8 +96,7 @@ def print_service_parameters(service: DiagService, print_fn(f" Positive Response '{resp.short_name}':") print_fn(f" Parameters:\n") table = extract_parameter_tabulation_data(list(resp.parameters)) - table_str = textwrap.indent(tabulate(table, headers='keys', tablefmt='presto'), " ") - print_fn(table_str) + print_fn(table) print_fn() # Negative Response @@ -109,8 +107,7 @@ def print_service_parameters(service: DiagService, print_fn(f" Negative Response '{resp.short_name}':") print_fn(f" Parameters:\n") table = extract_parameter_tabulation_data(list(resp.parameters)) - table_str = textwrap.indent(tabulate(table, headers='keys', tablefmt='presto'), " ") - print_fn(table_str) + print_fn(table) print_fn() print_fn("\n") @@ -138,10 +135,30 @@ def extract_service_tabulation_data(services: List[DiagService]) -> Dict[str, An return {'Name': name, 'Semantic': semantic, 'Hex-Request': request} -def extract_parameter_tabulation_data(parameters: List[Parameter]) -> Dict[str, Any]: +def extract_parameter_tabulation_data(parameters: List[Parameter]) -> Table: # extracts data of parameters of diagnostic services into Dictionary which can be printed by tabulate module # TODO: consider indentation + # Create Rich table + table = Table( + title="", + show_header=True, + header_style="bold cyan", + border_style="blue", + show_lines=True + ) + + # Add columns with appropriate styling + table.add_column("Name", style="green") + table.add_column("Byte Position", justify="right", style="yellow") + table.add_column("Bit Length", justify="right", style="yellow") + table.add_column("Semantic", justify="left", style="white") + table.add_column("Parameter Type", justify="left", style="white") + table.add_column("Data Type", justify="left", style="yellow") + table.add_column("Value", justify="left", style="white") + table.add_column("Value Type", justify="left", style="white") + table.add_column("Linked DOP", justify="left", style="white") + name = [] byte = [] bit_length: List[Optional[int]] = [] @@ -216,42 +233,55 @@ def extract_parameter_tabulation_data(parameters: List[Parameter]) -> Dict[str, value_type.append(None) dop.append(None) - return { - 'Name': name, - 'Byte Position': byte, - 'Bit Length': bit_length, - 'Semantic': semantic, - 'Parameter Type': param_type, - 'Data Type': data_type, - 'Value': value, - 'Value Type': value_type, - 'Linked DOP': dop - } + # Add all rows at once by zipping dictionary values + rows = zip(*[[value if value != None else "" for value in col] + for col in [name, byte, + bit_length, semantic, param_type, + value, value_type, data_type, dop]]) + for row in rows: + table.add_row(*map(str, row)) + + return table def print_dl_metrics(variants: List[DiagLayer], print_fn: Callable[..., Any] = print) -> None: - - name = [] - type = [] - num_services = [] - num_dops = [] - num_comparam_refs = [] + """ + Print diagnostic layer metrics using Rich tables. + + Args: + variants: List of diagnostic layer variants to analyze + print_fn: Optional callable for custom print handling (defaults to built-in print) + """ + # Create Rich table + table = Table( + title="", + show_header=True, + header_style="bold cyan", + border_style="blue", + show_lines=True + ) + + # Add columns with appropriate styling + table.add_column("Name", style="green") + table.add_column("Variant Type", style="magenta") + table.add_column("Number of Services", justify="right", style="yellow") + table.add_column("Number of DOPs", justify="right", style="yellow") + table.add_column("Number of communication parameters", justify="right", style="yellow") + + # Process each variant for variant in variants: assert isinstance(variant, DiagLayer) all_services: List[Union[DiagService, SingleEcuJob]] = sorted( - variant.services, key=lambda x: x.short_name) - name.append(variant.short_name) - type.append(variant.variant_type.value) - num_services.append(len(all_services)) + variant.services, key=lambda x: x.short_name + ) ddds = variant.diag_data_dictionary_spec - num_dops.append(len(ddds.data_object_props)) - num_comparam_refs.append(len(getattr(variant, "comparams_refs", []))) - - table = { - 'Name': name, - 'Variant Type': type, - 'Number of Services': num_services, - 'Number of DOPs': num_dops, - 'Number of communication parameters': num_comparam_refs - } - print_fn(tabulate(table, headers='keys', tablefmt='presto')) + + # Add row to table + table.add_row( + variant.short_name, + variant.variant_type.value, + str(len(all_services)), + str(len(ddds.data_object_props)), + str(len(getattr(variant, "comparams_refs", []))) + ) + print_fn(table) From ee1e7ec7b55c6ad5752a05c801e5529e99810639 Mon Sep 17 00:00:00 2001 From: Haris Ali Date: Mon, 4 Nov 2024 15:00:26 +0500 Subject: [PATCH 02/10] Add indentation to rich tables --- odxtools/cli/_print_utils.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/odxtools/cli/_print_utils.py b/odxtools/cli/_print_utils.py index aba6f0f3..d2b54783 100644 --- a/odxtools/cli/_print_utils.py +++ b/odxtools/cli/_print_utils.py @@ -5,7 +5,7 @@ import markdownify from rich.table import Table -from rich.console import Console +from rich.padding import Padding from ..description import Description from ..diaglayers.diaglayer import DiagLayer @@ -83,7 +83,7 @@ def print_service_parameters(service: DiagService, f" Identifying Prefix: 0x{const_prefix.hex().upper()} ({bytes(const_prefix)!r})") print_fn(f" Parameters:") table = extract_parameter_tabulation_data(list(service.request.parameters)) - print_fn(table) + print_fn(Padding(table, pad=(0, 0, 0, 4))) print_fn() else: print_fn(f" No Request!") @@ -96,7 +96,7 @@ def print_service_parameters(service: DiagService, print_fn(f" Positive Response '{resp.short_name}':") print_fn(f" Parameters:\n") table = extract_parameter_tabulation_data(list(resp.parameters)) - print_fn(table) + print_fn(Padding(table, pad=(0, 0, 0, 4))) print_fn() # Negative Response @@ -107,7 +107,7 @@ def print_service_parameters(service: DiagService, print_fn(f" Negative Response '{resp.short_name}':") print_fn(f" Parameters:\n") table = extract_parameter_tabulation_data(list(resp.parameters)) - print_fn(table) + print_fn(Padding(table, pad=(0, 0, 0, 4))) print_fn() print_fn("\n") From 129262f9b5a79742981e3302bd376412e4a9961a Mon Sep 17 00:00:00 2001 From: Haris Ali Date: Wed, 13 Nov 2024 11:40:48 +0500 Subject: [PATCH 03/10] fix coding style errors --- odxtools/cli/_print_utils.py | 41 +++++++++++------------------------- 1 file changed, 12 insertions(+), 29 deletions(-) diff --git a/odxtools/cli/_print_utils.py b/odxtools/cli/_print_utils.py index d2b54783..e20831f7 100644 --- a/odxtools/cli/_print_utils.py +++ b/odxtools/cli/_print_utils.py @@ -141,12 +141,7 @@ def extract_parameter_tabulation_data(parameters: List[Parameter]) -> Table: # Create Rich table table = Table( - title="", - show_header=True, - header_style="bold cyan", - border_style="blue", - show_lines=True - ) + title="", show_header=True, header_style="bold cyan", border_style="blue", show_lines=True) # Add columns with appropriate styling table.add_column("Name", style="green") @@ -234,13 +229,11 @@ def extract_parameter_tabulation_data(parameters: List[Parameter]) -> Table: dop.append(None) # Add all rows at once by zipping dictionary values - rows = zip(*[[value if value != None else "" for value in col] - for col in [name, byte, - bit_length, semantic, param_type, - value, value_type, data_type, dop]]) + rows = zip(*[[value if value != None else "" for value in col] for col in + [name, byte, bit_length, semantic, param_type, value, value_type, data_type, dop]]) for row in rows: table.add_row(*map(str, row)) - + return table @@ -254,34 +247,24 @@ def print_dl_metrics(variants: List[DiagLayer], print_fn: Callable[..., Any] = p """ # Create Rich table table = Table( - title="", - show_header=True, - header_style="bold cyan", - border_style="blue", - show_lines=True - ) - + title="", show_header=True, header_style="bold cyan", border_style="blue", show_lines=True) + # Add columns with appropriate styling table.add_column("Name", style="green") table.add_column("Variant Type", style="magenta") table.add_column("Number of Services", justify="right", style="yellow") table.add_column("Number of DOPs", justify="right", style="yellow") table.add_column("Number of communication parameters", justify="right", style="yellow") - + # Process each variant for variant in variants: assert isinstance(variant, DiagLayer) all_services: List[Union[DiagService, SingleEcuJob]] = sorted( - variant.services, key=lambda x: x.short_name - ) + variant.services, key=lambda x: x.short_name) ddds = variant.diag_data_dictionary_spec - + # Add row to table - table.add_row( - variant.short_name, - variant.variant_type.value, - str(len(all_services)), - str(len(ddds.data_object_props)), - str(len(getattr(variant, "comparams_refs", []))) - ) + table.add_row(variant.short_name, variant.variant_type.value, str(len(all_services)), + str(len(ddds.data_object_props)), + str(len(getattr(variant, "comparams_refs", [])))) print_fn(table) From 1b69202c2d76a88cf50544fd50c9b3d67c0acb5d Mon Sep 17 00:00:00 2001 From: Haris Ali Date: Thu, 14 Nov 2024 00:54:18 +0500 Subject: [PATCH 04/10] Resolve static type checking error in _print_utils.py --- odxtools/cli/_print_utils.py | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/odxtools/cli/_print_utils.py b/odxtools/cli/_print_utils.py index e20831f7..d5b8d858 100644 --- a/odxtools/cli/_print_utils.py +++ b/odxtools/cli/_print_utils.py @@ -154,15 +154,15 @@ def extract_parameter_tabulation_data(parameters: List[Parameter]) -> Table: table.add_column("Value Type", justify="left", style="white") table.add_column("Linked DOP", justify="left", style="white") - name = [] - byte = [] - bit_length: List[Optional[int]] = [] - semantic = [] - param_type = [] - value: List[Optional[str]] = [] - value_type: List[Optional[str]] = [] - data_type: List[Optional[str]] = [] - dop: List[Optional[str]] = [] + name: List[Optional[object]] = [] + byte: List[Optional[object]] = [] + bit_length: List[Optional[object]] = [] + semantic: List[Optional[object]] = [] + param_type: List[Optional[object]] = [] + value: List[Optional[object]] = [] + value_type: List[Optional[object]] = [] + data_type: List[Optional[object]] = [] + dop: List[Optional[object]] = [] for param in parameters: name.append(param.short_name) From d500a00610673b9c065e27cba6864413520e6003 Mon Sep 17 00:00:00 2001 From: Haris Ali Date: Fri, 15 Nov 2024 11:36:13 +0500 Subject: [PATCH 05/10] Update browse.py from tabulate to rich table --- odxtools/cli/browse.py | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/odxtools/cli/browse.py b/odxtools/cli/browse.py index 244ad468..a6665f5c 100644 --- a/odxtools/cli/browse.py +++ b/odxtools/cli/browse.py @@ -5,7 +5,7 @@ from typing import List, Optional, Union, cast import InquirerPy.prompt as IP_prompt -from tabulate import tabulate # TODO: switch to rich tables +# from tabulate import tabulate # TODO: switch to rich tables from ..database import Database from ..dataobjectproperty import DataObjectProperty @@ -356,9 +356,8 @@ def browse(odxdb: Database) -> None: if codec is not None: assert isinstance(codec, (Request, Response)) table = extract_parameter_tabulation_data(codec.parameters) - table_str = tabulate(table, headers='keys', tablefmt='presto') - print(table_str) - + print(table) + encode_message_interactively(codec, ask_user_confirmation=True) From 6be65e1d6fde03f109fecfd9a4b1b53e895602fb Mon Sep 17 00:00:00 2001 From: Haris Ali Date: Fri, 15 Nov 2024 12:31:07 +0500 Subject: [PATCH 06/10] Remove commented code --- odxtools/cli/browse.py | 1 - 1 file changed, 1 deletion(-) diff --git a/odxtools/cli/browse.py b/odxtools/cli/browse.py index a6665f5c..b9f5246a 100644 --- a/odxtools/cli/browse.py +++ b/odxtools/cli/browse.py @@ -5,7 +5,6 @@ from typing import List, Optional, Union, cast import InquirerPy.prompt as IP_prompt -# from tabulate import tabulate # TODO: switch to rich tables from ..database import Database from ..dataobjectproperty import DataObjectProperty From 48fa95dcc0426d5fa2ab8135bfb8dfb57d820d1b Mon Sep 17 00:00:00 2001 From: Haris Ali Date: Fri, 15 Nov 2024 14:01:27 +0500 Subject: [PATCH 07/10] Fix lint coding style issues --- odxtools/cli/_print_utils.py | 2 +- odxtools/cli/browse.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/odxtools/cli/_print_utils.py b/odxtools/cli/_print_utils.py index d5b8d858..9d5590ba 100644 --- a/odxtools/cli/_print_utils.py +++ b/odxtools/cli/_print_utils.py @@ -229,7 +229,7 @@ def extract_parameter_tabulation_data(parameters: List[Parameter]) -> Table: dop.append(None) # Add all rows at once by zipping dictionary values - rows = zip(*[[value if value != None else "" for value in col] for col in + rows = zip(*[[value if value is not None else "" for value in col] for col in [name, byte, bit_length, semantic, param_type, value, value_type, data_type, dop]]) for row in rows: table.add_row(*map(str, row)) diff --git a/odxtools/cli/browse.py b/odxtools/cli/browse.py index b9f5246a..468f2897 100644 --- a/odxtools/cli/browse.py +++ b/odxtools/cli/browse.py @@ -356,7 +356,7 @@ def browse(odxdb: Database) -> None: assert isinstance(codec, (Request, Response)) table = extract_parameter_tabulation_data(codec.parameters) print(table) - + encode_message_interactively(codec, ask_user_confirmation=True) From 4797cfb8c5cc4e640882f01d9fdc9d9fb69c8eff Mon Sep 17 00:00:00 2001 From: Haris Ali Date: Fri, 15 Nov 2024 16:24:37 +0500 Subject: [PATCH 08/10] Fix lint code quality issue --- odxtools/cli/_print_utils.py | 1 - 1 file changed, 1 deletion(-) diff --git a/odxtools/cli/_print_utils.py b/odxtools/cli/_print_utils.py index 9d5590ba..301ae77a 100644 --- a/odxtools/cli/_print_utils.py +++ b/odxtools/cli/_print_utils.py @@ -240,7 +240,6 @@ def extract_parameter_tabulation_data(parameters: List[Parameter]) -> Table: def print_dl_metrics(variants: List[DiagLayer], print_fn: Callable[..., Any] = print) -> None: """ Print diagnostic layer metrics using Rich tables. - Args: variants: List of diagnostic layer variants to analyze print_fn: Optional callable for custom print handling (defaults to built-in print) From 43261c86d63296b031bf014985896a019e50de72 Mon Sep 17 00:00:00 2001 From: Haris Ali Date: Tue, 19 Nov 2024 15:22:56 +0500 Subject: [PATCH 09/10] Fix static type definition for paramters --- odxtools/cli/_print_utils.py | 27 ++++++++++++++------------- 1 file changed, 14 insertions(+), 13 deletions(-) diff --git a/odxtools/cli/_print_utils.py b/odxtools/cli/_print_utils.py index 301ae77a..96051d00 100644 --- a/odxtools/cli/_print_utils.py +++ b/odxtools/cli/_print_utils.py @@ -149,20 +149,20 @@ def extract_parameter_tabulation_data(parameters: List[Parameter]) -> Table: table.add_column("Bit Length", justify="right", style="yellow") table.add_column("Semantic", justify="left", style="white") table.add_column("Parameter Type", justify="left", style="white") - table.add_column("Data Type", justify="left", style="yellow") - table.add_column("Value", justify="left", style="white") + table.add_column("Data Type", justify="left", style="white") + table.add_column("Value", justify="left", style="yellow") table.add_column("Value Type", justify="left", style="white") table.add_column("Linked DOP", justify="left", style="white") - name: List[Optional[object]] = [] - byte: List[Optional[object]] = [] - bit_length: List[Optional[object]] = [] - semantic: List[Optional[object]] = [] - param_type: List[Optional[object]] = [] - value: List[Optional[object]] = [] - value_type: List[Optional[object]] = [] - data_type: List[Optional[object]] = [] - dop: List[Optional[object]] = [] + name: List[str] = [] + byte: List[Any] = [] + bit_length: List[Any] = [] + semantic: List[Optional[str]] = [] + param_type: List[Optional[str]] = [] + value: List[Optional[str]] = [] + value_type: List[Optional[str]] = [] + data_type: List[Optional[str]] = [] + dop: List[Optional[str]] = [] for param in parameters: name.append(param.short_name) @@ -228,9 +228,10 @@ def extract_parameter_tabulation_data(parameters: List[Parameter]) -> Table: value_type.append(None) dop.append(None) + for lst in [byte, semantic, bit_length, value, value_type, data_type, dop]: + lst[:] = ["" if x is None else x for x in lst] # Add all rows at once by zipping dictionary values - rows = zip(*[[value if value is not None else "" for value in col] for col in - [name, byte, bit_length, semantic, param_type, value, value_type, data_type, dop]]) + rows = zip(name, byte, bit_length, semantic, param_type, data_type, value, value_type, dop) for row in rows: table.add_row(*map(str, row)) From cf6986c61a9c4f3984368b630c254447452ebe66 Mon Sep 17 00:00:00 2001 From: Haris Ali Date: Wed, 20 Nov 2024 11:06:42 +0500 Subject: [PATCH 10/10] Fix static type definitions for variables defined as List[Any] --- odxtools/cli/_print_utils.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/odxtools/cli/_print_utils.py b/odxtools/cli/_print_utils.py index 96051d00..4050a1e0 100644 --- a/odxtools/cli/_print_utils.py +++ b/odxtools/cli/_print_utils.py @@ -155,8 +155,8 @@ def extract_parameter_tabulation_data(parameters: List[Parameter]) -> Table: table.add_column("Linked DOP", justify="left", style="white") name: List[str] = [] - byte: List[Any] = [] - bit_length: List[Any] = [] + byte: List[Optional[int]] = [] + bit_length: List[Optional[int]] = [] semantic: List[Optional[str]] = [] param_type: List[Optional[str]] = [] value: List[Optional[str]] = [] @@ -229,7 +229,7 @@ def extract_parameter_tabulation_data(parameters: List[Parameter]) -> Table: dop.append(None) for lst in [byte, semantic, bit_length, value, value_type, data_type, dop]: - lst[:] = ["" if x is None else x for x in lst] + lst[:] = ["" if x is None else x for x in lst] # type: ignore[attr-defined, index] # Add all rows at once by zipping dictionary values rows = zip(name, byte, bit_length, semantic, param_type, data_type, value, value_type, dop) for row in rows: