Skip to content

Commit

Permalink
Provide unique module names for the generated measurement clients dur…
Browse files Browse the repository at this point in the history
…ing multiple client generation (#966)

* fix: provide unique names for similar service class name

* fix: update suffix adding logic

* fix: update print message
  • Loading branch information
Jotheeswaran-Nandagopal authored Oct 10, 2024
1 parent 70456e8 commit 4c0107a
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -103,21 +103,22 @@ 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'."
)


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")
Expand All @@ -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()
Expand All @@ -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,
Expand All @@ -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(
Expand All @@ -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)
Expand All @@ -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")
Expand All @@ -212,6 +217,7 @@ def _create_clients(
class_name=class_name,
directory_out=directory_out_path,
)
generated_modules.append(module_name)


@click.command()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down

0 comments on commit 4c0107a

Please sign in to comment.