Extended encoding updates

- more flexible and developer friendly. Still not active code
This commit is contained in:
Peter Šurda 2016-11-03 22:41:36 +01:00
parent 020a78b776
commit 8ce72d8d2d
Signed by untrusted user: PeterSurda
GPG Key ID: 0C5F50C0B5F37D87
4 changed files with 75 additions and 10 deletions

View File

@ -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:

View File

@ -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)

View File

@ -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)

8
src/messagetypes/vote.py Normal file
View File

@ -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)