diff tools/xml_tools.py @ 105:d2630fba8dfd

params to XMLUI tools - xml_tools: new paramsXml2xmlUI method, for conversion from params xml to User Interface XML - xml_tools: new addButton method - bridge: new method getParamsUI
author Goffi <goffi@goffi.org>
date Wed, 23 Jun 2010 17:26:21 +0800
parents 5458ac1380cc
children 138d82f64b6f
line wrap: on
line diff
--- a/tools/xml_tools.py	Wed Jun 23 14:55:04 2010 +0800
+++ b/tools/xml_tools.py	Wed Jun 23 17:26:21 2010 +0800
@@ -30,13 +30,12 @@
 def dataForm2xml(form):
     """Take a data form (xep-0004, Wokkel's implementation) and convert it to a SàT xml"""
     
-    form_panel = XMLUI("form", "vertical")
-    
+    form_ui = XMLUI("form", "vertical")
 
     if form.instructions:
-        form_panel.addText('\n'.join(form.instructions), 'instructions')
+        form_ui.addText('\n'.join(form.instructions), 'instructions')
     
-    form_panel.changeLayout("pairs")
+    form_ui.changeLayout("pairs")
     
     for field in form.fieldList:
         if field.fieldType == 'fixed':
@@ -52,12 +51,12 @@
             __field_type = "string"
         
         if field.label:
-            form_panel.addLabel(field.label)
+            form_ui.addLabel(field.label)
         else:
-            form_panel.addEmpty()
+            form_ui.addEmpty()
 
-        elem = form_panel.addElement(__field_type, field.var, None, field.value, [option.value for option in field.options]) 
-    return form_panel.toXml()
+        elem = form_ui.addElement(__field_type, field.var, None, field.value, [option.value for option in field.options]) 
+    return form_ui.toXml()
 
 def tupleList2dataForm(values):
     """convert a list of tuples (name,value) to a wokkel submit data form"""
@@ -68,6 +67,36 @@
 
     return form
 
+def paramsXml2xmlUI(xml):
+    """Convert the xml for parameter to a SàT XML User Interface"""
+    params_doc = minidom.parseString(xml)
+    top = params_doc.documentElement
+    if top.nodeName != 'params':
+        error(_('INTERNAL ERROR: parameters xml not valid'))
+        assert(False)
+    param_ui = XMLUI("param", "tabs")
+    for category in top.getElementsByTagName("category"):
+        name = category.getAttribute('name')
+        if not name:
+            error(_('INTERNAL ERROR: params categories must have a name'))
+            assert(False)
+        param_ui.addCategory(name, 'pairs')
+        for param in category.getElementsByTagName("param"):
+            name = param.getAttribute('name')
+            if not name:
+                error(_('INTERNAL ERROR: params must have a name'))
+                assert(False)
+            type = param.getAttribute('type')
+            value = param.getAttribute('value') or None
+            callback = param.getAttribute('callback') or None
+            param_ui.addLabel(name)
+            param_ui.addElement(name=name, type=type, value=value, callback=callback)
+
+    return param_ui.toXml()
+
+    
+
+
 class XMLUI:
     """This class is used to create a user interface (form/window/parameters/etc) using SàT XML"""
 
@@ -170,8 +199,16 @@
         self.addOptions(options, elem) 
         if value:
             elem.setAttribute('value', value)
+
+    def addButton(self, callback, name):
+        """Add a button
+        @param callback: callback which will be called if button is pressed
+        @param name: name shown on the button"""
+        elem = self.__createElem('button', name, self.currentLayout)
+        elem.setAttribute('callback', callback)
+
     
-    def addElement(self, type, name = None, content = None, value = None, options = None):
+    def addElement(self, type, name = None, content = None, value = None, options = None, callback = None):
         """Convenience method to add element, the params correspond to the ones in addSomething methods"""
         if type == 'empty':
             self.addEmpty(name)
@@ -186,6 +223,9 @@
             self.addPassword(name, value)
         elif type == 'list':
             self.addList(options, name, value)
+        elif type == 'button':
+            assert(callback and name)
+            self.addButton(callback, name)
 
     def addOptions(self, options, parent):
         """Add options to a multi-values element (e.g. list)