comparison src/tools/misc.py @ 250:ad3b820651fa

Tools: new misc library, Trigger management implementation
author Goffi <goffi@goffi.org>
date Mon, 17 Jan 2011 00:11:50 +0100
parents
children cf005701624b
comparison
equal deleted inserted replaced
249:0ed5553b5313 250:ad3b820651fa
1 #!/usr/bin/python
2 # -*- coding: utf-8 -*-
3
4 """
5 SAT: a jabber client
6 Copyright (C) 2009, 2010, 2011 Jérôme Poisson (goffi@goffi.org)
7
8 This program is free software: you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation, either version 3 of the License, or
11 (at your option) any later version.
12
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with this program. If not, see <http://www.gnu.org/licenses/>.
20 """
21
22 from logging import debug, info, error
23
24 """Misc usefull classes"""
25
26 class TriggerException(Exception):
27 pass
28
29 class TriggerManager:
30 """This class manage triggers: code which interact to change de behaviour of SàT"""
31
32 def __init__(self):
33 self.__triggers={}
34
35 def add(self, point_name, callback):
36 """Add a trigger to a point
37 @param point_name: name of the point when the trigger should be run
38 @param callback: method to call at the trigger point
39 """
40 if not self.__triggers.has_key(point_name):
41 self.__triggers[point_name]=[]
42 self.__triggers[point_name].append(callback)
43
44 def remove(self, point_name, callback):
45 """Remove a trigger from a point
46 @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"""
48 try:
49 self.__triggers[point_name].remove(callback)
50 except (KeyError,ValueError):
51 raise TriggerException("Trying to remove an unexisting trigger")
52
53
54 def point(self, point_name, *args, **kwargs):
55 """This put a trigger point
56 All the trigger for that point will be run
57 @param point_name: name of the trigger point
58 @return: True if the action must be continue, False else"""
59 if not self.__triggers.has_key(point_name):
60 return True
61
62 for trigger in self.__triggers[point_name]:
63 if not trigger(*args, **kwargs):
64 return False
65 return True
66