Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Code quality improvements to the Measurement Plug-In Client generator #901

Merged
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
a2dc76c
fix: add improvements
Jotheeswaran-Nandagopal Sep 19, 2024
f75401b
fix: update pyproject.toml
Jotheeswaran-Nandagopal Sep 19, 2024
1ca6efe
Merge branch 'main' into users/jothees/client-generator-improvements
MounikaBattu17 Sep 19, 2024
b02ab87
Merge branch 'main' into users/jothees/client-generator-improvements
Jotheeswaran-Nandagopal Sep 19, 2024
3c917ec
fix: resolve comments
Jotheeswaran-Nandagopal Sep 19, 2024
d22789b
fix: update stubs
Jotheeswaran-Nandagopal Sep 20, 2024
826b662
doc: update comment string
Jotheeswaran-Nandagopal Sep 20, 2024
535d42a
refactor: string concatination
Jotheeswaran-Nandagopal Sep 20, 2024
06a4445
Merge branch 'main' into users/jothees/client-generator-improvements
Jotheeswaran-Nandagopal Sep 20, 2024
d74944f
fix: resolve merge conflict
Jotheeswaran-Nandagopal Sep 20, 2024
7f7de09
fix: update doc strings
Jotheeswaran-Nandagopal Sep 20, 2024
63deba0
remove: namespace in proto
Jotheeswaran-Nandagopal Sep 20, 2024
ed30b02
fix: move enum type conversion to mako
Jotheeswaran-Nandagopal Sep 20, 2024
b029d60
fix: automate stubs generation in generator tests
Jotheeswaran-Nandagopal Sep 20, 2024
2172c90
fix: update pyproject
Jotheeswaran-Nandagopal Sep 20, 2024
2fe7c72
fix: update stubs
Jotheeswaran-Nandagopal Sep 20, 2024
2b3a8ea
fix: update mypy override
Jotheeswaran-Nandagopal Sep 20, 2024
a811110
fix: update proto
Jotheeswaran-Nandagopal Sep 20, 2024
4dd2b62
fix: update exception message
Jotheeswaran-Nandagopal Sep 20, 2024
926f723
fix: update mako
Jotheeswaran-Nandagopal Sep 20, 2024
7247618
update: formatting
Jotheeswaran-Nandagopal Sep 20, 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
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
"""Utilizes command line args to create a Measurement Plug-In Client using template files."""

import pathlib
import re
from enum import Enum
from typing import Any, Dict, List, Optional, Type

Expand All @@ -27,6 +26,7 @@
get_all_registered_measurement_info,
get_selected_measurement_service_class,
to_ordered_set,
replace_enum_class_type,
resolve_output_directory,
validate_identifier,
validate_measurement_service_classes,
Expand All @@ -39,18 +39,13 @@ def _render_template(template_name: str, **template_args: Any) -> bytes:
return template.render(**template_args)


