From ccce4df49a7432ab24243393050438c0275fa730 Mon Sep 17 00:00:00 2001 From: Gulshan Gupta Date: Tue, 25 Oct 2016 10:34:35 +0530 Subject: [PATCH 1/3] Including the states check in Machine execution --- transitions/core.py | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/transitions/core.py b/transitions/core.py index 2c1d0c1f..549cfb5a 100644 --- a/transitions/core.py +++ b/transitions/core.py @@ -172,7 +172,7 @@ def add_callback(self, trigger, func): class NonTransition(Transition): - def __init__(self, conditions=None, unless=None, before=None, after=None): + def __init__(self, states=[], conditions=None, unless=None, before=None, after=None): """ Args: conditions (string, list): Condition(s) that must pass in order for @@ -186,6 +186,7 @@ def __init__(self, conditions=None, unless=None, before=None, after=None): transition. after (string or list): callbacks to trigger after the transition. """ + self.states = [] if states is None else listify(states) self.before = [] if before is None else listify(before) self.after = [] if after is None else listify(after) @@ -198,17 +199,17 @@ def __init__(self, conditions=None, unless=None, before=None, after=None): self.conditions.append(Condition(u, target=False)) def execute(self, event_data): - """ Execute the transition. + """ Execute the Non Transition. Args: event: An instance of class EventData. - Returns: boolean indicating whether or not the transition was + Returns: boolean indicating whether or not the nontransition was successfully executed (True if successful, False if not). """ machine = event_data.machine for c in self.conditions: if not c.check(event_data): - logger.info("Transition condition failed: %s() does not " + - "return %s. Transition halted.", c.func, c.target) + logger.info("NonTransition condition failed: %s() does not " + + "return %s. NonTransition halted.", c.func, c.target) return False for func in self.before: machine.callback(getattr(event_data.model, func), event_data) @@ -289,6 +290,8 @@ def trigger(self, *args, **kwargs): event = EventData(self.machine.current_state, self, self.machine, self.machine.model, args=args, kwargs=kwargs) for nt in self.non_transitions: + if self.machine.current_state not in nt.states: + return False if nt.execute(event): return True From 565d1bdccc253860a6eac264c54cb540852b5ee3 Mon Sep 17 00:00:00 2001 From: Gulshan Gupta Date: Tue, 1 Nov 2016 20:44:05 +0530 Subject: [PATCH 2/3] using add_non_transition method to filter non transitions on a machine --- transitions/core.py | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/transitions/core.py b/transitions/core.py index 549cfb5a..2a49a25c 100644 --- a/transitions/core.py +++ b/transitions/core.py @@ -275,7 +275,8 @@ def add_non_transition(self, non_transition): non_transition (NonTransition): The NonTransition instance to add to the list. """ - self.non_transitions.append(non_transition) + if self.machine.current_state in non_transition.states: + self.non_transitions.append(non_transition) def trigger(self, *args, **kwargs): """ Serially execute all transitions that match the current state, @@ -290,8 +291,6 @@ def trigger(self, *args, **kwargs): event = EventData(self.machine.current_state, self, self.machine, self.machine.model, args=args, kwargs=kwargs) for nt in self.non_transitions: - if self.machine.current_state not in nt.states: - return False if nt.execute(event): return True From 569711f6e803082927c795b5de8a31fe5cd5133f Mon Sep 17 00:00:00 2001 From: Gulshan Gupta Date: Tue, 1 Nov 2016 20:53:31 +0530 Subject: [PATCH 3/3] None instead of empty list default arg --- transitions/core.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/transitions/core.py b/transitions/core.py index 2a49a25c..44af6044 100644 --- a/transitions/core.py +++ b/transitions/core.py @@ -172,9 +172,11 @@ def add_callback(self, trigger, func): class NonTransition(Transition): - def __init__(self, states=[], conditions=None, unless=None, before=None, after=None): + def __init__(self, states=None, conditions=None, unless=None, before=None, after=None): """ Args: + states (string, list): State(s) in which NonTransition is can + happen conditions (string, list): Condition(s) that must pass in order for the transition to take place. Either a string providing the name of a callable, or a list of callables. For the transition