comparison src/tools/misc.py @ 741:00318e60a06a

core (tools): set min and max priorities for triggers and warn if several triggers have the same not null priority
author souliane <souliane@mailoo.org>
date Fri, 13 Dec 2013 05:35:24 +0100
parents 84a6e83157c2
children 03744d9ebc13
comparison
equal deleted inserted replaced
740:aebc8ba05129 741:00318e60a06a
17 # You should have received a copy of the GNU Affero General Public License 17 # You should have received a copy of the GNU Affero General Public License
18 # along with this program. If not, see <http://www.gnu.org/licenses/>. 18 # along with this program. If not, see <http://www.gnu.org/licenses/>.
19 19
20 """Misc usefull classes""" 20 """Misc usefull classes"""
21 21
22 import sys
23 from logging import debug, warning
22 24
23 class TriggerException(Exception): 25 class TriggerException(Exception):
24 pass 26 pass
25 27
26 28
32 34
33 class TriggerManager(object): 35 class TriggerManager(object):
34 """This class manage triggers: code which interact to change de behaviour 36 """This class manage triggers: code which interact to change de behaviour
35 of SàT""" 37 of SàT"""
36 38
39 MIN_PRIORITY = float('-inf')
40 MAX_PRIORITY = float('+inf')
41
37 def __init__(self): 42 def __init__(self):
38 self.__triggers = {} 43 self.__triggers = {}
39 44
40 def add(self, point_name, callback, priority=0): 45 def add(self, point_name, callback, priority=0):
41 """Add a trigger to a point 46 """Add a trigger to a point
44 @param priority: callback will be called in priority order, biggest 49 @param priority: callback will be called in priority order, biggest
45 first 50 first
46 """ 51 """
47 if point_name not in self.__triggers: 52 if point_name not in self.__triggers:
48 self.__triggers[point_name] = [] 53 self.__triggers[point_name] = []
54 if priority != 0 and priority in [trigger_tuple[0] for trigger_tuple in self.__triggers[point_name]]:
55 if priority in (MIN_PRIORITY, MAX_PRIORITY):
56 warning(_("There is already a bound priority [%s]") % point_name)
57 else:
58 debug(_("There is already a trigger with the same priority [%s]") % point_name)
49 self.__triggers[point_name].append((priority, callback)) 59 self.__triggers[point_name].append((priority, callback))
50 self.__triggers[point_name].sort(key=lambda trigger_tuple: 60 self.__triggers[point_name].sort(key=lambda trigger_tuple:
51 trigger_tuple[0], reverse=True) 61 trigger_tuple[0], reverse=True)
52 62
53 def remove(self, point_name, callback): 63 def remove(self, point_name, callback):