flaschengeist/flaschengeist/utils/hook.py

27 lines
657 B
Python

_hook_dict = {}
class Hook(object):
"""Decorator for Hooks
Use to decorate system hooks where plugins should be able to hook in
"""
def __init__(self, function):
self.function = function
def __call__(self, *args, **kwargs):
if self.function.__name__ in _hook_dict:
_hook_dict[self.function.__name__](*args, **kwargs)
self.function(*args, **kwargs)
class HookCall(object):
"""Decorator for functions to be called if a Hook is called"""
def __init__(self, name):
self.name = name
def __call__(self, function):
_hook_dict[self.name] = function
return function