Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
36 commits
Select commit Hold shift + click to select a range
81eba0f
update : change the plugin generator files to separate folder
Jotheeswaran-Nandagopal Aug 7, 2024
8fe0744
fix : resolve formatting
Jotheeswaran-Nandagopal Aug 7, 2024
7d48408
feat : add changes to client template
Jotheeswaran-Nandagopal Aug 9, 2024
228b0f3
feat : add template creation implementation
Jotheeswaran-Nandagopal Aug 13, 2024
dfa65d3
feat : measurement plugin client generator
Jotheeswaran-Nandagopal Aug 14, 2024
32cbd19
fix : resolve formatting and lint issues
Jotheeswaran-Nandagopal Aug 14, 2024
b5c42b0
fix : add exception handling
Jotheeswaran-Nandagopal Aug 14, 2024
cbc24c6
update poetry lock
Jotheeswaran-Nandagopal Aug 14, 2024
1e2b815
fix : resolve issues due to merge
Jotheeswaran-Nandagopal Aug 14, 2024
44f2a66
fix : pr build and restructure folder
Jotheeswaran-Nandagopal Aug 16, 2024
9f8d977
fix : resolve comments
Jotheeswaran-Nandagopal Aug 19, 2024
9f2e78d
fix : resolve pr comments
Jotheeswaran-Nandagopal Aug 19, 2024
afa4a90
fix : resolve pr comments
Jotheeswaran-Nandagopal Aug 20, 2024
c315183
update: poetry lock
Jotheeswaran-Nandagopal Aug 20, 2024
77841a8
fix : resolve comments
Jotheeswaran-Nandagopal Aug 21, 2024
85fa5f4
fix : resolve comments
Jotheeswaran-Nandagopal Aug 21, 2024
becbf9a
fix :resolve comment
Jotheeswaran-Nandagopal Aug 21, 2024
07c9d2f
fix : resolve comments
Jotheeswaran-Nandagopal Aug 22, 2024
078260c
fix : formatting error
Jotheeswaran-Nandagopal Aug 22, 2024
e7af661
fix : update the argument in click
Jotheeswaran-Nandagopal Aug 23, 2024
7a664e9
fix : formatting
Jotheeswaran-Nandagopal Aug 23, 2024
bb50abe
fix : update the logic to use sets
Jotheeswaran-Nandagopal Aug 23, 2024
b2cb612
fix : exception handling
Jotheeswaran-Nandagopal Aug 23, 2024
c1c9c9b
refactor: remove unused import
Jotheeswaran-Nandagopal Aug 23, 2024
5e9a099
fix : resolve comments
Jotheeswaran-Nandagopal Aug 27, 2024
648b1f8
fix : pipeline failure
Jotheeswaran-Nandagopal Aug 27, 2024
13dc609
fix : resolve comments
Jotheeswaran-Nandagopal Aug 28, 2024
7eb96e2
refactor : formatting
Jotheeswaran-Nandagopal Aug 28, 2024
87f1d09
fix : add regex to validate class name
Jotheeswaran-Nandagopal Aug 28, 2024
3dc1062
fix : update error messages
Jotheeswaran-Nandagopal Aug 28, 2024
1527c59
fix : update validation
Jotheeswaran-Nandagopal Aug 28, 2024
b5a626d
fix: resolve comments
Jotheeswaran-Nandagopal Aug 29, 2024
73b7b44
refactor: update doc
Jotheeswaran-Nandagopal Aug 29, 2024
c5cef92
fix: formatting
Jotheeswaran-Nandagopal Aug 29, 2024
dfdf3f8
fix: formatting
Jotheeswaran-Nandagopal Aug 29, 2024
6b7b29d
fix: remove constants.py
Jotheeswaran-Nandagopal Aug 29, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,148 @@
"""Utilizes command line args to create a Measurement Plug-In Client using template files."""

import pathlib
import re
import sys
from typing import Any, List, Optional

import black
import click
from mako.template import Template
from ni_measurement_plugin_sdk_service._internal.stubs.ni.measurementlink.measurement.v2 import (
measurement_service_pb2 as v2_measurement_service_pb2,
)
from ni_measurement_plugin_sdk_service.discovery import DiscoveryClient
from ni_measurement_plugin_sdk_service.grpc.channelpool import GrpcChannelPool

