Skip to content

Commit 5fe98ce

Browse files
MP91glm2bg
andauthored
get node via string (#98)
Co-authored-by: glm2bg <[email protected]>
1 parent 5a2aa42 commit 5fe98ce

File tree

5 files changed

+38
-2
lines changed

5 files changed

+38
-2
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,3 +154,4 @@ dmypy.json
154154

155155
# MacOS specific
156156
.DS_Store
157+
results
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
git+https://github.com/eclipse-velocitas/[email protected].0
1+
git+https://github.com/eclipse-velocitas/[email protected].1

sdv/model.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -826,6 +826,21 @@ class Model(Node):
826826
def set_many(self) -> BatchSetBuilder:
827827
return BatchSetBuilder(self.get_client())
828828

829+
def getNode(self, datapoint_str: str) -> Node:
830+
if self.get_path() not in datapoint_str:
831+
raise ValueError("Input string has to start with the root")
832+
nodes = datapoint_str.split(".")
833+
dataPoint = self
834+
835+
if len(nodes) > 1:
836+
for node in nodes[1:]:
837+
try:
838+
dataPoint = getattr(dataPoint, node)
839+
except Exception as err:
840+
raise AttributeError("Node not found") from err
841+
842+
return dataPoint
843+
829844

830845
class Service(Node):
831846
"""The Service class contains a set of gRPC methods"""

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@
7070

7171
setup(
7272
name="sdv",
73-
version="0.10.0",
73+
version="0.10.1",
7474
description="A Python SDK for Vehicle app",
7575
long_description=long_description,
7676
long_description_content_type="text/markdown",

tests/unit/model_test.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1512,6 +1512,26 @@ async def test_fail_setting_multiple_data_points_atomically():
15121512
)
15131513

15141514

1515+
@pytest.mark.asyncio
1516+
async def test_get_node__available():
1517+
vehicle = get_vehicle_instance()
1518+
assert vehicle.getNode(vehicle.Speed.get_path()) == vehicle.Speed
1519+
1520+
1521+
@pytest.mark.asyncio
1522+
async def test_get_node__not_available():
1523+
vehicle = get_vehicle_instance()
1524+
with pytest.raises(AttributeError):
1525+
vehicle.getNode("Vehicle.NotAvailable")
1526+
1527+
1528+
@pytest.mark.asyncio
1529+
async def test_get_node__root_not_in_string():
1530+
vehicle = get_vehicle_instance()
1531+
with pytest.raises(ValueError):
1532+
vehicle.getNode("Speed")
1533+
1534+
15151535
DoorSides = ["Left", "Right"]
15161536
TrunkOptions = ["Front", "Rear"]
15171537

0 commit comments

Comments
 (0)