mirror of
https://github.com/trezor/trezor-firmware.git
synced 2025-01-03 12:00:59 +00:00
nem_mosaics: Refactor and use Python 2
This commit is contained in:
parent
128742d113
commit
296c120528
@ -1,8 +1,13 @@
|
|||||||
#!/usr/bin/env python3
|
#!/usr/bin/env python2
|
||||||
import json, os
|
import json, os, sys
|
||||||
|
|
||||||
|
import collections, numbers
|
||||||
|
|
||||||
from google.protobuf import json_format
|
from google.protobuf import json_format
|
||||||
from itertools import chain
|
from itertools import chain
|
||||||
import protob.types_pb2 as types
|
|
||||||
|
sys.path.insert(0, os.path.join(os.path.dirname(__file__), "protob"))
|
||||||
|
import types_pb2 as types
|
||||||
|
|
||||||
HEADER_TEMPLATE = """
|
HEADER_TEMPLATE = """
|
||||||
// This file is automatically generated by nem_mosaics.py -- DO NOT EDIT!
|
// This file is automatically generated by nem_mosaics.py -- DO NOT EDIT!
|
||||||
@ -31,13 +36,13 @@ const NEMMosaicDefinition *NEM_MOSAIC_DEFINITION_XEM = NEM_MOSAIC_DEFINITIONS;
|
|||||||
""".lstrip()
|
""".lstrip()
|
||||||
|
|
||||||
def format_primitive(value):
|
def format_primitive(value):
|
||||||
if type(value) is bool:
|
if isinstance(value, bool):
|
||||||
return ("false", "true")[value]
|
return ("false", "true")[value]
|
||||||
elif type(value) in (int, float):
|
elif isinstance(value, numbers.Number):
|
||||||
return str(value)
|
return str(value)
|
||||||
elif type(value) is str:
|
elif isinstance(value, basestring):
|
||||||
return json.dumps(value)
|
return json.dumps(value)
|
||||||
elif type(value) is list:
|
elif isinstance(value, collections.Sequence):
|
||||||
return "{ " + ", ".join(
|
return "{ " + ", ".join(
|
||||||
format_primitive(item) for item in value
|
format_primitive(item) for item in value
|
||||||
) + " }"
|
) + " }"
|
||||||
@ -61,14 +66,14 @@ def format_field(field, value):
|
|||||||
return format_primitive(value)
|
return format_primitive(value)
|
||||||
|
|
||||||
def field_to_meta(field, value):
|
def field_to_meta(field, value):
|
||||||
if hasattr(value, "_values"):
|
if field.label == field.LABEL_REPEATED:
|
||||||
return ("{}_count".format(field.name), format_primitive(len(value._values)))
|
return ("{}_count".format(field.name), format_primitive(len(value)))
|
||||||
else:
|
else:
|
||||||
return ("has_{}".format(field.name), format_primitive(True))
|
return ("has_{}".format(field.name), format_primitive(True))
|
||||||
|
|
||||||
def message_to_struct(_message, proto):
|
def message_to_struct(_message, proto):
|
||||||
message = json_format.ParseDict(_message, proto())
|
message = json_format.ParseDict(_message, proto())
|
||||||
return dict(chain.from_iterable(
|
return collections.OrderedDict(chain.from_iterable(
|
||||||
(
|
(
|
||||||
field_to_meta(field, value),
|
field_to_meta(field, value),
|
||||||
(field.name, format_field(field, value)),
|
(field.name, format_field(field, value)),
|
||||||
|
Loading…
Reference in New Issue
Block a user