From 8ce72d8d2d25973b7064b1cf76a6b0b3d62f0ba0 Mon Sep 17 00:00:00 2001 From: Peter Surda Date: Thu, 3 Nov 2016 22:41:36 +0100 Subject: [PATCH] Extended encoding updates - more flexible and developer friendly. Still not active code --- src/helper_msgcoding.py | 32 +++++++++++++++++++++---------- src/messagetypes/__init__.py | 37 ++++++++++++++++++++++++++++++++++++ src/messagetypes/message.py | 8 ++++++++ src/messagetypes/vote.py | 8 ++++++++ 4 files changed, 75 insertions(+), 10 deletions(-) create mode 100644 src/messagetypes/__init__.py create mode 100644 src/messagetypes/message.py create mode 100644 src/messagetypes/vote.py diff --git a/src/helper_msgcoding.py b/src/helper_msgcoding.py index 8d71aca0..04c03fed 100644 --- a/src/helper_msgcoding.py +++ b/src/helper_msgcoding.py @@ -5,14 +5,16 @@ import msgpack import zlib from debug import logger +import messagetypes BITMESSAGE_ENCODING_IGNORE = 0 BITMESSAGE_ENCODING_TRIVIAL = 1 BITMESSAGE_ENCODING_SIMPLE = 2 BITMESSAGE_ENCODING_EXTENDED = 3 + class MsgEncode(object): - def __init__(self, message, encoding = BITMESSAGE_ENCODING_SIMPLE): + def __init__(self, message, encoding=BITMESSAGE_ENCODING_SIMPLE): self.data = None self.encoding = encoding self.length = 0 @@ -27,10 +29,10 @@ class MsgEncode(object): try: self.data = zlib.compress(msgpack.dumps({"": "message", "subject": message['subject'], "message": ['body']}), 9) except zlib.error: - logger.error ("Error compressing message") + logger.error("Error compressing message") raise except msgpack.exceptions.PackException: - logger.error ("Error msgpacking message") + logger.error("Error msgpacking message") raise self.length = len(self.data) @@ -55,20 +57,30 @@ class MsgDecode(object): try: tmp = msgpack.loads(zlib.decompress(data)) except zlib.error: - logger.error ("Error decompressing message") + logger.error("Error decompressing message") raise except (msgpack.exceptions.UnpackException, msgpack.exceptions.ExtraData): - logger.error ("Error msgunpacking message") + logger.error("Error msgunpacking message") raise + try: - if tmp[""] == "message": - self.body = tmp["body"] - self.subject = tmp["subject"] - except: - logger.error ("Malformed message") + msgType = tmp[""] + except KeyError: + logger.error("Message type missing") raise + msgObj = messagetypes.constructObject(data) + if msgObj is None: + raise ValueError("Malformed message") + try: + msgObj.process() + except: + raise ValueError("Malformed message") + if msgType[""] == "message": + self.subject = msgObj.subject + self.body = msgObj.body + def decodeSimple(self, data): bodyPositionIndex = string.find(data, '\nBody:') if bodyPositionIndex > 1: diff --git a/src/messagetypes/__init__.py b/src/messagetypes/__init__.py new file mode 100644 index 00000000..9b5c5a05 --- /dev/null +++ b/src/messagetypes/__init__.py @@ -0,0 +1,37 @@ +from importlib import import_module +from pprint import pprint +from os import path, listdir +import sys + + +def constructObject(data): + try: + classBase = eval(data[""] + "." + data[""].title()) + except NameError: + print "Don't know how to handle message type: \"%s\"" % (data[""]) + return None + try: + returnObj = classBase(data) + except KeyError as e: + print "Missing mandatory key %s" % (e) + return None + except: + print "classBase fail:" + pprint(sys.exc_info()) + return None + else: + return returnObj + +for mod in listdir(path.dirname(__file__)): + if mod == "__init__.py": + continue + splitted = path.splitext(mod) + if splitted[1] != ".py": + continue + try: + import_module("." + splitted[0], "messagetypes") + except ImportError: + print "Error importing %s" % (mod) + pprint(sys.exc_info()) + else: + print "Imported %s" % (mod) diff --git a/src/messagetypes/message.py b/src/messagetypes/message.py new file mode 100644 index 00000000..654e6d6b --- /dev/null +++ b/src/messagetypes/message.py @@ -0,0 +1,8 @@ +class Message: + def __init__(self, data): + self.subject = data["subject"] + self.body = data["body"] + + def process(self): + print "Subject: %s" % (self.subject) + print "Body: %s" % (self.body) diff --git a/src/messagetypes/vote.py b/src/messagetypes/vote.py new file mode 100644 index 00000000..5b692eb2 --- /dev/null +++ b/src/messagetypes/vote.py @@ -0,0 +1,8 @@ +class Vote: + def __init__(self, data): + self.msgid = data["msgid"] + self.vote = data["vote"] + + def process(self): + print "msgid: %s" % (self.msgid) + print "vote: %s" % (self.vote)