comparison 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
comparison
equal deleted inserted replaced
1759:81923b3f8b14 1760:30efe084471c
140 return Extra(rsm_request, extra) 140 return Extra(rsm_request, extra)
141 141
142 def addManagedNode(self, node, **kwargs): 142 def addManagedNode(self, node, **kwargs):
143 """Add a handler for a node 143 """Add a handler for a node
144 144
145 @param node(unicode): node to monitor, or None to monitor all 145 @param node(unicode): node to monitor
146 all node *prefixed* with this one will be triggered
146 @param **kwargs: method(s) to call when the node is found 147 @param **kwargs: method(s) to call when the node is found
147 the methode must be named after PubSub constants in lower case 148 the methode must be named after PubSub constants in lower case
148 and suffixed with "_cb" 149 and suffixed with "_cb"
149 e.g.: "publish_cb" for C.PS_PUBLISH, "delete_cb" for C.PS_DELETE 150 e.g.: "publish_cb" for C.PS_PUBLISH, "delete_cb" for C.PS_DELETE
150 """ 151 """
152 assert node is not None
151 assert kwargs 153 assert kwargs
152 callbacks = self._node_cb.setdefault(node, {}) 154 callbacks = self._node_cb.setdefault(node, {})
153 for event, cb in kwargs.iteritems(): 155 for event, cb in kwargs.iteritems():
154 event_name = event[:-3] 156 event_name = event[:-3]
155 assert event_name in C.PS_EVENTS 157 assert event_name in C.PS_EVENTS
245 subscribe = False 247 subscribe = False
246 248
247 def subscribeEb(failure, service, node): 249 def subscribeEb(failure, service, node):
248 failure.trap(error.StanzaError) 250 failure.trap(error.StanzaError)
249 log.warning("Could not subscribe to node {} on service {}: {}".format(node, unicode(service), unicode(failure.value))) 251 log.warning("Could not subscribe to node {} on service {}: {}".format(node, unicode(service), unicode(failure.value)))
250 252
251 def doSubscribe(items): 253 def doSubscribe(items):
252 self.subscribe(service, node, profile_key=profile_key).addErrback(subscribeEb, service, node) 254 self.subscribe(service, node, profile_key=profile_key).addErrback(subscribeEb, service, node)
253 return items 255 return items
254 256
255 if subscribe: 257 if subscribe:
493 rsm.PubSubClient.__init__(self) 495 rsm.PubSubClient.__init__(self)
494 496
495 def connectionInitialized(self): 497 def connectionInitialized(self):
496 rsm.PubSubClient.connectionInitialized(self) 498 rsm.PubSubClient.connectionInitialized(self)
497 499
500 def _getNodeCallbacks(self, node, event):
501 """Generate callbacks from given node and event
502
503 @param node(unicode): node used for the item
504 any registered node which prefix the node will match
505 @param event(unicode): one of C.PS_ITEMS, C.PS_RETRACT, C.PS_DELETE
506 @return (iterator[callable]): callbacks for this node/event
507 """
508 for registered_node, callbacks_dict in self.parent_plugin._node_cb.iteritems():
509 if not node.startswith(registered_node):
510 continue
511 try:
512 for callback in callbacks_dict[event]:
513 yield callback
514 except KeyError:
515 continue
516
498 def itemsReceived(self, event): 517 def itemsReceived(self, event):
499 log.debug(u"Pubsub items received") 518 log.debug(u"Pubsub items received")
500 for node in (event.nodeIdentifier, None): 519 for callback in self._getNodeCallbacks(event.nodeIdentifier, C.PS_ITEMS):
501 try: 520 callback(event, self.parent.profile)
502 callbacks = self.parent_plugin._node_cb[node][C.PS_ITEMS]
503 except KeyError:
504 pass
505 else:
506 for callback in callbacks:
507 callback(event, self.parent.profile)
508 521
509 def deleteReceived(self, event): 522 def deleteReceived(self, event):
510 log.debug((u"Publish node deleted")) 523 log.debug((u"Publish node deleted"))
511 for node in (event.nodeIdentifier, None): 524 for callback in self._getNodeCallbacks(event.nodeIdentifier, C.PS_DELETE):
512 try: 525 callback(event, self.parent.profile)
513 callbacks = self.parent_plugin._node_cb[node][C.PS_DELETE]
514 except KeyError:
515 pass
516 else:
517 for callback in callbacks:
518 callback(event, self.parent.profile)
519 526
520 def subscriptions(self, service, nodeIdentifier, sender=None): 527 def subscriptions(self, service, nodeIdentifier, sender=None):
521 """Return the list of subscriptions to the given service and node. 528 """Return the list of subscriptions to the given service and node.
522 529
523 @param service: The publish subscribe service to retrieve the subscriptions from. 530 @param service: The publish subscribe service to retrieve the subscriptions from.