Example #1
0
    def add_state(self,
                  state,
                  on_enter=None,
                  on_exit=None,
                  target=None,
                  terminal=None):
        """Adds a given state to the state machine.

        The on_enter and on_exit callbacks, if provided will be expected to
        take two positional parameters, these being the state being exited (for
        on_exit) or the state being entered (for on_enter) and a second
        parameter which is the event that is being processed that caused the
        state transition.
        """
        if state in self._states:
            raise excp.Duplicate(_("State '%s' already defined") % state)
        if on_enter is not None:
            if not six.callable(on_enter):
                raise ValueError(_("On enter callback must be callable"))
        if on_exit is not None:
            if not six.callable(on_exit):
                raise ValueError(_("On exit callback must be callable"))
        if target is not None and target not in self._states:
            raise excp.InvalidState(
                _("Target state '%s' does not exist") % target)

        self._states[state] = {
            'terminal': bool(terminal),
            'reactions': {},
            'on_enter': on_enter,
            'on_exit': on_exit,
            'target': target,
        }
        self._transitions[state] = OrderedDict()
Example #2
0
 def wrapper(*args, **kwargs):
     try:
         return func(*args, **kwargs)
     except (automaton_exceptions.InvalidState,
             automaton_exceptions.NotInitialized,
             automaton_exceptions.FrozenMachine,
             automaton_exceptions.NotFound) as e:
         raise excp.InvalidState(str(e))
     except automaton_exceptions.Duplicate as e:
         raise excp.Duplicate(str(e))
Example #3
0
    def add_state(self,
                  state,
                  on_enter=None,
                  on_exit=None,
                  target=None,
                  terminal=None,
                  stable=False):
        """Adds a given state to the state machine.

        The on_enter and on_exit callbacks, if provided will be expected to
        take two positional parameters, these being the state being exited (for
        on_exit) or the state being entered (for on_enter) and a second
        parameter which is the event that is being processed that caused the
        state transition.

        :param stable: Use this to specify that this state is a stable/passive
                       state. A state must have been previously defined as
                       'stable' before it can be used as a 'target'
        :param target: The target state for 'state' to go to.  Before a state
                       can be used as a target it must have been previously
                       added and specified as 'stable'
        """
        if state in self._states:
            raise excp.Duplicate(_("State '%s' already defined") % state)
        if on_enter is not None:
            if not six.callable(on_enter):
                raise ValueError(_("On enter callback must be callable"))
        if on_exit is not None:
            if not six.callable(on_exit):
                raise ValueError(_("On exit callback must be callable"))
        if target is not None and target not in self._states:
            raise excp.InvalidState(
                _("Target state '%s' does not exist") % target)
        if target is not None and not self._states[target]['stable']:
            raise excp.InvalidState(
                _("Target state '%s' is not a 'stable' state") % target)

        self._states[state] = {
            'terminal': bool(terminal),
            'reactions': {},
            'on_enter': on_enter,
            'on_exit': on_exit,
            'target': target,
            'stable': stable,
        }
        self._transitions[state] = OrderedDict()