Mercurial > libervia-backend
annotate libervia/backend/tools/trigger.py @ 4192:1d24ff583794
plugin forums: parsing fix + formatting:
- reformatted with black
- fix parsing of forums
- minor improvements
author | Goffi <goffi@goffi.org> |
---|---|
date | Tue, 12 Dec 2023 12:17:15 +0100 |
parents | 4dc00e848961 |
children | 0d7bb4df2343 |
rev | line source |
---|---|
3028 | 1 #!/usr/bin/env python3 |
3137 | 2 |
250
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 |
3479 | 5 # Copyright (C) 2009-2021 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 |
4182
4dc00e848961
core (tools/trigger): new `add_with_check` method:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
22 import inspect |
4dc00e848961
core (tools/trigger): new `add_with_check` method:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
23 from typing import Callable |
4071
4b842c1fb686
refactoring: renamed `sat` package to `libervia.backend`
Goffi <goffi@goffi.org>
parents:
4037
diff
changeset
|
24 from libervia.backend.core.i18n import _ |
4b842c1fb686
refactoring: renamed `sat` package to `libervia.backend`
Goffi <goffi@goffi.org>
parents:
4037
diff
changeset
|
25 from libervia.backend.core.log import getLogger |
4182
4dc00e848961
core (tools/trigger): new `add_with_check` method:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
26 from libervia.backend.core.core_types import SatXMPPEntity |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
27 |
993
301b342c697a
core: use of the new core.log module:
Goffi <goffi@goffi.org>
parents:
876
diff
changeset
|
28 log = getLogger(__name__) |
250
ad3b820651fa
Tools: new misc library, Trigger management implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
29 |
742
03744d9ebc13
plugin XEP-0033: implementation of the addressing feature:
souliane <souliane@mailoo.org>
parents:
741
diff
changeset
|
30 |
250
ad3b820651fa
Tools: new misc library, Trigger management implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
31 class TriggerException(Exception): |
ad3b820651fa
Tools: new misc library, Trigger management implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
32 pass |
ad3b820651fa
Tools: new misc library, Trigger management implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
33 |
590
56531f9e9ac7
Fix pep8 support in src/tools.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
588
diff
changeset
|
34 |
525
5431136501ab
core: Triggers can now frobid other triggers execution
Goffi <goffi@goffi.org>
parents:
516
diff
changeset
|
35 class SkipOtherTriggers(Exception): |
2499
af4a38ebf52a
core (trigger): new returnPoint method:
Goffi <goffi@goffi.org>
parents:
2483
diff
changeset
|
36 """ Exception to raise if normal behaviour must be followed instead of following triggers list """ |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
37 |
525
5431136501ab
core: Triggers can now frobid other triggers execution
Goffi <goffi@goffi.org>
parents:
516
diff
changeset
|
38 pass |
5431136501ab
core: Triggers can now frobid other triggers execution
Goffi <goffi@goffi.org>
parents:
516
diff
changeset
|
39 |
590
56531f9e9ac7
Fix pep8 support in src/tools.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
588
diff
changeset
|
40 |
588
beaf6bec2fcd
Remove every old-style class.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
572
diff
changeset
|
41 class TriggerManager(object): |
2499
af4a38ebf52a
core (trigger): new returnPoint method:
Goffi <goffi@goffi.org>
parents:
2483
diff
changeset
|
42 """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
|
43 |
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
|
44 try: # FIXME: to be removed when a better solution is found |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
45 MIN_PRIORITY = float("-inf") |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
46 MAX_PRIORITY = float("+inf") |
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
|
47 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
|
48 # 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
|
49 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
|
50 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
|
51 |
250
ad3b820651fa
Tools: new misc library, Trigger management implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
52 def __init__(self): |
590
56531f9e9ac7
Fix pep8 support in src/tools.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
588
diff
changeset
|
53 self.__triggers = {} |
250
ad3b820651fa
Tools: new misc library, Trigger management implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
54 |
4182
4dc00e848961
core (tools/trigger): new `add_with_check` method:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
55 def is_available(self, args: list): |
4dc00e848961
core (tools/trigger): new `add_with_check` method:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
56 """Check if plugin used in a client context, and if it's available""" |
4dc00e848961
core (tools/trigger): new `add_with_check` method:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
57 if not args or not isinstance(args[0], SatXMPPEntity): |
4dc00e848961
core (tools/trigger): new `add_with_check` method:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
58 # we are not in the context of a client |
4dc00e848961
core (tools/trigger): new `add_with_check` method:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
59 return True |
4dc00e848961
core (tools/trigger): new `add_with_check` method:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
60 client = args[0] |
4dc00e848961
core (tools/trigger): new `add_with_check` method:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
61 if not client.is_component: |
4dc00e848961
core (tools/trigger): new `add_with_check` method:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
62 # plugins are always avaialble for normal clients |
4dc00e848961
core (tools/trigger): new `add_with_check` method:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
63 return True |
4dc00e848961
core (tools/trigger): new `add_with_check` method:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
64 |
4dc00e848961
core (tools/trigger): new `add_with_check` method:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
65 |
4dc00e848961
core (tools/trigger): new `add_with_check` method:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
66 def add(self, point_name, callback: Callable, priority=0): |
250
ad3b820651fa
Tools: new misc library, Trigger management implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
67 """Add a trigger to a point |
2499
af4a38ebf52a
core (trigger): new returnPoint method:
Goffi <goffi@goffi.org>
parents:
2483
diff
changeset
|
68 |
250
ad3b820651fa
Tools: new misc library, Trigger management implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
69 @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
|
70 @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
|
71 @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
|
72 first |
250
ad3b820651fa
Tools: new misc library, Trigger management implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
73 """ |
590
56531f9e9ac7
Fix pep8 support in src/tools.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
588
diff
changeset
|
74 if point_name not in self.__triggers: |
56531f9e9ac7
Fix pep8 support in src/tools.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
588
diff
changeset
|
75 self.__triggers[point_name] = [] |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
76 if priority != 0 and priority in [ |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
77 trigger_tuple[0] for trigger_tuple in self.__triggers[point_name] |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
78 ]: |
876
65bf1bc70f6b
tools, plugin XEP-0077: small fixes
souliane <souliane@mailoo.org>
parents:
811
diff
changeset
|
79 if priority in (self.MIN_PRIORITY, self.MAX_PRIORITY): |
3028 | 80 log.warning(_("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
|
81 else: |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
82 log.debug( |
3028 | 83 _("There is already a trigger with the same priority [%s]") |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
84 % point_name |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
85 ) |
516
7ee15fbe8c08
core: added priority management in triggers
Goffi <goffi@goffi.org>
parents:
501
diff
changeset
|
86 self.__triggers[point_name].append((priority, callback)) |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
87 self.__triggers[point_name].sort( |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
88 key=lambda trigger_tuple: trigger_tuple[0], reverse=True |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
89 ) |
250
ad3b820651fa
Tools: new misc library, Trigger management implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
90 |
4182
4dc00e848961
core (tools/trigger): new `add_with_check` method:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
91 def add_with_check( |
4dc00e848961
core (tools/trigger): new `add_with_check` method:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
92 self, |
4dc00e848961
core (tools/trigger): new `add_with_check` method:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
93 point_name: str, |
4dc00e848961
core (tools/trigger): new `add_with_check` method:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
94 plugin, |
4dc00e848961
core (tools/trigger): new `add_with_check` method:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
95 callback: Callable, |
4dc00e848961
core (tools/trigger): new `add_with_check` method:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
96 priority: int=0 |
4dc00e848961
core (tools/trigger): new `add_with_check` method:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
97 ) -> None: |
4dc00e848961
core (tools/trigger): new `add_with_check` method:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
98 """Like [Add], but check session before running the trigger |
4dc00e848961
core (tools/trigger): new `add_with_check` method:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
99 |
4dc00e848961
core (tools/trigger): new `add_with_check` method:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
100 This method is to be used for triggers which can run in components and are |
4dc00e848961
core (tools/trigger): new `add_with_check` method:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
101 expecting a ``client``: as all plugins are not run for component, a check will be |
4dc00e848961
core (tools/trigger): new `add_with_check` method:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
102 added before running the trigger, if the plugin is not valid for this component, |
4dc00e848961
core (tools/trigger): new `add_with_check` method:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
103 the trigger is ignored. ``client`` must be the first positional argument. |
4dc00e848961
core (tools/trigger): new `add_with_check` method:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
104 |
4dc00e848961
core (tools/trigger): new `add_with_check` method:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
105 @param point_name: name of the trigger point |
4dc00e848961
core (tools/trigger): new `add_with_check` method:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
106 @param plugin: instance of the plugin using this trigger. This is necessary to |
4dc00e848961
core (tools/trigger): new `add_with_check` method:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
107 check if the plugin is available in the session. |
4dc00e848961
core (tools/trigger): new `add_with_check` method:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
108 @param callback: method to call at the trigger point |
4dc00e848961
core (tools/trigger): new `add_with_check` method:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
109 @param priority: callback will be called in priority order, biggest first |
4dc00e848961
core (tools/trigger): new `add_with_check` method:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
110 """ |
4dc00e848961
core (tools/trigger): new `add_with_check` method:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
111 if inspect.iscoroutinefunction(callback): |
4dc00e848961
core (tools/trigger): new `add_with_check` method:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
112 async def async_wrapper(client: SatXMPPEntity, *args, **kwargs): |
4dc00e848961
core (tools/trigger): new `add_with_check` method:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
113 if client.is_component and plugin not in client.plugins: |
4dc00e848961
core (tools/trigger): new `add_with_check` method:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
114 log.debug(f"Ignoring {callback} as parent plugin is not available") |
4dc00e848961
core (tools/trigger): new `add_with_check` method:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
115 return True |
4dc00e848961
core (tools/trigger): new `add_with_check` method:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
116 else: |
4dc00e848961
core (tools/trigger): new `add_with_check` method:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
117 return await callback(client, *args, **kwargs) |
4dc00e848961
core (tools/trigger): new `add_with_check` method:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
118 self.add(point_name, async_wrapper, priority) |
4dc00e848961
core (tools/trigger): new `add_with_check` method:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
119 else: |
4dc00e848961
core (tools/trigger): new `add_with_check` method:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
120 def sync_wrapper(client: SatXMPPEntity, *args, **kwargs): |
4dc00e848961
core (tools/trigger): new `add_with_check` method:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
121 if client.is_component and plugin not in client.plugins: |
4dc00e848961
core (tools/trigger): new `add_with_check` method:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
122 log.debug(f"Ignoring {callback} as parent plugin is not available") |
4dc00e848961
core (tools/trigger): new `add_with_check` method:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
123 return True |
4dc00e848961
core (tools/trigger): new `add_with_check` method:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
124 else: |
4dc00e848961
core (tools/trigger): new `add_with_check` method:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
125 return callback(client, *args, **kwargs) |
4dc00e848961
core (tools/trigger): new `add_with_check` method:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
126 self.add(point_name, sync_wrapper, priority) |
4dc00e848961
core (tools/trigger): new `add_with_check` method:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
127 |
250
ad3b820651fa
Tools: new misc library, Trigger management implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
128 def remove(self, point_name, callback): |
ad3b820651fa
Tools: new misc library, Trigger management implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
129 """Remove a trigger from a point |
2499
af4a38ebf52a
core (trigger): new returnPoint method:
Goffi <goffi@goffi.org>
parents:
2483
diff
changeset
|
130 |
250
ad3b820651fa
Tools: new misc library, Trigger management implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
131 @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
|
132 @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
|
133 """ |
516
7ee15fbe8c08
core: added priority management in triggers
Goffi <goffi@goffi.org>
parents:
501
diff
changeset
|
134 for trigger_tuple in self.__triggers[point_name]: |
7ee15fbe8c08
core: added priority management in triggers
Goffi <goffi@goffi.org>
parents:
501
diff
changeset
|
135 if trigger_tuple[1] == callback: |
7ee15fbe8c08
core: added priority management in triggers
Goffi <goffi@goffi.org>
parents:
501
diff
changeset
|
136 self.__triggers[point_name].remove(trigger_tuple) |
7ee15fbe8c08
core: added priority management in triggers
Goffi <goffi@goffi.org>
parents:
501
diff
changeset
|
137 return |
7ee15fbe8c08
core: added priority management in triggers
Goffi <goffi@goffi.org>
parents:
501
diff
changeset
|
138 raise TriggerException("Trying to remove an unexisting trigger") |
250
ad3b820651fa
Tools: new misc library, Trigger management implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
139 |
ad3b820651fa
Tools: new misc library, Trigger management implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
140 def point(self, point_name, *args, **kwargs): |
ad3b820651fa
Tools: new misc library, Trigger management implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
141 """This put a trigger point |
2499
af4a38ebf52a
core (trigger): new returnPoint method:
Goffi <goffi@goffi.org>
parents:
2483
diff
changeset
|
142 |
af4a38ebf52a
core (trigger): new returnPoint method:
Goffi <goffi@goffi.org>
parents:
2483
diff
changeset
|
143 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
|
144 @param point_name: name of the trigger point |
2650
3a8e7ec4648a
tools (trigger, async_trigger): added no_cancel argument to point and asyncPoint when a trigger must not be cancellable
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
145 @param *args: args to transmit to trigger |
3a8e7ec4648a
tools (trigger, async_trigger): added no_cancel argument to point and asyncPoint when a trigger must not be cancellable
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
146 @param *kwargs: kwargs to transmit to trigger |
3a8e7ec4648a
tools (trigger, async_trigger): added no_cancel argument to point and asyncPoint when a trigger must not be cancellable
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
147 if "triggers_no_cancel" is present, it will be popup out |
3a8e7ec4648a
tools (trigger, async_trigger): added no_cancel argument to point and asyncPoint when a trigger must not be cancellable
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
148 when set to True, this argument don't let triggers stop |
3a8e7ec4648a
tools (trigger, async_trigger): added no_cancel argument to point and asyncPoint when a trigger must not be cancellable
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
149 the workflow |
2499
af4a38ebf52a
core (trigger): new returnPoint method:
Goffi <goffi@goffi.org>
parents:
2483
diff
changeset
|
150 @return: True if the action must be continued, False else |
af4a38ebf52a
core (trigger): new returnPoint method:
Goffi <goffi@goffi.org>
parents:
2483
diff
changeset
|
151 """ |
590
56531f9e9ac7
Fix pep8 support in src/tools.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
588
diff
changeset
|
152 if point_name not in self.__triggers: |
250
ad3b820651fa
Tools: new misc library, Trigger management implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
153 return True |
ad3b820651fa
Tools: new misc library, Trigger management implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
154 |
2650
3a8e7ec4648a
tools (trigger, async_trigger): added no_cancel argument to point and asyncPoint when a trigger must not be cancellable
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
155 can_cancel = not kwargs.pop('triggers_no_cancel', False) |
3a8e7ec4648a
tools (trigger, async_trigger): added no_cancel argument to point and asyncPoint when a trigger must not be cancellable
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
156 |
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
|
157 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
|
158 try: |
2650
3a8e7ec4648a
tools (trigger, async_trigger): added no_cancel argument to point and asyncPoint when a trigger must not be cancellable
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
159 if not trigger(*args, **kwargs) and can_cancel: |
525
5431136501ab
core: Triggers can now frobid other triggers execution
Goffi <goffi@goffi.org>
parents:
516
diff
changeset
|
160 return False |
5431136501ab
core: Triggers can now frobid other triggers execution
Goffi <goffi@goffi.org>
parents:
516
diff
changeset
|
161 except SkipOtherTriggers: |
5431136501ab
core: Triggers can now frobid other triggers execution
Goffi <goffi@goffi.org>
parents:
516
diff
changeset
|
162 break |
250
ad3b820651fa
Tools: new misc library, Trigger management implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
163 return True |
2499
af4a38ebf52a
core (trigger): new returnPoint method:
Goffi <goffi@goffi.org>
parents:
2483
diff
changeset
|
164 |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
165 def return_point(self, point_name, *args, **kwargs): |
2499
af4a38ebf52a
core (trigger): new returnPoint method:
Goffi <goffi@goffi.org>
parents:
2483
diff
changeset
|
166 """Like point but trigger must return (continue, return_value) |
af4a38ebf52a
core (trigger): new returnPoint method:
Goffi <goffi@goffi.org>
parents:
2483
diff
changeset
|
167 |
af4a38ebf52a
core (trigger): new returnPoint method:
Goffi <goffi@goffi.org>
parents:
2483
diff
changeset
|
168 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
|
169 - 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
|
170 - return_value: value to return ONLY IF CONTINUE IS FALSE |
af4a38ebf52a
core (trigger): new returnPoint method:
Goffi <goffi@goffi.org>
parents:
2483
diff
changeset
|
171 @param point_name: name of the trigger point |
af4a38ebf52a
core (trigger): new returnPoint method:
Goffi <goffi@goffi.org>
parents:
2483
diff
changeset
|
172 @return: True if the action must be continued, False else |
af4a38ebf52a
core (trigger): new returnPoint method:
Goffi <goffi@goffi.org>
parents:
2483
diff
changeset
|
173 """ |
af4a38ebf52a
core (trigger): new returnPoint method:
Goffi <goffi@goffi.org>
parents:
2483
diff
changeset
|
174 |
af4a38ebf52a
core (trigger): new returnPoint method:
Goffi <goffi@goffi.org>
parents:
2483
diff
changeset
|
175 if point_name not in self.__triggers: |
af4a38ebf52a
core (trigger): new returnPoint method:
Goffi <goffi@goffi.org>
parents:
2483
diff
changeset
|
176 return True |
af4a38ebf52a
core (trigger): new returnPoint method:
Goffi <goffi@goffi.org>
parents:
2483
diff
changeset
|
177 |
af4a38ebf52a
core (trigger): new returnPoint method:
Goffi <goffi@goffi.org>
parents:
2483
diff
changeset
|
178 for priority, trigger in self.__triggers[point_name]: |
af4a38ebf52a
core (trigger): new returnPoint method:
Goffi <goffi@goffi.org>
parents:
2483
diff
changeset
|
179 try: |
af4a38ebf52a
core (trigger): new returnPoint method:
Goffi <goffi@goffi.org>
parents:
2483
diff
changeset
|
180 cont, ret_value = trigger(*args, **kwargs) |
af4a38ebf52a
core (trigger): new returnPoint method:
Goffi <goffi@goffi.org>
parents:
2483
diff
changeset
|
181 if not cont: |
af4a38ebf52a
core (trigger): new returnPoint method:
Goffi <goffi@goffi.org>
parents:
2483
diff
changeset
|
182 return False, ret_value |
af4a38ebf52a
core (trigger): new returnPoint method:
Goffi <goffi@goffi.org>
parents:
2483
diff
changeset
|
183 except SkipOtherTriggers: |
af4a38ebf52a
core (trigger): new returnPoint method:
Goffi <goffi@goffi.org>
parents:
2483
diff
changeset
|
184 break |
af4a38ebf52a
core (trigger): new returnPoint method:
Goffi <goffi@goffi.org>
parents:
2483
diff
changeset
|
185 return True, None |