peltak.core.hooks
¶
Using Hooks¶
Hook allow responding to various events during command execution. peltak exposes a set of standard hooks that you can use to react to what’s happening. Commands (including ones in external packages) can provide their own hooks to be used by other commands.
To call a hook you don’t have to register it, just know it’s name. You can do it simply with:
from peltak.commands import root_cli
from peltak.core import hooks
@root_cli.command('test-command')
def test_command():
hooks.call('pre-test-command', 3.14159, msg='hello, world!')
Registering a handler for any hook is also very simple:
from peltak.core import hooks
@hooks.register('pre-test-command')
def pre_test_command(pi, msg='<msg missing>'):
print("pre_test_command called with ({}, {})".format(pi, msg))
- class peltak.core.hooks.HooksRegister[source]¶
Intermediary between hooks emitters and handlers.
- call(name, *args, **kw)[source]¶
Call all hooks registered for the given name.
This will pass all arguments except for the name directly to the hook. Remember that hook handler should not modify those arguments as that will affect all hooks called later in the sequence.
- Parameters
name (
str
) – Then name of the hook.*args – Arguments and keyword arguments that should be passed to the hook handlers.
**kw –
Arguments and keyword arguments that should be passed to the hook handlers.