Dmitri Bogomolov
7a89109fc9
and use logging without risk of circular import. Only subpackage that imports from debug is bitmessageqt - because it also uses debug.resetLogging(). Instead of from debug import logger is now recommended to use: import logging logger = logging.getLogger('default') All subclasses of StoppableThread now have a logger attribute. All threading related stuff except for set_thread_name() was moved from helper_threading to network.threads. Fixed two my mistakes from previous edit of debug in a1a8d3a: - logger.handlers is not dict but iterable - sys.excepthook should be set unconditionally
57 lines
1.5 KiB
Python
57 lines
1.5 KiB
Python
import logging
|
|
from importlib import import_module
|
|
from os import path, listdir
|
|
from string import lower
|
|
|
|
import messagetypes
|
|
import paths
|
|
|
|
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
|
|
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)
|
|
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)
|