1
0
mirror of https://github.com/trezor/trezor-firmware.git synced 2024-11-21 23:18:13 +00:00

trezorlib: allow text values for enums in dict_to_proto

This commit is contained in:
matejcik 2019-05-06 13:26:23 +02:00
parent e388b69fbd
commit 75fe46d067

View File

@ -364,12 +364,32 @@ def format_message(
)
_ALL_ENUMS = {}
def _make_all_enums():
if not _ALL_ENUMS:
import inspect
from . import messages
for attr in messages.__dict__.values():
if not inspect.ismodule(attr):
continue
for name in dir(attr):
value = getattr(attr, name)
if isinstance(value, int):
_ALL_ENUMS[name] = value
def value_to_proto(ftype, value):
if issubclass(ftype, MessageType):
raise TypeError("value_to_proto only converts simple values")
if ftype in (UVarintType, SVarintType):
return int(value)
if isinstance(value, str) and value in _ALL_ENUMS:
return _ALL_ENUMS[value]
else:
return int(value)
if ftype is BoolType:
return bool(value)
@ -387,6 +407,8 @@ def value_to_proto(ftype, value):
def dict_to_proto(message_type, d):
_make_all_enums()
params = {}
for fname, ftype, fflags in message_type.get_fields().values():
repeated = fflags & FLAG_REPEATED