-
-
Notifications
You must be signed in to change notification settings - Fork 103
Expand file tree
/
Copy pathtransition_mixin.py
More file actions
89 lines (67 loc) · 2.83 KB
/
transition_mixin.py
File metadata and controls
89 lines (67 loc) · 2.83 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
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
from typing import Callable
from .callbacks import CallbackGroup
from .i18n import _
class AddCallbacksMixin:
def _add_callback(self, callback, grouper: CallbackGroup, is_event=False, **kwargs):
raise NotImplementedError
def __call__(self, *args, **kwargs):
if len(args) == 1 and callable(args[0]) and not kwargs:
return self._add_callback(args[0], CallbackGroup.ON, is_event=True)
raise TypeError(
_("{} only supports the decorator syntax to register callbacks.").format(
type(self).__name__
)
)
def before(self, f: Callable):
"""Adds a ``before`` :ref:`transition actions` callback to every :ref:`transition` in the
:ref:`TransitionList` instance.
Args:
f: The ``before`` :ref:`transition actions` callback function to be added.
Returns:
The `f` callable.
"""
return self._add_callback(f, CallbackGroup.BEFORE)
def after(self, f: Callable):
"""Adds a ``after`` :ref:`transition actions` callback to every :ref:`transition` in the
:ref:`TransitionList` instance.
Args:
f: The ``after`` :ref:`transition actions` callback function to be added.
Returns:
The `f` callable.
"""
return self._add_callback(f, CallbackGroup.AFTER)
def on(self, f: Callable):
"""Adds a ``on`` :ref:`transition actions` callback to every :ref:`transition` in the
:ref:`TransitionList` instance.
Args:
f: The ``on`` :ref:`transition actions` callback function to be added.
Returns:
The `f` callable.
"""
return self._add_callback(f, CallbackGroup.ON)
def cond(self, f: Callable):
"""Adds a ``cond`` :ref:`guards` callback to every :ref:`transition` in the
:ref:`TransitionList` instance.
Args:
f: The ``cond`` :ref:`guards` callback function to be added.
Returns:
The `f` callable.
"""
return self._add_callback(f, CallbackGroup.COND, expected_value=True)
def unless(self, f: Callable):
"""Adds a ``unless`` :ref:`guards` callback with expected value ``False`` to every
:ref:`transition` in the :ref:`TransitionList` instance.
Args:
f: The ``unless`` :ref:`guards` callback function to be added.
Returns:
The `f` callable.
"""
return self._add_callback(f, CallbackGroup.COND, expected_value=False)
def validators(self, f: Callable):
"""Adds a :ref:`validators` callback to the :ref:`TransitionList` instance.
Args:
f: The ``validators`` callback function to be added.
Returns:
The callback function.
"""
return self._add_callback(f, CallbackGroup.VALIDATOR)