Skip to content

Commit

Permalink
remote: add ability to specify array as mapvalue for resources
Browse files Browse the repository at this point in the history
Signed-off-by: Felix Zwettler <[email protected]>
  • Loading branch information
flxzt committed Aug 14, 2024
1 parent ab8f79a commit 8fe8f75
Show file tree
Hide file tree
Showing 6 changed files with 434 additions and 196 deletions.
43 changes: 29 additions & 14 deletions labgrid/remote/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import logging
from datetime import datetime
from fnmatch import fnmatchcase
from typing import Any

import attr

Expand All @@ -27,34 +28,48 @@


def set_map_from_dict(m, d):
for k, v in d.items():
assert isinstance(k, str)
def mapval_from_val(mapval: labgrid_coordinator_pb2.MapValue, v):
if v is None:
m[k].Clear()
mapval.Clear()
elif isinstance(v, bool):
m[k].bool_value = v
mapval.bool_value = v
elif isinstance(v, int):
if v < 0:
m[k].int_value = v
mapval.int_value = v
else:
m[k].uint_value = v
mapval.uint_value = v
elif isinstance(v, float):
m[k].float_value = v
mapval.float_value = v
elif isinstance(v, str):
m[k].string_value = v
mapval.string_value = v
elif isinstance(v, list):
for listval in v:
mv = labgrid_coordinator_pb2.MapValue()
mapval_from_val(mv, listval)
mapval.array_value.values.append(mv)
else:
raise ValueError(f"cannot translate {repr(v)} to MapValue")

for k, v in d.items():
assert isinstance(k, str)
mapval_from_val(m[k], v)

def build_dict_from_map(m):
d = {}
for k, v in m.items():
v: labgrid_coordinator_pb2.MapValue
kind = v.WhichOneof("kind")
def val_from_mapval(mapval: labgrid_coordinator_pb2.MapValue) -> Any:
kind = mapval.WhichOneof("kind")
if kind is None:
d[k] = None
return None
elif kind == "array_value":
lv = list()
for mv in mapval.array_value.values:
lv.append(val_from_mapval(mv))
return lv
else:
d[k] = getattr(v, kind)
return getattr(mapval, kind)

d = {}
for k, v in m.items():
d[k] = val_from_mapval(v)
return d


Expand Down
Loading

0 comments on commit 8fe8f75

Please sign in to comment.