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

[gRPC] remote: add ability to specify array attributes in exported resources #1442

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
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
Loading