comparison src/tmp/wokkel/rsm.py @ 1283:7d9ff14a2d9d

tmp: update tmp.wokkel.rsm, add tmp.wokkel.mam
author souliane <souliane@mailoo.org>
date Fri, 09 Jan 2015 10:50:11 +0100
parents 9141bde7ff31
children be2df1ddea8e
comparison
equal deleted inserted replaced
1282:74d558e6c9fd 1283:7d9ff14a2d9d
8 8
9 This protocol is specified in 9 This protocol is specified in
10 U{XEP-0059<http://xmpp.org/extensions/xep-0059.html>}. 10 U{XEP-0059<http://xmpp.org/extensions/xep-0059.html>}.
11 """ 11 """
12 12
13 from twisted.python import log
14 from twisted.words.xish import domish 13 from twisted.words.xish import domish
15 14
16 import pubsub 15 import pubsub
17 import copy 16 import copy
18 17
19 18
20 # RSM namespace 19 # RSM namespace
21 NS_RSM = 'http://jabber.org/protocol/rsm' 20 NS_RSM = 'http://jabber.org/protocol/rsm'
21
22
23 class RSMError(Exception):
24 """
25 RSM error.
26 """
27
28
29 class RSMNotFoundError(Exception):
30 """
31 An expected RSM element has not been found.
32 """
22 33
23 34
24 class RSMRequest(): 35 class RSMRequest():
25 """ 36 """
26 A Result Set Management request. 37 A Result Set Management request.
77 try: 88 try:
78 set_elt = domish.generateElementsQNamed(element.elements(), 89 set_elt = domish.generateElementsQNamed(element.elements(),
79 name="set", 90 name="set",
80 uri=NS_RSM).next() 91 uri=NS_RSM).next()
81 except StopIteration: 92 except StopIteration:
82 return None 93 raise RSMNotFoundError()
83 94
84 request = RSMRequest() 95 request = RSMRequest()
85 for elt in list(set_elt.elements()): 96 for elt in list(set_elt.elements()):
86 if elt.name in ('before', 'after'): 97 if elt.name in ('before', 'after'):
87 setattr(request, elt.name, ''.join(elt.children)) 98 setattr(request, elt.name, ''.join(elt.children))
88 elif elt.name in ('max', 'index'): 99 elif elt.name in ('max', 'index'):
89 setattr(request, elt.name, int(''.join(elt.children))) 100 setattr(request, elt.name, int(''.join(elt.children)))
90 101
91 if request.max is None: 102 if request.max is None:
92 log.err("RSM request is missing its 'max' element!") 103 raise RSMError("RSM request is missing its 'max' element")
93 104
94 return request 105 return request
95 106
96 def render(self, element=None): 107 def toElement(self):
97 """Render a RSM page request, eventually embed it in the given element. 108 """
98 109 Return the DOM representation of this RSM request.
99 @param element: request element. 110
100 @type element: L{domish.Element}
101
102 @return: RSM request element.
103 @rtype: L{domish.Element} 111 @rtype: L{domish.Element}
104 """ 112 """
105 if element and element.name == 'pubsub' and hasattr(element, 'items'):
106 element.items.attributes['max_items'] = unicode(self.max)
107
108 set_elt = domish.Element((NS_RSM, 'set')) 113 set_elt = domish.Element((NS_RSM, 'set'))
109 set_elt.addElement('max').addContent(unicode(self.max)) 114 set_elt.addElement('max').addContent(unicode(self.max))
110 115
111 if self.index is not None: 116 if self.index is not None:
112 set_elt.addElement('index').addContent(unicode(self.index)) 117 set_elt.addElement('index').addContent(unicode(self.index))
118 set_elt.addElement('before').addContent(self.before) 123 set_elt.addElement('before').addContent(self.before)
119 124
120 if self.after is not None: 125 if self.after is not None:
121 set_elt.addElement('after').addContent(self.after) 126 set_elt.addElement('after').addContent(self.after)
122 127
123 if element: 128 return set_elt
124 element.addChild(set_elt) 129
130 def render(self, element):
131 """Embed the DOM representation of this RSM request in the given element.
132
133 @param element: Element to contain the RSM request.
134 @type element: L{domish.Element}
135
136 @return: RSM request element.
137 @rtype: L{domish.Element}
138 """
139 if element.name == 'pubsub' and hasattr(element, 'items'):
140 element.items.attributes['max_items'] = unicode(self.max)
141
142 set_elt = self.toElement()
143 element.addChild(set_elt)
125 144
126 return set_elt 145 return set_elt
127 146
128 147
129 class RSMResponse(): 148 class RSMResponse():
176 try: 195 try:
177 set_elt = domish.generateElementsQNamed(element.elements(), 196 set_elt = domish.generateElementsQNamed(element.elements(),
178 name="set", 197 name="set",
179 uri=NS_RSM).next() 198 uri=NS_RSM).next()
180 except StopIteration: 199 except StopIteration:
181 return None 200 return RSMNotFoundError()
182 201
183 response = RSMResponse() 202 response = RSMResponse()
184 for elt in list(set_elt.elements()): 203 for elt in list(set_elt.elements()):
185 if elt.name in ('first', 'last'): 204 if elt.name in ('first', 'last'):
186 setattr(response, elt.name, ''.join(elt.children)) 205 setattr(response, elt.name, ''.join(elt.children))
188 response.index = int(elt.getAttribute("index")) 207 response.index = int(elt.getAttribute("index"))
189 elif elt.name == 'count': 208 elif elt.name == 'count':
190 response.count = int(''.join(elt.children)) 209 response.count = int(''.join(elt.children))
191 210
192 if response.count is None: 211 if response.count is None:
193 log.err("RSM response is missing its 'count' element!") 212 raise RSMError("RSM response is missing its 'count' element")
194 213
195 return response 214 return response
196 215
197 def render(self, parent=None): 216 def toElement(self):
198 """Render a RSM page response, eventually embed it in the given element. 217 """
199 218 Return the DOM representation of this RSM request.
200 @param element: response element. 219
201 @type element: L{domish.Element}
202
203 @return: RSM request element.
204 @rtype: L{domish.Element} 220 @rtype: L{domish.Element}
205 """ 221 """
206 set_elt = domish.Element((NS_RSM, 'set')) 222 set_elt = domish.Element((NS_RSM, 'set'))
207 set_elt.addElement('count').addContent(unicode(self.count)) 223 set_elt.addElement('count').addContent(unicode(self.count))
208 224
211 first_elt.addContent(self.first) 227 first_elt.addContent(self.first)
212 first_elt['index'] = unicode(self.index) 228 first_elt['index'] = unicode(self.index)
213 229
214 set_elt.addElement('last').addContent(self.last) 230 set_elt.addElement('last').addContent(self.last)
215 231
216 if parent: 232 return set_elt
217 parent.addChild(set_elt) 233
218 234 def render(self, element):
235 """Embed the DOM representation of this RSM response in the given element.
236
237 @param element: Element to contain the RSM response.
238 @type element: L{domish.Element}
239
240 @return: RSM request element.
241 @rtype: L{domish.Element}
242 """
243 set_elt = self.toElement()
244 element.addChild(set_elt)
219 return set_elt 245 return set_elt
220 246
221 def toDict(self): 247 def toDict(self):
222 """Return a dict representation of the object. 248 """Return a dict representation of the object.
223 249
245 pubsub.PubSubRequest.__init__(self, verb) 271 pubsub.PubSubRequest.__init__(self, verb)
246 self._parameters = copy.deepcopy(pubsub.PubSubRequest._parameters) 272 self._parameters = copy.deepcopy(pubsub.PubSubRequest._parameters)
247 self._parameters['items'].append('rsm') 273 self._parameters['items'].append('rsm')
248 274
249 def _parse_rsm(self, verbElement): 275 def _parse_rsm(self, verbElement):
250 self.rsm = RSMRequest.parse(verbElement.parent) 276 try:
277 self.rsm = RSMRequest.parse(verbElement.parent)
278 except RSMNotFoundError:
279 self.rsm = None
251 280
252 def _render_rsm(self, verbElement): 281 def _render_rsm(self, verbElement):
253 if self.rsm: 282 if self.rsm:
254 self.rsm.render(verbElement.parent) 283 self.rsm.render(verbElement.parent)
255 284