34 lines
631 B
Python
34 lines
631 B
Python
from . import exceptions
|
|
|
|
hook_functions = {}
|
|
|
|
def hook(name):
|
|
|
|
"""
|
|
Decorator. Registers a function to a hook name.
|
|
"""
|
|
|
|
def decorator(function):
|
|
|
|
if not name in hook_functions:
|
|
hook_functions[name] = []
|
|
|
|
hook_functions[name].append(function)
|
|
|
|
return function
|
|
|
|
return decorator
|
|
|
|
def call(name, *args, **kwargs):
|
|
|
|
"""
|
|
Calls all registered hook callbacks for the
|
|
given name with the arguments passed.
|
|
"""
|
|
|
|
if not name in hook_functions:
|
|
raise exceptions.UnknownHook(name)
|
|
|
|
for function in hook_functions[name]:
|
|
function(*args, **kwargs)
|