def _replace_enum_class_type(output: str) -> str:
pattern = "<enum '([^']+)'>"
return re.sub(pattern, r"\1", output)


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")
output = _replace_enum_class_type(output)
output = replace_enum_class_type(output)
formatted_output = black.format_str(
src_contents=output,
mode=black.Mode(line_length=100),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import json
import keyword
import os
import pathlib
import re
import sys
Expand Down Expand Up @@ -244,10 +243,8 @@ def get_configuration_parameters_with_type_and_default_values(
configuration_parameters.append(f"{parameter_name}: {parameter_type} = {default_value}")

# Use line separator and spaces to align the parameters appropriately in the generated file.
configuration_parameters_with_type_and_value = f",{os.linesep} ".join(
configuration_parameters
)
parameter_names_as_str = ", ".join(parameter_names)
configuration_parameters_with_type_and_value = f",".join(configuration_parameters)
parameter_names_as_str = ",".join(parameter_names)

return (configuration_parameters_with_type_and_value, parameter_names_as_str)

Expand Down Expand Up @@ -284,7 +281,7 @@ def get_output_parameters_with_type(

output_parameters_with_type.append(f"{parameter_name}: {parameter_type}")

return f"{os.linesep} ".join(output_parameters_with_type)
return f";".join(output_parameters_with_type)


def to_ordered_set(values: Iterable[_T]) -> AbstractSet[_T]:
Expand Down Expand Up @@ -356,6 +353,12 @@ def validate_measurement_service_classes(measurement_service_classes: List[str])
raise click.ClickException("No registered measurements.")


def replace_enum_class_type(output: str) -> str:
"""Formats the data with enum type."""
pattern = "<enum '([^']+)'>"
return re.sub(pattern, r"\1", output)


def _get_python_identifier(input_string: str) -> str:
valid_identifier = input_string.lower()
if not valid_identifier.isidentifier():
Expand Down Expand Up @@ -433,4 +436,4 @@ def _get_enum_class_name(name: str) -> str:
name = "".join(s.capitalize() for s in split_string)
else:
name = name[0].upper() + name[1:]
return name + "Enum"
return f"{name}Enum"
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ class ${class_name}:

@property
def pin_map_context(self) -> PinMapContext:
"""Get the pin map context for the measurement."""
"""The pin map context for the measurement."""
return self._pin_map_context

@pin_map_context.setter
Expand Down
3 changes: 3 additions & 0 deletions packages/generator/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -75,3 +75,6 @@ skips = [
"B101", # assert_used
"B702", # use_of_mako_templates
]

[tool.ni-python-styleguide]
extend_exclude = '.tox/,*_pb2_grpc.py,*_pb2_grpc.pyi,*_pb2.py,*_pb2.pyi'
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,15 @@ class EnumInEnum(Enum):
BLUE = 3


class ProtobufEnumInEnum(Enum):
"""ProtobufEnumInEnum used for enum-typed measurement configs and outputs."""

NONE = 0
PINK = 1
WHITE = 2
BLACK = 3


def test___measurement_plugin_client___measure___returns_output(
measurement_plugin_client_module: ModuleType,
) -> None:
Expand Down Expand Up @@ -54,6 +63,7 @@ def test___measurement_plugin_client___measure___returns_output(
xy_data_out=None,
enum_out=EnumInEnum.BLUE,
enum_array_out=[EnumInEnum.RED, EnumInEnum.GREEN],
protobuf_enum_out=ProtobufEnumInEnum.BLACK,
)
measurement_plugin_client = test_measurement_client_type()

Expand Down Expand Up @@ -97,6 +107,7 @@ def test___measurement_plugin_client___stream_measure___returns_output(
xy_data_out=None,
enum_out=EnumInEnum.BLUE,
enum_array_out=[EnumInEnum.RED, EnumInEnum.GREEN],
protobuf_enum_out=ProtobufEnumInEnum.BLACK,
)
measurement_plugin_client = test_measurement_client_type()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,15 @@ class EnumInEnum(Enum):
BLUE = 3


class ProtobufEnumInEnum(Enum):
"""ProtobufEnumInEnum used for enum-typed measurement configs and outputs."""

NONE = 0
PINK = 1
WHITE = 2
BLACK = 3


class Outputs(NamedTuple):
"""Outputs for the 'Non-Streaming Data Measurement (Py)' measurement plug-in."""

Expand All @@ -58,6 +67,7 @@ class Outputs(NamedTuple):
xy_data_out: DoubleXYData
enum_out: EnumInEnum
enum_array_out: List[EnumInEnum]
protobuf_enum_out: ProtobufEnumInEnum


class NonStreamingDataMeasurementClient:
Expand Down Expand Up @@ -238,6 +248,19 @@ def __init__(
field_name="Enum_Array_In",
enum_type=EnumInEnum,
),
13: ParameterMetadata(
display_name="Protobuf Enum In",
type=14,
repeated=False,
default_value=3,
annotations={
"ni/enum.values": '{"NONE": 0, "PINK": 1, "WHITE": 2, "BLACK": 3}',
"ni/type_specialization": "enum",
},
message_type="",
field_name="Protobuf_Enum_In",
enum_type=ProtobufEnumInEnum,
),
}
self._output_metadata = {
1: ParameterMetadata(
Expand Down Expand Up @@ -382,6 +405,19 @@ def __init__(
field_name="Enum_Array_Out",
enum_type=EnumInEnum,
),
14: ParameterMetadata(
display_name="Protobuf Enum out",
type=14,
repeated=False,
default_value=None,
annotations={
"ni/enum.values": '{"NONE": 0, "PINK": 1, "WHITE": 2, "BLACK": 3}',
"ni/type_specialization": "enum",
},
message_type="",
field_name="Protobuf_Enum_out",
enum_type=ProtobufEnumInEnum,
),
}
if grpc_channel is not None:
self._stub = v2_measurement_service_pb2_grpc.MeasurementServiceStub(grpc_channel)
Expand All @@ -390,7 +426,7 @@ def __init__(

@property
def pin_map_context(self) -> PinMapContext:
"""Get the pin map context for the measurement."""
"""The pin map context for the measurement."""
return self._pin_map_context

@pin_map_context.setter
Expand Down Expand Up @@ -521,6 +557,7 @@ def measure(
integer_in: int = 10,
enum_in: EnumInEnum = EnumInEnum.BLUE,
enum_array_in: List[EnumInEnum] = [EnumInEnum.RED, EnumInEnum.GREEN],
protobuf_enum_in: ProtobufEnumInEnum = ProtobufEnumInEnum.BLACK,
) -> Outputs:
"""Perform a single measurement.

Expand All @@ -540,6 +577,7 @@ def measure(
integer_in,
enum_in,
enum_array_in,
protobuf_enum_in,
)
for response in stream_measure_response:
result = response
Expand Down Expand Up @@ -573,6 +611,7 @@ def stream_measure(
integer_in: int = 10,
enum_in: EnumInEnum = EnumInEnum.BLUE,
enum_array_in: List[EnumInEnum] = [EnumInEnum.RED, EnumInEnum.GREEN],
protobuf_enum_in: ProtobufEnumInEnum = ProtobufEnumInEnum.BLACK,
) -> Generator[Outputs, None, None]:
"""Perform a streaming measurement.

Expand All @@ -593,6 +632,7 @@ def stream_measure(
integer_in,
enum_in,
enum_array_in,
protobuf_enum_in,
]
)
with self._initialization_lock:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
import ni_measurement_plugin_sdk_service as nims
from ni_measurement_plugin_sdk_service._internal.stubs.ni.protobuf.types import xydata_pb2

from tests.utilities.measurements.non_streaming_data_measurement._stubs import color_pb2


service_directory = Path(__file__).resolve().parent
measurement_service = nims.MeasurementService(
Expand Down Expand Up @@ -66,6 +68,12 @@ class Color(Enum):
@measurement_service.configuration(
"Enum Array In", nims.DataType.EnumArray1D, [1, 2], enum_type=Color
)
@measurement_service.configuration(
"Protobuf Enum In",
nims.DataType.Enum,
color_pb2.ProtobufColor.BLACK,
enum_type=color_pb2.ProtobufColor,
)
@measurement_service.output("Float out", nims.DataType.Float)
@measurement_service.output("Double Array out", nims.DataType.DoubleArray1D)
@measurement_service.output("Bool out", nims.DataType.Boolean)
Expand All @@ -79,6 +87,9 @@ class Color(Enum):
@measurement_service.output("XY Data Out", nims.DataType.DoubleXYData)
@measurement_service.output("Enum Out", nims.DataType.Enum, enum_type=Color)
@measurement_service.output("Enum Array Out", nims.DataType.EnumArray1D, enum_type=Color)
@measurement_service.output(
"Protobuf Enum out", nims.DataType.Enum, enum_type=color_pb2.ProtobufColor
)
def measure(
float_input: float,
double_array_input: Iterable[float],
Expand All @@ -92,6 +103,7 @@ def measure(
integer_input: int,
enum_input: Color,
enum_array_input: Iterable[Color],
protobuf_enum_input: color_pb2.ProtobufColor.ValueType,
) -> Tuple[
float,
Iterable[float],
Expand All @@ -106,6 +118,7 @@ def measure(
xydata_pb2.DoubleXYData,
Color,
Iterable[Color],
color_pb2.ProtobufColor.ValueType,
]:
"""Perform a loopback measurement with various data types."""
float_output = float_input
Expand All @@ -121,6 +134,7 @@ def measure(
xy_data_output = xydata_pb2.DoubleXYData()
enum_output = enum_input
enum_array_output = enum_array_input
protobuf_enum_output = protobuf_enum_input

return (
float_output,
Expand All @@ -136,4 +150,5 @@ def measure(
xy_data_output,
enum_output,
enum_array_output,
protobuf_enum_output,
)
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
"""Stubs for non_streaming_data_measurement's color enum."""

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
"""
@generated by mypy-protobuf. Do not edit manually!
isort:skip_file
---------------------------------------------------------------------
---------------------------------------------------------------------
"""

import builtins
import google.protobuf.descriptor
import google.protobuf.internal.enum_type_wrapper
import sys
import typing

if sys.version_info >= (3, 10):
import typing as typing_extensions
else:
import typing_extensions

DESCRIPTOR: google.protobuf.descriptor.FileDescriptor

class _ProtobufColor:
ValueType = typing.NewType("ValueType", builtins.int)
V: typing_extensions.TypeAlias = ValueType

class _ProtobufColorEnumTypeWrapper(
google.protobuf.internal.enum_type_wrapper._EnumTypeWrapper[_ProtobufColor.ValueType],
builtins.type,
):
DESCRIPTOR: google.protobuf.descriptor.EnumDescriptor
NONE: _ProtobufColor.ValueType # 0
PINK: _ProtobufColor.ValueType # 1
WHITE: _ProtobufColor.ValueType # 2
BLACK: _ProtobufColor.ValueType # 3

class ProtobufColor(_ProtobufColor, metaclass=_ProtobufColorEnumTypeWrapper):
"""---------------------------------------------------------------------
---------------------------------------------------------------------
"""

NONE: ProtobufColor.ValueType # 0
PINK: ProtobufColor.ValueType # 1
WHITE: ProtobufColor.ValueType # 2
BLACK: ProtobufColor.ValueType # 3
global___ProtobufColor = ProtobufColor
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Generated by the gRPC Python protocol compiler plugin. DO NOT EDIT!
"""Client and server classes corresponding to protobuf-defined services."""
import grpc
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
"""
@generated by mypy-protobuf. Do not edit manually!
isort:skip_file
---------------------------------------------------------------------
---------------------------------------------------------------------
"""