from ni_measurement_plugin_sdk_generator.client._support import (
camel_to_snake_case,
get_configuration_metadata_by_index,
get_configuration_parameters_with_type_and_default_values,
get_measurement_service_stub,
get_output_metadata_by_index,
get_output_parameters_with_type,
is_python_identifier,
remove_suffix,
to_ordered_set,
)


def _render_template(template_name: str, **template_args: Any) -> bytes:
file_path = str(pathlib.Path(__file__).parent / "templates" / template_name)
template = Template(filename=file_path, input_encoding="utf-8", output_encoding="utf-8")
return template.render(**template_args)


def _create_file(
template_name: str, file_name: str, directory_out: pathlib.Path, **template_args: Any
) -> None:
output_file = directory_out / file_name

output = _render_template(template_name, **template_args).decode("utf-8")
formatted_output = black.format_str(
src_contents=output,
mode=black.Mode(line_length=100),
)

with output_file.open("w") as file:
file.write(formatted_output)


@click.command()
@click.argument("measurement_service_class")
@click.option(
"-m",
"--module-name",
help="Name for the Python Measurement Plug-In Client module.",
)
@click.option(
"-c",
"--class-name",
help="Name for the Python Measurement Plug-In Client Class in the generated module.",
)
@click.option(
"-o",
"--directory-out",
help="Output directory for Measurement Plug-In Client files. Default: '<current_directory>/<module_name>'",
)
def create_client(
measurement_service_class: str,
module_name: Optional[str],
class_name: Optional[str],
directory_out: Optional[str],
) -> None:
"""Generates a Python Measurement Plug-In Client module for the measurement service.

You can use the generated module to interact with the corresponding measurement service.

MEASUREMENT_SERVICE_CLASS: The service class of the measurement.
"""
channel_pool = GrpcChannelPool()
discovery_client = DiscoveryClient(grpc_channel_pool=channel_pool)
built_in_import_modules: List[str] = []
custom_import_modules: List[str] = []

if module_name is None or class_name is None:
base_service_class = measurement_service_class.split(".")[-1]
base_service_class = remove_suffix(base_service_class)
if not base_service_class.isidentifier():
raise click.ClickException(
"Unable to create client.\nPlease provide a valid module name or update the measurement with a valid service class."
)

if module_name is None:
module_name = camel_to_snake_case(base_service_class) + "_client"
if class_name is None:
class_name = base_service_class.replace("_", "") + "Client"
if not any(ch.isupper() for ch in base_service_class):
print(
f"Warning: The service class '{measurement_service_class}' does not follow the recommended format."
)

if not module_name.isidentifier():
raise click.ClickException(
f"The module name '{module_name}' is not a valid Python identifier."
)
if not is_python_identifier(class_name):
raise click.ClickException(
f"The class name '{class_name}' is not a valid Python identifier."
)

measurement_service_stub = get_measurement_service_stub(
discovery_client, channel_pool, measurement_service_class
)
metadata = measurement_service_stub.GetMetadata(v2_measurement_service_pb2.GetMetadataRequest())
configuration_metadata = get_configuration_metadata_by_index(
metadata, measurement_service_class
)
output_metadata = get_output_metadata_by_index(metadata)

configuration_parameters_with_type_and_default_values, measure_api_parameters = (
get_configuration_parameters_with_type_and_default_values(
configuration_metadata, built_in_import_modules
)
)
output_parameters_with_type = get_output_parameters_with_type(
output_metadata, built_in_import_modules, custom_import_modules
)

if directory_out is None:
directory_out_path = pathlib.Path.cwd()
else:
directory_out_path = pathlib.Path(directory_out)

_create_file(
template_name="measurement_plugin_client.py.mako",
file_name=f"{module_name}.py",
directory_out=directory_out_path,
class_name=class_name,
display_name=metadata.measurement_details.display_name,
configuration_metadata=configuration_metadata,
output_metadata=output_metadata,
service_class=measurement_service_class,
configuration_parameters_with_type_and_default_values=configuration_parameters_with_type_and_default_values,
measure_api_parameters=measure_api_parameters,
output_parameters_with_type=output_parameters_with_type,
built_in_import_modules=to_ordered_set(built_in_import_modules),
custom_import_modules=to_ordered_set(custom_import_modules),
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
"""Creates a measurement client through use of __init__.py."""

from ni_measurement_plugin_sdk_generator.client import create_client

create_client()
Loading
Loading