Mercurial > libervia-backend
comparison libervia/backend/plugins/plugin_exp_pubsub_hook.py @ 4270:0d7bb4df2343
Reformatted code base using black.
author | Goffi <goffi@goffi.org> |
---|---|
date | Wed, 19 Jun 2024 18:44:57 +0200 |
parents | 4b842c1fb686 |
children |
comparison
equal
deleted
inserted
replaced
4269:64a85ce8be70 | 4270:0d7bb4df2343 |
---|---|
53 | 53 |
54 class PubsubHook(object): | 54 class PubsubHook(object): |
55 def __init__(self, host): | 55 def __init__(self, host): |
56 log.info(_("PubSub Hook initialization")) | 56 log.info(_("PubSub Hook initialization")) |
57 self.host = host | 57 self.host = host |
58 self.node_hooks = {} # keep track of the number of hooks per node (for all profiles) | 58 self.node_hooks = ( |
59 {} | |
60 ) # keep track of the number of hooks per node (for all profiles) | |
59 host.bridge.add_method( | 61 host.bridge.add_method( |
60 "ps_hook_add", ".plugin", in_sign="ssssbs", out_sign="", method=self._addHook | 62 "ps_hook_add", ".plugin", in_sign="ssssbs", out_sign="", method=self._addHook |
61 ) | 63 ) |
62 host.bridge.add_method( | 64 host.bridge.add_method( |
63 "ps_hook_remove", | 65 "ps_hook_remove", |
106 except KeyError: | 108 except KeyError: |
107 log.error(_("trying to remove a {node} without hook").format(node=node)) | 109 log.error(_("trying to remove a {node} without hook").format(node=node)) |
108 else: | 110 else: |
109 if self.node_hooks[node] == 0: | 111 if self.node_hooks[node] == 0: |
110 del self.node_hooks[node] | 112 del self.node_hooks[node] |
111 self.host.plugins["XEP-0060"].remove_managed_node(node, self._items_received) | 113 self.host.plugins["XEP-0060"].remove_managed_node( |
114 node, self._items_received | |
115 ) | |
112 log.debug(_("hook removed")) | 116 log.debug(_("hook removed")) |
113 else: | 117 else: |
114 log.debug(_("node still needed for an other hook")) | 118 log.debug(_("node still needed for an other hook")) |
115 | 119 |
116 def install_hook(self, client, service, node, hook_type, hook_arg, persistent): | 120 def install_hook(self, client, service, node, hook_type, hook_arg, persistent): |
118 raise exceptions.DataError( | 122 raise exceptions.DataError( |
119 _("{hook_type} is not handled").format(hook_type=hook_type) | 123 _("{hook_type} is not handled").format(hook_type=hook_type) |
120 ) | 124 ) |
121 if hook_type != HOOK_TYPE_PYTHON_FILE: | 125 if hook_type != HOOK_TYPE_PYTHON_FILE: |
122 raise NotImplementedError( | 126 raise NotImplementedError( |
123 _("{hook_type} hook type not implemented yet").format( | 127 _("{hook_type} hook type not implemented yet").format(hook_type=hook_type) |
124 hook_type=hook_type | |
125 ) | |
126 ) | 128 ) |
127 self._install_node_manager(client, node) | 129 self._install_node_manager(client, node) |
128 hook_data = {"service": service, "type": hook_type, "arg": hook_arg} | 130 hook_data = {"service": service, "type": hook_type, "arg": hook_arg} |
129 | 131 |
130 if persistent: | 132 if persistent: |
158 # first time we get this hook, we create the callback | 160 # first time we get this hook, we create the callback |
159 hook_type = hook_data["type"] | 161 hook_type = hook_data["type"] |
160 try: | 162 try: |
161 if hook_type == HOOK_TYPE_PYTHON_FILE: | 163 if hook_type == HOOK_TYPE_PYTHON_FILE: |
162 hook_globals = {} | 164 hook_globals = {} |
163 exec(compile(open(hook_data["arg"], "rb").read(), hook_data["arg"], 'exec'), hook_globals) | 165 exec( |
166 compile( | |
167 open(hook_data["arg"], "rb").read(), | |
168 hook_data["arg"], | |
169 "exec", | |
170 ), | |
171 hook_globals, | |
172 ) | |
164 callback = hook_globals["hook"] | 173 callback = hook_globals["hook"] |
165 else: | 174 else: |
166 raise NotImplementedError( | 175 raise NotImplementedError( |
167 _("{hook_type} hook type not implemented yet").format( | 176 _("{hook_type} hook type not implemented yet").format( |
168 hook_type=hook_type | 177 hook_type=hook_type |
222 return self.install_hook(client, service, node, hook_type, hook_arg, persistent) | 231 return self.install_hook(client, service, node, hook_type, hook_arg, persistent) |
223 | 232 |
224 def _removeHook(self, service, node, hook_type, hook_arg, profile): | 233 def _removeHook(self, service, node, hook_type, hook_arg, profile): |
225 client = self.host.get_client(profile) | 234 client = self.host.get_client(profile) |
226 service = jid.JID(service) if service else client.jid.userhostJID() | 235 service = jid.JID(service) if service else client.jid.userhostJID() |
227 return self.remove_hook(client, service, node, hook_type or None, hook_arg or None) | 236 return self.remove_hook( |
237 client, service, node, hook_type or None, hook_arg or None | |
238 ) | |
228 | 239 |
229 def remove_hook(self, client, service, node, hook_type=None, hook_arg=None): | 240 def remove_hook(self, client, service, node, hook_type=None, hook_arg=None): |
230 """Remove a persistent or temporaty root | 241 """Remove a persistent or temporaty root |
231 | 242 |
232 @param service(jid.JID): service of the node | 243 @param service(jid.JID): service of the node |