Unload a module now unsubscribe auto-registered hooks
This commit is contained in:
parent
4055025160
commit
fc396ef313
3
bot.py
3
bot.py
@ -158,6 +158,9 @@ class Bot:
|
|||||||
self.modules[name].save()
|
self.modules[name].save()
|
||||||
if hasattr(self.modules[name], "unload"):
|
if hasattr(self.modules[name], "unload"):
|
||||||
self.modules[name].unload()
|
self.modules[name].unload()
|
||||||
|
# Remove registered hooks
|
||||||
|
for (s, h) in self.modules[name].REGISTERED_HOOKS:
|
||||||
|
self.hooks.del_hook(s, h)
|
||||||
# Remove from the dict
|
# Remove from the dict
|
||||||
del self.modules[name]
|
del self.modules[name]
|
||||||
return True
|
return True
|
||||||
|
30
hooks.py
30
hooks.py
@ -42,15 +42,15 @@ class MessagesHook:
|
|||||||
self.msg_default = list()
|
self.msg_default = list()
|
||||||
|
|
||||||
|
|
||||||
def add_hook(self, store, hook):
|
def add_hook(self, store, hook, module_src=None):
|
||||||
"""Insert in the right place a hook into the given store"""
|
"""Insert in the right place a hook into the given store"""
|
||||||
if store in self.context.hooks_cache:
|
if store in self.context.hooks_cache:
|
||||||
del self.context.hooks_cache[store]
|
del self.context.hooks_cache[store]
|
||||||
|
|
||||||
attr = getattr(self, store)
|
if not hasattr(self, store):
|
||||||
if attr is None:
|
|
||||||
print ("Warning: unrecognized hook store type")
|
print ("Warning: unrecognized hook store type")
|
||||||
return
|
return
|
||||||
|
attr = getattr(self, store)
|
||||||
|
|
||||||
if isinstance(attr, dict) and hook.name is not None:
|
if isinstance(attr, dict) and hook.name is not None:
|
||||||
if hook.name not in attr:
|
if hook.name not in attr:
|
||||||
@ -60,14 +60,19 @@ class MessagesHook:
|
|||||||
attr.append(hook)
|
attr.append(hook)
|
||||||
else:
|
else:
|
||||||
print ("Warning: unrecognized hook store type")
|
print ("Warning: unrecognized hook store type")
|
||||||
|
return
|
||||||
|
if module_src is not None:
|
||||||
|
module_src.REGISTERED_HOOKS.append((store, hook))
|
||||||
|
|
||||||
def register_hook_attributes(self, store, module, node):
|
def register_hook_attributes(self, store, module, node):
|
||||||
if node.hasAttribute("name"):
|
if node.hasAttribute("name"):
|
||||||
self.add_hook(store + "_hook", Hook(getattr(module, node["call"]),
|
self.add_hook(store + "_hook", Hook(getattr(module, node["call"]),
|
||||||
node["name"]))
|
node["name"]),
|
||||||
|
module)
|
||||||
elif node.hasAttribute("regexp"):
|
elif node.hasAttribute("regexp"):
|
||||||
self.add_hook(store + "_rgxp", Hook(getattr(module, node["call"]),
|
self.add_hook(store + "_rgxp", Hook(getattr(module, node["call"]),
|
||||||
None, None, node["regexp"]))
|
None, None, node["regexp"]),
|
||||||
|
module)
|
||||||
|
|
||||||
def register_hook(self, module, node):
|
def register_hook(self, module, node):
|
||||||
"""Create a hook from configuration node"""
|
"""Create a hook from configuration node"""
|
||||||
@ -82,6 +87,21 @@ class MessagesHook:
|
|||||||
node["type"] == "all"):
|
node["type"] == "all"):
|
||||||
self.register_hook_attributes("answer", module, node)
|
self.register_hook_attributes("answer", module, node)
|
||||||
|
|
||||||
|
def del_hook(self, store, hook):
|
||||||
|
"""Remove a registered hook from a given store"""
|
||||||
|
if store in self.context.hooks_cache:
|
||||||
|
del self.context.hooks_cache[store]
|
||||||
|
|
||||||
|
if not hasattr(self, store):
|
||||||
|
print ("Warning: unrecognized hook store type")
|
||||||
|
return
|
||||||
|
attr = getattr(self, store)
|
||||||
|
|
||||||
|
if isinstance(attr, dict) and hook.name is not None:
|
||||||
|
if hook.name in attr:
|
||||||
|
attr[hook.name].remove(hook)
|
||||||
|
else:
|
||||||
|
attr.remove(hook)
|
||||||
|
|
||||||
class Hook:
|
class Hook:
|
||||||
"""Class storing hook informations"""
|
"""Class storing hook informations"""
|
||||||
|
@ -22,6 +22,7 @@ import imp
|
|||||||
import os
|
import os
|
||||||
import sys
|
import sys
|
||||||
|
|
||||||
|
import event
|
||||||
from hooks import Hook
|
from hooks import Hook
|
||||||
import response
|
import response
|
||||||
import xmlparser
|
import xmlparser
|
||||||
@ -141,10 +142,12 @@ class ModuleLoader(SourceLoader):
|
|||||||
module.__LOADED__ = True
|
module.__LOADED__ = True
|
||||||
|
|
||||||
# Set module common functions and datas
|
# Set module common functions and datas
|
||||||
|
module.REGISTERED_HOOKS = list()
|
||||||
module.DEBUG = False
|
module.DEBUG = False
|
||||||
module.name = fullname
|
module.name = fullname
|
||||||
module.print = lambda msg: print("[%s] %s"%(module.name, msg))
|
module.print = lambda msg: print("[%s] %s"%(module.name, msg))
|
||||||
module.print_debug = lambda msg: mod_print_dbg(module, msg)
|
module.print_debug = lambda msg: mod_print_dbg(module, msg)
|
||||||
|
module.send_response = lambda srv, res: mod_send_response(context, srv, res)
|
||||||
|
|
||||||
if not hasattr(module, "NODATA"):
|
if not hasattr(module, "NODATA"):
|
||||||
module.DATAS = xmlparser.parse_file(self.context.datas_path
|
module.DATAS = xmlparser.parse_file(self.context.datas_path
|
||||||
@ -157,6 +160,7 @@ class ModuleLoader(SourceLoader):
|
|||||||
module.has_access = lambda msg: mod_has_access(module,
|
module.has_access = lambda msg: mod_has_access(module,
|
||||||
module.CONF, msg)
|
module.CONF, msg)
|
||||||
|
|
||||||
|
module.ModuleEvent = event.ModuleEvent
|
||||||
module.ModuleState = xmlparser.module_state.ModuleState
|
module.ModuleState = xmlparser.module_state.ModuleState
|
||||||
module.Response = response.Response
|
module.Response = response.Response
|
||||||
|
|
||||||
@ -243,3 +247,6 @@ def mod_has_access(mod, config, msg):
|
|||||||
return False
|
return False
|
||||||
else:
|
else:
|
||||||
return True
|
return True
|
||||||
|
|
||||||
|
def mod_send_response(context, server, res):
|
||||||
|
context.servers[server].send_response(res)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user