1
0
mirror of https://github.com/trezor/trezor-firmware.git synced 2024-11-26 17:38:39 +00:00

protobuf.py uses get_fields()

This commit is contained in:
Dusan Klinec 2018-09-20 13:53:21 +02:00
parent 376503817c
commit 6b32e33c58
No known key found for this signature in database
GPG Key ID: 6337E118CCBCE103

View File

@ -127,7 +127,10 @@ class UnicodeType:
class MessageType: class MessageType:
WIRE_TYPE = 2 WIRE_TYPE = 2
FIELDS = {}
@classmethod
def get_fields(cls):
return {}
def __init__(self, **kwargs): def __init__(self, **kwargs):
for kw in kwargs: for kw in kwargs:
@ -171,7 +174,7 @@ class MessageType:
def _additem(self, attr): def _additem(self, attr):
# Add new item for repeated field type # Add new item for repeated field type
for v in self.FIELDS.values(): for v in self.get_fields().values():
if v[0] != attr: if v[0] != attr:
continue continue
if not (v[2] & FLAG_REPEATED): if not (v[2] & FLAG_REPEATED):
@ -191,7 +194,7 @@ class MessageType:
def _fill_missing(self): def _fill_missing(self):
# fill missing fields # fill missing fields
for fname, ftype, fflags in self.FIELDS.values(): for fname, ftype, fflags in self.get_fields().values():
if not hasattr(self, fname): if not hasattr(self, fname):
if fflags & FLAG_REPEATED: if fflags & FLAG_REPEATED:
setattr(self, fname, []) setattr(self, fname, [])
@ -235,7 +238,7 @@ FLAG_REPEATED = 1
def load_message(reader, msg_type): def load_message(reader, msg_type):
fields = msg_type.FIELDS fields = msg_type.get_fields()
msg = msg_type() msg = msg_type()
while True: while True:
@ -296,7 +299,7 @@ def load_message(reader, msg_type):
def dump_message(writer, msg): def dump_message(writer, msg):
repvalue = [0] repvalue = [0]
mtype = msg.__class__ mtype = msg.__class__
fields = mtype.FIELDS fields = mtype.get_fields()
for ftag in fields: for ftag in fields:
fname, ftype, fflags = fields[ftag] fname, ftype, fflags = fields[ftag]
@ -424,7 +427,7 @@ def value_to_proto(ftype, value):
def dict_to_proto(message_type, d): def dict_to_proto(message_type, d):
params = {} params = {}
for fname, ftype, fflags in message_type.FIELDS.values(): for fname, ftype, fflags in message_type.get_fields().values():
repeated = fflags & FLAG_REPEATED repeated = fflags & FLAG_REPEATED
value = d.get(fname) value = d.get(fname)
if value is None: if value is None: