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

pb2py: use new protobuf message format

This commit is contained in:
Jan Pochyla 2016-10-26 17:32:08 +02:00
parent 36784bf0f5
commit 20b612f8c1

View File

@ -7,17 +7,17 @@ from google.protobuf.internal.enum_type_wrapper import EnumTypeWrapper
def process_type(t, cls, msg_id, indexfile):
imports = ["import protobuf as p", "from micropython import const"]
out = ["t = p.MessageType('%s')" % t, ]
if msg_id is not None:
out.append("t.wire_type = const(%d)" % msg_id)
if indexfile is not None:
indexfile.write("%s = const(%d)\n" % (t, msg_id))
print(" * type %s" % t)
imports = ["import protobuf as p", "from micropython import const", ]
out = ["", "class %s(p.MessageType):" % t, ]
if cls.DESCRIPTOR.fields_by_name:
out.append(" FIELDS = {")
elif msg_id is None:
out.append(" pass")
for v in sorted(cls.DESCRIPTOR.fields_by_name.values(), key=lambda x: x.number):
number = v.number
fieldname = v.name
@ -47,29 +47,30 @@ def process_type(t, cls, msg_id, indexfile):
elif v.type == 11:
# TYPE_MESSAGE = 1
type = "p.EmbeddedMessage(%s)" % v.message_type.name
type = v.message_type.name
imports.append("from .%s import %s" %
(v.message_type.name, v.message_type.name))
else:
raise Exception("Unknown field type %s for field %s" % (v.type, k))
raise Exception("Unknown field type %s for field %s" %
(v.type, fieldname))
if required:
comment = ' # required'
elif v.has_default_value:
comment = ' # default=%s' % repr(v.default_value)
else:
comment = ''
if repeated:
flags = ', flags=p.FLAG_REPEATED'
elif required:
flags = ', flags=p.FLAG_REQUIRED'
flags = 'p.FLAG_REPEATED'
else:
flags = ''
flags = '0'
if v.has_default_value:
default = ', default=%s' % repr(v.default_value)
else:
default = ''
out.append(" %d: ('%s', %s, %s),%s" %
(number, fieldname, type, flags, comment))
out.append("t.add_field(%d, '%s', %s%s%s)" %
(number, fieldname, type, flags, default))
# print fieldname, number, type, repeated, default
# print fieldname, number, type, repeated, comment
# print v.__dict__
# print v.CPPTYPE_STRING
# print v.LABEL_REPEATED
@ -78,7 +79,14 @@ def process_type(t, cls, msg_id, indexfile):
# v.label == 3 # repeated
# print v.number
out.append("%s = t" % t)
if cls.DESCRIPTOR.fields_by_name:
out.append(" }")
if msg_id is not None:
out.append(" MESSAGE_WIRE_TYPE = %d" % msg_id)
if indexfile is not None:
indexfile.write("%s = const(%d)\n" % (t, msg_id))
return imports + out