From 52dd8b3b8fd6989b51a49dedb7283c28d53e1085 Mon Sep 17 00:00:00 2001 From: Dusan Klinec Date: Wed, 3 Oct 2018 17:23:50 +0200 Subject: [PATCH] xmr: serializer - erefs kicked out --- src/apps/monero/xmr/serialize/erefs.py | 69 ------------------- .../monero/xmr/serialize/message_types.py | 12 ---- src/apps/monero/xmr/serialize/xmrserialize.py | 30 ++++---- 3 files changed, 15 insertions(+), 96 deletions(-) delete mode 100644 src/apps/monero/xmr/serialize/erefs.py diff --git a/src/apps/monero/xmr/serialize/erefs.py b/src/apps/monero/xmr/serialize/erefs.py deleted file mode 100644 index bd4c7aa5f..000000000 --- a/src/apps/monero/xmr/serialize/erefs.py +++ /dev/null @@ -1,69 +0,0 @@ -class ElemRefObj: - def __repr__(self): - return "RefObj" - - -class ElemRefArr: - def __repr__(self): - return "RefAssoc" - - -def is_elem_ref(elem_ref): - """ - Returns true if the elem_ref is an element reference - """ - return ( - elem_ref - and isinstance(elem_ref, tuple) - and len(elem_ref) == 3 - and (elem_ref[0] == ElemRefObj or elem_ref[0] == ElemRefArr) - ) - - -def has_elem(elem_ref): - if not is_elem_ref(elem_ref): - return False - elif elem_ref[0] == ElemRefObj: - return hasattr(elem_ref[1], elem_ref[2]) - elif elem_ref[0] == ElemRefArr: - return elem_ref[2] in elem_ref[1] - - -def get_elem(elem_ref, default=None): - """ - Gets the element referenced by elem_ref or returns the elem_ref directly if its not a reference. - """ - if not is_elem_ref(elem_ref): - return elem_ref - elif elem_ref[0] == ElemRefObj: - return getattr(elem_ref[1], elem_ref[2], default) - elif elem_ref[0] == ElemRefArr: - return elem_ref[1][elem_ref[2]] - - -def set_elem(elem_ref, elem): - """ - Sets element referenced by the elem_ref. Returns the elem. - """ - if elem_ref is None or elem_ref == elem or not is_elem_ref(elem_ref): - return elem - - elif elem_ref[0] == ElemRefObj: - setattr(elem_ref[1], elem_ref[2], elem) - return elem - - elif elem_ref[0] == ElemRefArr: - elem_ref[1][elem_ref[2]] = elem - return elem - - -def eref(obj, key, is_assoc=None): - """ - Returns element reference - """ - if obj is None: - return None - if isinstance(key, int) or (is_assoc is not None and is_assoc): - return ElemRefArr, get_elem(obj), key - else: - return ElemRefObj, get_elem(obj), key diff --git a/src/apps/monero/xmr/serialize/message_types.py b/src/apps/monero/xmr/serialize/message_types.py index becae5db8..62261236f 100644 --- a/src/apps/monero/xmr/serialize/message_types.py +++ b/src/apps/monero/xmr/serialize/message_types.py @@ -87,18 +87,6 @@ def __repr__(self): def f_specs(cls): return () - def _field(self, fname=None, idx=None): - fld = None - specs = self.f_specs() - if fname is not None: - fld = [x for x in specs if x[0] == fname][0] - elif idx is not None: - fld = specs[idx] - return fld - - def _msg_field(self, ar, fname=None, idx=None, **kwargs): - return ar.message_field(self, self._field(fname=fname, idx=idx), **kwargs) - def container_elem_type(container_type, params): """ diff --git a/src/apps/monero/xmr/serialize/xmrserialize.py b/src/apps/monero/xmr/serialize/xmrserialize.py index 8d2fdbfc4..d6050ee76 100644 --- a/src/apps/monero/xmr/serialize/xmrserialize.py +++ b/src/apps/monero/xmr/serialize/xmrserialize.py @@ -32,7 +32,6 @@ ''' from apps.monero.xmr.serialize.base_types import IntType, UVarintType, XmrType -from apps.monero.xmr.serialize.erefs import eref, get_elem, set_elem from apps.monero.xmr.serialize.int_serialize import ( dump_uint, dump_uvarint, @@ -158,9 +157,9 @@ def message_field(self, msg, field, fvalue=None): Dumps/Loads message field """ if self.writing: - self._dump_message_field(msg, field, fvalue=fvalue) + return self._dump_message_field(msg, field, fvalue=fvalue) else: - self._load_message_field(msg, field) + return self._load_message_field(field) def _get_type(self, elem_type): if issubclass(elem_type, XmrType): @@ -178,32 +177,32 @@ def field(self, elem=None, elem_type=None, params=None): etype = self._get_type(elem_type) if self._is_type(etype, UVarintType): - fvalue = self.uvarint(get_elem(elem)) + fvalue = self.uvarint(elem) elif self._is_type(etype, IntType): - fvalue = self.uint(get_elem(elem), elem_type) + fvalue = self.uint(elem, elem_type) elif self._is_type(etype, BlobType): - fvalue = self.blob(get_elem(elem), elem_type, params) + fvalue = self.blob(elem, elem_type, params) elif self._is_type(etype, UnicodeType): - fvalue = self.unicode_type(get_elem(elem)) + fvalue = self.unicode_type(elem) elif self._is_type(etype, VariantType): - fvalue = self.variant(get_elem(elem), elem_type, params) + fvalue = self.variant(elem, elem_type, params) elif self._is_type(etype, ContainerType): - fvalue = self.container(get_elem(elem), elem_type, params) + fvalue = self.container(elem, elem_type, params) elif self._is_type(etype, MessageType): - fvalue = self.message(get_elem(elem), elem_type) + fvalue = self.message(elem, elem_type) else: raise TypeError( "unknown type: %s %s %s" % (elem_type, type(elem_type), elem) ) - return fvalue if self.writing else set_elem(elem, fvalue) + return fvalue def dump_field(self, elem, elem_type, params=None): return self.field(elem, elem_type, params) @@ -251,7 +250,7 @@ def _load_container(self, container_type, params=None, container=None): fvalue = self.load_field( elem_type, params[1:] if params else None, - eref(res, i) if container else None, + None, ) if not container: res.append(fvalue) @@ -265,13 +264,13 @@ def _dump_message_field(self, msg, field, fvalue=None): fvalue = getattr(msg, fname, None) if fvalue is None else fvalue self.dump_field(fvalue, ftype, params) - def _load_message_field(self, msg, field): + def _load_message_field(self, field): """ Loads message field from the reader. Field is defined by the message field specification. Returns loaded value, supports field reference. """ fname, ftype, params = field[0], field[1], field[2:] - self.load_field(ftype, params, eref(msg, fname)) + return self.load_field(ftype, params, None) def _dump_message(self, msg, msg_type=None): """ @@ -290,7 +289,8 @@ def _load_message(self, msg_type, msg=None): msg = msg_type() if msg is None else msg fields = msg_type.f_specs() if msg_type else msg.__class__.f_specs() for field in fields: - self._load_message_field(msg, field) + fval = self._load_message_field(field) + setattr(msg, field[0], fval) return msg