Interceptors

Interceptors can be thought of as hooks for your hooks.

from pytapable import Hook, HookInterceptor


class TapLogger(HookInterceptor):

    def create(self, hook):
        print(f"Hook {hook.name} initialized")

    def register(self, context, tap):
        print(f"Hook being tapping is '${context['hook']}'")
        print(f"Hook being tapped by '${tap.name}'")
        return tap

tap_logger = TapLogger()

my_hook = Hook(interceptor=tap_logger)

>>> Hook XYZ initialized


my_hook.tap('My Tap', my_callback)

>>> Hook being tapped by 'My Tap'

They are a mechanism for you to intercept whenever one of your hooks are tapped into or when a hook was created

The return value of the register method must be a tap. If you need to modify the behavior of the callback in the tap, this is the place to do it.

API Documentation

HookInterceptor

class pytapable.HookInterceptor[source]

Interceptors allow you to intercept actions that are being performed on hooks and optionally modify it

create(hook)[source]

Triggered when a hook has been initialized

Parameters

hook (BaseHook) – The hook that was created

register(context, tap)[source]

Triggered for each added tap and allows you to modify the tap

context = {
  'hook': BaseHook,  # The hook that's being tapped
}
Parameters
  • context (dict) –

  • tap (Tap) – The Tap that is going to be installed on the hook

Returns

The Tap to install on the hook

Return type

modified_tap (Tap)

Tap

class pytapable.Tap(name, fn)[source]

A Tap is an object created when you tap into a hook. It holds a reference to the function you want to execute when the hook triggers