mirror of
https://github.com/trezor/trezor-firmware.git
synced 2024-12-23 14:58:09 +00:00
nem_mosaics: Fix Flake8 warnings
This commit is contained in:
parent
7092951a40
commit
88230e33c4
@ -1,13 +1,13 @@
|
|||||||
#!/usr/bin/env python
|
#!/usr/bin/env python
|
||||||
import json, os, sys
|
import json
|
||||||
|
import os
|
||||||
|
import sys
|
||||||
|
|
||||||
import collections, numbers
|
import collections
|
||||||
|
import itertools
|
||||||
|
import numbers
|
||||||
|
|
||||||
from google.protobuf import json_format
|
from google.protobuf import json_format
|
||||||
from itertools import chain
|
|
||||||
|
|
||||||
sys.path.insert(0, os.path.join(os.path.dirname(__file__), "protob"))
|
|
||||||
import types_pb2 as types
|
|
||||||
|
|
||||||
try:
|
try:
|
||||||
basestring
|
basestring
|
||||||
@ -28,7 +28,7 @@ extern const NEMMosaicDefinition NEM_MOSAIC_DEFINITIONS[NEM_MOSAIC_DEFINITIONS_C
|
|||||||
extern const NEMMosaicDefinition *NEM_MOSAIC_DEFINITION_XEM;
|
extern const NEMMosaicDefinition *NEM_MOSAIC_DEFINITION_XEM;
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
""".lstrip()
|
""".lstrip() # noqa: E501
|
||||||
|
|
||||||
CODE_TEMPLATE = """
|
CODE_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!
|
||||||
@ -38,7 +38,8 @@ CODE_TEMPLATE = """
|
|||||||
const NEMMosaicDefinition NEM_MOSAIC_DEFINITIONS[NEM_MOSAIC_DEFINITIONS_COUNT] = {code};
|
const NEMMosaicDefinition NEM_MOSAIC_DEFINITIONS[NEM_MOSAIC_DEFINITIONS_COUNT] = {code};
|
||||||
|
|
||||||
const NEMMosaicDefinition *NEM_MOSAIC_DEFINITION_XEM = NEM_MOSAIC_DEFINITIONS;
|
const NEMMosaicDefinition *NEM_MOSAIC_DEFINITION_XEM = NEM_MOSAIC_DEFINITIONS;
|
||||||
""".lstrip()
|
""".lstrip() # noqa: E501
|
||||||
|
|
||||||
|
|
||||||
def format_primitive(value):
|
def format_primitive(value):
|
||||||
if isinstance(value, bool):
|
if isinstance(value, bool):
|
||||||
@ -54,9 +55,11 @@ def format_primitive(value):
|
|||||||
else:
|
else:
|
||||||
raise TypeError
|
raise TypeError
|
||||||
|
|
||||||
|
|
||||||
def format_struct(struct):
|
def format_struct(struct):
|
||||||
return "{\n" + "\n".join(
|
return "{\n" + "\n".join(
|
||||||
"\t.{0} = {1},".format(member, value) for member, value in struct.items()
|
"\t.{0} = {1},".format(member, value)
|
||||||
|
for member, value in struct.items()
|
||||||
) + "\n}"
|
) + "\n}"
|
||||||
|
|
||||||
|
|
||||||
@ -64,42 +67,56 @@ def format_field(field, value):
|
|||||||
if field.message_type is not None:
|
if field.message_type is not None:
|
||||||
raise TypeError
|
raise TypeError
|
||||||
elif field.enum_type:
|
elif field.enum_type:
|
||||||
return "{0}_{1}".format(field.enum_type.name, field.enum_type.values_by_number[value].name)
|
type_name = field.enum_type.name
|
||||||
|
enum_name = field.enum_type.values_by_number[value].name
|
||||||
|
return "{0}_{1}".format(type_name, enum_name)
|
||||||
elif hasattr(value, "_values"):
|
elif hasattr(value, "_values"):
|
||||||
return format_primitive(value._values)
|
return format_primitive(value._values)
|
||||||
else:
|
else:
|
||||||
return format_primitive(value)
|
return format_primitive(value)
|
||||||
|
|
||||||
|
|
||||||
def field_to_meta(field, value):
|
def field_to_meta(field, value):
|
||||||
if field.label == field.LABEL_REPEATED:
|
if field.label == field.LABEL_REPEATED:
|
||||||
return ("{}_count".format(field.name), format_primitive(len(value)))
|
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 collections.OrderedDict(chain.from_iterable(
|
return collections.OrderedDict(itertools.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)),
|
||||||
) for field, value in message.ListFields()
|
) for field, value in message.ListFields()
|
||||||
))
|
))
|
||||||
|
|
||||||
|
|
||||||
def format_message(message, proto):
|
def format_message(message, proto):
|
||||||
return format_struct(message_to_struct(message, proto))
|
return format_struct(message_to_struct(message, proto))
|
||||||
|
|
||||||
|
|
||||||
def format_messages(messages, proto):
|
def format_messages(messages, proto):
|
||||||
return "{" + ",\n".join(
|
return "{" + ",\n".join(
|
||||||
format_message(message, proto) for message in messages
|
format_message(message, proto) for message in messages
|
||||||
) + "}"
|
) + "}"
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
os.chdir(os.path.abspath(os.path.dirname(__file__)))
|
os.chdir(os.path.abspath(os.path.dirname(__file__)))
|
||||||
|
|
||||||
|
sys.path.insert(0, "protob")
|
||||||
|
import types_pb2 as types
|
||||||
|
|
||||||
messages = json.load(open("nem_mosaics.json"))
|
messages = json.load(open("nem_mosaics.json"))
|
||||||
|
|
||||||
with open("nem_mosaics.h", "w+") as f:
|
with open("nem_mosaics.h", "w+") as f:
|
||||||
f.write(HEADER_TEMPLATE.format(count=format_primitive(len(messages))))
|
f.write(HEADER_TEMPLATE.format(
|
||||||
|
count=format_primitive(len(messages)))
|
||||||
|
)
|
||||||
|
|
||||||
with open("nem_mosaics.c", "w+") as f:
|
with open("nem_mosaics.c", "w+") as f:
|
||||||
f.write(CODE_TEMPLATE.format(code=format_messages(messages, types.NEMMosaicDefinition)))
|
f.write(CODE_TEMPLATE.format(
|
||||||
|
code=format_messages(messages, types.NEMMosaicDefinition))
|
||||||
|
)
|
||||||
|
Loading…
Reference in New Issue
Block a user