PyBitmessage-2021-04-27/src/messagetypes/__init__.py
Peter Surda 67c8966a21
Windows compatibility fixes
- spec file for pyinstaller detects architecture (32 or 64bit)
- spec file uses os.path.join
- spec file creates and adds the list of messagetypes
- added MinGW/MSyS support in Makefile
- separate Makefile.msvc for MCVC
- bitmsghash.cpp minor adjustments to build also on MSVC/MinGW
- if frozen mode, messagetypes loads the list of files from a text file
  generated during archive building rather than from a directory
2017-02-19 14:48:53 +01:00

51 lines
1.3 KiB
Python

from importlib import import_module
from os import path, listdir
from string import lower
from debug import logger
import paths
class MsgBase(object):
def encode(self):
self.data = {"": lower(type(self).__name__)}
def constructObject(data):
try:
classBase = eval(data[""] + "." + data[""].title())
except NameError:
logger.error("Don't know how to handle message type: \"%s\"", data[""])
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
mods = []
if paths.frozen is not None:
with open(path.join(path.dirname(path.dirname(__file__)), 'messagetypes.txt'), 'rt') as f:
for m in f.readline():
mods.append(m.rstrip())
else:
mods = listdir(path.dirname(__file__))
for mod in mods:
if mod == "__init__.py":
continue
splitted = path.splitext(mod)
if splitted[1] != ".py":
continue
try:
import_module("." + splitted[0], "messagetypes")
except ImportError:
logger.error("Error importing %s", mod, exc_info=True)
else:
logger.debug("Imported message type module %s", mod)