Remove messagetypes quirks

This commit is contained in:
Dmitri Bogomolov 2021-05-30 20:57:07 +03:00
parent d54159f356
commit 30f3c5cc3c
Signed by untrusted user: g1itch
GPG Key ID: 720A756F18DEED13
4 changed files with 15 additions and 46 deletions

View File

@ -1,56 +1,18 @@
import logging import logging
from importlib import import_module
from os import listdir, path
from string import lower
import messagetypes from .message import Message
import paths
logger = logging.getLogger('default') 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): def constructObject(data):
"""Constructing an object""" """Constructing an object using `.Message` class"""
whitelist = ["message"]
if data[""] not in whitelist:
return None
try: try:
classBase = getattr(getattr(messagetypes, data[""]), data[""].title()) obj = Message()
except (NameError, AttributeError): obj.decode(data)
logger.error("Don't know how to handle message type: \"%s\"", data[""], exc_info=True)
return None
try:
returnObj = classBase()
returnObj.decode(data)
except KeyError as e: except KeyError as e:
logger.error("Missing mandatory key %s", e) logger.error("Missing mandatory key %s", e)
return None
except: except:
logger.error("classBase fail", exc_info=True) logger.error("Error creating Message instance", exc_info=True)
return None
else: else:
return returnObj return obj
if paths.frozen is not None:
import message # noqa : F401 flake8: disable=unused-import
import vote # noqa : F401 flake8: disable=unused-import
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)

View File

@ -0,0 +1,7 @@
"""MsgBase class definition"""
class MsgBase(object): # pylint: disable=too-few-public-methods
"""Base class for message types"""
def __init__(self):
self.data = {"": type(self).__name__.lower()}

View File

@ -1,6 +1,6 @@
import logging import logging
from messagetypes import MsgBase from ._base import MsgBase
logger = logging.getLogger('default') logger = logging.getLogger('default')

View File

@ -1,6 +1,6 @@
import logging import logging
from messagetypes import MsgBase from ._base import MsgBase
logger = logging.getLogger('default') logger = logging.getLogger('default')