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
|
2018-02-19 16:57:47 +01:00
|
|
|
import messagetypes
|
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):
|
2018-02-13 23:33:12 +01:00
|
|
|
whitelist = ["message"]
|
|
|
|
if data[""] not in whitelist:
|
|
|
|
return None
|
2016-11-03 22:41:36 +01:00
|
|
|
try:
|
2018-02-19 16:57:47 +01:00
|
|
|
classBase = getattr(getattr(messagetypes, data[""]), data[""].title())
|
|
|
|
except (NameError, AttributeError):
|
2018-02-13 16:39:35 +01:00
|
|
|
logger.error("Don't know how to handle message type: \"%s\"", data[""], exc_info=True)
|
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:
|
2018-02-19 16:57:47 +01:00
|
|
|
import_module(".{}".format(splitted[0]), "messagetypes")
|
2017-02-26 20:50:06 +01:00
|
|
|
except ImportError:
|
|
|
|
logger.error("Error importing %s", mod, exc_info=True)
|
|
|
|
else:
|
|
|
|
logger.debug("Imported message type module %s", mod)
|