Remove messagetypes quirks
This commit is contained in:
parent
140383c5dd
commit
c0c40a7680
|
@ -1,56 +1,19 @@
|
|||
import logging
|
||||
from importlib import import_module
|
||||
from os import listdir, path
|
||||
from string import lower
|
||||
|
||||
import messagetypes
|
||||
import paths
|
||||
from message import Message
|
||||
|
||||
logger = logging.getLogger('default')
|
||||
|
||||
|
||||
class MsgBase(object): # pylint: disable=too-few-public-methods
|
||||
"""Base class for message types"""
|
||||
def __init__(self):
|
||||
self.data = {"": lower(type(self).__name__)}
|
||||
|
||||
|
||||
def constructObject(data):
|
||||
"""Constructing an object"""
|
||||
whitelist = ["message"]
|
||||
if data[""] not in whitelist:
|
||||
return None
|
||||
"""Constructing an object using `.Message` class"""
|
||||
|
||||
try:
|
||||
classBase = getattr(getattr(messagetypes, data[""]), data[""].title())
|
||||
except (NameError, AttributeError):
|
||||
logger.error("Don't know how to handle message type: \"%s\"", data[""], exc_info=True)
|
||||
return None
|
||||
try:
|
||||
returnObj = classBase()
|
||||
returnObj.decode(data)
|
||||
obj = Message()
|
||||
obj.decode(data)
|
||||
except KeyError as e:
|
||||
logger.error("Missing mandatory key %s", e)
|
||||
return None
|
||||
except:
|
||||
logger.error("classBase fail", exc_info=True)
|
||||
return None
|
||||
else:
|
||||
return returnObj
|
||||
|
||||
|
||||
if paths.frozen is not None:
|
||||
import messagetypes.message
|
||||
import messagetypes.vote
|
||||
else:
|
||||
for mod in listdir(path.dirname(__file__)):
|
||||
if mod == "__init__.py":
|
||||
continue
|
||||
splitted = path.splitext(mod)
|
||||
if splitted[1] != ".py":
|
||||
continue
|
||||
try:
|
||||
import_module(".{}".format(splitted[0]), "messagetypes")
|
||||
except ImportError:
|
||||
logger.error("Error importing %s", mod, exc_info=True)
|
||||
else:
|
||||
logger.debug("Imported message type module %s", mod)
|
||||
return obj
|
||||
|
|
7
src/messagetypes/_base.py
Normal file
7
src/messagetypes/_base.py
Normal file
|
@ -0,0 +1,7 @@
|
|||
"""Base class for message types"""
|
||||
|
||||
|
||||
class MsgBase(object): # pylint: disable=too-few-public-methods
|
||||
"""Base class for message types"""
|
||||
def __init__(self):
|
||||
self.data = {"": type(self).__name__.lower()}
|
|
@ -1,6 +1,6 @@
|
|||
import logging
|
||||
|
||||
from messagetypes import MsgBase
|
||||
from _base import MsgBase
|
||||
|
||||
logger = logging.getLogger('default')
|
||||
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
import logging
|
||||
|
||||
from messagetypes import MsgBase
|
||||
from . import MsgBase
|
||||
|
||||
logger = logging.getLogger('default')
|
||||
|
||||
|
|
Reference in New Issue
Block a user