[26] | 1 | diff -r e58b9f2cf8a3 wokkel/pubsub.py |
---|
| 2 | --- a/wokkel/pubsub.py Fri Jan 01 01:34:14 2010 +0100 |
---|
| 3 | +++ b/wokkel/pubsub.py Fri Jan 01 11:12:36 2010 +0100 |
---|
[18] | 4 | @@ -119,33 +119,47 @@ |
---|
| 5 | |
---|
| 6 | |
---|
| 7 | |
---|
| 8 | -class Item(domish.Element): |
---|
| 9 | +class Item(object): |
---|
| 10 | """ |
---|
| 11 | Publish subscribe item. |
---|
| 12 | |
---|
| 13 | - This behaves like an object providing L{domish.IElement}. |
---|
| 14 | - |
---|
| 15 | - Item payload can be added using C{addChild} or C{addRawXml}, or using the |
---|
| 16 | - C{payload} keyword argument to C{__init__}. |
---|
| 17 | + @ivar itemIdentifier: optional item identifier |
---|
| 18 | + @type itemIdentifier: L{unicode} |
---|
| 19 | + @ivar payload: optional item payload. Either as a domish element, or as |
---|
| 20 | + serialized XML. |
---|
| 21 | + @type payload: object providing L{domish.IElement} or L{unicode}. |
---|
| 22 | """ |
---|
| 23 | |
---|
| 24 | - def __init__(self, id=None, payload=None): |
---|
| 25 | + def __init__(self, itemIdentifier=None, payload=None): |
---|
| 26 | + self.itemIdentifier = itemIdentifier |
---|
| 27 | + self.payload = payload |
---|
| 28 | + |
---|
| 29 | + |
---|
| 30 | + def toElement(self, namespace=NS_PUBSUB): |
---|
| 31 | """ |
---|
| 32 | - @param id: optional item identifier |
---|
| 33 | - @type id: L{unicode} |
---|
| 34 | - @param payload: optional item payload. Either as a domish element, or |
---|
| 35 | - as serialized XML. |
---|
| 36 | - @type payload: object providing L{domish.IElement} or L{unicode}. |
---|
| 37 | + Render to a DOM representation. |
---|
| 38 | + |
---|
| 39 | + Usually called by when rendering requests or responses. The optional |
---|
| 40 | + C{namespace} argument is for rendering the item in that namespace, so |
---|
| 41 | + L{Item} is usable in all contexts involving items. |
---|
| 42 | + |
---|
[26] | 43 | + @param namespace: The namespace of the item. Usually L{NS_PUBSUB} or |
---|
| 44 | + L{NS_PUBSUB_EVENT}. |
---|
| 45 | + @type: C{unicode} |
---|
| 46 | + @rtype: L{domish.Element} |
---|
[18] | 47 | """ |
---|
| 48 | + element = domish.Element((namespace, 'item')) |
---|
| 49 | |
---|
| 50 | - domish.Element.__init__(self, (NS_PUBSUB, 'item')) |
---|
| 51 | - if id is not None: |
---|
| 52 | - self['id'] = id |
---|
| 53 | - if payload is not None: |
---|
| 54 | - if isinstance(payload, basestring): |
---|
| 55 | - self.addRawXml(payload) |
---|
| 56 | + if self.itemIdentifier is not None: |
---|
| 57 | + element['id'] = self.itemIdentifier |
---|
| 58 | + |
---|
| 59 | + if self.payload is not None: |
---|
| 60 | + if isinstance(self.payload, basestring): |
---|
| 61 | + self.addRawXml(self.payload) |
---|
| 62 | else: |
---|
| 63 | - self.addChild(payload) |
---|
| 64 | + self.addChild(self.payload) |
---|
| 65 | + |
---|
| 66 | + return element |
---|
| 67 | |
---|
| 68 | |
---|
| 69 | |
---|
[26] | 70 | @@ -309,7 +323,13 @@ |
---|
[18] | 71 | self.items = [] |
---|
| 72 | for element in verbElement.elements(): |
---|
| 73 | if element.uri == NS_PUBSUB and element.name == 'item': |
---|
| 74 | - self.items.append(element) |
---|
| 75 | + item = Item() |
---|
| 76 | + item.itemIdentifier = element.getAttribute('id') |
---|
| 77 | + try: |
---|
| 78 | + item.payload = element.elements().next() |
---|
| 79 | + except StopIteration: |
---|
| 80 | + pass |
---|
| 81 | + self.items.append(item) |
---|
| 82 | |
---|
| 83 | |
---|
| 84 | def _render_items(self, verbElement): |
---|
[26] | 85 | @@ -318,7 +338,7 @@ |
---|
[18] | 86 | """ |
---|
| 87 | if self.items: |
---|
| 88 | for item in self.items: |
---|
| 89 | - verbElement.addChild(item) |
---|
| 90 | + verbElement.addChild(item.toElement()) |
---|
| 91 | |
---|
| 92 | |
---|
| 93 | def _parse_jid(self, verbElement): |
---|
[26] | 94 | @@ -1124,7 +1144,7 @@ |
---|
[18] | 95 | items["node"] = request.nodeIdentifier |
---|
| 96 | |
---|
| 97 | for item in result: |
---|
| 98 | - items.addChild(item) |
---|
| 99 | + items.addChild(item.toElement()) |
---|
| 100 | |
---|
| 101 | return response |
---|
| 102 | |
---|
[26] | 103 | diff -r e58b9f2cf8a3 wokkel/test/test_pubsub.py |
---|
| 104 | --- a/wokkel/test/test_pubsub.py Fri Jan 01 01:34:14 2010 +0100 |
---|
| 105 | +++ b/wokkel/test/test_pubsub.py Fri Jan 01 11:12:36 2010 +0100 |
---|
| 106 | @@ -365,7 +365,6 @@ |
---|
[18] | 107 | items = list(domish.generateElementsQNamed(child.children, |
---|
| 108 | 'item', NS_PUBSUB)) |
---|
| 109 | self.assertEquals(1, len(items)) |
---|
| 110 | - self.assertIdentical(item, items[0]) |
---|
| 111 | |
---|
| 112 | response = toResponse(iq, 'result') |
---|
| 113 | self.stub.send(response) |
---|
[26] | 114 | @@ -699,8 +698,8 @@ |
---|
[18] | 115 | |
---|
| 116 | request = pubsub.PubSubRequest.fromElement(parseXml(xml)) |
---|
| 117 | self.assertEqual(2, len(request.items)) |
---|
| 118 | - self.assertEqual(u'item1', request.items[0]["id"]) |
---|
| 119 | - self.assertEqual(u'item2', request.items[1]["id"]) |
---|
| 120 | + self.assertEqual(u'item1', request.items[0].itemIdentifier) |
---|
| 121 | + self.assertEqual(u'item2', request.items[1].itemIdentifier) |
---|
| 122 | |
---|
| 123 | |
---|
| 124 | def test_fromElementPublishNoNode(self): |
---|