Commits

Ginés Martínez Sánchez committed a82c7f8 Draft

inject_event must not raise any exception, must be ever running

  • Participants
  • Parent commits 13209fc
  • Tags 0.6.9

Comments (0)

Files changed (2)

File ginsfsm/smachine.py

                 # Save action/next-state
                 self._states[st_idx][iev] = [ac, next_state_id]
 
+    def __str__(self):
+        return "%s:%s" % (self.__class__.__name__, self.name)
+
+    def __repr__(self):
+        return "%s:%s" % (self.__class__.__name__, self.name)
+
     def set_new_state(self, new_state):
         """  Set a new state.
         Method to used inside actions, to force the change of state.
             if self._last_state != state_id:
                 hora = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
                 logger = self.logger
-                logger and logger.info("%s%s - mach: (%s:%s), new_st: %s" % (
+                logger and logger.info("%s%s - mach: (%r), new_st: %s" % (
                     hora,
                     self._tab(),
-                    self.__class__.__name__, self.name,
+                    self,
                     self._state_list[self._current_state - 1]))
         return True
 
         Execute the associated :term:`action` to the ``event`` in the current
         state.
 
-        The `event name` must match some of the :term:`event-name` of the
-        machine's :term:`event-list` or a :exc:`EventError` exception
-        will be raised.
+        If the `event name` doesn't match any of the :term:`event-name` of the
+        machine's :term:`event-list`,
+        then function **returns** :exc:`EventNotAcceptedError`.
 
-        If the :term:`event-name` exists in the machine , but it's not accepted
-        by the current state, then no exception is raised but the
-        function **returns** :exc:`EventNotAcceptedError`.
+        If the :term:`event-name` exists in the machine ,
+        but it's not accepted by the current state,
+        then function **returns** :exc:`EventNotAcceptedError`.
 
             .. note:: The :meth:`inject_event` method doesn't
                 **raise** :exc:`EventNotAcceptedError`
 
         event_id = self._event_index.get(event_name, 0)
         if event_id <= 0:
-            raise EventError("ERROR inject_event %r UNKNOWN in (%s,%s)" % (
-                event_name, self.__class__.__name__, self.name))
+            hora = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
+            logger and logger.warn(
+                "%s%s<> mach: (%r), st: %s, ev: %s "
+                "(NOT ACCEPTED by UNKNOWN)" % (
+                    hora,
+                    self._tab(),
+                    self,
+                    self._state_list[self._current_state - 1],
+                    event.event_name
+                )
+            )
+            self._decrease_inside()
+            return EventNotAcceptedError
 
         self._increase_inside()
 
 
             hora = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
             logger and logger.warn(
-                "%s%s<> mach: (%s:%s), st: %s, ev: %s "
+                "%s%s<> mach: (%r), st: %s, ev: %s "
                 "(NOT ACCEPTED, no match action)" % (
                     hora,
                     self._tab(),
-                    self.__class__.__name__, self.name,
+                    self,
                     self._state_list[self._current_state - 1],
                     event.event_name
                 )
                 action_name = action[0].__name__
             hora = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
             logger and logger.info(
-                "%s%s-> mach: (%s:%s), st: %s, ev: (%s,%s), ac: %s()" % (
+                "%s%s-> mach: (%r), st: %s, ev: (%s,%s), ac: %s()" % (
                     hora,
                     self._tab(),
-                    self.__class__.__name__, self.name,
+                    self,
                     self._state_list[self._current_state - 1],
                     self._event_list[event_id - 1],
                     '' if trace_mach == 1 else repr(event.kw),
 
         if trace_mach:
             hora = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
-            logger and logger.info("%s%s<- mach: (%s:%s), st: %s, ret: %s" % (
+            logger and logger.info("%s%s<- mach: (%r), st: %s, ret: %s" % (
                 hora,
                 self._tab(),
-                self.__class__.__name__, self.name,
+                self,
                 self._state_list[self._current_state - 1],
                 repr(result)))
 
         hora = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
         if not old_value and new_value:
             self.__trace_mach = new_value
-            logger and logger.info("%s%s* trace_mach ON: (%s:%s)" % (
+            logger and logger.info("%s%s* trace_mach ON: (%r)" % (
                 hora,
                 self._tab(),
-                self.__class__.__name__, self.name))
+                self))
         elif old_value and not new_value:
             self.__trace_mach = False
-            logger and logger.info("%s%s* trace_mach OFF: (%s:%s)" % (
+            logger and logger.info("%s%s* trace_mach OFF: (%r)" % (
                 hora,
                 self._tab(),
-                self.__class__.__name__, self.name))
+                self))
 
     def _increase_inside(self):
         """ TO BE OVERRIDEN by gaplic

File ginsfsm/tests/test_smachine.py

         self.assertEqual(self.fsm._state_index.get('ST_STATE2'), 2)
 
     def test_inject_event1(self):
-        self.assertRaises(EventError, self.fsm.inject_event, 'EV_XXX')
         self.assertRaises(EventError, self.fsm.inject_event, {})
+
+        ret = self.fsm.inject_event('EV_XXX')
+        self.assertEqual(ret, EventNotAcceptedError)
+
         ret = self.fsm.inject_event('EV_TEST2')
         self.assertEqual(ret, EventNotAcceptedError)
         ret = self.fsm.inject_event('EV_TEST1')