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
|
2017-02-19 13:48:53 +00:00
|
|
|
import paths
|
2016-11-12 16:20:45 +00:00
|
|
|
|
|
|
|
class MsgBase(object):
|
|
|
|
def encode(self):
|
|
|
|
self.data = {"": lower(type(self).__name__)}
|
2016-11-03 21:41:36 +00:00
|
|
|
|
|
|
|
|
|
|
|
def constructObject(data):
|
|
|
|
try:
|
|
|
|
classBase = eval(data[""] + "." + data[""].title())
|
|
|
|
except NameError:
|
2016-11-15 16:10:14 +00:00
|
|
|
logger.error("Don't know how to handle message type: \"%s\"", data[""])
|
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
|
|
|
|
|
2017-02-19 13:48:53 +00:00
|
|
|
mods = []
|
|
|
|
if paths.frozen is not None:
|
|
|
|
with open(path.join(path.dirname(path.dirname(__file__)), 'messagetypes.txt'), 'rt') as f:
|
2017-02-19 18:48:45 +00:00
|
|
|
for m in f.readlines():
|
2017-02-19 13:48:53 +00:00
|
|
|
mods.append(m.rstrip())
|
|
|
|
else:
|
|
|
|
mods = listdir(path.dirname(__file__))
|
|
|
|
|
|
|
|
for mod in mods:
|
2016-11-03 21:41:36 +00:00
|
|
|
if mod == "__init__.py":
|
|
|
|
continue
|
|
|
|
splitted = path.splitext(mod)
|
|
|
|
if splitted[1] != ".py":
|
|
|
|
continue
|
|
|
|
try:
|
|
|
|
import_module("." + splitted[0], "messagetypes")
|
|
|
|
except ImportError:
|
2016-11-12 16:20:45 +00:00
|
|
|
logger.error("Error importing %s", mod, exc_info=True)
|
2016-11-03 21:41:36 +00:00
|
|
|
else:
|
2016-11-12 16:20:45 +00:00
|
|
|
logger.debug("Imported message type module %s", mod)
|