source: ralphm-patches/pubsub-item.patch

Last change on this file was 57:0d8b6cf41728, checked in by Ralph Meijer <ralphm@…>, 9 years ago

Wokkel 0.7.0 release, clean up various patches.

File size: 7.3 KB
  • wokkel/generic.py

    diff -r 2bc0db311133 wokkel/generic.py
    a b  
    3434    @return: The DOM structure, or C{None} on empty or incomplete input.
    3535    @rtype: L{domish.Element}
    3636    """
    37     roots = []
    3837    results = []
    3938    elementStream = domish.elementStream()
    40     elementStream.DocumentStartEvent = roots.append
    41     elementStream.ElementEvent = lambda elem: roots[0].addChild(elem)
    42     elementStream.DocumentEndEvent = lambda: results.append(roots[0])
    43     elementStream.parse(string)
     39    elementStream.DocumentStartEvent = lambda _: None
     40    elementStream.ElementEvent = lambda elem: results.append(elem)
     41    elementStream.DocumentEndEvent = lambda: None
     42    elementStream.parse('<dummy>%s</dummy>' % string)
    4443    return results and results[0] or None
    4544
    4645
  • wokkel/pubsub.py

    diff -r 2bc0db311133 wokkel/pubsub.py
    a b  
    157157
    158158
    159159
    160 class Item(domish.Element):
     160class Item(object):
    161161    """
    162162    Publish subscribe item.
    163163
    164     This behaves like an object providing L{domish.IElement}.
    165 
    166     Item payload can be added using C{addChild} or C{addRawXml}, or using the
    167     C{payload} keyword argument to C{__init__}.
     164    @ivar itemIdentifier: Optional item identifier.
     165    @type itemIdentifier: L{unicode}
     166    @ivar payload: Optional item payload. Either as a L{domish.Element}, or as
     167        serialized XML.
     168    @type payload: Object providing L{domish.IElement} or L{unicode}.
     169    @ivar timestamp: Optional timestamp when the item was published as 8601
     170    string. Timestamps that include a time component must have a time zone
     171    indicated.
     172    @type timestamp: C{unicode}
     173    @ivar publisher: Optional entity that published this item.
     174    @type publisher: L{JID}
    168175    """
    169176
    170     def __init__(self, id=None, payload=None):
     177    def __init__(self, itemIdentifier=None, payload=None,
     178                       timestamp=None, publisher=None):
     179        self.itemIdentifier = itemIdentifier
     180        self.payload = payload
     181        self.timestamp = timestamp
     182        self.publisher = publisher
     183
     184
     185    def toElement(self, namespace=NS_PUBSUB):
    171186        """
    172         @param id: optional item identifier
    173         @type id: C{unicode}
    174         @param payload: optional item payload. Either as a domish element, or
    175                         as serialized XML.
    176         @type payload: object providing L{domish.IElement} or C{unicode}.
     187        Render to a DOM representation.
     188
     189        Usually called by when rendering requests or responses. The optional
     190        C{namespace} argument is for rendering the item in that namespace, so
     191        L{Item} is usable in all contexts involving items.
     192
     193        @param namespace: The namespace of the item. Usually L{NS_PUBSUB} or
     194            L{NS_PUBSUB_EVENT}.
     195        @type: C{unicode}
     196        @rtype: L{domish.Element}
    177197        """
     198        element = domish.Element((namespace, 'item'))
    178199
    179         domish.Element.__init__(self, (None, 'item'))
    180         if id is not None:
    181             self['id'] = id
    182         if payload is not None:
    183             if isinstance(payload, basestring):
    184                 self.addRawXml(payload)
     200        if self.itemIdentifier is not None:
     201            element['id'] = self.itemIdentifier
     202
     203        if self.payload is not None:
     204            if isinstance(self.payload, basestring):
     205                element.addRawXml(self.payload)
    185206            else:
    186                 self.addChild(payload)
     207                element.addChild(self.payload)
     208
     209        return element
    187210
    188211
    189212
     
    362385        self.items = []
    363386        for element in verbElement.elements():
    364387            if element.uri == NS_PUBSUB and element.name == 'item':
    365                 self.items.append(element)
     388                item = Item()
     389                item.itemIdentifier = element.getAttribute('id')
     390                try:
     391                    item.payload = element.elements().next()
     392                except StopIteration:
     393                    pass
     394                self.items.append(item)
    366395
    367396
    368397    def _render_items(self, verbElement):
     
    371400        """
    372401        if self.items:
    373402            for item in self.items:
    374                 item.uri = NS_PUBSUB
    375                 verbElement.addChild(item)
     403                verbElement.addChild(item.toElement())
    376404
    377405
    378406    def _parse_jid(self, verbElement):
     
    13191347        items["node"] = request.nodeIdentifier
    13201348
    13211349        for item in result:
    1322             item.uri = NS_PUBSUB
    1323             items.addChild(item)
     1350            items.addChild(item.toElement())
    13241351
    13251352        return response
    13261353
     
    13711398                                               nodeIdentifier, subscriber,
    13721399                                               subscriptions)
    13731400            for item in items:
    1374                 item.uri = NS_PUBSUB_EVENT
    1375                 message.event.items.addChild(item)
     1401                message.event.items.addChild(item.toElement(NS_PUBSUB_EVENT))
    13761402            self.send(message)
    13771403
    13781404
  • wokkel/test/test_pubsub.py

    diff -r 2bc0db311133 wokkel/test/test_pubsub.py
    a b  
    115115
    116116
    117117
     118class ItemTest(unittest.TestCase):
     119
     120    def test_toElement(self):
     121        item = pubsub.Item(itemIdentifier='test')
     122        element = item.toElement()
     123        self.assertEquals('test', element.getAttribute('id'))
     124
     125
     126    def test_toElementPayload(self):
     127        payload = domish.Element(('testns', 'test'))
     128        item = pubsub.Item(itemIdentifier='test', payload=payload)
     129        element = item.toElement()
     130        self.assertNotIdentical(None, element.test)
     131        self.assertEquals('testns', element.test.uri)
     132
     133
     134    def test_toElementPayloadRawXML(self):
     135        payload = u"<test xmlns='testns'/>"
     136        item = pubsub.Item(itemIdentifier='test', payload=payload)
     137        element = item.toElement()
     138        self.assertEquals(1, len(element.children))
     139        self.assertEquals(payload, element.children[0])
     140
     141
    118142class PubSubClientTest(unittest.TestCase):
    119143    timeout = 2
    120144
     
    465489        items = list(domish.generateElementsQNamed(child.children,
    466490                                                   'item', NS_PUBSUB))
    467491        self.assertEquals(1, len(items))
    468         self.assertIdentical(item, items[0])
    469492
    470493        response = toResponse(iq, 'result')
    471494        self.stub.send(response)
     
    10481071
    10491072        request = pubsub.PubSubRequest.fromElement(parseXml(xml))
    10501073        self.assertEqual(2, len(request.items))
    1051         self.assertEqual(u'item1', request.items[0]["id"])
    1052         self.assertEqual(u'item2', request.items[1]["id"])
     1074        self.assertEqual(u'item1', request.items[0].itemIdentifier)
     1075        self.assertEqual(u'item2', request.items[1].itemIdentifier)
    10531076
    10541077
    10551078    def test_fromElementPublishItemsOptions(self):
     
    10751098
    10761099        request = pubsub.PubSubRequest.fromElement(parseXml(xml))
    10771100        self.assertEqual(2, len(request.items))
    1078         self.assertEqual(u'item1', request.items[0]["id"])
    1079         self.assertEqual(u'item2', request.items[1]["id"])
     1101        self.assertEqual(u'item1', request.items[0].itemIdentifier)
     1102        self.assertEqual(u'item2', request.items[1].itemIdentifier)
     1103
    10801104
    10811105    def test_fromElementPublishNoNode(self):
    10821106        """
Note: See TracBrowser for help on using the repository browser.