Mercurial > libervia-backend
comparison src/tools/misc.py @ 516:7ee15fbe8c08
core: added priority management in triggers
author | Goffi <goffi@goffi.org> |
---|---|
date | Sat, 20 Oct 2012 18:18:40 +0200 |
parents | e9634d2e7b38 |
children | 5431136501ab |
comparison
equal
deleted
inserted
replaced
515:29b5ef129488 | 516:7ee15fbe8c08 |
---|---|
30 """This class manage triggers: code which interact to change de behaviour of SàT""" | 30 """This class manage triggers: code which interact to change de behaviour of SàT""" |
31 | 31 |
32 def __init__(self): | 32 def __init__(self): |
33 self.__triggers={} | 33 self.__triggers={} |
34 | 34 |
35 def add(self, point_name, callback): | 35 def add(self, point_name, callback, priority=0): |
36 """Add a trigger to a point | 36 """Add a trigger to a point |
37 @param point_name: name of the point when the trigger should be run | 37 @param point_name: name of the point when the trigger should be run |
38 @param callback: method to call at the trigger point | 38 @param callback: method to call at the trigger point |
39 @param priority: callback will be called in priority order, biggest first | |
39 """ | 40 """ |
40 if not self.__triggers.has_key(point_name): | 41 if not self.__triggers.has_key(point_name): |
41 self.__triggers[point_name]=[] | 42 self.__triggers[point_name]=[] |
42 self.__triggers[point_name].append(callback) | 43 self.__triggers[point_name].append((priority, callback)) |
44 self.__triggers[point_name].sort(key=lambda trigger_tuple: trigger_tuple[0], reverse=True) | |
43 | 45 |
44 def remove(self, point_name, callback): | 46 def remove(self, point_name, callback): |
45 """Remove a trigger from a point | 47 """Remove a trigger from a point |
46 @param point_name: name of the point when the trigger should be run | 48 @param point_name: name of the point when the trigger should be run |
47 @param callback: method to remove, must exists in the trigger point""" | 49 @param callback: method to remove, must exists in the trigger point""" |
48 try: | 50 for trigger_tuple in self.__triggers[point_name]: |
49 self.__triggers[point_name].remove(callback) | 51 if trigger_tuple[1] == callback: |
50 except (KeyError,ValueError): | 52 self.__triggers[point_name].remove(trigger_tuple) |
51 raise TriggerException("Trying to remove an unexisting trigger") | 53 return |
52 | 54 raise TriggerException("Trying to remove an unexisting trigger") |
53 | 55 |
54 def point(self, point_name, *args, **kwargs): | 56 def point(self, point_name, *args, **kwargs): |
55 """This put a trigger point | 57 """This put a trigger point |
56 All the trigger for that point will be run | 58 All the trigger for that point will be run |
57 @param point_name: name of the trigger point | 59 @param point_name: name of the trigger point |
58 @return: True if the action must be continued, False else""" | 60 @return: True if the action must be continued, False else""" |
59 if not self.__triggers.has_key(point_name): | 61 if not self.__triggers.has_key(point_name): |
60 return True | 62 return True |
61 | 63 |
62 for trigger in self.__triggers[point_name]: | 64 for priority,trigger in self.__triggers[point_name]: |
63 if not trigger(*args, **kwargs): | 65 if not trigger(*args, **kwargs): |
64 return False | 66 return False |
65 return True | 67 return True |
66 | 68 |