Skip to content

Декоратор @support_hooks

skip_hooks(cls)

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

Examples

.. tabs::

.. code-tab:: py Context manager syntax

    @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)

.. code-tab:: py Decorator syntax

    @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)

.. versionadded:: 0.7.0

Source code in onetl/hooks/support_hooks.py
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
@contextmanager
def skip_hooks(cls: type):
    """
    Context manager (and decorator) which temporary disables hooks for all the methods of a specific class.

    Examples
    --------

    .. tabs::

        .. code-tab:: py Context manager syntax

            @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)

        .. code-tab:: py Decorator syntax

            @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)

    .. versionadded:: 0.7.0
    """

    slots = get_slots(cls)

    with ExitStack() as stack:
        for slot in slots:
            stack.enter_context(slot.skip_hooks())
        yield

suspend_hooks(cls)

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

Examples

.. code:: python

@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

.. versionadded:: 0.7.0

Source code in onetl/hooks/support_hooks.py
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
def suspend_hooks(cls: type) -> None:
    """
    Disables all hooks for all the methods of a specific class.

    Examples
    --------

    .. code:: python

        @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

    .. versionadded:: 0.7.0
    """

    slots = get_slots(cls)

    for slot in slots:
        slot.suspend_hooks()

resume_hooks(cls)

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

Examples

.. code:: python

@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)

.. versionadded:: 0.7.0

Source code in onetl/hooks/support_hooks.py
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
def resume_hooks(cls: type) -> None:
    """
    Enables all hooks for all the methods of a specific class.

    Examples
    --------

    .. code:: python

        @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)

    .. versionadded:: 0.7.0
    """

    slots = get_slots(cls)

    for slot in slots:
        slot.resume_hooks()

support_hooks(cls)

Decorator which adds hooks functionality to a specific class.

Only methods decorated with :obj:~slot can be used for connecting hooks.

Adds :obj:~skip_hooks, :obj:~suspend_hooks and :obj:~resume_hooks to the class.

.. versionadded:: 0.7.0

Examples

.. code:: python

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
Source code in onetl/hooks/support_hooks.py
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
def support_hooks(cls: Klass) -> Klass:
    """
    Decorator which adds hooks functionality to a specific class.

    Only methods decorated with :obj:`~slot` can be used for connecting hooks.

    Adds :obj:`~skip_hooks`, :obj:`~suspend_hooks` and :obj:`~resume_hooks` to the class.

    .. versionadded:: 0.7.0

    Examples
    --------

    .. code:: python

        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
    """

    has_slots = False
    for method_name, method in cls.__dict__.items():
        if is_slot(method):
            has_slots = True
            setattr(cls, method_name, register_slot(cls, method_name))

    if not has_slots:
        raise SyntaxError("@support_hooks can be used only with @slot decorator on some of class methods")

    cls.skip_hooks = partial(skip_hooks, cls)  # type: ignore[attr-defined]
    cls.suspend_hooks = partial(suspend_hooks, cls)  # type: ignore[attr-defined]
    cls.resume_hooks = partial(resume_hooks, cls)  # type: ignore[attr-defined]
    return cls