Compatibly with Python 3.4
This commit is contained in:
parent
fc1bc135df
commit
500e3a6e01
2 changed files with 14 additions and 9 deletions
|
@ -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"):
|
||||
|
|
|
@ -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
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue