Extended encoding updates
- more flexible and developer friendly. Still not active code
This commit is contained in:
parent
020a78b776
commit
8ce72d8d2d
|
@ -5,12 +5,14 @@ import msgpack
|
||||||
import zlib
|
import zlib
|
||||||
|
|
||||||
from debug import logger
|
from debug import logger
|
||||||
|
import messagetypes
|
||||||
|
|
||||||
BITMESSAGE_ENCODING_IGNORE = 0
|
BITMESSAGE_ENCODING_IGNORE = 0
|
||||||
BITMESSAGE_ENCODING_TRIVIAL = 1
|
BITMESSAGE_ENCODING_TRIVIAL = 1
|
||||||
BITMESSAGE_ENCODING_SIMPLE = 2
|
BITMESSAGE_ENCODING_SIMPLE = 2
|
||||||
BITMESSAGE_ENCODING_EXTENDED = 3
|
BITMESSAGE_ENCODING_EXTENDED = 3
|
||||||
|
|
||||||
|
|
||||||
class MsgEncode(object):
|
class MsgEncode(object):
|
||||||
def __init__(self, message, encoding=BITMESSAGE_ENCODING_SIMPLE):
|
def __init__(self, message, encoding=BITMESSAGE_ENCODING_SIMPLE):
|
||||||
self.data = None
|
self.data = None
|
||||||
|
@ -61,14 +63,24 @@ class MsgDecode(object):
|
||||||
msgpack.exceptions.ExtraData):
|
msgpack.exceptions.ExtraData):
|
||||||
logger.error("Error msgunpacking message")
|
logger.error("Error msgunpacking message")
|
||||||
raise
|
raise
|
||||||
|
|
||||||
try:
|
try:
|
||||||
if tmp[""] == "message":
|
msgType = tmp[""]
|
||||||
self.body = tmp["body"]
|
except KeyError:
|
||||||
self.subject = tmp["subject"]
|
logger.error("Message type missing")
|
||||||
except:
|
|
||||||
logger.error ("Malformed message")
|
|
||||||
raise
|
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):
|
def decodeSimple(self, data):
|
||||||
bodyPositionIndex = string.find(data, '\nBody:')
|
bodyPositionIndex = string.find(data, '\nBody:')
|
||||||
if bodyPositionIndex > 1:
|
if bodyPositionIndex > 1:
|
||||||
|
|
37
src/messagetypes/__init__.py
Normal file
37
src/messagetypes/__init__.py
Normal 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)
|
8
src/messagetypes/message.py
Normal file
8
src/messagetypes/message.py
Normal 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
8
src/messagetypes/vote.py
Normal 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)
|
Reference in New Issue
Block a user