mirror of
https://github.com/trezor/trezor-firmware.git
synced 2025-01-11 16:00:57 +00:00
pb2py: use new protobuf message format
This commit is contained in:
parent
36784bf0f5
commit
20b612f8c1
56
tools/pb2py
56
tools/pb2py
@ -7,17 +7,17 @@ from google.protobuf.internal.enum_type_wrapper import EnumTypeWrapper
|
|||||||
|
|
||||||
|
|
||||||
def process_type(t, cls, msg_id, indexfile):
|
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)
|
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):
|
for v in sorted(cls.DESCRIPTOR.fields_by_name.values(), key=lambda x: x.number):
|
||||||
number = v.number
|
number = v.number
|
||||||
fieldname = v.name
|
fieldname = v.name
|
||||||
@ -47,29 +47,30 @@ def process_type(t, cls, msg_id, indexfile):
|
|||||||
|
|
||||||
elif v.type == 11:
|
elif v.type == 11:
|
||||||
# TYPE_MESSAGE = 1
|
# TYPE_MESSAGE = 1
|
||||||
type = "p.EmbeddedMessage(%s)" % v.message_type.name
|
type = v.message_type.name
|
||||||
imports.append("from .%s import %s" %
|
imports.append("from .%s import %s" %
|
||||||
(v.message_type.name, v.message_type.name))
|
(v.message_type.name, v.message_type.name))
|
||||||
|
|
||||||
else:
|
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:
|
if repeated:
|
||||||
flags = ', flags=p.FLAG_REPEATED'
|
flags = 'p.FLAG_REPEATED'
|
||||||
elif required:
|
|
||||||
flags = ', flags=p.FLAG_REQUIRED'
|
|
||||||
else:
|
else:
|
||||||
flags = ''
|
flags = '0'
|
||||||
|
|
||||||
if v.has_default_value:
|
out.append(" %d: ('%s', %s, %s),%s" %
|
||||||
default = ', default=%s' % repr(v.default_value)
|
(number, fieldname, type, flags, comment))
|
||||||
else:
|
|
||||||
default = ''
|
|
||||||
|
|
||||||
out.append("t.add_field(%d, '%s', %s%s%s)" %
|
# print fieldname, number, type, repeated, comment
|
||||||
(number, fieldname, type, flags, default))
|
|
||||||
|
|
||||||
# print fieldname, number, type, repeated, default
|
|
||||||
# print v.__dict__
|
# print v.__dict__
|
||||||
# print v.CPPTYPE_STRING
|
# print v.CPPTYPE_STRING
|
||||||
# print v.LABEL_REPEATED
|
# print v.LABEL_REPEATED
|
||||||
@ -78,7 +79,14 @@ def process_type(t, cls, msg_id, indexfile):
|
|||||||
# v.label == 3 # repeated
|
# v.label == 3 # repeated
|
||||||
# print v.number
|
# 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
|
return imports + out
|
||||||
|
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user