Mercurial > libervia-backend
annotate src/tools/misc.py @ 573:db7a03a33612
installation: added mutagen to setup.py's dependencies
author | Goffi <goffi@goffi.org> |
---|---|
date | Mon, 07 Jan 2013 23:54:17 +0100 |
parents | ca13633d3b6b |
children | beaf6bec2fcd |
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 |
572 | 6 Copyright (C) 2009, 2010, 2011, 2012, 2013 Jérôme Poisson (goffi@goffi.org) |
250
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 |
480
2a072735e459
Licence modification: the full project is now under AGPL v3+ instead of GPL v3+
Goffi <goffi@goffi.org>
parents:
459
diff
changeset
|
9 it under the terms of the GNU Affero General Public License as published by |
250
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 |
480
2a072735e459
Licence modification: the full project is now under AGPL v3+ instead of GPL v3+
Goffi <goffi@goffi.org>
parents:
459
diff
changeset
|
16 GNU Affero General Public License for more details. |
250
ad3b820651fa
Tools: new misc library, Trigger management implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
17 |
480
2a072735e459
Licence modification: the full project is now under AGPL v3+ instead of GPL v3+
Goffi <goffi@goffi.org>
parents:
459
diff
changeset
|
18 You should have received a copy of the GNU Affero General Public License |
250
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 |
525
5431136501ab
core: Triggers can now frobid other triggers execution
Goffi <goffi@goffi.org>
parents:
516
diff
changeset
|
29 class SkipOtherTriggers(Exception): |
5431136501ab
core: Triggers can now frobid other triggers execution
Goffi <goffi@goffi.org>
parents:
516
diff
changeset
|
30 """ Exception to raise if normal behaviour must be followed instead of |
5431136501ab
core: Triggers can now frobid other triggers execution
Goffi <goffi@goffi.org>
parents:
516
diff
changeset
|
31 followind triggers list """ |
5431136501ab
core: Triggers can now frobid other triggers execution
Goffi <goffi@goffi.org>
parents:
516
diff
changeset
|
32 pass |
5431136501ab
core: Triggers can now frobid other triggers execution
Goffi <goffi@goffi.org>
parents:
516
diff
changeset
|
33 |
250
ad3b820651fa
Tools: new misc library, Trigger management implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
34 class TriggerManager: |
ad3b820651fa
Tools: new misc library, Trigger management implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
35 """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
|
36 |
ad3b820651fa
Tools: new misc library, Trigger management implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
37 def __init__(self): |
ad3b820651fa
Tools: new misc library, Trigger management implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
38 self.__triggers={} |
ad3b820651fa
Tools: new misc library, Trigger management implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
39 |
516
7ee15fbe8c08
core: added priority management in triggers
Goffi <goffi@goffi.org>
parents:
501
diff
changeset
|
40 def add(self, point_name, callback, priority=0): |
250
ad3b820651fa
Tools: new misc library, Trigger management implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
41 """Add a trigger to a point |
ad3b820651fa
Tools: new misc library, Trigger management implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
42 @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
|
43 @param callback: method to call at the trigger point |
516
7ee15fbe8c08
core: added priority management in triggers
Goffi <goffi@goffi.org>
parents:
501
diff
changeset
|
44 @param priority: callback will be called in priority order, biggest first |
250
ad3b820651fa
Tools: new misc library, Trigger management implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
45 """ |
ad3b820651fa
Tools: new misc library, Trigger management implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
46 if not self.__triggers.has_key(point_name): |
ad3b820651fa
Tools: new misc library, Trigger management implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
47 self.__triggers[point_name]=[] |
516
7ee15fbe8c08
core: added priority management in triggers
Goffi <goffi@goffi.org>
parents:
501
diff
changeset
|
48 self.__triggers[point_name].append((priority, callback)) |
7ee15fbe8c08
core: added priority management in triggers
Goffi <goffi@goffi.org>
parents:
501
diff
changeset
|
49 self.__triggers[point_name].sort(key=lambda trigger_tuple: trigger_tuple[0], reverse=True) |
250
ad3b820651fa
Tools: new misc library, Trigger management implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
50 |
ad3b820651fa
Tools: new misc library, Trigger management implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
51 def remove(self, point_name, callback): |
ad3b820651fa
Tools: new misc library, Trigger management implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
52 """Remove a trigger from a point |
ad3b820651fa
Tools: new misc library, Trigger management implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
53 @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
|
54 @param callback: method to remove, must exists in the trigger point""" |
516
7ee15fbe8c08
core: added priority management in triggers
Goffi <goffi@goffi.org>
parents:
501
diff
changeset
|
55 for trigger_tuple in self.__triggers[point_name]: |
7ee15fbe8c08
core: added priority management in triggers
Goffi <goffi@goffi.org>
parents:
501
diff
changeset
|
56 if trigger_tuple[1] == callback: |
7ee15fbe8c08
core: added priority management in triggers
Goffi <goffi@goffi.org>
parents:
501
diff
changeset
|
57 self.__triggers[point_name].remove(trigger_tuple) |
7ee15fbe8c08
core: added priority management in triggers
Goffi <goffi@goffi.org>
parents:
501
diff
changeset
|
58 return |
7ee15fbe8c08
core: added priority management in triggers
Goffi <goffi@goffi.org>
parents:
501
diff
changeset
|
59 raise TriggerException("Trying to remove an unexisting trigger") |
250
ad3b820651fa
Tools: new misc library, Trigger management implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
60 |
ad3b820651fa
Tools: new misc library, Trigger management implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
61 def point(self, point_name, *args, **kwargs): |
ad3b820651fa
Tools: new misc library, Trigger management implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
62 """This put a trigger point |
ad3b820651fa
Tools: new misc library, Trigger management implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
63 All the trigger for that point will be run |
ad3b820651fa
Tools: new misc library, Trigger management implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
64 @param point_name: name of the trigger point |
501
e9634d2e7b38
core, quick_frontend, primitivus, wix: Contacts List refactoring phase 1:
Goffi <goffi@goffi.org>
parents:
480
diff
changeset
|
65 @return: True if the action must be continued, False else""" |
250
ad3b820651fa
Tools: new misc library, Trigger management implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
66 if not self.__triggers.has_key(point_name): |
ad3b820651fa
Tools: new misc library, Trigger management implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
67 return True |
ad3b820651fa
Tools: new misc library, Trigger management implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
68 |
516
7ee15fbe8c08
core: added priority management in triggers
Goffi <goffi@goffi.org>
parents:
501
diff
changeset
|
69 for priority,trigger in self.__triggers[point_name]: |
525
5431136501ab
core: Triggers can now frobid other triggers execution
Goffi <goffi@goffi.org>
parents:
516
diff
changeset
|
70 try: |
5431136501ab
core: Triggers can now frobid other triggers execution
Goffi <goffi@goffi.org>
parents:
516
diff
changeset
|
71 if not trigger(*args, **kwargs): |
5431136501ab
core: Triggers can now frobid other triggers execution
Goffi <goffi@goffi.org>
parents:
516
diff
changeset
|
72 return False |
5431136501ab
core: Triggers can now frobid other triggers execution
Goffi <goffi@goffi.org>
parents:
516
diff
changeset
|
73 except SkipOtherTriggers: |
5431136501ab
core: Triggers can now frobid other triggers execution
Goffi <goffi@goffi.org>
parents:
516
diff
changeset
|
74 break |
250
ad3b820651fa
Tools: new misc library, Trigger management implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
75 return True |
ad3b820651fa
Tools: new misc library, Trigger management implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
76 |