2019-09-27 14:01:55 +00:00
|
|
|
"""
|
|
|
|
src/messagetypes/__init__.py
|
|
|
|
============================
|
|
|
|
"""
|
2016-11-03 21:41:36 +00:00
|
|
|
from importlib import import_module
|
|
|
|
from os import path, listdir
|
2016-11-12 16:20:45 +00:00
|
|
|
from string import lower
|
|
|
|
|
|
|
|
from debug import logger
|
2018-02-19 15:57:47 +00:00
|
|
|
import messagetypes
|
2017-02-19 13:48:53 +00:00
|
|
|
import paths
|
2016-11-12 16:20:45 +00:00
|
|
|
|
2019-09-27 14:01:55 +00:00
|
|
|
|
|
|
|
class MsgBase(object): # pylint: disable=too-few-public-methods
|
|
|
|
"""Base class for message types"""
|
|
|
|
def __init__(self):
|
2016-11-12 16:20:45 +00:00
|
|
|
self.data = {"": lower(type(self).__name__)}
|
2016-11-03 21:41:36 +00:00
|
|
|
|
|
|
|
|
|
|
|
def constructObject(data):
|
2019-09-27 14:01:55 +00:00
|
|
|
"""Constructing an object"""
|
2018-02-13 22:33:12 +00:00
|
|
|
whitelist = ["message"]
|
|
|
|
if data[""] not in whitelist:
|
|
|
|
return None
|
2016-11-03 21:41:36 +00:00
|
|
|
try:
|
2018-02-19 15:57:47 +00:00
|
|
|
classBase = getattr(getattr(messagetypes, data[""]), data[""].title())
|
|
|
|
except (NameError, AttributeError):
|
2018-02-13 15:39:35 +00:00
|
|
|
logger.error("Don't know how to handle message type: \"%s\"", data[""], exc_info=True)
|
2016-11-03 21:41:36 +00:00
|
|
|
return None
|
|
|
|
try:
|
2016-11-12 16:20:45 +00:00
|
|
|
returnObj = classBase()
|
|
|
|
returnObj.decode(data)
|
2016-11-03 21:41:36 +00:00
|
|
|
except KeyError as e:
|
2016-11-12 16:20:45 +00:00
|
|
|
logger.error("Missing mandatory key %s", e)
|
2016-11-03 21:41:36 +00:00
|
|
|
return None
|
|
|
|
except:
|
2016-11-12 16:20:45 +00:00
|
|
|
logger.error("classBase fail", exc_info=True)
|
2016-11-03 21:41:36 +00:00
|
|
|
return None
|
|
|
|
else:
|
|
|
|
return returnObj
|
|
|
|
|
2019-09-27 14:01:55 +00:00
|
|
|
|
2017-02-19 13:48:53 +00:00
|
|
|
if paths.frozen is not None:
|
2017-02-26 19:50:06 +00:00
|
|
|
import messagetypes.message
|
|
|
|
import messagetypes.vote
|
2017-02-19 13:48:53 +00:00
|
|
|
else:
|
2017-02-26 19:50:06 +00: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 15:57:47 +00:00
|
|
|
import_module(".{}".format(splitted[0]), "messagetypes")
|
2017-02-26 19:50:06 +00:00
|
|
|
except ImportError:
|
|
|
|
logger.error("Error importing %s", mod, exc_info=True)
|
|
|
|
else:
|
|
|
|
logger.debug("Imported message type module %s", mod)
|