Store module into weakref

This commit is contained in:
nemunaire 2017-07-29 15:22:57 +02:00
parent 76bea2bc15
commit 9d0ab88c12

View File

@ -20,6 +20,7 @@ from multiprocessing import JoinableQueue
import threading import threading
import select import select
import sys import sys
import weakref
from nemubot import __version__ from nemubot import __version__
from nemubot.consumer import Consumer, EventConsumer, MessageConsumer from nemubot.consumer import Consumer, EventConsumer, MessageConsumer
@ -99,15 +100,15 @@ class Bot(threading.Thread):
from more import Response from more import Response
res = Response(channel=msg.to_response) res = Response(channel=msg.to_response)
if len(msg.args) >= 1: if len(msg.args) >= 1:
if msg.args[0] in self.modules: if msg.args[0] in self.modules and self.modules[msg.args[0]]() is not None:
if hasattr(self.modules[msg.args[0]], "help_full"): if hasattr(self.modules[msg.args[0]](), "help_full"):
hlp = self.modules[msg.args[0]].help_full() hlp = self.modules[msg.args[0]]().help_full()
if isinstance(hlp, Response): if isinstance(hlp, Response):
return hlp return hlp
else: else:
res.append_message(hlp) res.append_message(hlp)
else: else:
res.append_message([str(h) for s,h in self.modules[msg.args[0]].__nemubot_context__.hooks], title="Available commands for module " + msg.args[0]) res.append_message([str(h) for s,h in self.modules[msg.args[0]]().__nemubot_context__.hooks], title="Available commands for module " + msg.args[0])
elif msg.args[0][0] == "!": elif msg.args[0][0] == "!":
from nemubot.message.command import Command from nemubot.message.command import Command
for h in self.treater._in_hooks(Command(msg.args[0][1:])): for h in self.treater._in_hooks(Command(msg.args[0][1:])):
@ -137,7 +138,7 @@ class Bot(threading.Thread):
res.append_message(title="Pour plus de détails sur un module, " res.append_message(title="Pour plus de détails sur un module, "
"envoyez \"!help nomdumodule\". Voici la liste" "envoyez \"!help nomdumodule\". Voici la liste"
" de tous les modules disponibles localement", " de tous les modules disponibles localement",
message=["\x03\x02%s\x03\x02 (%s)" % (im, self.modules[im].__doc__) for im in self.modules if self.modules[im].__doc__]) message=["\x03\x02%s\x03\x02 (%s)" % (im, self.modules[im]().__doc__) for im in self.modules if self.modules[im]() is not None and self.modules[im]().__doc__])
return res return res
self.treater.hm.add_hook(nemubot.hooks.Command(_help_msg, "help"), "in", "Command") self.treater.hm.add_hook(nemubot.hooks.Command(_help_msg, "help"), "in", "Command")
@ -518,18 +519,20 @@ class Bot(threading.Thread):
raise raise
# Save a reference to the module # Save a reference to the module
self.modules[module_name] = module self.modules[module_name] = weakref.ref(module)
logger.info("Module '%s' successfully loaded.", module_name)
def unload_module(self, name): def unload_module(self, name):
"""Unload a module""" """Unload a module"""
if name in self.modules: if name in self.modules and self.modules[name]() is not None:
self.modules[name].print("Unloading module %s" % name) module = self.modules[name]()
module.print("Unloading module %s" % name)
# Call the user defined unload method # Call the user defined unload method
if hasattr(self.modules[name], "unload"): if hasattr(module, "unload"):
self.modules[name].unload(self) module.unload(self)
self.modules[name].__nemubot_context__.unload() module.__nemubot_context__.unload()
# Remove from the nemubot dict # Remove from the nemubot dict
del self.modules[name] del self.modules[name]