comparison src/tools/xml_tools.py @ 803:f100fd8d279f

core, frontends: implementation of AdvancedListContainer first draft + misc: /!\ Urwid SàText must be updated as the new TableContainer is used /!\ - fixed button value in paramsXML2XMLUI - new Urwid SàText TabContainer is used in Primitivus for PairsContainer and AdvancedListContainer - fixed in core's XMLUI AdvanceListContainer
author Goffi <goffi@goffi.org>
date Tue, 04 Feb 2014 18:19:29 +0100
parents 9007bb133009
children 5174657b3378
comparison
equal deleted inserted replaced
802:9007bb133009 803:f100fd8d279f
90 90
91 form_ui.addWidget(widget_type, *widget_args, **widget_kwargs) 91 form_ui.addWidget(widget_type, *widget_args, **widget_kwargs)
92 92
93 return form_ui 93 return form_ui
94 94
95 def dataFormResult2AdvancedList(form_ui, form_xml): 95 def dataFormResult2AdvancedList(xmlui, form_xml):
96 """Take a raw data form (not parsed by XEP-0004) and convert it to an advanced list 96 """Take a raw data form (not parsed by XEP-0004) and convert it to an advanced list
97 raw data form is used because Wokkel doesn't manage result items parsing yet 97 raw data form is used because Wokkel doesn't manage result items parsing yet
98 @param form_ui: the XMLUI where the AdvancedList will be added 98 @param xmlui: the XMLUI where the AdvancedList will be added
99 @param form_xml: domish.Element of the data form 99 @param form_xml: domish.Element of the data form
100 @return: AdvancedList element 100 @return: AdvancedList element
101 """ 101 """
102 headers = {} 102 headers = {}
103 try: 103 try:
114 headers[name] = (label, type_) 114 headers[name] = (label, type_)
115 115
116 if not headers: 116 if not headers:
117 raise exceptions.DataError("No reported fields (see XEP-0004 §3.4)") 117 raise exceptions.DataError("No reported fields (see XEP-0004 §3.4)")
118 118
119 adv_list = AdvancedListContainer(form_ui, headers=headers, columns=len(headers), parent=form_ui.current_container) 119 adv_list = AdvancedListContainer(xmlui, headers=headers, columns=len(headers), parent=xmlui.current_container)
120 form_ui.changeContainer(adv_list) 120 xmlui.changeContainer(adv_list)
121 121
122 item_elts = form_xml.elements('jabber:x:data', 'item') 122 item_elts = form_xml.elements('jabber:x:data', 'item')
123 123
124 for item_elt in item_elts: 124 for item_elt in item_elts:
125 for elt in item_elt.elements(): 125 for elt in item_elt.elements():
127 warning("Unexpected tag (%s)" % elt.name) 127 warning("Unexpected tag (%s)" % elt.name)
128 continue 128 continue
129 field = data_form.Field.fromElement(elt) 129 field = data_form.Field.fromElement(elt)
130 130
131 widget_type, widget_args, widget_kwargs = _dataFormField2XMLUIData(field) 131 widget_type, widget_args, widget_kwargs = _dataFormField2XMLUIData(field)
132 form_ui.addWidget(widget_type, *widget_args, **widget_kwargs) 132 xmlui.addWidget(widget_type, *widget_args, **widget_kwargs)
133 133
134 return form_ui 134 return xmlui
135 135
136 def dataFormResult2XMLUI(form_xml, session_id=None): 136 def dataFormResult2XMLUI(form_xml, session_id=None):
137 """Take a raw data form (not parsed by XEP-0004) and convert it to a SàT XMLUI 137 """Take a raw data form (not parsed by XEP-0004) and convert it to a SàT XMLUI
138 raw data form is used because Wokkel doesn't manage result items parsing yet 138 raw data form is used because Wokkel doesn't manage result items parsing yet
139 @param form_xml: domish.Element of the data form 139 @param form_xml: domish.Element of the data form
140 @return: XMLUI interface 140 @return: XMLUI interface
141 """ 141 """
142 142
143 form_ui = XMLUI("window", "vertical", session_id=session_id) 143 xmlui = XMLUI("window", "vertical", session_id=session_id)
144 dataFormResult2AdvancedList(form_ui, form_xml) 144 dataFormResult2AdvancedList(xmlui, form_xml)
145 return form_ui 145 return xmlui
146 146
147 def XMLUIResult2DataFormResult(xmlui_data): 147 def XMLUIResult2DataFormResult(xmlui_data):
148 """ Extract form data from a XMLUI return 148 """ Extract form data from a XMLUI return
149 @xmlui_data: data returned by frontends for XMLUI form 149 @xmlui_data: data returned by frontends for XMLUI form
150 @return: dict of data usable by Wokkel's dataform 150 @return: dict of data usable by Wokkel's dataform
200 options = _getParamListOptions(param) 200 options = _getParamListOptions(param)
201 widget_kwargs['options'] = options 201 widget_kwargs['options'] = options
202 202
203 if type_ == "button": 203 if type_ == "button":
204 param_ui.addEmpty() 204 param_ui.addEmpty()
205 value = param_label
205 else: 206 else:
206 param_ui.addLabel(param_label or param_name) 207 param_ui.addLabel(param_label or param_name)
207 208
208 if value: 209 if value:
209 widget_kwargs["value"] = value 210 widget_kwargs["value"] = value
247 if not hasattr(self, 'elem'): 248 if not hasattr(self, 'elem'):
248 self.elem = parent.xmlui.doc.createElement(self.type) 249 self.elem = parent.xmlui.doc.createElement(self.type)
249 self.xmlui = xmlui 250 self.xmlui = xmlui
250 if parent is not None: 251 if parent is not None:
251 parent.append(self) 252 parent.append(self)
252 else: 253 self.parent = parent
253 self.parent = parent
254 254
255 def append(self, child): 255 def append(self, child):
256 self.elem.appendChild(child.elem) 256 self.elem.appendChild(child.elem)
257 child.parent = self 257 child.parent = self
258 258
407 items = [] 407 items = []
408 super(AdvancedListContainer, self).__init__(xmlui, parent) 408 super(AdvancedListContainer, self).__init__(xmlui, parent)
409 if columns is None: 409 if columns is None:
410 columns = len(items[0]) 410 columns = len(items[0])
411 self._columns = columns 411 self._columns = columns
412 self._current_column = 0 412 self._item_idx = 0
413 self.current_row = None 413 self.current_row = None
414 if headers: 414 if headers:
415 if len(headers) != self._columns: 415 if len(headers) != self._columns:
416 raise exceptions.DataError(_("Headers lenght doesn't correspond to columns")) 416 raise exceptions.DataError(_("Headers lenght doesn't correspond to columns"))
417 self.addHeaders(headers) 417 self.addHeaders(headers)
418 if items: 418 if items:
419 self.addItems(items) 419 self.addItems(items)
420 self.elem.setAttribute('columns', str(self._columns))
420 421
421 def addHeaders(self, headers): 422 def addHeaders(self, headers):
422 for header in headers: 423 for header in headers:
423 self.addHeader(header) 424 self.addHeader(header)
424 425
425 def addHeader(self, header): 426 def addHeader(self, header):
426 pass # TODO 427 pass # TODO
427 428
428 def addItems(self, items): 429 def addItems(self, items):
429 for item in items: 430 for item in items:
430 self.addItem(item) 431 self.append(item)
431 432
432 def addItem(self, item): 433 def append(self, child):
433 if self._current_column % self._columns == 0: 434 if isinstance(child, RowElement):
435 return super(AdvancedListContainer, self).append(child)
436 if self._item_idx % self._columns == 0:
434 self.current_row = RowElement(self) 437 self.current_row = RowElement(self)
435 self.current_row.append(item) 438 self.current_row.append(child)
439 self._item_idx += 1
436 440
437 def end(self): 441 def end(self):
438 """ Called when we have finished list 442 """ Called when we have finished list
439 change current container to first container parent 443 change current container to first container parent
440 444