source: ralphm-patches/pubsub-item.patch

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

Wokkel 0.7.0 release, clean up various patches.

File size: 7.3 KB
RevLine 
[57]1diff -r 2bc0db311133 wokkel/generic.py
2--- a/wokkel/generic.py Wed Mar 28 13:28:55 2012 +0200
3+++ b/wokkel/generic.py Wed Mar 28 13:30:27 2012 +0200
4@@ -34,13 +34,12 @@
[47]5     @return: The DOM structure, or C{None} on empty or incomplete input.
6     @rtype: L{domish.Element}
7     """
8-    roots = []
9     results = []
10     elementStream = domish.elementStream()
11-    elementStream.DocumentStartEvent = roots.append
12-    elementStream.ElementEvent = lambda elem: roots[0].addChild(elem)
13-    elementStream.DocumentEndEvent = lambda: results.append(roots[0])
14-    elementStream.parse(string)
15+    elementStream.DocumentStartEvent = lambda _: None
16+    elementStream.ElementEvent = lambda elem: results.append(elem)
17+    elementStream.DocumentEndEvent = lambda: None
18+    elementStream.parse('<dummy>%s</dummy>' % string)
19     return results and results[0] or None
20 
21 
[57]22diff -r 2bc0db311133 wokkel/pubsub.py
23--- a/wokkel/pubsub.py  Wed Mar 28 13:28:55 2012 +0200
24+++ b/wokkel/pubsub.py  Wed Mar 28 13:30:27 2012 +0200
[47]25@@ -157,33 +157,56 @@
[18]26 
27 
28 
29-class Item(domish.Element):
30+class Item(object):
31     """
32     Publish subscribe item.
33 
34-    This behaves like an object providing L{domish.IElement}.
35-
36-    Item payload can be added using C{addChild} or C{addRawXml}, or using the
37-    C{payload} keyword argument to C{__init__}.
[47]38+    @ivar itemIdentifier: Optional item identifier.
[18]39+    @type itemIdentifier: L{unicode}
[47]40+    @ivar payload: Optional item payload. Either as a L{domish.Element}, or as
[18]41+        serialized XML.
[47]42+    @type payload: Object providing L{domish.IElement} or L{unicode}.
43+    @ivar timestamp: Optional timestamp when the item was published as 8601
44+    string. Timestamps that include a time component must have a time zone
45+    indicated.
46+    @type timestamp: C{unicode}
47+    @ivar publisher: Optional entity that published this item.
48+    @type publisher: L{JID}
[18]49     """
50 
51-    def __init__(self, id=None, payload=None):
[47]52+    def __init__(self, itemIdentifier=None, payload=None,
53+                       timestamp=None, publisher=None):
[18]54+        self.itemIdentifier = itemIdentifier
55+        self.payload = payload
[47]56+        self.timestamp = timestamp
57+        self.publisher = publisher
[18]58+
59+
60+    def toElement(self, namespace=NS_PUBSUB):
61         """
62-        @param id: optional item identifier
[57]63-        @type id: C{unicode}
[18]64-        @param payload: optional item payload. Either as a domish element, or
65-                        as serialized XML.
[57]66-        @type payload: object providing L{domish.IElement} or C{unicode}.
[18]67+        Render to a DOM representation.
68+
69+        Usually called by when rendering requests or responses. The optional
70+        C{namespace} argument is for rendering the item in that namespace, so
71+        L{Item} is usable in all contexts involving items.
72+
[26]73+        @param namespace: The namespace of the item. Usually L{NS_PUBSUB} or
74+            L{NS_PUBSUB_EVENT}.
75+        @type: C{unicode}
76+        @rtype: L{domish.Element}
[18]77         """
78+        element = domish.Element((namespace, 'item'))
79 
[45]80-        domish.Element.__init__(self, (None, 'item'))
[18]81-        if id is not None:
82-            self['id'] = id
83-        if payload is not None:
84-            if isinstance(payload, basestring):
85-                self.addRawXml(payload)
86+        if self.itemIdentifier is not None:
87+            element['id'] = self.itemIdentifier
88+
89+        if self.payload is not None:
90+            if isinstance(self.payload, basestring):
[47]91+                element.addRawXml(self.payload)
[18]92             else:
93-                self.addChild(payload)
[47]94+                element.addChild(self.payload)
[18]95+
96+        return element
97 
98 
99 
[57]100@@ -362,7 +385,13 @@
[18]101         self.items = []
102         for element in verbElement.elements():
103             if element.uri == NS_PUBSUB and element.name == 'item':
104-                self.items.append(element)
105+                item = Item()
106+                item.itemIdentifier = element.getAttribute('id')
107+                try:
108+                    item.payload = element.elements().next()
109+                except StopIteration:
110+                    pass
111+                self.items.append(item)
112 
113 
114     def _render_items(self, verbElement):
[57]115@@ -371,8 +400,7 @@
[18]116         """
117         if self.items:
118             for item in self.items:
[47]119-                item.uri = NS_PUBSUB
[18]120-                verbElement.addChild(item)
121+                verbElement.addChild(item.toElement())
122 
123 
124     def _parse_jid(self, verbElement):
[57]125@@ -1319,8 +1347,7 @@
[18]126         items["node"] = request.nodeIdentifier
127 
128         for item in result:
[47]129-            item.uri = NS_PUBSUB
[18]130-            items.addChild(item)
131+            items.addChild(item.toElement())
132 
133         return response
134 
[57]135@@ -1371,8 +1398,7 @@
[47]136                                                nodeIdentifier, subscriber,
137                                                subscriptions)
138             for item in items:
139-                item.uri = NS_PUBSUB_EVENT
140-                message.event.items.addChild(item)
141+                message.event.items.addChild(item.toElement(NS_PUBSUB_EVENT))
142             self.send(message)
143 
144 
[57]145diff -r 2bc0db311133 wokkel/test/test_pubsub.py
146--- a/wokkel/test/test_pubsub.py        Wed Mar 28 13:28:55 2012 +0200
147+++ b/wokkel/test/test_pubsub.py        Wed Mar 28 13:30:27 2012 +0200
[47]148@@ -115,6 +115,30 @@
149 
150 
151 
152+class ItemTest(unittest.TestCase):
153+
154+    def test_toElement(self):
155+        item = pubsub.Item(itemIdentifier='test')
156+        element = item.toElement()
157+        self.assertEquals('test', element.getAttribute('id'))
158+
159+
160+    def test_toElementPayload(self):
161+        payload = domish.Element(('testns', 'test'))
162+        item = pubsub.Item(itemIdentifier='test', payload=payload)
163+        element = item.toElement()
164+        self.assertNotIdentical(None, element.test)
165+        self.assertEquals('testns', element.test.uri)
166+
167+
168+    def test_toElementPayloadRawXML(self):
169+        payload = u"<test xmlns='testns'/>"
170+        item = pubsub.Item(itemIdentifier='test', payload=payload)
171+        element = item.toElement()
172+        self.assertEquals(1, len(element.children))
173+        self.assertEquals(payload, element.children[0])
174+
175+
176 class PubSubClientTest(unittest.TestCase):
177     timeout = 2
178 
179@@ -465,7 +489,6 @@
[18]180         items = list(domish.generateElementsQNamed(child.children,
181                                                    'item', NS_PUBSUB))
182         self.assertEquals(1, len(items))
183-        self.assertIdentical(item, items[0])
184 
185         response = toResponse(iq, 'result')
186         self.stub.send(response)
[47]187@@ -1048,8 +1071,8 @@
[18]188 
189         request = pubsub.PubSubRequest.fromElement(parseXml(xml))
190         self.assertEqual(2, len(request.items))
191-        self.assertEqual(u'item1', request.items[0]["id"])
192-        self.assertEqual(u'item2', request.items[1]["id"])
193+        self.assertEqual(u'item1', request.items[0].itemIdentifier)
194+        self.assertEqual(u'item2', request.items[1].itemIdentifier)
195 
196 
[47]197     def test_fromElementPublishItemsOptions(self):
198@@ -1075,8 +1098,9 @@
199 
200         request = pubsub.PubSubRequest.fromElement(parseXml(xml))
201         self.assertEqual(2, len(request.items))
202-        self.assertEqual(u'item1', request.items[0]["id"])
203-        self.assertEqual(u'item2', request.items[1]["id"])
204+        self.assertEqual(u'item1', request.items[0].itemIdentifier)
205+        self.assertEqual(u'item2', request.items[1].itemIdentifier)
206+
207 
[18]208     def test_fromElementPublishNoNode(self):
[47]209         """
Note: See TracBrowser for help on using the repository browser.