mirror of
https://github.com/trezor/trezor-firmware.git
synced 2024-11-22 15:38:11 +00:00
pb2py: fields moved to classmethod get_fields()
- garbage collectable after (de)serialization
This commit is contained in:
parent
08e89cabd2
commit
034289ddd2
62
protob/pb2py
62
protob/pb2py
@ -211,6 +211,39 @@ class Descriptor:
|
|||||||
yield " self.{0} = {0}".format(field.name)
|
yield " self.{0} = {0}".format(field.name)
|
||||||
# fmt: on
|
# fmt: on
|
||||||
|
|
||||||
|
def create_fields_method(self, fields):
|
||||||
|
# fmt: off
|
||||||
|
yield " @classmethod"
|
||||||
|
yield " def get_fields(cls):"
|
||||||
|
yield " return {"
|
||||||
|
for field in fields:
|
||||||
|
comments = []
|
||||||
|
if field.required:
|
||||||
|
comments.append("required")
|
||||||
|
if field.orig.HasField("default_value"):
|
||||||
|
comments.append("default={}".format(field.orig.default_value))
|
||||||
|
|
||||||
|
if comments:
|
||||||
|
comment = " # " + " ".join(comments)
|
||||||
|
else:
|
||||||
|
comment = ""
|
||||||
|
|
||||||
|
if field.repeated:
|
||||||
|
flags = "p.FLAG_REPEATED"
|
||||||
|
else:
|
||||||
|
flags = "0"
|
||||||
|
|
||||||
|
yield " {num}: ('{name}', {type}, {flags}),{comment}".format(
|
||||||
|
num=field.number,
|
||||||
|
name=field.name,
|
||||||
|
type=field.proto_type,
|
||||||
|
flags=flags,
|
||||||
|
comment=comment,
|
||||||
|
)
|
||||||
|
|
||||||
|
yield " }"
|
||||||
|
# fmt: on
|
||||||
|
|
||||||
def process_message(self, message):
|
def process_message(self, message):
|
||||||
logging.debug("Processing message {}".format(message.name))
|
logging.debug("Processing message {}".format(message.name))
|
||||||
msg_id = self.message_types.get(message.name)
|
msg_id = self.message_types.get(message.name)
|
||||||
@ -238,35 +271,10 @@ class Descriptor:
|
|||||||
yield " MESSAGE_WIRE_TYPE = {}".format(msg_id)
|
yield " MESSAGE_WIRE_TYPE = {}".format(msg_id)
|
||||||
|
|
||||||
if fields:
|
if fields:
|
||||||
yield " FIELDS = {"
|
|
||||||
for field in fields:
|
|
||||||
comments = []
|
|
||||||
if field.required:
|
|
||||||
comments.append("required")
|
|
||||||
if field.orig.HasField("default_value"):
|
|
||||||
comments.append("default={}".format(field.orig.default_value))
|
|
||||||
|
|
||||||
if comments:
|
|
||||||
comment = " # " + " ".join(comments)
|
|
||||||
else:
|
|
||||||
comment = ""
|
|
||||||
|
|
||||||
if field.repeated:
|
|
||||||
flags = "p.FLAG_REPEATED"
|
|
||||||
else:
|
|
||||||
flags = "0"
|
|
||||||
|
|
||||||
yield " {num}: ('{name}', {type}, {flags}),{comment}".format(
|
|
||||||
num=field.number,
|
|
||||||
name=field.name,
|
|
||||||
type=field.proto_type,
|
|
||||||
flags=flags,
|
|
||||||
comment=comment,
|
|
||||||
)
|
|
||||||
|
|
||||||
yield " }"
|
|
||||||
yield ""
|
yield ""
|
||||||
yield from self.create_init_method(fields)
|
yield from self.create_init_method(fields)
|
||||||
|
yield ""
|
||||||
|
yield from self.create_fields_method(fields)
|
||||||
|
|
||||||
if not fields and not msg_id:
|
if not fields and not msg_id:
|
||||||
yield " pass"
|
yield " pass"
|
||||||
|
Loading…
Reference in New Issue
Block a user