2016-11-03 22:41:36 +01:00
|
|
|
from importlib import import_module
|
|
|
|
from os import path, listdir
|
2016-11-12 17:20:45 +01:00
|
|
|
from string import lower
|
|
|
|
|
|
|
|
from debug import logger
|
2017-02-19 14:48:53 +01:00
|
|
|
import paths
|
2016-11-12 17:20:45 +01:00
|
|
|
|
|
|
|
class MsgBase(object):
|
|
|
|
def encode(self):
|
|
|
|
self.data = {"": lower(type(self).__name__)}
|
2016-11-03 22:41:36 +01:00
|
|
|
|
|
|
|
|
|
|
|
def constructObject(data):
|
|
|
|
try:
|
|
|
|
classBase = eval(data[""] + "." + data[""].title())
|
|
|
|
except NameError:
|
2016-11-15 17:10:14 +01:00
|
|
|
logger.error("Don't know how to handle message type: \"%s\"", data[""])
|
2016-11-03 22:41:36 +01:00
|
|
|
return None
|
|
|
|
try:
|
2016-11-12 17:20:45 +01:00
|
|
|
returnObj = classBase()
|
|
|
|
returnObj.decode(data)
|
2016-11-03 22:41:36 +01:00
|
|
|
except KeyError as e:
|
2016-11-12 17:20:45 +01:00
|
|
|
logger.error("Missing mandatory key %s", e)
|
2016-11-03 22:41:36 +01:00
|
|
|
return None
|
|
|
|
except:
|
2016-11-12 17:20:45 +01:00
|
|
|
logger.error("classBase fail", exc_info=True)
|
2016-11-03 22:41:36 +01:00
|
|
|
return None
|
|
|
|
else:
|
|
|
|
return returnObj
|
|
|
|
|
2017-02-19 14:48:53 +01:00
|
|
|
if paths.frozen is not None:
|
2017-02-26 20:50:06 +01:00
|
|
|
import messagetypes.message
|
|
|
|
import messagetypes.vote
|
2017-02-19 14:48:53 +01:00
|
|
|
else:
|
2017-02-26 20:50:06 +01:00
|
|
|
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:
|
|
|
|
logger.error("Error importing %s", mod, exc_info=True)
|
|
|
|
else:
|
|
|
|
logger.debug("Imported message type module %s", mod)
|