source: ralphm-patches/pubsub-item.patch @ 25:e467ff96b011

Last change on this file since 25:e467ff96b011 was 18:0ea51a6cd849, checked in by Ralph Meijer <ralphm@…>, 11 years ago

Add patch for turning item in a non-Element object.

File size: 4.2 KB
  • wokkel/pubsub.py

    diff -r 2919e60d3588 -r 90bd422b8ac6 wokkel/pubsub.py
    a b  
    119119
    120120
    121121
    122 class Item(domish.Element):
     122class Item(object):
    123123    """
    124124    Publish subscribe item.
    125125
    126     This behaves like an object providing L{domish.IElement}.
    127 
    128     Item payload can be added using C{addChild} or C{addRawXml}, or using the
    129     C{payload} keyword argument to C{__init__}.
     126    @ivar itemIdentifier: optional item identifier
     127    @type itemIdentifier: L{unicode}
     128    @ivar payload: optional item payload. Either as a domish element, or as
     129        serialized XML.
     130    @type payload: object providing L{domish.IElement} or L{unicode}.
    130131    """
    131132
    132     def __init__(self, id=None, payload=None):
     133    def __init__(self, itemIdentifier=None, payload=None):
     134        self.itemIdentifier = itemIdentifier
     135        self.payload = payload
     136
     137
     138    def toElement(self, namespace=NS_PUBSUB):
    133139        """
    134         @param id: optional item identifier
    135         @type id: L{unicode}
    136         @param payload: optional item payload. Either as a domish element, or
    137                         as serialized XML.
    138         @type payload: object providing L{domish.IElement} or L{unicode}.
     140        Render to a DOM representation.
     141
     142        Usually called by when rendering requests or responses. The optional
     143        C{namespace} argument is for rendering the item in that namespace, so
     144        L{Item} is usable in all contexts involving items.
     145
     146        @param namespace: The namespace of the item. Usually L{'NS_PUBSUB'} or
     147            L{'NS_PUBSUB_EVENT'}.
     148        @type: C{unicode}.
     149        @rtype: L{domish.Element}.
    139150        """
     151        element = domish.Element((namespace, 'item'))
    140152
    141         domish.Element.__init__(self, (NS_PUBSUB, 'item'))
    142         if id is not None:
    143             self['id'] = id
    144         if payload is not None:
    145             if isinstance(payload, basestring):
    146                 self.addRawXml(payload)
     153        if self.itemIdentifier is not None:
     154            element['id'] = self.itemIdentifier
     155
     156        if self.payload is not None:
     157            if isinstance(self.payload, basestring):
     158                self.addRawXml(self.payload)
    147159            else:
    148                 self.addChild(payload)
     160                self.addChild(self.payload)
     161
     162        return element
    149163
    150164
    151165
     
    333347        self.items = []
    334348        for element in verbElement.elements():
    335349            if element.uri == NS_PUBSUB and element.name == 'item':
    336                 self.items.append(element)
     350                item = Item()
     351                item.itemIdentifier = element.getAttribute('id')
     352                try:
     353                    item.payload = element.elements().next()
     354                except StopIteration:
     355                    pass
     356                self.items.append(item)
    337357
    338358
    339359    def _render_items(self, verbElement):
     
    342362        """
    343363        if self.items:
    344364            for item in self.items:
    345                 verbElement.addChild(item)
     365                verbElement.addChild(item.toElement())
    346366
    347367
    348368    def _parse_jid(self, verbElement):
     
    11051125        items["node"] = request.nodeIdentifier
    11061126
    11071127        for item in result:
    1108             items.addChild(item)
     1128            items.addChild(item.toElement())
    11091129
    11101130        return response
    11111131
  • wokkel/test/test_pubsub.py

    diff -r 2919e60d3588 -r 90bd422b8ac6 wokkel/test/test_pubsub.py
    a b  
    331331        items = list(domish.generateElementsQNamed(child.children,
    332332                                                   'item', NS_PUBSUB))
    333333        self.assertEquals(1, len(items))
    334         self.assertIdentical(item, items[0])
    335334
    336335        response = toResponse(iq, 'result')
    337336        self.stub.send(response)
     
    634633
    635634        request = pubsub.PubSubRequest.fromElement(parseXml(xml))
    636635        self.assertEqual(2, len(request.items))
    637         self.assertEqual(u'item1', request.items[0]["id"])
    638         self.assertEqual(u'item2', request.items[1]["id"])
     636        self.assertEqual(u'item1', request.items[0].itemIdentifier)
     637        self.assertEqual(u'item2', request.items[1].itemIdentifier)
    639638
    640639
    641640    def test_fromElementPublishNoNode(self):
Note: See TracBrowser for help on using the repository browser.