comparison src/tools/misc.py @ 590:56531f9e9ac7

Fix pep8 support in src/tools.
author Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
date Fri, 18 Jan 2013 17:55:35 +0100
parents beaf6bec2fcd
children 84a6e83157c2
comparison
equal deleted inserted replaced
589:d1b4805124a1 590:56531f9e9ac7
17 17
18 You should have received a copy of the GNU Affero General Public License 18 You should have received a copy of the GNU Affero General Public License
19 along with this program. If not, see <http://www.gnu.org/licenses/>. 19 along with this program. If not, see <http://www.gnu.org/licenses/>.
20 """ 20 """
21 21
22 from logging import debug, info, error 22 """Misc usefull classes"""
23 23
24 """Misc usefull classes"""
25 24
26 class TriggerException(Exception): 25 class TriggerException(Exception):
27 pass 26 pass
27
28 28
29 class SkipOtherTriggers(Exception): 29 class SkipOtherTriggers(Exception):
30 """ Exception to raise if normal behaviour must be followed instead of 30 """ Exception to raise if normal behaviour must be followed instead of
31 followind triggers list """ 31 followind triggers list """
32 pass 32 pass
33 33
34
34 class TriggerManager(object): 35 class TriggerManager(object):
35 """This class manage triggers: code which interact to change de behaviour of SàT""" 36 """This class manage triggers: code which interact to change de behaviour
37 of SàT"""
36 38
37 def __init__(self): 39 def __init__(self):
38 self.__triggers={} 40 self.__triggers = {}
39 41
40 def add(self, point_name, callback, priority=0): 42 def add(self, point_name, callback, priority=0):
41 """Add a trigger to a point 43 """Add a trigger to a point
42 @param point_name: name of the point when the trigger should be run 44 @param point_name: name of the point when the trigger should be run
43 @param callback: method to call at the trigger point 45 @param callback: method to call at the trigger point
44 @param priority: callback will be called in priority order, biggest first 46 @param priority: callback will be called in priority order, biggest
47 first
45 """ 48 """
46 if not self.__triggers.has_key(point_name): 49 if point_name not in self.__triggers:
47 self.__triggers[point_name]=[] 50 self.__triggers[point_name] = []
48 self.__triggers[point_name].append((priority, callback)) 51 self.__triggers[point_name].append((priority, callback))
49 self.__triggers[point_name].sort(key=lambda trigger_tuple: trigger_tuple[0], reverse=True) 52 self.__triggers[point_name].sort(key=lambda trigger_tuple:
53 trigger_tuple[0], reverse=True)
50 54
51 def remove(self, point_name, callback): 55 def remove(self, point_name, callback):
52 """Remove a trigger from a point 56 """Remove a trigger from a point
53 @param point_name: name of the point when the trigger should be run 57 @param point_name: name of the point when the trigger should be run
54 @param callback: method to remove, must exists in the trigger point""" 58 @param callback: method to remove, must exists in the trigger point"""
61 def point(self, point_name, *args, **kwargs): 65 def point(self, point_name, *args, **kwargs):
62 """This put a trigger point 66 """This put a trigger point
63 All the trigger for that point will be run 67 All the trigger for that point will be run
64 @param point_name: name of the trigger point 68 @param point_name: name of the trigger point
65 @return: True if the action must be continued, False else""" 69 @return: True if the action must be continued, False else"""
66 if not self.__triggers.has_key(point_name): 70 if point_name not in self.__triggers:
67 return True 71 return True
68 72
69 for priority,trigger in self.__triggers[point_name]: 73 for priority,trigger in self.__triggers[point_name]:
70 try: 74 try:
71 if not trigger(*args, **kwargs): 75 if not trigger(*args, **kwargs):
72 return False 76 return False
73 except SkipOtherTriggers: 77 except SkipOtherTriggers:
74 break 78 break
75 return True 79 return True
76