mirror of
https://github.com/trezor/trezor-firmware.git
synced 2025-05-19 23:38:46 +00:00
pb2py: Use generators instead of lists
This commit is contained in:
parent
0ad0035aad
commit
d32cbe3466
67
tools/pb2py
67
tools/pb2py
@ -30,16 +30,34 @@ def remove_from_start(s, prefix):
|
|||||||
return s
|
return s
|
||||||
|
|
||||||
|
|
||||||
|
def process_message_imports(descriptor):
|
||||||
|
imports = set()
|
||||||
|
|
||||||
|
for field in descriptor.fields:
|
||||||
|
if field.type == field.TYPE_MESSAGE:
|
||||||
|
imports.add(field.message_type.name)
|
||||||
|
|
||||||
|
for name in sorted(imports):
|
||||||
|
yield create_message_import(name)
|
||||||
|
|
||||||
|
|
||||||
def process_message(descriptor, protobuf_module, msg_id, indexfile, is_upy):
|
def process_message(descriptor, protobuf_module, msg_id, indexfile, is_upy):
|
||||||
print(" * type %s" % descriptor.name)
|
print(" * type %s" % descriptor.name)
|
||||||
|
|
||||||
imports = []
|
if is_upy:
|
||||||
out = ["", "", "class %s(p.MessageType):" % descriptor.name, ]
|
yield "import protobuf as p"
|
||||||
|
else:
|
||||||
|
yield "from .. import protobuf as p"
|
||||||
|
yield from process_message_imports(descriptor)
|
||||||
|
|
||||||
|
yield ""
|
||||||
|
yield ""
|
||||||
|
yield "class %s(p.MessageType):" % descriptor.name
|
||||||
|
|
||||||
if descriptor.fields_by_number:
|
if descriptor.fields_by_number:
|
||||||
out.append(" FIELDS = {")
|
yield " FIELDS = {"
|
||||||
elif msg_id is None:
|
elif msg_id is None:
|
||||||
out.append(" pass")
|
yield " pass"
|
||||||
|
|
||||||
for number, field in descriptor.fields_by_number.items():
|
for number, field in descriptor.fields_by_number.items():
|
||||||
field_name = field.name
|
field_name = field.name
|
||||||
@ -60,7 +78,6 @@ def process_message(descriptor, protobuf_module, msg_id, indexfile, is_upy):
|
|||||||
|
|
||||||
if field.type == field.TYPE_MESSAGE:
|
if field.type == field.TYPE_MESSAGE:
|
||||||
field_type = field.message_type.name
|
field_type = field.message_type.name
|
||||||
imports.append(create_message_import(field_type))
|
|
||||||
else:
|
else:
|
||||||
try:
|
try:
|
||||||
field_type = types[field.type]
|
field_type = types[field.type]
|
||||||
@ -83,35 +100,23 @@ def process_message(descriptor, protobuf_module, msg_id, indexfile, is_upy):
|
|||||||
else:
|
else:
|
||||||
flags = '0'
|
flags = '0'
|
||||||
|
|
||||||
out.append(" %d: ('%s', %s, %s),%s" %
|
yield " %d: ('%s', %s, %s),%s" % (number, field_name, field_type, flags, comment)
|
||||||
(number, field_name, field_type, flags, comment))
|
|
||||||
|
|
||||||
if descriptor.fields_by_name:
|
if descriptor.fields_by_name:
|
||||||
out.append(" }")
|
yield " }"
|
||||||
|
|
||||||
if msg_id is not None:
|
if msg_id is not None:
|
||||||
out.append(" MESSAGE_WIRE_TYPE = %d" % msg_id)
|
yield " MESSAGE_WIRE_TYPE = %d" % msg_id
|
||||||
if indexfile is not None:
|
if indexfile is not None:
|
||||||
indexfile.write(create_const(t, msg_id, is_upy))
|
indexfile.write(create_const(t, msg_id, is_upy))
|
||||||
|
|
||||||
# Remove duplicate imports
|
|
||||||
imports = sorted(list(set(imports)))
|
|
||||||
|
|
||||||
if is_upy:
|
|
||||||
imports = ['import protobuf as p'] + imports
|
|
||||||
else:
|
|
||||||
imports = ['from .. import protobuf as p'] + imports
|
|
||||||
|
|
||||||
return imports + out
|
|
||||||
|
|
||||||
|
|
||||||
def process_enum(descriptor, is_upy):
|
def process_enum(descriptor, is_upy):
|
||||||
out = []
|
print(" * enum %s" % descriptor.name)
|
||||||
|
|
||||||
if is_upy:
|
if is_upy:
|
||||||
out += ("from micropython import const", "")
|
yield "from micropython import const"
|
||||||
|
yield ""
|
||||||
print(" * enum %s" % descriptor.name)
|
|
||||||
|
|
||||||
for name, value in descriptor.values_by_name.items():
|
for name, value in descriptor.values_by_name.items():
|
||||||
# Remove type name from the beginning of the constant
|
# Remove type name from the beginning of the constant
|
||||||
@ -125,9 +130,7 @@ def process_enum(descriptor, is_upy):
|
|||||||
enum_prefix, _ = enum_prefix.rsplit("Type", 1)
|
enum_prefix, _ = enum_prefix.rsplit("Type", 1)
|
||||||
name = remove_from_start(name, "%s_" % enum_prefix)
|
name = remove_from_start(name, "%s_" % enum_prefix)
|
||||||
|
|
||||||
out.append(create_const(name, value.number, is_upy))
|
yield create_const(name, value.number, is_upy)
|
||||||
|
|
||||||
return out
|
|
||||||
|
|
||||||
|
|
||||||
def process_file(descriptor, protobuf_module, genpath, indexfile, modlist, is_upy):
|
def process_file(descriptor, protobuf_module, genpath, indexfile, modlist, is_upy):
|
||||||
@ -144,7 +147,6 @@ def process_file(descriptor, protobuf_module, genpath, indexfile, modlist, is_up
|
|||||||
msg_id = None
|
msg_id = None
|
||||||
|
|
||||||
out = process_message(message_descriptor, protobuf_module, msg_id, indexfile, is_upy)
|
out = process_message(message_descriptor, protobuf_module, msg_id, indexfile, is_upy)
|
||||||
|
|
||||||
write_to_file(genpath, name, out)
|
write_to_file(genpath, name, out)
|
||||||
if modlist:
|
if modlist:
|
||||||
modlist.write(create_message_import(name) + "\n")
|
modlist.write(create_message_import(name) + "\n")
|
||||||
@ -158,13 +160,10 @@ def process_file(descriptor, protobuf_module, genpath, indexfile, modlist, is_up
|
|||||||
|
|
||||||
def write_to_file(genpath, t, out):
|
def write_to_file(genpath, t, out):
|
||||||
# Write generated sourcecode to given file
|
# Write generated sourcecode to given file
|
||||||
f = open(os.path.join(genpath, "%s.py" % t), 'w')
|
with open(os.path.join(genpath, "%s.py" % t), 'w') as f:
|
||||||
out = ["# Automatically generated by pb2py"] + out
|
f.write("# Automatically generated by pb2py\n")
|
||||||
|
for line in out:
|
||||||
data = "\n".join(out) + "\n"
|
f.write(line + "\n")
|
||||||
|
|
||||||
f.write(data)
|
|
||||||
f.close()
|
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
|
Loading…
Reference in New Issue
Block a user