From dc5b5646f4e9b09f5e18b279d1542ecd788fb06d Mon Sep 17 00:00:00 2001
From: Hendrik Buschmeier <hbuschme@uni-bielefeld.de>
Date: Fri, 19 Dec 2014 11:44:09 +0100
Subject: [PATCH] Reduced code duplication, MessageConverter now inherits from
 IUConverter.

---
 ipaacalib/python/src/ipaaca/converter.py | 81 ++----------------------
 1 file changed, 4 insertions(+), 77 deletions(-)

diff --git a/ipaacalib/python/src/ipaaca/converter.py b/ipaacalib/python/src/ipaaca/converter.py
index fac72e8..3782cbd 100644
--- a/ipaacalib/python/src/ipaaca/converter.py
+++ b/ipaacalib/python/src/ipaaca/converter.py
@@ -168,87 +168,14 @@ class IUConverter(rsb.converter.Converter):
 			raise ValueError("Inacceptable dataType %s" % type)
 
 
-class MessageConverter(rsb.converter.Converter):
+class MessageConverter(IUConverter):
 	'''
-	Converter class for Full IU representations
-	wire:bytearray <-> wire-schema:ipaaca-full-iu <-> class ipaacaRSB.IU
+	Converter class for Message IU representations
+	wire:bytearray <-> wire-schema:ipaaca-messageiu <-> class ipaacaRSB.IU
 	'''
 	def __init__(self, wireSchema="ipaaca-messageiu", dataType=ipaaca.iu.Message):
-		super(IUConverter, self).__init__(bytearray, dataType, wireSchema)
-
-	def serialize(self, iu):
-		pbo = ipaaca_pb2.IU()
-		pbo.uid = iu._uid
-		pbo.revision = iu._revision
-		pbo.category = iu._category
-		pbo.payload_type = iu._payload_type
-		pbo.owner_name = iu._owner_name
-		pbo.committed = iu._committed
-		am=ipaaca_pb2.IU.PUSH #default
-		if iu._access_mode == ipaaca.iu.IUAccessMode.MESSAGE:
-			am=ipaaca_pb2.IU.MESSAGE
-		# TODO add other types later
-		pbo.access_mode = am
-		pbo.read_only = iu._read_only
-		for k,v in iu._payload.items():
-			entry = pbo.payload.add()
-			pack_payload_entry(entry, k, v)
-		for type_ in iu._links.keys():
-			linkset = pbo.links.add()
-			linkset.type = type_
-			linkset.targets.extend(iu._links[type_])
-		ws = "ipaaca-messageiu" if iu._access_mode == ipaaca.iu.IUAccessMode.MESSAGE else self.wireSchema
-		return bytearray(pbo.SerializeToString()), ws
+		super(MessageConverter, self).__init__(wireSchema=wireSchema, dataType=dataType)
 
-	def deserialize(self, byte_stream, ws):
-		type = self.getDataType()
-		if type == ipaaca.iu.IU or type == ipaaca.iu.Message:
-			pbo = ipaaca_pb2.IU()
-			pbo.ParseFromString( str(byte_stream) )
-			if pbo.access_mode == ipaaca_pb2.IU.PUSH:
-				_payload = {}
-				for entry in pbo.payload:
-					_payload[entry.key] = unpack_payload_entry(entry)
-				_links = collections.defaultdict(set)
-				for linkset in pbo.links:
-					for target_uid in linkset.targets:
-						_links[linkset.type].add(target_uid)
-				remote_push_iu = ipaaca.iu.RemotePushIU(
-						uid=pbo.uid,
-						revision=pbo.revision,
-						read_only = pbo.read_only,
-						owner_name = pbo.owner_name,
-						category = pbo.category,
-						payload_type = pbo.payload_type,
-						committed = pbo.committed,
-						payload=_payload,
-						links=_links
-					)
-				return remote_push_iu
-			elif pbo.access_mode ==  ipaaca_pb2.IU.MESSAGE:
-				_payload = {}
-				for entry in pbo.payload:
-					_payload[entry.key] = unpack_payload_entry(entry)
-				_links = collections.defaultdict(set)
-				for linkset in pbo.links:
-					for target_uid in linkset.targets:
-						_links[linkset.type].add(target_uid)
-				remote_message = ipaaca.iu.RemoteMessage(
-						uid=pbo.uid,
-						revision=pbo.revision,
-						read_only = pbo.read_only,
-						owner_name = pbo.owner_name,
-						category = pbo.category,
-						payload_type = pbo.payload_type,
-						committed = pbo.committed,
-						payload=_payload,
-						links=_links
-					)
-				return remote_message
-			else:
-				raise Exception("We can only handle IUs with access mode 'PUSH' or 'MESSAGE' for now!")
-		else:
-			raise ValueError("Inacceptable dataType %s" % type)
 
 class IULinkUpdate(object):
 
-- 
GitLab