comparison wokkel/rsm.py @ 7:1e21b4e94566

plugin groupblog, tmp (mam, rsm): some style improvments/fixes: - renamed variables nammed after reserved word/module - removed class variables which are not needed - removed external parenthesis from asserts - in mam.MAMPrefs, removed default value for default argument, as None can't be used - (groupblog) extended docstring for DeferredItems and DeferredItemsFromMany
author Goffi <goffi@goffi.org>
date Thu, 23 Apr 2015 13:35:21 +0200
parents dc3a3f454f39
children a07bf3fb4b54
comparison
equal deleted inserted replaced
6:dc3a3f454f39 7:1e21b4e94566
51 51
52 @ivar max_: limit on the number of retrieved items. 52 @ivar max_: limit on the number of retrieved items.
53 @itype max_: C{int} or C{unicode} 53 @itype max_: C{int} or C{unicode}
54 54
55 @ivar index: starting index of the requested page. 55 @ivar index: starting index of the requested page.
56 @itype index: C{int} or C{unicode} 56 @itype index: C{int} or C{unicode} or C{None}
57 57
58 @ivar after: ID of the element immediately preceding the page. 58 @ivar after: ID of the element immediately preceding the page.
59 @itype after: C{unicode} 59 @itype after: C{unicode}
60 60
61 @ivar before: ID of the element immediately following the page. 61 @ivar before: ID of the element immediately following the page.
62 @itype before: C{unicode} 62 @itype before: C{unicode}
63 """ 63 """
64 64
65 max_ = 10 65 def __init__(self, max_=10, index=None, after=None, before=None):
66 index = None 66 max_ = int(max_)
67 after = None 67 assert max_ >= 0
68 before = None 68 self.max = max_
69
70 def __init__(self, max_=None, index=None, after=None, before=None):
71 if max_ is not None:
72 max_ = int(max_)
73 assert max_ >= 0
74 self.max_ = max_
75 69
76 if index is not None: 70 if index is not None:
77 assert after is None and before is None 71 assert after is None and before is None
78 index = int(index) 72 index = int(index)
79 assert index >= 0 73 assert index >= 0
80 self.index = index 74 self.index = index
81 75
82 if after is not None: 76 if after is not None:
83 assert before is None 77 assert before is None
84 assert isinstance(after, unicode) 78 assert isinstance(after, unicode)
85 self.after = after 79 self.after = after
86 80
87 if before is not None: 81 if before is not None:
88 assert isinstance(before, unicode) 82 assert isinstance(before, unicode)
89 self.before = before 83 self.before = before
90 84
91 @classmethod 85 @classmethod
92 def parse(cls, element): 86 def parse(cls, element):
93 """Parse the given request element. 87 """Parse the given request element.
94 88
110 if elt.name in ('before', 'after'): 104 if elt.name in ('before', 'after'):
111 setattr(request, elt.name, ''.join(elt.children)) 105 setattr(request, elt.name, ''.join(elt.children))
112 elif elt.name in ('max', 'index'): 106 elif elt.name in ('max', 'index'):
113 setattr(request, elt.name, int(''.join(elt.children))) 107 setattr(request, elt.name, int(''.join(elt.children)))
114 108
115 if request.max_ is None: 109 if request.max is None:
116 raise RSMError("RSM request is missing its 'max_' element") 110 raise RSMError("RSM request is missing its 'max' element")
117 111
118 return request 112 return request
119 113
120 def toElement(self): 114 def toElement(self):
121 """ 115 """
122 Return the DOM representation of this RSM request. 116 Return the DOM representation of this RSM request.
123 117
124 @rtype: L{domish.Element} 118 @rtype: L{domish.Element}
125 """ 119 """
126 set_elt = domish.Element((NS_RSM, 'set')) 120 set_elt = domish.Element((NS_RSM, 'set'))
127 set_elt.addElement('max').addContent(unicode(self.max_)) 121 set_elt.addElement('max').addContent(unicode(self.max))
128 122
129 if self.index is not None: 123 if self.index is not None:
130 set_elt.addElement('index').addContent(unicode(self.index)) 124 set_elt.addElement('index').addContent(unicode(self.index))
131 125
132 if self.before is not None: 126 if self.before is not None:
148 142
149 @return: RSM request element. 143 @return: RSM request element.
150 @rtype: L{domish.Element} 144 @rtype: L{domish.Element}
151 """ 145 """
152 if element.name == 'pubsub' and hasattr(element, 'items'): 146 if element.name == 'pubsub' and hasattr(element, 'items'):
153 element.items.attributes['max_items'] = unicode(self.max_) 147 element.items.attributes['max_items'] = unicode(self.max)
154 148
155 set_elt = self.toElement() 149 set_elt = self.toElement()
156 element.addChild(set_elt) 150 element.addChild(set_elt)
157 151
158 return set_elt 152 return set_elt
173 167
174 @ivar last: ID of the last element of the returned page. 168 @ivar last: ID of the last element of the returned page.
175 @itype last: C{unicode} 169 @itype last: C{unicode}
176 """ 170 """
177 171
178 count = 0 172 def __init__(self, count=0, index=None, first=None, last=None):
179 index = None 173 assert isinstance(count, int) and count >= 0
180 first = None 174 self.count = count
181 last = None
182
183 def __init__(self, count=None, index=None, first=None, last=None):
184 if count is not None:
185 assert isinstance(count, int) and count >= 0
186 self.count = count
187
188 if index is not None: 175 if index is not None:
189 assert isinstance(index, int) and index >= 0 176 assert isinstance(index, int) and index >= 0
190 self.index = index
191 assert isinstance(first, unicode) 177 assert isinstance(first, unicode)
192 self.first = first
193 assert isinstance(last, unicode) 178 assert isinstance(last, unicode)
194 self.last = last
195 else: 179 else:
196 assert first is None and last is None 180 assert first is None and last is None
181 self.index = index
182 self.first = first
183 self.last = last
197 184
198 @classmethod 185 @classmethod
199 def parse(cls, element): 186 def parse(cls, element):
200 """Parse the given response element. 187 """Parse the given response element.
201 188