annotate sat/tools/trigger.py @ 2617:81b70eeb710f

quick_frontend(contact list): refactored update: update is now called with appropriate constant value (C.UPDATE_ADD, C.UPDATE_DELETE, C.UPDATE_MODIFY and so on) when a widget change visibility according to current options. Before it was linked to cache only (C.UPDATE_ADD was only called when contact was first added to cache). This make widget handling in frontends more easy. Renamed entityToShow to entityVisible, which seems to correspond better. Started reducing lines lenght to 90 chars as a test. May become the new coding style soon.
author Goffi <goffi@goffi.org>
date Sun, 24 Jun 2018 21:59:29 +0200
parents 26edcf3a30eb
children 56f94936df1e
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
1934
2daf7b4c6756 use of /usr/bin/env instead of /usr/bin/python in shebang
Goffi <goffi@goffi.org>
parents: 1766
diff changeset
1 #!/usr/bin/env python2
250
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
609
84a6e83157c2 fixed licences in docstrings (they are now in comments)
Goffi <goffi@goffi.org>
parents: 590
diff changeset
4 # SAT: a jabber client
2483
0046283a285d dates update
Goffi <goffi@goffi.org>
parents: 2414
diff changeset
5 # Copyright (C) 2009-2018 Jérôme Poisson (goffi@goffi.org)
250
ad3b820651fa Tools: new misc library, Trigger management implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
6
609
84a6e83157c2 fixed licences in docstrings (they are now in comments)
Goffi <goffi@goffi.org>
parents: 590
diff changeset
7 # This program is free software: you can redistribute it and/or modify
84a6e83157c2 fixed licences in docstrings (they are now in comments)
Goffi <goffi@goffi.org>
parents: 590
diff changeset
8 # it under the terms of the GNU Affero General Public License as published by
84a6e83157c2 fixed licences in docstrings (they are now in comments)
Goffi <goffi@goffi.org>
parents: 590
diff changeset
9 # the Free Software Foundation, either version 3 of the License, or
84a6e83157c2 fixed licences in docstrings (they are now in comments)
Goffi <goffi@goffi.org>
parents: 590
diff changeset
10 # (at your option) any later version.
250
ad3b820651fa Tools: new misc library, Trigger management implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
11
609
84a6e83157c2 fixed licences in docstrings (they are now in comments)
Goffi <goffi@goffi.org>
parents: 590
diff changeset
12 # This program is distributed in the hope that it will be useful,
84a6e83157c2 fixed licences in docstrings (they are now in comments)
Goffi <goffi@goffi.org>
parents: 590
diff changeset
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
84a6e83157c2 fixed licences in docstrings (they are now in comments)
Goffi <goffi@goffi.org>
parents: 590
diff changeset
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
84a6e83157c2 fixed licences in docstrings (they are now in comments)
Goffi <goffi@goffi.org>
parents: 590
diff changeset
15 # GNU Affero General Public License for more details.
250
ad3b820651fa Tools: new misc library, Trigger management implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
16
609
84a6e83157c2 fixed licences in docstrings (they are now in comments)
Goffi <goffi@goffi.org>
parents: 590
diff changeset
17 # You should have received a copy of the GNU Affero General Public License
84a6e83157c2 fixed licences in docstrings (they are now in comments)
Goffi <goffi@goffi.org>
parents: 590
diff changeset
18 # along with this program. If not, see <http://www.gnu.org/licenses/>.
250
ad3b820651fa Tools: new misc library, Trigger management implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
19
590
56531f9e9ac7 Fix pep8 support in src/tools.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 588
diff changeset
20 """Misc usefull classes"""
250
ad3b820651fa Tools: new misc library, Trigger management implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
21
771
bfabeedbf32e core: i18n refactoring:
Goffi <goffi@goffi.org>
parents: 742
diff changeset
22 from sat.core.i18n import _
993
301b342c697a core: use of the new core.log module:
Goffi <goffi@goffi.org>
parents: 876
diff changeset
23 from sat.core.log import getLogger
301b342c697a core: use of the new core.log module:
Goffi <goffi@goffi.org>
parents: 876
diff changeset
24 log = getLogger(__name__)
250
ad3b820651fa Tools: new misc library, Trigger management implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
25
742
03744d9ebc13 plugin XEP-0033: implementation of the addressing feature:
souliane <souliane@mailoo.org>
parents: 741
diff changeset
26
250
ad3b820651fa Tools: new misc library, Trigger management implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
27 class TriggerException(Exception):
ad3b820651fa Tools: new misc library, Trigger management implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
28 pass
ad3b820651fa Tools: new misc library, Trigger management implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
29
590
56531f9e9ac7 Fix pep8 support in src/tools.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 588
diff changeset
30
525
5431136501ab core: Triggers can now frobid other triggers execution
Goffi <goffi@goffi.org>
parents: 516
diff changeset
31 class SkipOtherTriggers(Exception):
2499
af4a38ebf52a core (trigger): new returnPoint method:
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
32 """ Exception to raise if normal behaviour must be followed instead of following triggers list """
525
5431136501ab core: Triggers can now frobid other triggers execution
Goffi <goffi@goffi.org>
parents: 516
diff changeset
33 pass
5431136501ab core: Triggers can now frobid other triggers execution
Goffi <goffi@goffi.org>
parents: 516
diff changeset
34
590
56531f9e9ac7 Fix pep8 support in src/tools.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 588
diff changeset
35
588
beaf6bec2fcd Remove every old-style class.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 572
diff changeset
36 class TriggerManager(object):
2499
af4a38ebf52a core (trigger): new returnPoint method:
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
37 """This class manage triggers: code which interact to change the behaviour of SàT"""
250
ad3b820651fa Tools: new misc library, Trigger management implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
38
1347
ba41a81d14c2 frontends (QuickApp), tools (TriggerManager): frontends can also use triggers + add those for sending and receiving a message
souliane <souliane@mailoo.org>
parents: 993
diff changeset
39 try: # FIXME: to be removed when a better solution is found
ba41a81d14c2 frontends (QuickApp), tools (TriggerManager): frontends can also use triggers + add those for sending and receiving a message
souliane <souliane@mailoo.org>
parents: 993
diff changeset
40 MIN_PRIORITY = float('-inf')
ba41a81d14c2 frontends (QuickApp), tools (TriggerManager): frontends can also use triggers + add those for sending and receiving a message
souliane <souliane@mailoo.org>
parents: 993
diff changeset
41 MAX_PRIORITY = float('+inf')
ba41a81d14c2 frontends (QuickApp), tools (TriggerManager): frontends can also use triggers + add those for sending and receiving a message
souliane <souliane@mailoo.org>
parents: 993
diff changeset
42 except: # XXX: Pyjamas will bug if you specify ValueError here
ba41a81d14c2 frontends (QuickApp), tools (TriggerManager): frontends can also use triggers + add those for sending and receiving a message
souliane <souliane@mailoo.org>
parents: 993
diff changeset
43 # Pyjamas uses the JS Float class
ba41a81d14c2 frontends (QuickApp), tools (TriggerManager): frontends can also use triggers + add those for sending and receiving a message
souliane <souliane@mailoo.org>
parents: 993
diff changeset
44 MIN_PRIORITY = Number.NEGATIVE_INFINITY
ba41a81d14c2 frontends (QuickApp), tools (TriggerManager): frontends can also use triggers + add those for sending and receiving a message
souliane <souliane@mailoo.org>
parents: 993
diff changeset
45 MAX_PRIORITY = Number.POSITIVE_INFINITY
741
00318e60a06a core (tools): set min and max priorities for triggers and warn if several triggers have the same not null priority
souliane <souliane@mailoo.org>
parents: 609
diff changeset
46
250
ad3b820651fa Tools: new misc library, Trigger management implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
47 def __init__(self):
590
56531f9e9ac7 Fix pep8 support in src/tools.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 588
diff changeset
48 self.__triggers = {}
250
ad3b820651fa Tools: new misc library, Trigger management implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
49
516
7ee15fbe8c08 core: added priority management in triggers
Goffi <goffi@goffi.org>
parents: 501
diff changeset
50 def add(self, point_name, callback, priority=0):
250
ad3b820651fa Tools: new misc library, Trigger management implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
51 """Add a trigger to a point
2499
af4a38ebf52a core (trigger): new returnPoint method:
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
52
250
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 call at the trigger point
590
56531f9e9ac7 Fix pep8 support in src/tools.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 588
diff changeset
55 @param priority: callback will be called in priority order, biggest
56531f9e9ac7 Fix pep8 support in src/tools.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 588
diff changeset
56 first
250
ad3b820651fa Tools: new misc library, Trigger management implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
57 """
590
56531f9e9ac7 Fix pep8 support in src/tools.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 588
diff changeset
58 if point_name not in self.__triggers:
56531f9e9ac7 Fix pep8 support in src/tools.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 588
diff changeset
59 self.__triggers[point_name] = []
741
00318e60a06a core (tools): set min and max priorities for triggers and warn if several triggers have the same not null priority
souliane <souliane@mailoo.org>
parents: 609
diff changeset
60 if priority != 0 and priority in [trigger_tuple[0] for trigger_tuple in self.__triggers[point_name]]:
876
65bf1bc70f6b tools, plugin XEP-0077: small fixes
souliane <souliane@mailoo.org>
parents: 811
diff changeset
61 if priority in (self.MIN_PRIORITY, self.MAX_PRIORITY):
1409
3265a2639182 massive (preventive) addition of 'u' (unicode) before the strings passed to logging functions
souliane <souliane@mailoo.org>
parents: 1396
diff changeset
62 log.warning(_(u"There is already a bound priority [%s]") % point_name)
741
00318e60a06a core (tools): set min and max priorities for triggers and warn if several triggers have the same not null priority
souliane <souliane@mailoo.org>
parents: 609
diff changeset
63 else:
1409
3265a2639182 massive (preventive) addition of 'u' (unicode) before the strings passed to logging functions
souliane <souliane@mailoo.org>
parents: 1396
diff changeset
64 log.debug(_(u"There is already a trigger with the same priority [%s]") % point_name)
516
7ee15fbe8c08 core: added priority management in triggers
Goffi <goffi@goffi.org>
parents: 501
diff changeset
65 self.__triggers[point_name].append((priority, callback))
590
56531f9e9ac7 Fix pep8 support in src/tools.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 588
diff changeset
66 self.__triggers[point_name].sort(key=lambda trigger_tuple:
56531f9e9ac7 Fix pep8 support in src/tools.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 588
diff changeset
67 trigger_tuple[0], reverse=True)
250
ad3b820651fa Tools: new misc library, Trigger management implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
68
ad3b820651fa Tools: new misc library, Trigger management implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
69 def remove(self, point_name, callback):
ad3b820651fa Tools: new misc library, Trigger management implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
70 """Remove a trigger from a point
2499
af4a38ebf52a core (trigger): new returnPoint method:
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
71
250
ad3b820651fa Tools: new misc library, Trigger management implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
72 @param point_name: name of the point when the trigger should be run
2499
af4a38ebf52a core (trigger): new returnPoint method:
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
73 @param callback: method to remove, must exists in the trigger point
af4a38ebf52a core (trigger): new returnPoint method:
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
74 """
516
7ee15fbe8c08 core: added priority management in triggers
Goffi <goffi@goffi.org>
parents: 501
diff changeset
75 for trigger_tuple in self.__triggers[point_name]:
7ee15fbe8c08 core: added priority management in triggers
Goffi <goffi@goffi.org>
parents: 501
diff changeset
76 if trigger_tuple[1] == callback:
7ee15fbe8c08 core: added priority management in triggers
Goffi <goffi@goffi.org>
parents: 501
diff changeset
77 self.__triggers[point_name].remove(trigger_tuple)
7ee15fbe8c08 core: added priority management in triggers
Goffi <goffi@goffi.org>
parents: 501
diff changeset
78 return
7ee15fbe8c08 core: added priority management in triggers
Goffi <goffi@goffi.org>
parents: 501
diff changeset
79 raise TriggerException("Trying to remove an unexisting trigger")
250
ad3b820651fa Tools: new misc library, Trigger management implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
80
ad3b820651fa Tools: new misc library, Trigger management implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
81 def point(self, point_name, *args, **kwargs):
ad3b820651fa Tools: new misc library, Trigger management implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
82 """This put a trigger point
2499
af4a38ebf52a core (trigger): new returnPoint method:
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
83
af4a38ebf52a core (trigger): new returnPoint method:
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
84 All the triggers for that point will be run
250
ad3b820651fa Tools: new misc library, Trigger management implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
85 @param point_name: name of the trigger point
2499
af4a38ebf52a core (trigger): new returnPoint method:
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
86 @return: True if the action must be continued, False else
af4a38ebf52a core (trigger): new returnPoint method:
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
87 """
590
56531f9e9ac7 Fix pep8 support in src/tools.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 588
diff changeset
88 if point_name not in self.__triggers:
250
ad3b820651fa Tools: new misc library, Trigger management implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
89 return True
ad3b820651fa Tools: new misc library, Trigger management implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
90
1347
ba41a81d14c2 frontends (QuickApp), tools (TriggerManager): frontends can also use triggers + add those for sending and receiving a message
souliane <souliane@mailoo.org>
parents: 993
diff changeset
91 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
92 try:
5431136501ab core: Triggers can now frobid other triggers execution
Goffi <goffi@goffi.org>
parents: 516
diff changeset
93 if not trigger(*args, **kwargs):
5431136501ab core: Triggers can now frobid other triggers execution
Goffi <goffi@goffi.org>
parents: 516
diff changeset
94 return False
5431136501ab core: Triggers can now frobid other triggers execution
Goffi <goffi@goffi.org>
parents: 516
diff changeset
95 except SkipOtherTriggers:
5431136501ab core: Triggers can now frobid other triggers execution
Goffi <goffi@goffi.org>
parents: 516
diff changeset
96 break
250
ad3b820651fa Tools: new misc library, Trigger management implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
97 return True
2499
af4a38ebf52a core (trigger): new returnPoint method:
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
98
af4a38ebf52a core (trigger): new returnPoint method:
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
99 def returnPoint(self, point_name, *args, **kwargs):
af4a38ebf52a core (trigger): new returnPoint method:
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
100 """Like point but trigger must return (continue, return_value)
af4a38ebf52a core (trigger): new returnPoint method:
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
101
af4a38ebf52a core (trigger): new returnPoint method:
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
102 All triggers for that point must return a tuple with 2 values:
af4a38ebf52a core (trigger): new returnPoint method:
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
103 - continue, same as for point, if False action must be finished
af4a38ebf52a core (trigger): new returnPoint method:
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
104 - return_value: value to return ONLY IF CONTINUE IS FALSE
af4a38ebf52a core (trigger): new returnPoint method:
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
105 @param point_name: name of the trigger point
af4a38ebf52a core (trigger): new returnPoint method:
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
106 @return: True if the action must be continued, False else
af4a38ebf52a core (trigger): new returnPoint method:
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
107 """
af4a38ebf52a core (trigger): new returnPoint method:
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
108
af4a38ebf52a core (trigger): new returnPoint method:
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
109 if point_name not in self.__triggers:
af4a38ebf52a core (trigger): new returnPoint method:
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
110 return True
af4a38ebf52a core (trigger): new returnPoint method:
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
111
af4a38ebf52a core (trigger): new returnPoint method:
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
112 for priority, trigger in self.__triggers[point_name]:
af4a38ebf52a core (trigger): new returnPoint method:
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
113 try:
af4a38ebf52a core (trigger): new returnPoint method:
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
114 cont, ret_value = trigger(*args, **kwargs)
af4a38ebf52a core (trigger): new returnPoint method:
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
115 if not cont:
af4a38ebf52a core (trigger): new returnPoint method:
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
116 return False, ret_value
af4a38ebf52a core (trigger): new returnPoint method:
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
117 except SkipOtherTriggers:
af4a38ebf52a core (trigger): new returnPoint method:
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
118 break
af4a38ebf52a core (trigger): new returnPoint method:
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
119 return True, None