From 4c0107aba9fc4151cd112dd671eb0b325b7e175d Mon Sep 17 00:00:00 2001 From: Jotheeswaran-Nandagopal <163156604+Jotheeswaran-Nandagopal@users.noreply.github.com> Date: Fri, 11 Oct 2024 00:43:35 +0530 Subject: [PATCH] Provide unique module names for the generated measurement clients during multiple client generation (#966) * fix: provide unique names for similar service class name * fix: update suffix adding logic * fix: update print message --- .../client/__init__.py | 14 ++++++++++---- .../client/_support.py | 14 +++++++++++--- 2 files changed, 21 insertions(+), 7 deletions(-) diff --git a/packages/generator/ni_measurement_plugin_sdk_generator/client/__init__.py b/packages/generator/ni_measurement_plugin_sdk_generator/client/__init__.py index 6853f77ad..50030c064 100644 --- a/packages/generator/ni_measurement_plugin_sdk_generator/client/__init__.py +++ b/packages/generator/ni_measurement_plugin_sdk_generator/client/__init__.py @@ -103,7 +103,7 @@ def _create_client( ) print( - f"The measurement plug-in client for the service class '{measurement_service_class}' is created successfully." + f"The measurement plug-in client for the service class '{measurement_service_class}' has been successfully created as '{module_name}.py'." ) @@ -111,13 +111,14 @@ def _create_all_clients(directory_out: Optional[str]) -> None: channel_pool = GrpcChannelPool() discovery_client = DiscoveryClient(grpc_channel_pool=channel_pool) + generated_modules: List[str] = [] directory_out_path = resolve_output_directory(directory_out) measurement_service_classes, _ = get_all_registered_measurement_info(discovery_client) validate_measurement_service_classes(measurement_service_classes) for service_class in measurement_service_classes: base_service_class = extract_base_service_class(service_class) - module_name = create_module_name(base_service_class) + module_name = create_module_name(base_service_class, generated_modules) class_name = create_class_name(base_service_class) validate_identifier(module_name, "module") validate_identifier(class_name, "class") @@ -130,10 +131,12 @@ def _create_all_clients(directory_out: Optional[str]) -> None: class_name=class_name, directory_out=directory_out_path, ) + generated_modules.append(module_name) def _create_clients_interactively() -> None: print("Creating the Python Measurement Plug-In Client in interactive mode...") + generated_modules: List[str] = [] channel_pool = GrpcChannelPool() discovery_client = DiscoveryClient(grpc_channel_pool=channel_pool) directory_out_path = resolve_output_directory() @@ -159,7 +162,7 @@ def _create_clients_interactively() -> None: ) base_service_class = extract_base_service_class(service_class) - default_module_name = create_module_name(base_service_class) + default_module_name = create_module_name(base_service_class, generated_modules) module_name = click.prompt( "Enter a name for the Python client module, or press Enter to use the default name.", type=str, @@ -182,6 +185,7 @@ def _create_clients_interactively() -> None: class_name=class_name, directory_out=directory_out_path, ) + generated_modules.append(module_name) def _create_clients( @@ -190,6 +194,7 @@ def _create_clients( class_name: Optional[str], directory_out: Optional[str], ) -> None: + generated_modules: List[str] = [] channel_pool = GrpcChannelPool() discovery_client = DiscoveryClient(grpc_channel_pool=channel_pool) directory_out_path = resolve_output_directory(directory_out) @@ -198,7 +203,7 @@ def _create_clients( for service_class in measurement_service_classes: base_service_class = extract_base_service_class(service_class) if has_multiple_service_classes or module_name is None: - module_name = create_module_name(base_service_class) + module_name = create_module_name(base_service_class, generated_modules) if has_multiple_service_classes or class_name is None: class_name = create_class_name(base_service_class) validate_identifier(module_name, "module") @@ -212,6 +217,7 @@ def _create_clients( class_name=class_name, directory_out=directory_out_path, ) + generated_modules.append(module_name) @click.command() diff --git a/packages/generator/ni_measurement_plugin_sdk_generator/client/_support.py b/packages/generator/ni_measurement_plugin_sdk_generator/client/_support.py index 8b8f1998b..3f0f40476 100644 --- a/packages/generator/ni_measurement_plugin_sdk_generator/client/_support.py +++ b/packages/generator/ni_measurement_plugin_sdk_generator/client/_support.py @@ -344,9 +344,17 @@ def extract_base_service_class(service_class: str) -> str: return base_service_class -def create_module_name(base_service_class: str) -> str: - """Creates a module name using base service class.""" - return _camel_to_snake_case(base_service_class) + "_client" +def create_module_name(base_service_class: str, generated_modules: List[str]) -> str: + """Creates a unique module name using the base service class.""" + base_module_name = _camel_to_snake_case(base_service_class) + "_client" + module_name = base_module_name + counter = 2 + + while module_name in generated_modules: + module_name = f"{base_module_name}{counter}" + counter += 1 + + return module_name def create_class_name(base_service_class: str) -> str: