comparison src/tools/xml_tools.py @ 1507:84250128e425

tools (xml_tools): split dataFormResult2AdvancedList in dataFormResult2XMLUIData and XMLUIData2AdvancedList
author souliane <souliane@mailoo.org>
date Wed, 02 Sep 2015 13:56:34 +0200
parents a2e4b976e707
children d2ab9c62ac3a
comparison
equal deleted inserted replaced
1506:8405d622bde0 1507:84250128e425
133 """ 133 """
134 form_ui = XMLUI("form", "vertical", submit_id=submit_id, session_id=session_id) 134 form_ui = XMLUI("form", "vertical", submit_id=submit_id, session_id=session_id)
135 return dataForm2Widgets(form_ui, form, read_only=read_only) 135 return dataForm2Widgets(form_ui, form, read_only=read_only)
136 136
137 137
138 def dataFormResult2AdvancedList(xmlui, form_xml): 138 def dataFormResult2XMLUIData(form_xml):
139 """Take a raw data form result (not parsed by Wokkel's XEP-0004 implementation) and convert it to an advanced list. 139 """Parse a data form result (not parsed by Wokkel's XEP-0004 implementation).
140 140
141 The raw data form is used because Wokkel doesn't manage result items parsing yet. 141 The raw data form is used because Wokkel doesn't manage result items parsing yet.
142 @param xmlui (XMLUI): the XMLUI where the AdvancedList will be added
143 @param form_xml (domish.Element): element of the data form 142 @param form_xml (domish.Element): element of the data form
144 @return: the completed XMLUI instance 143 @return: a couple (headers, result_list):
144 - headers (dict{unicode: unicode}): form headers (field labels and types)
145 - xmlui_data (list[tuple]): list of (widget_type, widget_args, widget_kwargs)
145 """ 146 """
146 headers = OrderedDict() 147 headers = OrderedDict()
147 try: 148 try:
148 reported_elt = form_xml.elements('jabber:x:data', 'reported').next() 149 reported_elt = form_xml.elements('jabber:x:data', 'reported').next()
149 except StopIteration: 150 except StopIteration:
158 headers[name] = (label, type_) 159 headers[name] = (label, type_)
159 160
160 if not headers: 161 if not headers:
161 raise exceptions.DataError("No reported fields (see XEP-0004 §3.4)") 162 raise exceptions.DataError("No reported fields (see XEP-0004 §3.4)")
162 163
163 adv_list = AdvancedListContainer(xmlui, headers=headers, columns=len(headers), parent=xmlui.current_container) 164 xmlui_data = []
164 xmlui.changeContainer(adv_list)
165
166 item_elts = form_xml.elements('jabber:x:data', 'item') 165 item_elts = form_xml.elements('jabber:x:data', 'item')
167 166
168 for item_elt in item_elts: 167 for item_elt in item_elts:
169 for elt in item_elt.elements(): 168 for elt in item_elt.elements():
170 if elt.name != 'field': 169 if elt.name != 'field':
171 log.warning(u"Unexpected tag (%s)" % elt.name) 170 log.warning(u"Unexpected tag (%s)" % elt.name)
172 continue 171 continue
173 field = data_form.Field.fromElement(elt) 172 field = data_form.Field.fromElement(elt)
174 173
175 widget_type, widget_args, widget_kwargs = _dataFormField2XMLUIData(field) 174 xmlui_data.append(_dataFormField2XMLUIData(field))
176 xmlui.addWidget(widget_type, *widget_args, **widget_kwargs) 175
176 return headers, xmlui_data
177
178
179 def XMLUIData2AdvancedList(xmlui, headers, xmlui_data):
180 """Take a raw data form result (not parsed by Wokkel's XEP-0004 implementation) and convert it to an advanced list.
181
182 The raw data form is used because Wokkel doesn't manage result items parsing yet.
183 @param xmlui (XMLUI): the XMLUI where the AdvancedList will be added
184 @param headers (dict{unicode: unicode}): form headers (field labels and types)
185 @param xmlui_data (list[tuple]): list of (widget_type, widget_args, widget_kwargs)
186 @return: the completed XMLUI instance
187 """
188 adv_list = AdvancedListContainer(xmlui, headers=headers, columns=len(headers), parent=xmlui.current_container)
189 xmlui.changeContainer(adv_list)
190
191 for widget_type, widget_args, widget_kwargs in xmlui_data:
192 xmlui.addWidget(widget_type, *widget_args, **widget_kwargs)
177 193
178 return xmlui 194 return xmlui
195
196
197 def dataFormResult2AdvancedList(xmlui, form_xml):
198 """Take a raw data form result (not parsed by Wokkel's XEP-0004 implementation) and convert it to an advanced list.
199
200 The raw data form is used because Wokkel doesn't manage result items parsing yet.
201 @param xmlui (XMLUI): the XMLUI where the AdvancedList will be added
202 @param form_xml (domish.Element): element of the data form
203 @return: the completed XMLUI instance
204 """
205 headers, xmlui_data = dataFormResult2XMLUIData(form_xml)
206 XMLUIData2AdvancedList(xmlui, headers, xmlui_data)
179 207
180 208
181 def dataFormResult2XMLUI(form_elt, session_id=None): 209 def dataFormResult2XMLUI(form_elt, session_id=None):
182 """Take a raw data form (not parsed by XEP-0004) and convert it to a SàT XMLUI. 210 """Take a raw data form (not parsed by XEP-0004) and convert it to a SàT XMLUI.
183 211