changeset 2499:af4a38ebf52a

core (trigger): new returnPoint method: Normally trigger return value is a boolean used to check if normal workflow must be continued or stopped. This method add a way to also specify a return value to the method, in addition to the "continue or not" boolean.
author Goffi <goffi@goffi.org>
date Wed, 28 Feb 2018 18:28:39 +0100
parents d6de69da3dd4
children 898b6e1fdc7a
files src/tools/trigger.py
diffstat 1 files changed, 32 insertions(+), 7 deletions(-) [+]
line wrap: on
line diff
--- a/src/tools/trigger.py	Wed Feb 28 18:28:39 2018 +0100
+++ b/src/tools/trigger.py	Wed Feb 28 18:28:39 2018 +0100
@@ -29,14 +29,12 @@
 
 
 class SkipOtherTriggers(Exception):
-    """ Exception to raise if normal behaviour must be followed instead of
-        following triggers list """
+    """ Exception to raise if normal behaviour must be followed instead of following triggers list """
     pass
 
 
 class TriggerManager(object):
-    """This class manage triggers: code which interact to change the behaviour
-    of SàT"""
+    """This class manage triggers: code which interact to change the behaviour of SàT"""
 
     try:  # FIXME: to be removed when a better solution is found
         MIN_PRIORITY = float('-inf')
@@ -51,6 +49,7 @@
 
     def add(self, point_name, callback, priority=0):
         """Add a trigger to a point
+
         @param point_name: name of the point when the trigger should be run
         @param callback: method to call at the trigger point
         @param priority: callback will be called in priority order, biggest
@@ -69,8 +68,10 @@
 
     def remove(self, point_name, callback):
         """Remove a trigger from a point
+
         @param point_name: name of the point when the trigger should be run
-        @param callback: method to remove, must exists in the trigger point"""
+        @param callback: method to remove, must exists in the trigger point
+        """
         for trigger_tuple in self.__triggers[point_name]:
             if trigger_tuple[1] == callback:
                 self.__triggers[point_name].remove(trigger_tuple)
@@ -79,9 +80,11 @@
 
     def point(self, point_name, *args, **kwargs):
         """This put a trigger point
-        All the trigger for that point will be run
+
+        All the triggers for that point will be run
         @param point_name: name of the trigger point
-        @return: True if the action must be continued, False else"""
+        @return: True if the action must be continued, False else
+        """
         if point_name not in self.__triggers:
             return True
 
@@ -92,3 +95,25 @@
             except SkipOtherTriggers:
                 break
         return True
+
+    def returnPoint(self, point_name, *args, **kwargs):
+        """Like point but trigger must return (continue, return_value)
+
+        All triggers for that point must return a tuple with 2 values:
+            - continue, same as for point, if False action must be finished
+            - return_value: value to return ONLY IF CONTINUE IS FALSE
+        @param point_name: name of the trigger point
+        @return: True if the action must be continued, False else
+        """
+
+        if point_name not in self.__triggers:
+            return True
+
+        for priority, trigger in self.__triggers[point_name]:
+            try:
+                cont, ret_value = trigger(*args, **kwargs)
+                if not cont:
+                    return False, ret_value
+            except SkipOtherTriggers:
+                break
+        return True, None