mirror of
https://github.com/trezor/trezor-firmware.git
synced 2025-01-11 16:00:57 +00:00
protobuf: make MessageType more dict-like, drop _add_ and _extend_
so that SignTx(**tx) works _add_x and _extend_x methods are left-overs from google protobuf and shouldn't be used anymore
This commit is contained in:
parent
e5e0759dc8
commit
c248946b3d
@ -150,46 +150,11 @@ class MessageType:
|
|||||||
def __iter__(self):
|
def __iter__(self):
|
||||||
return self.__dict__.__iter__()
|
return self.__dict__.__iter__()
|
||||||
|
|
||||||
def __getattr__(self, attr):
|
def keys(self):
|
||||||
if attr.startswith("_add_"):
|
return (name for name, _, _ in self.get_fields().values())
|
||||||
return self._additem(attr[5:])
|
|
||||||
|
|
||||||
if attr.startswith("_extend_"):
|
def __getitem__(self, key):
|
||||||
return self._extenditem(attr[8:])
|
return getattr(self, key)
|
||||||
|
|
||||||
raise AttributeError(attr)
|
|
||||||
|
|
||||||
def _extenditem(self, attr):
|
|
||||||
def f(param):
|
|
||||||
try:
|
|
||||||
l = getattr(self, attr)
|
|
||||||
except AttributeError:
|
|
||||||
l = []
|
|
||||||
setattr(self, attr, l)
|
|
||||||
|
|
||||||
l += param
|
|
||||||
|
|
||||||
return f
|
|
||||||
|
|
||||||
def _additem(self, attr):
|
|
||||||
# Add new item for repeated field type
|
|
||||||
for v in self.get_fields().values():
|
|
||||||
if v[0] != attr:
|
|
||||||
continue
|
|
||||||
if not (v[2] & FLAG_REPEATED):
|
|
||||||
raise AttributeError
|
|
||||||
|
|
||||||
try:
|
|
||||||
l = getattr(self, v[0])
|
|
||||||
except AttributeError:
|
|
||||||
l = []
|
|
||||||
setattr(self, v[0], l)
|
|
||||||
|
|
||||||
item = v[1]()
|
|
||||||
l.append(item)
|
|
||||||
return lambda: item
|
|
||||||
|
|
||||||
raise AttributeError
|
|
||||||
|
|
||||||
def _fill_missing(self):
|
def _fill_missing(self):
|
||||||
# fill missing fields
|
# fill missing fields
|
||||||
|
Loading…
Reference in New Issue
Block a user