Changeset 13:b460ae320c23 for wokkel
- Timestamp:
- Feb 20, 2008, 3:44:44 PM (14 years ago)
- Branch:
- default
- Convert:
- svn:b33ecbfc-034c-dc11-8662-000475d9059e/trunk@41
- Location:
- wokkel
- Files:
-
- 3 edited
Legend:
- Unmodified
- Added
- Removed
-
wokkel/iwokkel.py
r10 r13 138 138 def itemsReceived(recipient, service, nodeIdentifier, items): 139 139 """ 140 Called when items have been received from a node. 140 Called when an items notification has been received for a node. 141 142 An item can be an element named C{item} or C{retract}. Respectively, 143 they signal an item being published or retracted, optionally 144 accompanied with an item identifier in the C{id} attribute. 141 145 142 146 @param recipient: The entity to which the notification was sent. … … 148 152 @param items: List of received items as domish elements. 149 153 @type items: C{list} of L{domish.Element} 154 """ 155 156 def deleteReceived(recipient, service, nodeIdentifier, items): 157 """ 158 Called when a deletion notification has been received for a node. 159 160 @param recipient: The entity to which the notification was sent. 161 @type recipient: L{jid.JID} 162 @param service: The entity from which the notification was received. 163 @type service: L{jid.JID} 164 @param nodeIdentifier: Identifier of the node that has been deleted. 165 @type nodeIdentifier: C{unicode} 166 """ 167 168 def purgeReceived(recipient, service, nodeIdentifier, items): 169 """ 170 Called when a purge notification has been received for a node. 171 172 Upon receiving this notification all items associated should be 173 considered retracted. 174 175 @param recipient: The entity to which the notification was sent. 176 @type recipient: L{jid.JID} 177 @param service: The entity from which the notification was received. 178 @type service: L{jid.JID} 179 @param nodeIdentifier: Identifier of the node that has been purged. 180 @type nodeIdentifier: C{unicode} 150 181 """ 151 182 -
wokkel/pubsub.py
r11 r13 13 13 from zope.interface import implements 14 14 15 from twisted.internet import defer 15 from twisted.internet import defer, reactor 16 16 from twisted.words.protocols.jabber import jid, error, xmlstream 17 17 from twisted.words.xish import domish … … 240 240 241 241 def connectionInitialized(self): 242 self.xmlstream.addObserver('/message/event[@xmlns="%s"] /items' %243 NS_PUBSUB_EVENT, self._on Items)244 245 def _on Items(self, message):242 self.xmlstream.addObserver('/message/event[@xmlns="%s"]' % 243 NS_PUBSUB_EVENT, self._onEvent) 244 245 def _onEvent(self, message): 246 246 try: 247 247 service = jid.JID(message["from"]) 248 248 recipient = jid.JID(message["to"]) 249 nodeIdentifier = message.event.items["node"]250 249 except KeyError: 251 250 return 252 251 253 items = [element for element in message.event.items.elements() 254 if element.name == 'item'] 252 for element in message.event.elements(): 253 if element.uri == NS_PUBSUB_EVENT: 254 actionElement = element 255 256 if not actionElement: 257 return 258 259 eventHandler = getattr(self, "_onEvent_%s" % actionElement.name, None) 260 261 if eventHandler: 262 eventHandler(service, recipient, actionElement) 263 message.handled = True 264 265 def _onEvent_items(self, service, recipient, action): 266 nodeIdentifier = action["node"] 267 268 items = [element for element in action.elements() 269 if element.name in ('item', 'retract')] 255 270 256 271 self.itemsReceived(recipient, service, nodeIdentifier, items) 257 272 273 def _onEvent_delete(self, service, recipient, action): 274 nodeIdentifier = action["node"] 275 self.deleteReceived(recipient, service, nodeIdentifier) 276 277 def _onEvent_purge(self, service, recipient, action): 278 nodeIdentifier = action["node"] 279 self.purgeReceived(recipient, service, nodeIdentifier) 280 258 281 def itemsReceived(self, recipient, service, nodeIdentifier, items): 282 pass 283 284 def deleteReceived(self, recipient, service, nodeIdentifier): 285 pass 286 287 def purgeReceived(self, recipient, service, nodeIdentifier): 259 288 pass 260 289 -
wokkel/test/test_pubsub.py
r10 r13 7 7 8 8 from twisted.trial import unittest 9 from twisted.python import failure 10 from twisted.internet import defer, reactor 9 11 from twisted.words.xish import domish 10 12 from twisted.words.protocols.jabber import error … … 21 23 NS_PUBSUB = 'http://jabber.org/protocol/pubsub' 22 24 NS_PUBSUB_ERRORS = 'http://jabber.org/protocol/pubsub#errors' 25 NS_PUBSUB_EVENT = 'http://jabber.org/protocol/pubsub#event' 26 27 def calledAsync(fn): 28 """ 29 Function wrapper that fires a deferred upon calling the given function. 30 """ 31 d = defer.Deferred() 32 33 def func(*args, **kwargs): 34 try: 35 result = fn(*args, **kwargs) 36 except: 37 d.errback() 38 else: 39 d.callback(result) 40 41 return d, func 42 23 43 24 44 class PubSubClientTest(unittest.TestCase): 45 timeout = 2 25 46 26 47 def setUp(self): … … 28 49 self.protocol = pubsub.PubSubClient() 29 50 self.protocol.xmlstream = self.stub.xmlstream 51 self.protocol.connectionInitialized() 30 52 31 53 def test_unsubscribe(self): … … 51 73 return d 52 74 75 def test_event_items(self): 76 """ 77 Test receiving an items event resulting in a call to itemsReceived. 78 """ 79 message = domish.Element((None, 'message')) 80 message['from'] = 'pubsub.example.org' 81 message['to'] = 'user@example.org/home' 82 event = message.addElement((NS_PUBSUB_EVENT, 'event')) 83 items = event.addElement('items') 84 items['node'] = 'test' 85 item1 = items.addElement('item') 86 item1['id'] = 'item1' 87 item2 = items.addElement('retract') 88 item2['id'] = 'item2' 89 item3 = items.addElement('item') 90 item3['id'] = 'item3' 91 92 def itemsReceived(recipient, service, nodeIdentifier, items): 93 self.assertEquals(JID('user@example.org/home'), recipient) 94 self.assertEquals(JID('pubsub.example.org'), service) 95 self.assertEquals('test', nodeIdentifier) 96 self.assertEquals([item1, item2, item3], items) 97 98 d, self.protocol.itemsReceived = calledAsync(itemsReceived) 99 self.stub.send(message) 100 return d 101 102 def test_event_delete(self): 103 """ 104 Test receiving a delete event resulting in a call to deleteReceived. 105 """ 106 message = domish.Element((None, 'message')) 107 message['from'] = 'pubsub.example.org' 108 message['to'] = 'user@example.org/home' 109 event = message.addElement((NS_PUBSUB_EVENT, 'event')) 110 items = event.addElement('delete') 111 items['node'] = 'test' 112 113 def deleteReceived(recipient, service, nodeIdentifier): 114 self.assertEquals(JID('user@example.org/home'), recipient) 115 self.assertEquals(JID('pubsub.example.org'), service) 116 self.assertEquals('test', nodeIdentifier) 117 118 d, self.protocol.deleteReceived = calledAsync(deleteReceived) 119 self.stub.send(message) 120 return d 121 122 def test_event_purge(self): 123 """ 124 Test receiving a purge event resulting in a call to purgeReceived. 125 """ 126 message = domish.Element((None, 'message')) 127 message['from'] = 'pubsub.example.org' 128 message['to'] = 'user@example.org/home' 129 event = message.addElement((NS_PUBSUB_EVENT, 'event')) 130 items = event.addElement('purge') 131 items['node'] = 'test' 132 133 def purgeReceived(recipient, service, nodeIdentifier): 134 self.assertEquals(JID('user@example.org/home'), recipient) 135 self.assertEquals(JID('pubsub.example.org'), service) 136 self.assertEquals('test', nodeIdentifier) 137 138 d, self.protocol.purgeReceived = calledAsync(purgeReceived) 139 self.stub.send(message) 140 return d 53 141 54 142 class PubSubServiceTest(unittest.TestCase):
Note: See TracChangeset
for help on using the changeset viewer.