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.
This commit is contained in:
parent
b27f385511
commit
6f64d7d0a3
1 changed files with 32 additions and 7 deletions
|
|
@ -29,14 +29,12 @@ class TriggerException(Exception):
|
|||
|
||||
|
||||
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 @@ class TriggerManager(object):
|
|||
|
||||
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 @@ class TriggerManager(object):
|
|||
|
||||
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 @@ class TriggerManager(object):
|
|||
|
||||
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 @@ class TriggerManager(object):
|
|||
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
|
||||
|
|
|
|||
Loading…
Reference in a new issue