comparison src/tmp/wokkel/rsm.py @ 1419:be2df1ddea8e

plugins (groupblog, xep-0277) + tmp(rsm): improved style: - removed external parenthesis in assertions - added blank line after first line of docstrings - replaced "__" prefixes by "_" - renamed variabled named after the reserverd word "max" to "max_"
author Goffi <goffi@goffi.org>
date Wed, 22 Apr 2015 18:30:28 +0200
parents 7d9ff14a2d9d
children be1fccf4854d
comparison
equal deleted inserted replaced
1418:6adf1b0be609 1419:be2df1ddea8e
34 34
35 class RSMRequest(): 35 class RSMRequest():
36 """ 36 """
37 A Result Set Management request. 37 A Result Set Management request.
38 38
39 @ivar max: limit on the number of retrieved items. 39 @ivar max_: limit on the number of retrieved items.
40 @itype max: C{int} or C{unicode} 40 @itype max_: C{int} or C{unicode}
41 41
42 @ivar index: starting index of the requested page. 42 @ivar index: starting index of the requested page.
43 @itype index: C{int} or C{unicode} 43 @itype index: C{int} or C{unicode}
44 44
45 @ivar after: ID of the element immediately preceding the page. 45 @ivar after: ID of the element immediately preceding the page.
47 47
48 @ivar before: ID of the element immediately following the page. 48 @ivar before: ID of the element immediately following the page.
49 @itype before: C{unicode} 49 @itype before: C{unicode}
50 """ 50 """
51 51
52 max = 10 52 max_ = 10
53 index = None 53 index = None
54 after = None 54 after = None
55 before = None 55 before = None
56 56
57 def __init__(self, max=None, index=None, after=None, before=None): 57 def __init__(self, max_=None, index=None, after=None, before=None):
58 if max is not None: 58 if max_ is not None:
59 max = int(max) 59 max_ = int(max_)
60 assert(max >= 0) 60 assert max_ >= 0
61 self.max = max 61 self.max_ = max_
62 62
63 if index is not None: 63 if index is not None:
64 assert(after is None and before is None) 64 assert after is None and before is None
65 index = int(index) 65 index = int(index)
66 assert(index >= 0) 66 assert index >= 0
67 self.index = index 67 self.index = index
68 68
69 if after is not None: 69 if after is not None:
70 assert(before is None) 70 assert before is None
71 assert(isinstance(after, unicode)) 71 assert isinstance(after, unicode)
72 self.after = after 72 self.after = after
73 73
74 if before is not None: 74 if before is not None:
75 assert(isinstance(before, unicode)) 75 assert isinstance(before, unicode)
76 self.before = before 76 self.before = before
77 77
78 @classmethod 78 @classmethod
79 def parse(cls, element): 79 def parse(cls, element):
80 """Parse the given request element. 80 """Parse the given request element.
97 if elt.name in ('before', 'after'): 97 if elt.name in ('before', 'after'):
98 setattr(request, elt.name, ''.join(elt.children)) 98 setattr(request, elt.name, ''.join(elt.children))
99 elif elt.name in ('max', 'index'): 99 elif elt.name in ('max', 'index'):
100 setattr(request, elt.name, int(''.join(elt.children))) 100 setattr(request, elt.name, int(''.join(elt.children)))
101 101
102 if request.max is None: 102 if request.max_ is None:
103 raise RSMError("RSM request is missing its 'max' element") 103 raise RSMError("RSM request is missing its 'max_' element")
104 104
105 return request 105 return request
106 106
107 def toElement(self): 107 def toElement(self):
108 """ 108 """
109 Return the DOM representation of this RSM request. 109 Return the DOM representation of this RSM request.
110 110
111 @rtype: L{domish.Element} 111 @rtype: L{domish.Element}
112 """ 112 """
113 set_elt = domish.Element((NS_RSM, 'set')) 113 set_elt = domish.Element((NS_RSM, 'set'))
114 set_elt.addElement('max').addContent(unicode(self.max)) 114 set_elt.addElement('max').addContent(unicode(self.max_))
115 115
116 if self.index is not None: 116 if self.index is not None:
117 set_elt.addElement('index').addContent(unicode(self.index)) 117 set_elt.addElement('index').addContent(unicode(self.index))
118 118
119 if self.before is not None: 119 if self.before is not None:
135 135
136 @return: RSM request element. 136 @return: RSM request element.
137 @rtype: L{domish.Element} 137 @rtype: L{domish.Element}
138 """ 138 """
139 if element.name == 'pubsub' and hasattr(element, 'items'): 139 if element.name == 'pubsub' and hasattr(element, 'items'):
140 element.items.attributes['max_items'] = unicode(self.max) 140 element.items.attributes['max_items'] = unicode(self.max_)
141 141
142 set_elt = self.toElement() 142 set_elt = self.toElement()
143 element.addChild(set_elt) 143 element.addChild(set_elt)
144 144
145 return set_elt 145 return set_elt
167 first = None 167 first = None
168 last = None 168 last = None
169 169
170 def __init__(self, count=None, index=None, first=None, last=None): 170 def __init__(self, count=None, index=None, first=None, last=None):
171 if count is not None: 171 if count is not None:
172 assert(isinstance(count, int) and count >= 0) 172 assert isinstance(count, int) and count >= 0
173 self.count = count 173 self.count = count
174 174
175 if index is not None: 175 if index is not None:
176 assert(isinstance(index, int) and index >= 0) 176 assert isinstance(index, int) and index >= 0
177 self.index = index 177 self.index = index
178 assert(isinstance(first, unicode)) 178 assert isinstance(first, unicode)
179 self.first = first 179 self.first = first
180 assert(isinstance(last, unicode)) 180 assert isinstance(last, unicode)
181 self.last = last 181 self.last = last
182 else: 182 else:
183 assert(first is None and last is None) 183 assert first is None and last is None
184 184
185 @classmethod 185 @classmethod
186 def parse(cls, element): 186 def parse(cls, element):
187 """Parse the given response element. 187 """Parse the given response element.
188 188
370 def _toResponse_items(self, result, resource, request): 370 def _toResponse_items(self, result, resource, request):
371 response = pubsub.PubSubService._toResponse_items(self, result, 371 response = pubsub.PubSubService._toResponse_items(self, result,
372 resource, request) 372 resource, request)
373 set_elts = [elt for elt in result if elt.name == 'set'] 373 set_elts = [elt for elt in result if elt.name == 'set']
374 if set_elts: 374 if set_elts:
375 assert(len(set_elts) == 1) 375 assert len(set_elts) == 1
376 response.addChild(set_elts[0]) 376 response.addChild(set_elts[0])
377 377
378 return response 378 return response