Skip to content

Commit

Permalink
xmr: serializer - erefs kicked out
Browse files Browse the repository at this point in the history
  • Loading branch information
ph4r05 committed Oct 3, 2018
1 parent 9e3be78 commit 52dd8b3
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 96 deletions.
69 changes: 0 additions & 69 deletions src/apps/monero/xmr/serialize/erefs.py

This file was deleted.

12 changes: 0 additions & 12 deletions src/apps/monero/xmr/serialize/message_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
"""
Expand Down
30 changes: 15 additions & 15 deletions src/apps/monero/xmr/serialize/xmrserialize.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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):
Expand All @@ -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)
Expand Down Expand Up @@ -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)
Expand All @@ -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):
"""
Expand All @@ -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

Expand Down

0 comments on commit 52dd8b3

Please sign in to comment.