From 500e3a6e0164b15a25ef93024d1b25af189dd8c0 Mon Sep 17 00:00:00 2001 From: nemunaire Date: Sat, 23 May 2015 00:33:15 +0200 Subject: [PATCH] Compatibly with Python 3.4 --- nemubot/bot.py | 12 +++++++----- nemubot/modulecontext.py | 11 +++++++---- 2 files changed, 14 insertions(+), 9 deletions(-) diff --git a/nemubot/bot.py b/nemubot/bot.py index 28b5980..65d8221 100644 --- a/nemubot/bot.py +++ b/nemubot/bot.py @@ -355,13 +355,15 @@ class Bot(threading.Thread): def add_module(self, module): """Add a module to the context, if already exists, unload the old one before""" + module_name = module.__spec__.name if hasattr(module, "__spec__") else module.__name__ + # Check if the module already exists - if module.__name__ in self.modules: - self.unload_module(module.__name__) + if module_name in self.modules: + self.unload_module(module_name) # Overwrite print built-in def prnt(*args): - print("[%s]" % module.__name__, *args) + print("[%s]" % module_name, *args) if hasattr(module, "logger"): module.logger.info(" ".join(args)) module.print = prnt @@ -370,7 +372,7 @@ class Bot(threading.Thread): module.__nemubot_context__ = ModuleContext(self, module) if not hasattr(module, "logger"): - module.logger = logging.getLogger("nemubot.module." + module.__name__) + module.logger = logging.getLogger("nemubot.module." + module_name) # Replace imported context by real one for attr in module.__dict__: @@ -384,7 +386,7 @@ class Bot(threading.Thread): nemubot.hooks.last_registered = [] # Save a reference to the module - self.modules[module.__name__] = module + self.modules[module_name] = module # Launch the module if hasattr(module, "load"): diff --git a/nemubot/modulecontext.py b/nemubot/modulecontext.py index 625d526..731dfba 100644 --- a/nemubot/modulecontext.py +++ b/nemubot/modulecontext.py @@ -40,10 +40,13 @@ class ModuleContext: module -- the module """ + if module is not None: + module_name = module.__spec__.name if hasattr(module, "__spec__") else module.__name__ + # Load module configuration if exists if (context is not None and - module.__name__ in context.modules_configuration): - self.config = context.modules_configuration[module.__name__] + module_name in context.modules_configuration): + self.config = context.modules_configuration[module_name] else: self.config = None @@ -54,7 +57,7 @@ class ModuleContext: # Define some callbacks if context is not None: # Load module data - self.data = context.datastore.load(module.__name__) + self.data = context.datastore.load(module_name) def add_hook(store, hook): store = convert_legacy_store(store) @@ -99,7 +102,7 @@ class ModuleContext: print(res) def save(): - context.datastore.save(module.__name__, self.data) + context.datastore.save(module_name, self.data) self.add_hook = add_hook self.del_hook = del_hook