diff --git a/transitions/experimental/decoration.py b/transitions/experimental/decoration.py index 2a76e266..b2d488ae 100644 --- a/transitions/experimental/decoration.py +++ b/transitions/experimental/decoration.py @@ -1,3 +1,33 @@ -def expect_override(func): - setattr(func, "expect_override", True) - return func +from transitions.core import listify +from collections import deque + +_attribute_name = "transition_definitions" + + +def with_trigger_decorator(cls): + add_model = getattr(cls, "add_model") + + def add_model_override(self, model, initial=None) -> None: + self.model_override = True + for model in listify(model): + model = self if model == "self" else model + for name, method in model.__class__.__dict__.items(): + for spec in getattr(method, _attribute_name, []): + self.add_transition(name, **spec) + add_model(self, model, initial) + + setattr(cls, 'add_model', add_model_override) + return cls + + +def trigger(source, dest, conditions=None, unless=None, before=None, after=None, prepare=None): + def _outer(trigger_func): + if not hasattr(trigger_func, _attribute_name): + setattr(trigger_func, _attribute_name, deque()) + getattr(trigger_func, _attribute_name).appendleft({ + "source": source, "dest": dest, "conditions": conditions, "unless": unless, "before": before, + "after": after, "prepare": prepare + }) + return trigger_func + + return _outer diff --git a/transitions/experimental/decoration.pyi b/transitions/experimental/decoration.pyi index 55945c60..9716a6c6 100644 --- a/transitions/experimental/decoration.pyi +++ b/transitions/experimental/decoration.pyi @@ -1,6 +1,11 @@ -from typing import Callable, ParamSpec +from typing import Union, Type, Callable +from transitions.core import StateIdentifier, CallbacksArg, CallbackFunc, Machine -P = ParamSpec("P") +def with_trigger_decorator(cls: Type[Machine]) -> Type[Machine]: ... -def expect_override(func: Callable[P, bool | None]) -> Callable[P, bool | None]: ... +def trigger(source: Union[StateIdentifier, list[StateIdentifier]], + dest: StateIdentifier | None = ..., + conditions: CallbacksArg = ..., unless: CallbacksArg = ..., + before: CallbacksArg = ..., after: CallbacksArg = ..., + prepare: CallbacksArg = ...) -> Callable[[CallbackFunc], CallbackFunc]: ... \ No newline at end of file