changeset 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 8405d622bde0
children ec60682a9d17
files src/tools/xml_tools.py
diffstat 1 files changed, 37 insertions(+), 9 deletions(-) [+]
line wrap: on
line diff
--- a/src/tools/xml_tools.py	Wed Sep 02 13:54:57 2015 +0200
+++ b/src/tools/xml_tools.py	Wed Sep 02 13:56:34 2015 +0200
@@ -135,13 +135,14 @@
     return dataForm2Widgets(form_ui, form, read_only=read_only)
 
 
-def dataFormResult2AdvancedList(xmlui, form_xml):
-    """Take a raw data form result (not parsed by Wokkel's XEP-0004 implementation) and convert it to an advanced list.
+def dataFormResult2XMLUIData(form_xml):
+    """Parse a data form result (not parsed by Wokkel's XEP-0004 implementation).
 
     The raw data form is used because Wokkel doesn't manage result items parsing yet.
-    @param xmlui (XMLUI): the XMLUI where the AdvancedList will be added
     @param form_xml (domish.Element): element of the data form
-    @return: the completed XMLUI instance
+    @return: a couple (headers, result_list):
+        - headers (dict{unicode: unicode}): form headers (field labels and types)
+        - xmlui_data (list[tuple]): list of (widget_type, widget_args, widget_kwargs)
     """
     headers = OrderedDict()
     try:
@@ -160,9 +161,7 @@
     if not headers:
         raise exceptions.DataError("No reported fields (see XEP-0004 §3.4)")
 
-    adv_list = AdvancedListContainer(xmlui, headers=headers, columns=len(headers), parent=xmlui.current_container)
-    xmlui.changeContainer(adv_list)
-
+    xmlui_data = []
     item_elts = form_xml.elements('jabber:x:data', 'item')
 
     for item_elt in item_elts:
@@ -172,12 +171,41 @@
                 continue
             field = data_form.Field.fromElement(elt)
 
-            widget_type, widget_args, widget_kwargs = _dataFormField2XMLUIData(field)
-            xmlui.addWidget(widget_type, *widget_args, **widget_kwargs)
+            xmlui_data.append(_dataFormField2XMLUIData(field))
+
+    return headers, xmlui_data
+
+
+def XMLUIData2AdvancedList(xmlui, headers, xmlui_data):
+    """Take a raw data form result (not parsed by Wokkel's XEP-0004 implementation) and convert it to an advanced list.
+
+    The raw data form is used because Wokkel doesn't manage result items parsing yet.
+    @param xmlui (XMLUI): the XMLUI where the AdvancedList will be added
+    @param headers (dict{unicode: unicode}): form headers (field labels and types)
+    @param xmlui_data (list[tuple]): list of (widget_type, widget_args, widget_kwargs)
+    @return: the completed XMLUI instance
+    """
+    adv_list = AdvancedListContainer(xmlui, headers=headers, columns=len(headers), parent=xmlui.current_container)
+    xmlui.changeContainer(adv_list)
+
+    for widget_type, widget_args, widget_kwargs in xmlui_data:
+        xmlui.addWidget(widget_type, *widget_args, **widget_kwargs)
 
     return xmlui
 
 
+def dataFormResult2AdvancedList(xmlui, form_xml):
+    """Take a raw data form result (not parsed by Wokkel's XEP-0004 implementation) and convert it to an advanced list.
+
+    The raw data form is used because Wokkel doesn't manage result items parsing yet.
+    @param xmlui (XMLUI): the XMLUI where the AdvancedList will be added
+    @param form_xml (domish.Element): element of the data form
+    @return: the completed XMLUI instance
+    """
+    headers, xmlui_data = dataFormResult2XMLUIData(form_xml)
+    XMLUIData2AdvancedList(xmlui, headers, xmlui_data)
+
+
 def dataFormResult2XMLUI(form_elt, session_id=None):
     """Take a raw data form (not parsed by XEP-0004) and convert it to a SàT XMLUI.