Skip to content

Templatehook

hypha.core.templatehook

hook module-attribute

hook = Hook()

TemplateHook

TemplateHook(providing_args=None)

Bases: object

A hook for templates. This can be used directly or through the 🇵🇾class:Hook dispatcher

:param list providing_args: A list of the arguments this hook can pass along in a 🇵🇾func:.__call__

Source code in hypha/core/templatehook.py
def __init__(self, providing_args=None):
    self.providing_args = providing_args or []
    self._registry = []

providing_args instance-attribute

providing_args = providing_args or []

register

register(func)

Register a new callback

:param callable func: A function reference used as a callback

Source code in hypha/core/templatehook.py
def register(self, func):
    """
    Register a new callback

    :param callable func: A function reference used as a callback
    """
    assert callable(func), "Callback func must be a callable"

    self._registry.append(func)

unregister

unregister(func)

Remove a previously registered callback

:param callable func: A function reference that was registered previously

Source code in hypha/core/templatehook.py
def unregister(self, func):
    """
    Remove a previously registered callback

    :param callable func: A function reference\
    that was registered previously
    """
    try:
        self._registry.remove(func)
    except ValueError:
        pass

unregister_all

unregister_all()

Remove all callbacks

Source code in hypha/core/templatehook.py
def unregister_all(self):
    """
    Remove all callbacks
    """
    del self._registry[:]

Hook

Hook()

Bases: object

Dynamic dispatcher (proxy) for 🇵🇾class:TemplateHook

Source code in hypha/core/templatehook.py
def __init__(self):
    self._registry = {}

register

register(name, func)

Register a new callback. When the name/id is not found a new hook is created under its name, meaning the hook is usually created by the first registered callback

:param str name: Hook name :param callable func: A func reference (callback)

Source code in hypha/core/templatehook.py
def register(self, name, func):
    """
    Register a new callback.\
    When the name/id is not found\
    a new hook is created under its name,\
    meaning the hook is usually created by\
    the first registered callback

    :param str name: Hook name
    :param callable func: A func reference (callback)
    """
    try:
        templatehook = self._registry[name]
    except KeyError:
        templatehook = self._register(name)

    templatehook.register(func)

unregister

unregister(name, func)

Remove a previously registered callback

:param str name: Hook name :param callable func: A function reference that was registered previously

Source code in hypha/core/templatehook.py
def unregister(self, name, func):
    """
    Remove a previously registered callback

    :param str name: Hook name
    :param callable func: A function reference\
    that was registered previously
    """
    try:
        templatehook = self._registry[name]
    except KeyError:
        return

    templatehook.unregister(func)

unregister_all

unregister_all(name)

Remove all callbacks

:param str name: Hook name

Source code in hypha/core/templatehook.py
def unregister_all(self, name):
    """
    Remove all callbacks

    :param str name: Hook name
    """
    try:
        templatehook = self._registry[name]
    except KeyError:
        return

    templatehook.unregister_all()