Can register hooks thanks to Python decorator; fixes #43

This commit is contained in:
nemunaire 2014-08-12 20:08:55 +02:00
parent 1464f92c87
commit 23bc61cce0
2 changed files with 19 additions and 5 deletions

View File

@ -165,7 +165,7 @@ class MessagesHook:
class Hook: class Hook:
"""Class storing hook informations""" """Class storing hook informations"""
def __init__(self, call, name=None, data=None, regexp=None, channels=list(), server=None, end=None, call_end=None): def __init__(self, call, name=None, data=None, regexp=None, channels=list(), server=None, end=None, call_end=None, help=None):
self.name = name self.name = name
self.end = end self.end = end
self.call = call self.call = call
@ -178,6 +178,7 @@ class Hook:
self.times = -1 self.times = -1
self.server = server self.server = server
self.channels = channels self.channels = channels
self.help = help
def is_matching(self, strcmp, channel=None, server=None): def is_matching(self, strcmp, channel=None, server=None):
"""Test if the current hook correspond to the message""" """Test if the current hook correspond to the message"""
@ -222,3 +223,11 @@ class Hook:
return call(msg, self.data, data2) return call(msg, self.data, data2)
except IRCException as e: except IRCException as e:
return e.fill_response(msg) return e.fill_response(msg)
last_registered = []
def hook(store, *args, **kargs):
def sec(call):
last_registered.append((store, Hook(call, *args, **kargs)))
return call
return sec

View File

@ -24,7 +24,7 @@ import sys
import event import event
import exception import exception
from hooks import Hook import hooks
import response import response
import xmlparser import xmlparser
@ -241,6 +241,11 @@ def add_cap_hook(prompt, module, cmd):
def register_hooks(module, context, prompt): def register_hooks(module, context, prompt):
"""Register all available hooks""" """Register all available hooks"""
# Register decorated functions
for s, h in hooks.last_registered:
context.hooks.add_hook(s, h, module)
hooks.last_registered = []
if module.CONF is not None: if module.CONF is not None:
# Register command hooks # Register command hooks
if module.CONF.hasNode("command"): if module.CONF.hasNode("command"):
@ -255,8 +260,8 @@ def register_hooks(module, context, prompt):
# Register legacy hooks # Register legacy hooks
if hasattr(module, "parseanswer"): if hasattr(module, "parseanswer"):
context.hooks.add_hook("cmd_default", Hook(module.parseanswer), module) context.hooks.add_hook("cmd_default", hooks.Hook(module.parseanswer), module)
if hasattr(module, "parseask"): if hasattr(module, "parseask"):
context.hooks.add_hook("ask_default", Hook(module.parseask), module) context.hooks.add_hook("ask_default", hooks.Hook(module.parseask), module)
if hasattr(module, "parselisten"): if hasattr(module, "parselisten"):
context.hooks.add_hook("msg_default", Hook(module.parselisten), module) context.hooks.add_hook("msg_default", hooks.Hook(module.parselisten), module)