From 30f3c5cc3c1fca783655c2d3ff9ceb026554cda5 Mon Sep 17 00:00:00 2001 From: Dmitri Bogomolov <4glitch@gmail.com> Date: Sun, 30 May 2021 20:57:07 +0300 Subject: [PATCH] Remove messagetypes quirks --- src/messagetypes/__init__.py | 50 +++++------------------------------- src/messagetypes/_base.py | 7 +++++ src/messagetypes/message.py | 2 +- src/messagetypes/vote.py | 2 +- 4 files changed, 15 insertions(+), 46 deletions(-) create mode 100644 src/messagetypes/_base.py diff --git a/src/messagetypes/__init__.py b/src/messagetypes/__init__.py index 88c37f47..8bad5a92 100644 --- a/src/messagetypes/__init__.py +++ b/src/messagetypes/__init__.py @@ -1,56 +1,18 @@ 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 + logger.error("Error creating Message instance", exc_info=True) else: - return returnObj - - -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) + return obj diff --git a/src/messagetypes/_base.py b/src/messagetypes/_base.py new file mode 100644 index 00000000..a2a77013 --- /dev/null +++ b/src/messagetypes/_base.py @@ -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()} diff --git a/src/messagetypes/message.py b/src/messagetypes/message.py index 573732d4..0e6f3194 100644 --- a/src/messagetypes/message.py +++ b/src/messagetypes/message.py @@ -1,6 +1,6 @@ import logging -from messagetypes import MsgBase +from ._base import MsgBase logger = logging.getLogger('default') diff --git a/src/messagetypes/vote.py b/src/messagetypes/vote.py index b559c256..38de6d38 100644 --- a/src/messagetypes/vote.py +++ b/src/messagetypes/vote.py @@ -1,6 +1,6 @@ import logging -from messagetypes import MsgBase +from ._base import MsgBase logger = logging.getLogger('default')