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):
|
def add_module(self, module):
|
||||||
"""Add a module to the context, if already exists, unload the
|
"""Add a module to the context, if already exists, unload the
|
||||||
old one before"""
|
old one before"""
|
||||||
|
module_name = module.__spec__.name if hasattr(module, "__spec__") else module.__name__
|
||||||
|
|
||||||
# Check if the module already exists
|
# Check if the module already exists
|
||||||
if module.__name__ in self.modules:
|
if module_name in self.modules:
|
||||||
self.unload_module(module.__name__)
|
self.unload_module(module_name)
|
||||||
|
|
||||||
# Overwrite print built-in
|
# Overwrite print built-in
|
||||||
def prnt(*args):
|
def prnt(*args):
|
||||||
print("[%s]" % module.__name__, *args)
|
print("[%s]" % module_name, *args)
|
||||||
if hasattr(module, "logger"):
|
if hasattr(module, "logger"):
|
||||||
module.logger.info(" ".join(args))
|
module.logger.info(" ".join(args))
|
||||||
module.print = prnt
|
module.print = prnt
|
||||||
|
@ -370,7 +372,7 @@ class Bot(threading.Thread):
|
||||||
module.__nemubot_context__ = ModuleContext(self, module)
|
module.__nemubot_context__ = ModuleContext(self, module)
|
||||||
|
|
||||||
if not hasattr(module, "logger"):
|
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
|
# Replace imported context by real one
|
||||||
for attr in module.__dict__:
|
for attr in module.__dict__:
|
||||||
|
@ -384,7 +386,7 @@ class Bot(threading.Thread):
|
||||||
nemubot.hooks.last_registered = []
|
nemubot.hooks.last_registered = []
|
||||||
|
|
||||||
# Save a reference to the module
|
# Save a reference to the module
|
||||||
self.modules[module.__name__] = module
|
self.modules[module_name] = module
|
||||||
|
|
||||||
# Launch the module
|
# Launch the module
|
||||||
if hasattr(module, "load"):
|
if hasattr(module, "load"):
|
||||||
|
|
|
@ -40,10 +40,13 @@ class ModuleContext:
|
||||||
module -- the module
|
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
|
# Load module configuration if exists
|
||||||
if (context is not None and
|
if (context is not None and
|
||||||
module.__name__ in context.modules_configuration):
|
module_name in context.modules_configuration):
|
||||||
self.config = context.modules_configuration[module.__name__]
|
self.config = context.modules_configuration[module_name]
|
||||||
else:
|
else:
|
||||||
self.config = None
|
self.config = None
|
||||||
|
|
||||||
|
@ -54,7 +57,7 @@ class ModuleContext:
|
||||||
# Define some callbacks
|
# Define some callbacks
|
||||||
if context is not None:
|
if context is not None:
|
||||||
# Load module data
|
# Load module data
|
||||||
self.data = context.datastore.load(module.__name__)
|
self.data = context.datastore.load(module_name)
|
||||||
|
|
||||||
def add_hook(store, hook):
|
def add_hook(store, hook):
|
||||||
store = convert_legacy_store(store)
|
store = convert_legacy_store(store)
|
||||||
|
@ -99,7 +102,7 @@ class ModuleContext:
|
||||||
print(res)
|
print(res)
|
||||||
|
|
||||||
def save():
|
def save():
|
||||||
context.datastore.save(module.__name__, self.data)
|
context.datastore.save(module_name, self.data)
|
||||||
|
|
||||||
self.add_hook = add_hook
|
self.add_hook = add_hook
|
||||||
self.del_hook = del_hook
|
self.del_hook = del_hook
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue