Mercurial > libervia-backend
diff sat/plugins/plugin_exp_pubsub_hook.py @ 2624:56f94936df1e
code style reformatting using black
author | Goffi <goffi@goffi.org> |
---|---|
date | Wed, 27 Jun 2018 20:14:46 +0200 |
parents | 26edcf3a30eb |
children | 003b8b4b56a7 |
line wrap: on
line diff
--- a/sat/plugins/plugin_exp_pubsub_hook.py Wed Jun 27 07:51:29 2018 +0200 +++ b/sat/plugins/plugin_exp_pubsub_hook.py Wed Jun 27 20:14:46 2018 +0200 @@ -24,9 +24,10 @@ from sat.memory import persistent from twisted.words.protocols.jabber import jid from twisted.internet import defer + log = getLogger(__name__) -NS_PUBSUB_HOOK = 'PUBSUB_HOOK' +NS_PUBSUB_HOOK = "PUBSUB_HOOK" PLUGIN_INFO = { C.PI_NAME: "PubSub Hook", @@ -36,40 +37,48 @@ C.PI_DEPENDENCIES: ["XEP-0060"], C.PI_MAIN: "PubsubHook", C.PI_HANDLER: "no", - C.PI_DESCRIPTION: _("""Experimental plugin to launch on action on Pubsub notifications""") + C.PI_DESCRIPTION: _( + """Experimental plugin to launch on action on Pubsub notifications""" + ), } -# python module -HOOK_TYPE_PYTHON = u'python' +# python module +HOOK_TYPE_PYTHON = u"python" # python file path -HOOK_TYPE_PYTHON_FILE = u'python_file' +HOOK_TYPE_PYTHON_FILE = u"python_file" # python code directly -HOOK_TYPE_PYTHON_CODE = u'python_code' +HOOK_TYPE_PYTHON_CODE = u"python_code" HOOK_TYPES = (HOOK_TYPE_PYTHON, HOOK_TYPE_PYTHON_FILE, HOOK_TYPE_PYTHON_CODE) class PubsubHook(object): - def __init__(self, host): log.info(_(u"PubSub Hook initialization")) self.host = host self.node_hooks = {} # keep track of the number of hooks per node (for all profiles) - host.bridge.addMethod("psHookAdd", ".plugin", - in_sign='ssssbs', out_sign='', - method=self._addHook - ) - host.bridge.addMethod("psHookRemove", ".plugin", - in_sign='sssss', out_sign='i', - method=self._removeHook - ) - host.bridge.addMethod("psHookList", ".plugin", - in_sign='s', out_sign='aa{ss}', - method=self._listHooks - ) + host.bridge.addMethod( + "psHookAdd", ".plugin", in_sign="ssssbs", out_sign="", method=self._addHook + ) + host.bridge.addMethod( + "psHookRemove", + ".plugin", + in_sign="sssss", + out_sign="i", + method=self._removeHook, + ) + host.bridge.addMethod( + "psHookList", + ".plugin", + in_sign="s", + out_sign="aa{ss}", + method=self._listHooks, + ) @defer.inlineCallbacks def profileConnected(self, client): - hooks = client._hooks = persistent.PersistentBinaryDict(NS_PUBSUB_HOOK, client.profile) + hooks = client._hooks = persistent.PersistentBinaryDict( + NS_PUBSUB_HOOK, client.profile + ) client._hooks_temporary = {} yield hooks.load() for node in hooks: @@ -85,10 +94,11 @@ self.node_hooks[node] += 1 else: # first hook on this node - self.host.plugins['XEP-0060'].addManagedNode(node, items_cb=self._itemsReceived) + self.host.plugins["XEP-0060"].addManagedNode( + node, items_cb=self._itemsReceived + ) self.node_hooks[node] = 0 - log.info(_(u"node manager installed on {node}").format( - node = node)) + log.info(_(u"node manager installed on {node}").format(node=node)) def _removeNodeManager(self, client, node): try: @@ -98,34 +108,40 @@ else: if self.node_hooks[node] == 0: del self.node_hooks[node] - self.host.plugins['XEP-0060'].removeManagedNode(node, self._itemsReceived) + self.host.plugins["XEP-0060"].removeManagedNode(node, self._itemsReceived) log.debug(_(u"hook removed")) else: log.debug(_(u"node still needed for an other hook")) def installHook(self, client, service, node, hook_type, hook_arg, persistent): if hook_type not in HOOK_TYPES: - raise exceptions.DataError(_(u'{hook_type} is not handled').format(hook_type=hook_type)) + raise exceptions.DataError( + _(u"{hook_type} is not handled").format(hook_type=hook_type) + ) if hook_type != HOOK_TYPE_PYTHON_FILE: - raise NotImplementedError(_(u'{hook_type} hook type not implemented yet').format(hook_type=hook_type)) + raise NotImplementedError( + _(u"{hook_type} hook type not implemented yet").format( + hook_type=hook_type + ) + ) self._installNodeManager(client, node) - hook_data = {'service': service, - 'type': hook_type, - 'arg': hook_arg - } + hook_data = {"service": service, "type": hook_type, "arg": hook_arg} if persistent: - hooks_list = client._hooks.setdefault(node,[]) + hooks_list = client._hooks.setdefault(node, []) hooks_list.append(hook_data) client._hooks.force(node) else: - hooks_list = client._hooks_temporary.setdefault(node,[]) + hooks_list = client._hooks_temporary.setdefault(node, []) hooks_list.append(hook_data) - log.info(_(u"{persistent} hook installed on {node} for {profile}").format( - persistent = _(u'persistent') if persistent else _(u'temporary'), - node = node, - profile = client.profile)) + log.info( + _(u"{persistent} hook installed on {node} for {profile}").format( + persistent=_(u"persistent") if persistent else _(u"temporary"), + node=node, + profile=client.profile, + ) + ) def _itemsReceived(self, client, itemsEvent): node = itemsEvent.nodeIdentifier @@ -134,24 +150,30 @@ continue hooks_list = hooks[node] for hook_data in hooks_list[:]: - if hook_data['service'] != itemsEvent.sender.userhostJID(): + if hook_data["service"] != itemsEvent.sender.userhostJID(): continue try: - callback = hook_data['callback'] + callback = hook_data["callback"] except KeyError: # first time we get this hook, we create the callback - hook_type = hook_data['type'] + hook_type = hook_data["type"] try: if hook_type == HOOK_TYPE_PYTHON_FILE: hook_globals = {} - execfile(hook_data['arg'], hook_globals) - callback = hook_globals['hook'] + execfile(hook_data["arg"], hook_globals) + callback = hook_globals["hook"] else: - raise NotImplementedError(_(u'{hook_type} hook type not implemented yet').format( - hook_type=hook_type)) + raise NotImplementedError( + _(u"{hook_type} hook type not implemented yet").format( + hook_type=hook_type + ) + ) except Exception as e: - log.warning(_(u"Can't load Pubsub hook at node {node}, it will be removed: {reason}").format( - node=node, reason=e)) + log.warning( + _( + u"Can't load Pubsub hook at node {node}, it will be removed: {reason}" + ).format(node=node, reason=e) + ) hooks_list.remove(hook_data) continue @@ -159,14 +181,23 @@ try: callback(self.host, client, item) except Exception as e: - log.warning(_(u"Error while running Pubsub hook for node {node}: {msg}").format( - node = node, - msg = e)) + log.warning( + _( + u"Error while running Pubsub hook for node {node}: {msg}" + ).format(node=node, msg=e) + ) def _addHook(self, service, node, hook_type, hook_arg, persistent, profile): client = self.host.getClient(profile) service = jid.JID(service) if service else client.jid.userhostJID() - return self.addHook(client, service, unicode(node), unicode(hook_type), unicode(hook_arg), persistent) + return self.addHook( + client, + service, + unicode(node), + unicode(hook_type), + unicode(hook_arg), + persistent, + ) def addHook(self, client, service, node, hook_type, hook_arg, persistent): r"""Add a hook which will be triggered on a pubsub notification @@ -210,14 +241,18 @@ for hooks in (client._hooks, client._hooks_temporary): if node in hooks: for hook_data in hooks[node]: - if (service != hook_data[u'service'] - or hook_type is not None and hook_type != hook_data[u'type'] - or hook_arg is not None and hook_arg != hook_data[u'arg']): + if ( + service != hook_data[u"service"] + or hook_type is not None + and hook_type != hook_data[u"type"] + or hook_arg is not None + and hook_arg != hook_data[u"arg"] + ): continue hooks[node].remove(hook_data) removed += 1 if not hooks[node]: - # no more hooks, we can remove the node + # no more hooks, we can remove the node del hooks[node] self._removeNodeManager(client, node) else: @@ -228,8 +263,8 @@ def _listHooks(self, profile): hooks_list = self.listHooks(self.host.getClient(profile)) for hook in hooks_list: - hook[u'service'] = hook[u'service'].full() - hook[u'persistent'] = C.boolConst(hook[u'persistent']) + hook[u"service"] = hook[u"service"].full() + hook[u"persistent"] = C.boolConst(hook[u"persistent"]) return hooks_list def listHooks(self, client): @@ -239,11 +274,13 @@ persistent = hooks is client._hooks for node, hooks_data in hooks.iteritems(): for hook_data in hooks_data: - hooks_list.append({u'service': hook_data[u'service'], - u'node': node, - u'type': hook_data[u'type'], - u'arg': hook_data[u'arg'], - u'persistent': persistent - }) + hooks_list.append( + { + u"service": hook_data[u"service"], + u"node": node, + u"type": hook_data[u"type"], + u"arg": hook_data[u"arg"], + u"persistent": persistent, + } + ) return hooks_list -