annotate src/tools/misc.py @ 297:c5554e2939dd

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