comparison libervia/backend/tools/trigger.py @ 4270:0d7bb4df2343

Reformatted code base using black.
author Goffi <goffi@goffi.org>
date Wed, 19 Jun 2024 18:44:57 +0200
parents 4dc00e848961
children
comparison
equal deleted inserted replaced
4269:64a85ce8be70 4270:0d7bb4df2343
31 class TriggerException(Exception): 31 class TriggerException(Exception):
32 pass 32 pass
33 33
34 34
35 class SkipOtherTriggers(Exception): 35 class SkipOtherTriggers(Exception):
36 """ Exception to raise if normal behaviour must be followed instead of following triggers list """ 36 """Exception to raise if normal behaviour must be followed instead of following triggers list"""
37 37
38 pass 38 pass
39 39
40 40
41 class TriggerManager(object): 41 class TriggerManager(object):
60 client = args[0] 60 client = args[0]
61 if not client.is_component: 61 if not client.is_component:
62 # plugins are always avaialble for normal clients 62 # plugins are always avaialble for normal clients
63 return True 63 return True
64 64
65
66 def add(self, point_name, callback: Callable, priority=0): 65 def add(self, point_name, callback: Callable, priority=0):
67 """Add a trigger to a point 66 """Add a trigger to a point
68 67
69 @param point_name: name of the point when the trigger should be run 68 @param point_name: name of the point when the trigger should be run
70 @param callback: method to call at the trigger point 69 @param callback: method to call at the trigger point
87 self.__triggers[point_name].sort( 86 self.__triggers[point_name].sort(
88 key=lambda trigger_tuple: trigger_tuple[0], reverse=True 87 key=lambda trigger_tuple: trigger_tuple[0], reverse=True
89 ) 88 )
90 89
91 def add_with_check( 90 def add_with_check(
92 self, 91 self, point_name: str, plugin, callback: Callable, priority: int = 0
93 point_name: str,
94 plugin,
95 callback: Callable,
96 priority: int=0
97 ) -> None: 92 ) -> None:
98 """Like [Add], but check session before running the trigger 93 """Like [Add], but check session before running the trigger
99 94
100 This method is to be used for triggers which can run in components and are 95 This method is to be used for triggers which can run in components and are
101 expecting a ``client``: as all plugins are not run for component, a check will be 96 expecting a ``client``: as all plugins are not run for component, a check will be
107 check if the plugin is available in the session. 102 check if the plugin is available in the session.
108 @param callback: method to call at the trigger point 103 @param callback: method to call at the trigger point
109 @param priority: callback will be called in priority order, biggest first 104 @param priority: callback will be called in priority order, biggest first
110 """ 105 """
111 if inspect.iscoroutinefunction(callback): 106 if inspect.iscoroutinefunction(callback):
107
112 async def async_wrapper(client: SatXMPPEntity, *args, **kwargs): 108 async def async_wrapper(client: SatXMPPEntity, *args, **kwargs):
113 if client.is_component and plugin not in client.plugins: 109 if client.is_component and plugin not in client.plugins:
114 log.debug(f"Ignoring {callback} as parent plugin is not available") 110 log.debug(f"Ignoring {callback} as parent plugin is not available")
115 return True 111 return True
116 else: 112 else:
117 return await callback(client, *args, **kwargs) 113 return await callback(client, *args, **kwargs)
114
118 self.add(point_name, async_wrapper, priority) 115 self.add(point_name, async_wrapper, priority)
119 else: 116 else:
117
120 def sync_wrapper(client: SatXMPPEntity, *args, **kwargs): 118 def sync_wrapper(client: SatXMPPEntity, *args, **kwargs):
121 if client.is_component and plugin not in client.plugins: 119 if client.is_component and plugin not in client.plugins:
122 log.debug(f"Ignoring {callback} as parent plugin is not available") 120 log.debug(f"Ignoring {callback} as parent plugin is not available")
123 return True 121 return True
124 else: 122 else:
125 return callback(client, *args, **kwargs) 123 return callback(client, *args, **kwargs)
124
126 self.add(point_name, sync_wrapper, priority) 125 self.add(point_name, sync_wrapper, priority)
127 126
128 def remove(self, point_name, callback): 127 def remove(self, point_name, callback):
129 """Remove a trigger from a point 128 """Remove a trigger from a point
130 129
150 @return: True if the action must be continued, False else 149 @return: True if the action must be continued, False else
151 """ 150 """
152 if point_name not in self.__triggers: 151 if point_name not in self.__triggers:
153 return True 152 return True
154 153
155 can_cancel = not kwargs.pop('triggers_no_cancel', False) 154 can_cancel = not kwargs.pop("triggers_no_cancel", False)
156 155
157 for priority, trigger in self.__triggers[point_name]: 156 for priority, trigger in self.__triggers[point_name]:
158 try: 157 try:
159 if not trigger(*args, **kwargs) and can_cancel: 158 if not trigger(*args, **kwargs) and can_cancel:
160 return False 159 return False