add ModuleManager.try_reload_module(), use it for !reloadmodule
This commit is contained in:
parent
94197b33a2
commit
f3ac154fe2
2 changed files with 16 additions and 6 deletions
|
@ -402,6 +402,20 @@ class ModuleManager(object):
|
||||||
[loaded_module.name,
|
[loaded_module.name,
|
||||||
", ".join([str(referrer) for referrer in referrers])])
|
", ".join([str(referrer) for referrer in referrers])])
|
||||||
|
|
||||||
|
def try_reload_module(self, bot: "IRCBot.Bot", name: str):
|
||||||
|
loaded_module = self.modules.pop(name)
|
||||||
|
loaded_module.module.on_pause()
|
||||||
|
|
||||||
|
new_definition = self.find_module(name)
|
||||||
|
try:
|
||||||
|
self.load_module(bot, new_definition)
|
||||||
|
except:
|
||||||
|
loaded_module.module.on_resume()
|
||||||
|
self.modules[name] = loaded_module
|
||||||
|
raise
|
||||||
|
|
||||||
|
self._unload_module(loaded_module)
|
||||||
|
|
||||||
def try_reload_modules(self, bot: "IRCBot.Bot",
|
def try_reload_modules(self, bot: "IRCBot.Bot",
|
||||||
whitelist: typing.List[str], blacklist: typing.List[str]):
|
whitelist: typing.List[str], blacklist: typing.List[str]):
|
||||||
loadable, nonloadable = self._list_valid_modules(
|
loadable, nonloadable = self._list_valid_modules(
|
||||||
|
|
|
@ -68,18 +68,14 @@ class Module(ModuleManager.BaseModule):
|
||||||
self._catch(name, lambda: self.bot.modules.unload_module(name))
|
self._catch(name, lambda: self.bot.modules.unload_module(name))
|
||||||
event["stdout"].write("Unloaded '%s'" % name)
|
event["stdout"].write("Unloaded '%s'" % name)
|
||||||
|
|
||||||
def _reload(self, name):
|
|
||||||
self.bot.modules.unload_module(name)
|
|
||||||
definition = self._catch(name,
|
|
||||||
lambda: self.bot.modules.find_module(name))
|
|
||||||
self.bot.modules.load_module(self.bot, definition)
|
|
||||||
@utils.hook("received.command.reloadmodule")
|
@utils.hook("received.command.reloadmodule")
|
||||||
@utils.kwarg("help", "Reload a module")
|
@utils.kwarg("help", "Reload a module")
|
||||||
@utils.kwarg("permission", "reloadmodule")
|
@utils.kwarg("permission", "reloadmodule")
|
||||||
@utils.spec("!<name>wordlower")
|
@utils.spec("!<name>wordlower")
|
||||||
def reload(self, event):
|
def reload(self, event):
|
||||||
name = event["spec"][0]
|
name = event["spec"][0]
|
||||||
self._catch(name, lambda: self._reload(name))
|
self._catch(name,
|
||||||
|
lambda: self.bot.modules.try_reload_module(self.bot, name))
|
||||||
event["stdout"].write("Reloaded '%s'" % name)
|
event["stdout"].write("Reloaded '%s'" % name)
|
||||||
|
|
||||||
@utils.hook("received.command.reloadallmodules")
|
@utils.hook("received.command.reloadallmodules")
|
||||||
|
|
Loading…
Reference in a new issue