Skip to content

Commit 96ab459

Browse files
authored
Expose client types in 'ni.datastore' / Make extension-related logic consistent across supported types (#29)
* Adding client types for various messages. Using list() for comparing equality of iterables. * Re-adding definitions for Operator, Test, and TestDescription * Updating client code, tests, and examples to use newly added types. Adding to-string methods for wrapper types. * Exposing remaining needed types in ni.datastore package. * Initial work - abstracting away ExtensionValue concept from client API * Adding support for the case of no value being set for the Metadata of the returned ExtensionValue * Cleanup: Re-order entries in __init__.py * Review feedback - switch extensions to use a mapping of [str, str] instead of [str, object] * Review feedback - update to_protobuf and from_protobuf parameter and local variable names. * Review feedback - Use more descriptive local variable names * Review feedback - Mark client.py as essentially an implementation detail by renaming it to _client.py. Mask imported types of ni.datastore as belonging to main module. * Reorder methods for consistency with other types * Review feedback - rename 'types' folder to '_types' folder Signed-off-by: Hunter Kennon hunter.kennon@emerson.com
1 parent e6d7657 commit 96ab459

29 files changed

Lines changed: 1441 additions & 245 deletions

examples/notebooks/alias/alias.ipynb

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,7 @@
1717
"metadata": {},
1818
"outputs": [],
1919
"source": [
20-
"from ni.datastore import Client\n",
21-
"from ni.measurements.metadata.v1.metadata_store_pb2 import Operator\n",
20+
"from ni.datastore import Client, Operator\n",
2221
"\n",
2322
"client = Client()\n",
2423
"operatorOne = Operator(operator_name=\"Jane Doe\")\n",

examples/notebooks/custom-metadata/custom_metadata.ipynb

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -49,33 +49,33 @@
4949
"metadata": {},
5050
"outputs": [],
5151
"source": [
52-
"from ni.measurements.metadata.v1.metadata_store_pb2 import HardwareItem, ExtensionValue\n",
52+
"from ni.datastore import HardwareItem\n",
5353
"\n",
5454
"cable = HardwareItem(\n",
5555
" manufacturer=\"NI\",\n",
5656
" model=\"cable\",\n",
5757
" serial_number=\"7u2349\",\n",
5858
" schema_id=cable_schema_id,\n",
5959
")\n",
60-
"cable.extensions[\"cable_length\"].CopyFrom(ExtensionValue(string_value=\"1.5\"))\n",
61-
"cable.extensions[\"manufacture_date\"].CopyFrom(ExtensionValue(string_value=\"2023-01-01\"))\n",
60+
"cable.extensions[\"cable_length\"] = \"1.5\"\n",
61+
"cable.extensions[\"manufacture_date\"] = \"2023-01-01\"\n",
6262
"\n",
6363
"socket = HardwareItem(\n",
6464
" manufacturer=\"NI\",\n",
6565
" model=\"socket\",\n",
6666
" schema_id=socket_schema_id,\n",
6767
")\n",
68-
"socket.extensions[\"socket_number\"].CopyFrom(ExtensionValue(string_value=\"3\"))\n",
69-
"socket.extensions[\"manufacture_date\"].CopyFrom(ExtensionValue(string_value=\"2024-05-01\"))\n",
68+
"socket.extensions[\"socket_number\"] = \"3\"\n",
69+
"socket.extensions[\"manufacture_date\"] = \"2024-05-01\"\n",
7070
"\n",
7171
"scope = HardwareItem(\n",
7272
" manufacturer=\"NI\",\n",
7373
" model=\"PXIe-5171\",\n",
7474
" serial_number=\"1933B4E\",\n",
7575
" schema_id=scope_schema_id,\n",
7676
")\n",
77-
"scope.extensions[\"bandwidth\"].CopyFrom(ExtensionValue(string_value=\"250 MHz\"))\n",
78-
"scope.extensions[\"manufacture_date\"].CopyFrom(ExtensionValue(string_value=\"2024-11-03\"))"
77+
"scope.extensions[\"bandwidth\"] = \"250 MHz\"\n",
78+
"scope.extensions[\"manufacture_date\"] = \"2024-11-03\""
7979
]
8080
},
8181
{
@@ -120,7 +120,6 @@
120120
"from datetime import timezone\n",
121121
"import hightime as ht\n",
122122
"from ni.datastore import TestResult, Step\n",
123-
"from ni.protobuf.types.precision_timestamp_pb2 import PrecisionTimestamp\n",
124123
"from nitypes.waveform import AnalogWaveform\n",
125124
"from nitypes.waveform import Timing\n",
126125
"import numpy as np\n",
@@ -137,7 +136,6 @@
137136
" )\n",
138137
")\n",
139138
"\n",
140-
"precision_timestamp = PrecisionTimestamp()\n",
141139
"waveform = AnalogWaveform(\n",
142140
" sample_count=3,\n",
143141
" raw_data=np.array([1.0, 2.0, 3.0]),\n",

examples/notebooks/overview/publish_measurement.ipynb

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,7 @@
4444
"outputs": [],
4545
"source": [
4646
"import grpc\n",
47-
"from ni.datastore import TestResult\n",
48-
"from ni.measurements.metadata.v1.metadata_store_pb2 import Operator, TestStation, SoftwareItem, ExtensionValue\n",
47+
"from ni.datastore import Operator, TestResult, TestStation, SoftwareItem\n",
4948
"\n",
5049
"# Invalid creation of operator - no badge_number provided\n",
5150
"operator = Operator(operator_name=\"James Bowery\", schema_id=schema_id)\n",
@@ -56,33 +55,33 @@
5655
" print(f\" {e.details()}\")\n",
5756
"\n",
5857
"# Add the required attribute to the operator object\n",
59-
"operator.extensions[\"badge_number\"].CopyFrom(ExtensionValue(string_value=\"emp-128256\"))\n",
58+
"operator.extensions[\"badge_number\"] = \"emp-128256\"\n",
6059
"operator_id = client.create_operator(operator)\n",
6160
"\n",
6261
"# Invalid creation of test station - location is invalid\n",
6362
"test_station = TestStation(test_station_name=\"TestStation_12\", schema_id=schema_id)\n",
64-
"test_station.extensions[\"location\"].CopyFrom(ExtensionValue(string_value=\"Texas\"))\n",
63+
"test_station.extensions[\"location\"] = \"Texas\"\n",
6564
"try:\n",
6665
" test_station_id = client.create_test_station(test_station)\n",
6766
"except grpc.RpcError as e:\n",
6867
" print(\"Failed to create test station:\")\n",
6968
" print(f\" {e.details()}\")\n",
7069
"\n",
7170
"# Fix the location\n",
72-
"test_station.extensions[\"location\"].CopyFrom(ExtensionValue(string_value=\"USA\"))\n",
71+
"test_station.extensions[\"location\"] = \"USA\"\n",
7372
"test_station_id = client.create_test_station(test_station)\n",
7473
"\n",
7574
"# Invalid creation of software item - software license is invalid (not matching the pattern defined in the schema)\n",
7675
"software_item = SoftwareItem(product=\"Windows\", version=\"10.0.19044\", schema_id=schema_id)\n",
77-
"software_item.extensions[\"license\"].CopyFrom(ExtensionValue(string_value=\"enterprise_LIC\"))\n",
76+
"software_item.extensions[\"license\"] = \"enterprise_LIC\"\n",
7877
"try:\n",
7978
" software_item_id = client.create_software_item(software_item)\n",
8079
"except grpc.RpcError as e:\n",
8180
" print(\"Failed to create software item:\")\n",
8281
" print(f\" {e.details()}\")\n",
8382
"\n",
8483
"# Fix the software license to create the software item\n",
85-
"software_item.extensions[\"license\"].CopyFrom(ExtensionValue(string_value=\"LIC_enterprise\"))\n",
84+
"software_item.extensions[\"license\"] = \"LIC_enterprise\"\n",
8685
"software_item_id = client.create_software_item(software_item)\n",
8786
" \n",
8887
"test_result = TestResult(\n",
@@ -91,7 +90,7 @@
9190
" test_station_id=test_station_id,\n",
9291
" schema_id=schema_id,\n",
9392
" software_item_ids=[software_item_id])\n",
94-
"test_result.extensions[\"session_file_path\"] = ExtensionValue(string_value=\"C:\\\\my_test_description.xlsx\")\n"
93+
"test_result.extensions[\"session_file_path\"] = \"C:\\\\my_test_description.xlsx\"\n"
9594
]
9695
},
9796
{
@@ -186,7 +185,7 @@
186185
" operator = client.get_operator(test_result.operator_id)\n",
187186
" # badge_number is a custom attribute in the schema\n",
188187
" # It's not a standard attribute of the operator object\n",
189-
" badge_number = operator.extensions[\"badge_number\"].string_value\n",
188+
" badge_number = operator.extensions[\"badge_number\"]\n",
190189
" print(f\"operator {operator.operator_name}'s badge number is: {badge_number}\")\n",
191190
"\n",
192191
" waveform = client.read_data(found_measurement, expected_type=AnalogWaveform)\n",

examples/notebooks/query/query_measurements.ipynb

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,7 @@
1818
"outputs": [],
1919
"source": [
2020
"from typing import List\n",
21-
"from ni.datastore import Client, PublishedMeasurement\n",
22-
"from ni.measurements.data.v1.data_store_pb2 import Outcome\n",
21+
"from ni.datastore import Client, Outcome, PublishedMeasurement\n",
2322
"\n",
2423
"client = Client()\n",
2524
"\n",

examples/notebooks/voltage-regulator/publish_waveforms.ipynb

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,7 @@
2727
"metadata": {},
2828
"outputs": [],
2929
"source": [
30-
"from ni.datastore import Client, TestResult\n",
31-
"from ni.measurements.metadata.v1.metadata_store_pb2 import Operator, TestStation\n",
30+
"from ni.datastore import Client, Operator, TestResult, TestStation\n",
3231
"\n",
3332
"client = Client()\n",
3433
"\n",

examples/overview/src/overview.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,12 @@
44

55
import hightime as ht
66
import numpy as np
7-
from ni.datastore import Client, Step, TestResult
8-
from ni.measurements.metadata.v1.metadata_store_pb2 import (
7+
from ni.datastore import (
8+
Client,
99
Operator,
1010
SoftwareItem,
11+
Step,
12+
TestResult,
1113
TestStation,
1214
Uut,
1315
UutInstance,

src/ni/datastore/__init__.py

Lines changed: 58 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,66 @@
11
"""Public API for accessing the NI Data Store Service."""
22

3-
from ni.datastore.client import Client
4-
from ni.datastore.types._published_measurement import PublishedMeasurement
5-
from ni.datastore.types._step import Step
6-
from ni.datastore.types._test_result import TestResult
3+
from ni.datamonikers.v1.data_moniker_pb2 import Moniker
4+
from ni.datastore._client import Client
5+
from ni.datastore._types._alias import Alias
6+
from ni.datastore._types._extension_schema import ExtensionSchema
7+
from ni.datastore._types._hardware_item import HardwareItem
8+
from ni.datastore._types._operator import Operator
9+
from ni.datastore._types._published_condition import PublishedCondition
10+
from ni.datastore._types._published_measurement import PublishedMeasurement
11+
from ni.datastore._types._software_item import SoftwareItem
12+
from ni.datastore._types._step import Step
13+
from ni.datastore._types._test import Test
14+
from ni.datastore._types._test_adapter import TestAdapter
15+
from ni.datastore._types._test_description import TestDescription
16+
from ni.datastore._types._test_result import TestResult
17+
from ni.datastore._types._test_station import TestStation
18+
from ni.datastore._types._uut import Uut
19+
from ni.datastore._types._uut_instance import UutInstance
20+
from ni.measurements.data.v1.data_store_pb2 import ErrorInformation, Outcome
21+
from ni.measurements.metadata.v1.metadata_store_pb2 import AliasTargetType
722

823
__all__ = [
924
"Client",
25+
"Alias",
26+
"AliasTargetType",
27+
"ErrorInformation",
28+
"ExtensionSchema",
29+
"HardwareItem",
30+
"Moniker",
31+
"Operator",
32+
"Outcome",
33+
"PublishedCondition",
34+
"PublishedMeasurement",
35+
"SoftwareItem",
1036
"Step",
37+
"Test",
38+
"TestAdapter",
39+
"TestDescription",
1140
"TestResult",
12-
"PublishedMeasurement",
41+
"TestStation",
42+
"Uut",
43+
"UutInstance",
1344
]
45+
46+
# Hide that it was not defined in this top-level package
47+
Client.__module__ = __name__
48+
Alias.__module__ = __name__
49+
AliasTargetType.__module__ = __name__
50+
ErrorInformation.__module__ = __name__
51+
ExtensionSchema.__module__ = __name__
52+
HardwareItem.__module__ = __name__
53+
Moniker.__module__ = __name__
54+
Operator.__module__ = __name__
55+
Outcome.__module__ = __name__
56+
PublishedCondition.__module__ = __name__
57+
PublishedMeasurement.__module__ = __name__
58+
SoftwareItem.__module__ = __name__
59+
Step.__module__ = __name__
60+
Test.__module__ = __name__
61+
TestAdapter.__module__ = __name__
62+
TestDescription.__module__ = __name__
63+
TestResult.__module__ = __name__
64+
TestStation.__module__ = __name__
65+
Uut.__module__ = __name__
66+
UutInstance.__module__ = __name__

0 commit comments

Comments
 (0)