mirror of
https://github.com/trezor/trezor-firmware.git
synced 2024-11-22 07:28:10 +00:00
trezorlib: allow text values for enums in dict_to_proto
This commit is contained in:
parent
e388b69fbd
commit
75fe46d067
@ -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):
|
def value_to_proto(ftype, value):
|
||||||
if issubclass(ftype, MessageType):
|
if issubclass(ftype, MessageType):
|
||||||
raise TypeError("value_to_proto only converts simple values")
|
raise TypeError("value_to_proto only converts simple values")
|
||||||
|
|
||||||
if ftype in (UVarintType, SVarintType):
|
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:
|
if ftype is BoolType:
|
||||||
return bool(value)
|
return bool(value)
|
||||||
@ -387,6 +407,8 @@ def value_to_proto(ftype, value):
|
|||||||
|
|
||||||
|
|
||||||
def dict_to_proto(message_type, d):
|
def dict_to_proto(message_type, d):
|
||||||
|
_make_all_enums()
|
||||||
|
|
||||||
params = {}
|
params = {}
|
||||||
for fname, ftype, fflags in message_type.get_fields().values():
|
for fname, ftype, fflags in message_type.get_fields().values():
|
||||||
repeated = fflags & FLAG_REPEATED
|
repeated = fflags & FLAG_REPEATED
|
||||||
|
Loading…
Reference in New Issue
Block a user