3
0
mirror of https://github.com/jlu5/PyLink.git synced 2024-12-28 13:42:37 +01:00

Implement priorities in utils.add_hook()

This changes world.hooks to store lists of tuples indicating (priority, hook_func).
This commit is contained in:
James Lu 2017-09-02 21:15:59 -07:00
parent 0ea35dab18
commit 5e92aefcd4
3 changed files with 13 additions and 6 deletions

View File

@ -226,7 +226,8 @@ class PyLinkNetworkCore(structures.DeprecatedAttributesObject, structures.CamelC
command, hook_cmd)
# Iterate over registered hook functions, catching errors accordingly.
for hook_func in world.hooks[hook_cmd]:
for hook_pair in world.hooks[hook_cmd]:
hook_func = hook_pair[1]
try:
log.debug('(%s) Calling hook function %s from plugin "%s"', self.name,
hook_func, hook_func.__module__)

View File

@ -157,8 +157,9 @@ def unload(irc, source, args):
del world.services['pylink'].commands[cmdname]
# Remove any command hooks set by the plugin.
for hookname, hookfuncs in world.hooks.copy().items():
for hookfunc in hookfuncs:
for hookname, hookpairs in world.hooks.copy().items():
for hookpair in hookpairs:
hookfunc = hookpair[1]
if hookfunc.__module__ == modulename:
world.hooks[hookname].remove(hookfunc)
# If the hookfuncs list is empty, remove it.

View File

@ -44,10 +44,15 @@ def add_cmd(func, name=None, **kwargs):
world.services['pylink'].add_cmd(func, name=name, **kwargs)
return func
def add_hook(func, command):
"""Binds a hook function to the given command name."""
def add_hook(func, command, priority=100):
"""
Binds a hook function to the given command name.
A custom priority can also be given (defaults to 100), and hooks with
higher priority values will be called first."""
command = command.upper()
world.hooks[command].append(func)
world.hooks[command].append((priority, func))
world.hooks[command].sort(key=lambda pair: pair[0], reverse=True)
return func
# DEPRECATED