Skip to content

Commit

Permalink
Add python as a default annotation under programming language. (#599)
Browse files Browse the repository at this point in the history
* Force add an annotation programming language Python
  • Loading branch information
gpachecoNI authored Jan 30, 2024
1 parent 78f938b commit 4f625db
Show file tree
Hide file tree
Showing 9 changed files with 69 additions and 35 deletions.
5 changes: 5 additions & 0 deletions ni_measurementlink_service/_annotations.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
"""Constants for annotations."""

ENUM_VALUES_KEY = "ni/enum.values"
TYPE_SPECIALIZATION_KEY = "ni/type_specialization"
SERVICE_PROGRAMMINGLANGUAGE_KEY = "ni/service.programminglanguage"
11 changes: 6 additions & 5 deletions ni_measurementlink_service/_internal/parameter/metadata.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@

from google.protobuf import type_pb2

from ni_measurementlink_service._annotations import (
ENUM_VALUES_KEY,
TYPE_SPECIALIZATION_KEY,
)
from ni_measurementlink_service._internal.parameter import serialization_strategy
from ni_measurementlink_service.measurement.info import TypeSpecialization

Expand Down Expand Up @@ -147,11 +151,8 @@ def get_enum_values_annotation(parameter_metadata: ParameterMetadata) -> str:
Returns:
str: The value of "ni/enum.values" annotation
"""
if (
parameter_metadata.annotations.get("ni/type_specialization")
== TypeSpecialization.Enum.value
):
return parameter_metadata.annotations.get("ni/enum.values", "")
if parameter_metadata.annotations.get(TYPE_SPECIALIZATION_KEY) == TypeSpecialization.Enum.value:
return parameter_metadata.annotations.get(ENUM_VALUES_KEY, "")
else:
return ""

Expand Down
5 changes: 4 additions & 1 deletion ni_measurementlink_service/_internal/parameter/serializer.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@
)
from google.protobuf.message import Message

from ni_measurementlink_service._annotations import (
TYPE_SPECIALIZATION_KEY,
)
from ni_measurementlink_service._internal.parameter import serialization_strategy
from ni_measurementlink_service._internal.parameter.metadata import (
ParameterMetadata,
Expand Down Expand Up @@ -76,7 +79,7 @@ def serialize_parameters(
)
# Convert enum parameters to their underlying value if necessary.
if (
parameter_metadata.annotations.get("ni/type_specialization")
parameter_metadata.annotations.get(TYPE_SPECIALIZATION_KEY)
== TypeSpecialization.Enum.value
):
parameter = _get_enum_value(parameter, parameter_metadata.repeated)
Expand Down
7 changes: 6 additions & 1 deletion ni_measurementlink_service/discovery/_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@
import grpc
from deprecation import deprecated

from ni_measurementlink_service._annotations import (
SERVICE_PROGRAMMINGLANGUAGE_KEY,
)
from ni_measurementlink_service._internal.stubs.ni.measurementlink.discovery.v1 import (
discovery_service_pb2,
discovery_service_pb2_grpc,
Expand Down Expand Up @@ -112,13 +115,15 @@ def register_service(self, service_info: ServiceInfo, service_location: ServiceL
Returns:
ID that can be used to unregister the service.
"""
annotations = service_info.annotations.copy()
annotations[SERVICE_PROGRAMMINGLANGUAGE_KEY] = "Python"
try:
grpc_service_description = discovery_service_pb2.ServiceDescriptor(
display_name=service_info.display_name,
description_url=service_info.description_url,
provided_interfaces=service_info.provided_interfaces,
service_class=service_info.service_class,
annotations=service_info.annotations,
annotations=annotations,
)

grpc_service_location = discovery_service_pb2.ServiceLocation(
Expand Down
8 changes: 6 additions & 2 deletions ni_measurementlink_service/measurement/service.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@
from google.protobuf.descriptor import EnumDescriptor

from ni_measurementlink_service import _datatypeinfo
from ni_measurementlink_service._annotations import (
ENUM_VALUES_KEY,
TYPE_SPECIALIZATION_KEY,
)
from ni_measurementlink_service._featuretoggles import (
SESSION_MANAGEMENT_2024Q1,
requires_feature,
Expand Down Expand Up @@ -512,13 +516,13 @@ def _make_annotations_dict(
if type_specialization == TypeSpecialization.NoType:
return annotations

annotations["ni/type_specialization"] = type_specialization.value
annotations[TYPE_SPECIALIZATION_KEY] = type_specialization.value
if type_specialization == TypeSpecialization.Pin:
if instrument_type != "" or instrument_type is not None:
annotations["ni/pin.instrument_type"] = instrument_type
if type_specialization == TypeSpecialization.Enum:
if enum_type is not None:
annotations["ni/enum.values"] = self._enum_to_annotations_value(enum_type)
annotations[ENUM_VALUES_KEY] = self._enum_to_annotations_value(enum_type)
else:
raise ValueError("enum_type is required for enum parameters.")

Expand Down
4 changes: 4 additions & 0 deletions tests/unit/test_discovery_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@
import pytest
from pytest_mock import MockerFixture

from ni_measurementlink_service._annotations import (
SERVICE_PROGRAMMINGLANGUAGE_KEY,
)
from ni_measurementlink_service._internal.stubs.ni.measurementlink.discovery.v1.discovery_service_pb2 import (
RegisterServiceRequest,
RegisterServiceResponse,
Expand Down Expand Up @@ -383,6 +386,7 @@ def _assert_service_location_equal(
def _assert_service_info_equal(
expected: ServiceInfo, actual: Union[ServiceInfo, GrpcServiceDescriptor]
) -> None:
expected.annotations[SERVICE_PROGRAMMINGLANGUAGE_KEY] = "Python"
assert expected.display_name == actual.display_name
assert expected.description_url == actual.description_url
assert set(expected.provided_interfaces) == set(actual.provided_interfaces)
Expand Down
32 changes: 18 additions & 14 deletions tests/unit/test_metadata.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@
import pytest

from ni_measurementlink_service import _datatypeinfo
from ni_measurementlink_service._annotations import (
ENUM_VALUES_KEY,
TYPE_SPECIALIZATION_KEY,
)
from ni_measurementlink_service._internal.parameter import metadata
from ni_measurementlink_service.measurement.info import DataType, TypeSpecialization

Expand Down Expand Up @@ -50,25 +54,25 @@ class Countries(IntEnum):
DataType.Enum,
1.0,
{
"ni/type_specialization": TypeSpecialization.Enum.value,
"ni/enum.values": '{"NONE":0, "RED": 1, "GREEN": 2, "BLUE": 3}',
TYPE_SPECIALIZATION_KEY: TypeSpecialization.Enum.value,
ENUM_VALUES_KEY: '{"NONE":0, "RED": 1, "GREEN": 2, "BLUE": 3}',
},
),
(
DataType.Enum,
DifferentColor.TEAL,
{
"ni/type_specialization": TypeSpecialization.Enum.value,
"ni/enum.values": '{"NONE":0, "RED": 1, "GREEN": 2, "BLUE": 3}',
TYPE_SPECIALIZATION_KEY: TypeSpecialization.Enum.value,
ENUM_VALUES_KEY: '{"NONE":0, "RED": 1, "GREEN": 2, "BLUE": 3}',
},
),
(DataType.EnumArray1D, 1, {}),
(
DataType.EnumArray1D,
[1.0, 2.0],
{
"ni/type_specialization": TypeSpecialization.Enum.value,
"ni/enum.values": '{"NONE":0, "RED": 1, "GREEN": 2, "BLUE": 3}',
TYPE_SPECIALIZATION_KEY: TypeSpecialization.Enum.value,
ENUM_VALUES_KEY: '{"NONE":0, "RED": 1, "GREEN": 2, "BLUE": 3}',
},
),
],
Expand Down Expand Up @@ -101,32 +105,32 @@ def test___default_value_different_from_type___validate___raises_type_exception(
DataType.Enum,
Color.BLUE,
{
"ni/type_specialization": TypeSpecialization.Enum.value,
"ni/enum.values": '{"NONE":0, "RED": 1, "GREEN": 2, "BLUE": 3}',
TYPE_SPECIALIZATION_KEY: TypeSpecialization.Enum.value,
ENUM_VALUES_KEY: '{"NONE":0, "RED": 1, "GREEN": 2, "BLUE": 3}',
},
),
(
DataType.EnumArray1D,
[Color.BLUE, Color.GREEN],
{
"ni/type_specialization": TypeSpecialization.Enum.value,
"ni/enum.values": '{"NONE":0, "RED": 1, "GREEN": 2, "BLUE": 3}',
TYPE_SPECIALIZATION_KEY: TypeSpecialization.Enum.value,
ENUM_VALUES_KEY: '{"NONE":0, "RED": 1, "GREEN": 2, "BLUE": 3}',
},
),
(
DataType.Enum,
Countries.AUSTRALIA,
{
"ni/type_specialization": TypeSpecialization.Enum.value,
"ni/enum.values": '{"AMERICA":0, "TAIWAN": 1, "AUSTRALIA": 2, "CANADA": 3}',
TYPE_SPECIALIZATION_KEY: TypeSpecialization.Enum.value,
ENUM_VALUES_KEY: '{"AMERICA":0, "TAIWAN": 1, "AUSTRALIA": 2, "CANADA": 3}',
},
),
(
DataType.EnumArray1D,
[Countries.AUSTRALIA, Countries.CANADA],
{
"ni/type_specialization": TypeSpecialization.Enum.value,
"ni/enum.values": '{"AMERICA":0, "TAIWAN": 1, "AUSTRALIA": 2, "CANADA": 3}',
TYPE_SPECIALIZATION_KEY: TypeSpecialization.Enum.value,
ENUM_VALUES_KEY: '{"AMERICA":0, "TAIWAN": 1, "AUSTRALIA": 2, "CANADA": 3}',
},
),
],
Expand Down
21 changes: 13 additions & 8 deletions tests/unit/test_serializer.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@
import pytest
from google.protobuf import any_pb2, type_pb2


from ni_measurementlink_service._annotations import (
ENUM_VALUES_KEY,
TYPE_SPECIALIZATION_KEY,
)
from ni_measurementlink_service._internal.parameter import serializer
from ni_measurementlink_service._internal.parameter.metadata import (
ParameterMetadata,
Expand Down Expand Up @@ -402,8 +407,8 @@ def _get_test_parameter_by_id(default_values):
repeated=False,
default_value=default_values[16],
annotations={
"ni/type_specialization": TypeSpecialization.Enum.value,
"ni/enum.values": '{"PURPLE": 0, "ORANGE": 1, "TEAL": 2, "BROWN": 3}',
TYPE_SPECIALIZATION_KEY: TypeSpecialization.Enum.value,
ENUM_VALUES_KEY: '{"PURPLE": 0, "ORANGE": 1, "TEAL": 2, "BROWN": 3}',
},
),
18: ParameterMetadata(
Expand All @@ -412,8 +417,8 @@ def _get_test_parameter_by_id(default_values):
repeated=True,
default_value=default_values[17],
annotations={
"ni/type_specialization": TypeSpecialization.Enum.value,
"ni/enum.values": '{"PURPLE": 0, "ORANGE": 1, "TEAL": 2, "BROWN": 3}',
TYPE_SPECIALIZATION_KEY: TypeSpecialization.Enum.value,
ENUM_VALUES_KEY: '{"PURPLE": 0, "ORANGE": 1, "TEAL": 2, "BROWN": 3}',
},
),
19: ParameterMetadata(
Expand All @@ -422,8 +427,8 @@ def _get_test_parameter_by_id(default_values):
repeated=False,
default_value=default_values[18],
annotations={
"ni/type_specialization": TypeSpecialization.Enum.value,
"ni/enum.values": '{"AMERICA": 0, "TAIWAN": 1, "AUSTRALIA": 2, "CANADA": 3}',
TYPE_SPECIALIZATION_KEY: TypeSpecialization.Enum.value,
ENUM_VALUES_KEY: '{"AMERICA": 0, "TAIWAN": 1, "AUSTRALIA": 2, "CANADA": 3}',
},
),
20: ParameterMetadata(
Expand All @@ -432,8 +437,8 @@ def _get_test_parameter_by_id(default_values):
repeated=True,
default_value=default_values[19],
annotations={
"ni/type_specialization": TypeSpecialization.Enum.value,
"ni/enum.values": '{"AMERICA": 0, "TAIWAN": 1, "AUSTRALIA": 2, "CANADA": 3}',
TYPE_SPECIALIZATION_KEY: TypeSpecialization.Enum.value,
ENUM_VALUES_KEY: '{"AMERICA": 0, "TAIWAN": 1, "AUSTRALIA": 2, "CANADA": 3}',
},
),
21: ParameterMetadata(
Expand Down
11 changes: 7 additions & 4 deletions tests/unit/test_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@
from pytest_mock import MockerFixture

from ni_measurementlink_service import _datatypeinfo
from ni_measurementlink_service._annotations import (
TYPE_SPECIALIZATION_KEY,
)
from ni_measurementlink_service.measurement.info import DataType, TypeSpecialization
from ni_measurementlink_service.measurement.service import MeasurementService

Expand Down Expand Up @@ -100,7 +103,7 @@ def test___measurement_service___add_pin_configuration__pin_configuration_added(
and param.default_value == default_value
and param.annotations
== {
"ni/type_specialization": TypeSpecialization.Pin.value,
TYPE_SPECIALIZATION_KEY: TypeSpecialization.Pin.value,
"ni/pin.instrument_type": instrument_type,
}
for param in measurement_service._configuration_parameter_list
Expand Down Expand Up @@ -131,7 +134,7 @@ def test___measurement_service___add_non_pin_configuration__pin_type_annotations
measurement_service.configuration(display_name, type, default_value)(_fake_measurement_function)

assert not all(
param.annotations.get("ni/type_specialization") == TypeSpecialization.Pin.value
param.annotations.get(TYPE_SPECIALIZATION_KEY) == TypeSpecialization.Pin.value
for param in measurement_service._configuration_parameter_list
)

Expand Down Expand Up @@ -159,7 +162,7 @@ def test___measurement_service___add_path_configuration__path_configuration_adde
and param.default_value == default_value
and param.annotations
== {
"ni/type_specialization": TypeSpecialization.Path.value,
TYPE_SPECIALIZATION_KEY: TypeSpecialization.Path.value,
}
for param in measurement_service._configuration_parameter_list
)
Expand Down Expand Up @@ -189,7 +192,7 @@ def test___measurement_service___add_non_path_configuration__path_type_annotatio
measurement_service.configuration(display_name, type, default_value)(_fake_measurement_function)

assert not all(
param.annotations.get("ni/type_specialization") == TypeSpecialization.Path.value
param.annotations.get(TYPE_SPECIALIZATION_KEY) == TypeSpecialization.Path.value
for param in measurement_service._configuration_parameter_list
)

Expand Down

0 comments on commit 4f625db

Please sign in to comment.