diff src/plugins/plugin_xep_0060.py @ 1760:30efe084471c

plugin XEP-0060: None is no more used in addManagedNode, but all nodes prefixed with a registered one are now triggered
author Goffi <goffi@goffi.org>
date Fri, 18 Dec 2015 15:06:58 +0100
parents c2d96f74105e
children d17772b0fe22
line wrap: on
line diff
--- a/src/plugins/plugin_xep_0060.py	Thu Dec 17 22:38:00 2015 +0100
+++ b/src/plugins/plugin_xep_0060.py	Fri Dec 18 15:06:58 2015 +0100
@@ -142,12 +142,14 @@
     def addManagedNode(self, node, **kwargs):
         """Add a handler for a node
 
-        @param node(unicode): node to monitor, or None to monitor all
+        @param node(unicode): node to monitor
+            all node *prefixed* with this one will be triggered
         @param **kwargs: method(s) to call when the node is found
             the methode must be named after PubSub constants in lower case
             and suffixed with "_cb"
             e.g.: "publish_cb" for C.PS_PUBLISH, "delete_cb" for C.PS_DELETE
         """
+        assert node is not None
         assert kwargs
         callbacks = self._node_cb.setdefault(node, {})
         for event, cb in kwargs.iteritems():
@@ -247,7 +249,7 @@
         def subscribeEb(failure, service, node):
             failure.trap(error.StanzaError)
             log.warning("Could not subscribe to node {} on service {}: {}".format(node, unicode(service), unicode(failure.value)))
-            
+
         def doSubscribe(items):
             self.subscribe(service, node, profile_key=profile_key).addErrback(subscribeEb, service, node)
             return items
@@ -495,27 +497,32 @@
     def connectionInitialized(self):
         rsm.PubSubClient.connectionInitialized(self)
 
+    def _getNodeCallbacks(self, node, event):
+        """Generate callbacks from given node and event
+
+        @param node(unicode): node used for the item
+            any registered node which prefix the node will match
+        @param event(unicode): one of C.PS_ITEMS, C.PS_RETRACT, C.PS_DELETE
+        @return (iterator[callable]): callbacks for this node/event
+        """
+        for registered_node, callbacks_dict in self.parent_plugin._node_cb.iteritems():
+            if not node.startswith(registered_node):
+                continue
+            try:
+                for callback in callbacks_dict[event]:
+                    yield callback
+            except KeyError:
+                continue
+
     def itemsReceived(self, event):
         log.debug(u"Pubsub items received")
-        for node in (event.nodeIdentifier, None):
-            try:
-                callbacks = self.parent_plugin._node_cb[node][C.PS_ITEMS]
-            except KeyError:
-                pass
-            else:
-                for callback in callbacks:
-                    callback(event, self.parent.profile)
+        for callback in self._getNodeCallbacks(event.nodeIdentifier, C.PS_ITEMS):
+            callback(event, self.parent.profile)
 
     def deleteReceived(self, event):
         log.debug((u"Publish node deleted"))
-        for node in (event.nodeIdentifier, None):
-            try:
-                callbacks = self.parent_plugin._node_cb[node][C.PS_DELETE]
-            except KeyError:
-                pass
-            else:
-                for callback in callbacks:
-                    callback(event, self.parent.profile)
+        for callback in self._getNodeCallbacks(event.nodeIdentifier, C.PS_DELETE):
+            callback(event, self.parent.profile)
 
     def subscriptions(self, service, nodeIdentifier, sender=None):
         """Return the list of subscriptions to the given service and node.