mirror of
https://github.com/jlu5/PyLink.git
synced 2024-11-27 13:09:23 +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:
parent
0ea35dab18
commit
5e92aefcd4
@ -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__)
|
||||
|
@ -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.
|
||||
|
11
utils.py
11
utils.py
@ -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
|
||||
|
Loading…
Reference in New Issue
Block a user