3
0
mirror of https://github.com/jlu5/PyLink.git synced 2024-11-01 01:09:22 +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) command, hook_cmd)
# Iterate over registered hook functions, catching errors accordingly. # 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: try:
log.debug('(%s) Calling hook function %s from plugin "%s"', self.name, log.debug('(%s) Calling hook function %s from plugin "%s"', self.name,
hook_func, hook_func.__module__) hook_func, hook_func.__module__)

View File

@ -157,8 +157,9 @@ def unload(irc, source, args):
del world.services['pylink'].commands[cmdname] del world.services['pylink'].commands[cmdname]
# Remove any command hooks set by the plugin. # Remove any command hooks set by the plugin.
for hookname, hookfuncs in world.hooks.copy().items(): for hookname, hookpairs in world.hooks.copy().items():
for hookfunc in hookfuncs: for hookpair in hookpairs:
hookfunc = hookpair[1]
if hookfunc.__module__ == modulename: if hookfunc.__module__ == modulename:
world.hooks[hookname].remove(hookfunc) world.hooks[hookname].remove(hookfunc)
# If the hookfuncs list is empty, remove it. # 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) world.services['pylink'].add_cmd(func, name=name, **kwargs)
return func return func
def add_hook(func, command): def add_hook(func, command, priority=100):
"""Binds a hook function to the given command name.""" """
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() 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 return func
# DEPRECATED # DEPRECATED