Skip to content

@support_hooks decorator

skip_hooks(cls)

Context manager (and decorator) which temporary disables hooks for all the methods of a specific class.

Examples:

@support_hooks
class MyClass:
    @slot
    def my_method(self, arg):
        ...


@MyClass.my_method.hook
def callback(self, arg):
    ...


obj = MyClass()
obj.my_method(1)  # will execute callback(obj, 1)

with MyClass.skip_hooks():
    obj.my_method()  # will NOT execute callback

# running outside the context restores previous behavior
obj.my_method(2)  # will execute callback(obj, 2)
@support_hooks
class MyClass:
    @slot
    def my_method(self, arg):
        ...


@MyClass.my_method.hook
def callback(self, arg):
    ...


def with_hook_enabled():
    obj = MyClass()
    obj.my_method(1)


with_hook_enabled()  # will execute callback(obj, 1)


@MyClass.skip_hooks()
def with_all_hooks_disabled():
    obj = MyClass()
    obj.my_method(1)


with_all_hooks_disabled()  # will NOT execute callback function

# running outside a decorated function restores previous behavior
obj = MyClass()
obj.my_method(2)  # will execute callback(obj, 2)

Added in 0.7.0

suspend_hooks(cls)

Disables all hooks for all the methods of a specific class.

Examples:

@support_hooks
class MyClass:
    @slot
    def my_method(self, arg): ...


@MyClass.my_method.hook
def callback(self, arg): ...


obj = MyClass()
obj.my_method(1)  # will execute callback(obj, 1)

MyClass.suspend_hooks()

obj.my_method(2)  # will NOT execute callback

Added in 0.7.0

resume_hooks(cls)

Enables all hooks for all the methods of a specific class.

Examples:

@support_hooks
class MyClass:
    @slot
    def my_method(self, arg): ...


@MyClass.my_method.hook
def callback(self, arg): ...


obj = MyClass()

MyClass.suspend_hooks()
obj.my_method(1)  # will NOT execute callback

MyClass.resume_hooks()

obj.my_method(2)  # will execute callback(obj, 2)

Added in 0.7.0

support_hooks(cls)

Decorator which adds hooks functionality to a specific class.

Only methods decorated with [slot][] can be used for connecting hooks.

Adds [skip_hooks][], [suspend_hooks][] and [resume_hooks][] to the class.

Added in 0.7.0

Examples:

from onetl.hooks.hook import support_hooks, slot


@support_hooks
class MyClass:
    @slot
    def my_method(self, arg): ...


@MyClass.my_method.hook
def callback(self, arg): ...


MyClass().my_method()  # will execute callback function