1
0
mirror of https://github.com/trezor/trezor-firmware.git synced 2024-11-10 01:30:19 +00:00
trezor-firmware/firmware/nem_mosaics.py

123 lines
3.2 KiB
Python
Raw Normal View History

2018-01-11 23:03:55 +00:00
#!/usr/bin/env python
2017-12-24 12:33:18 +00:00
import json
import os
import sys
2017-10-09 17:06:02 +00:00
2017-12-24 12:33:18 +00:00
import collections
import itertools
import numbers
2017-10-09 17:06:02 +00:00
2017-07-20 21:58:19 +00:00
from google.protobuf import json_format
2017-12-18 21:48:52 +00:00
try:
basestring
except NameError:
basestring = (str, bytes)
2017-07-20 21:58:19 +00:00
HEADER_TEMPLATE = """
// This file is automatically generated by nem_mosaics.py -- DO NOT EDIT!
#ifndef __NEM_MOSAICS_H__
#define __NEM_MOSAICS_H__
#include "types.pb.h"
#define NEM_MOSAIC_DEFINITIONS_COUNT ({count})
extern const NEMMosaicDefinition NEM_MOSAIC_DEFINITIONS[NEM_MOSAIC_DEFINITIONS_COUNT];
extern const NEMMosaicDefinition *NEM_MOSAIC_DEFINITION_XEM;
#endif
2017-12-24 12:33:18 +00:00
""".lstrip() # noqa: E501
2017-07-20 21:58:19 +00:00
CODE_TEMPLATE = """
// This file is automatically generated by nem_mosaics.py -- DO NOT EDIT!
#include "nem_mosaics.h"
const NEMMosaicDefinition NEM_MOSAIC_DEFINITIONS[NEM_MOSAIC_DEFINITIONS_COUNT] = {code};
const NEMMosaicDefinition *NEM_MOSAIC_DEFINITION_XEM = NEM_MOSAIC_DEFINITIONS;
2017-12-24 12:33:18 +00:00
""".lstrip() # noqa: E501
2017-07-20 21:58:19 +00:00
def format_primitive(value):
2017-10-09 17:06:02 +00:00
if isinstance(value, bool):
2017-07-20 21:58:19 +00:00
return ("false", "true")[value]
2017-10-09 17:06:02 +00:00
elif isinstance(value, numbers.Number):
2017-07-20 21:58:19 +00:00
return str(value)
2017-10-09 17:06:02 +00:00
elif isinstance(value, basestring):
2017-07-20 21:58:19 +00:00
return json.dumps(value)
2017-10-09 17:06:02 +00:00
elif isinstance(value, collections.Sequence):
2017-07-20 21:58:19 +00:00
return "{ " + ", ".join(
format_primitive(item) for item in value
) + " }"
else:
raise TypeError
2017-12-24 12:33:18 +00:00
2017-07-20 21:58:19 +00:00
def format_struct(struct):
return "{\n" + "\n".join(
2017-12-24 12:33:18 +00:00
"\t.{0} = {1},".format(member, value)
for member, value in struct.items()
2017-07-20 21:58:19 +00:00
) + "\n}"
def format_field(field, value):
if field.message_type is not None:
raise TypeError
elif field.enum_type:
2017-12-24 12:33:18 +00:00
type_name = field.enum_type.name
enum_name = field.enum_type.values_by_number[value].name
return "{0}_{1}".format(type_name, enum_name)
2017-07-20 21:58:19 +00:00
elif hasattr(value, "_values"):
return format_primitive(value._values)
else:
return format_primitive(value)
2017-12-24 12:33:18 +00:00
2017-07-20 21:58:19 +00:00
def field_to_meta(field, value):
2017-10-09 17:06:02 +00:00
if field.label == field.LABEL_REPEATED:
return ("{}_count".format(field.name), format_primitive(len(value)))
2017-07-20 21:58:19 +00:00
else:
return ("has_{}".format(field.name), format_primitive(True))
2017-12-24 12:33:18 +00:00
2017-07-20 21:58:19 +00:00
def message_to_struct(_message, proto):
message = json_format.ParseDict(_message, proto())
2017-12-24 12:33:18 +00:00
return collections.OrderedDict(itertools.chain.from_iterable(
2017-07-20 21:58:19 +00:00
(
field_to_meta(field, value),
(field.name, format_field(field, value)),
) for field, value in message.ListFields()
))
2017-12-24 12:33:18 +00:00
2017-07-20 21:58:19 +00:00
def format_message(message, proto):
return format_struct(message_to_struct(message, proto))
2017-12-24 12:33:18 +00:00
2017-07-20 21:58:19 +00:00
def format_messages(messages, proto):
return "{" + ",\n".join(
format_message(message, proto) for message in messages
) + "}"
2017-12-24 12:33:18 +00:00
2017-07-20 21:58:19 +00:00
if __name__ == "__main__":
os.chdir(os.path.abspath(os.path.dirname(__file__)))
2017-12-24 12:33:18 +00:00
sys.path.insert(0, "protob")
import types_pb2 as types
messages = json.load(open("defs/nem/nem_mosaics.json"))
2017-07-20 21:58:19 +00:00
with open("nem_mosaics.h", "w+") as f:
2017-12-24 12:33:18 +00:00
f.write(HEADER_TEMPLATE.format(
count=format_primitive(len(messages)))
)
2017-07-20 21:58:19 +00:00
with open("nem_mosaics.c", "w+") as f:
2017-12-24 12:33:18 +00:00
f.write(CODE_TEMPLATE.format(
code=format_messages(messages, types.NEMMosaicDefinition))
)