# HG changeset patch # User Goffi # Date 1519838919 -3600 # Node ID af4a38ebf52a7bf1667e650cdbbe578614bf4374 # Parent d6de69da3dd446d605d5ab0604e4477a1da7e8a4 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. diff -r d6de69da3dd4 -r af4a38ebf52a src/tools/trigger.py --- 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