annotate tools/xml_tools.py @ 103:6be927a465ed

XMLUI refactoring, step 1
author Goffi <goffi@goffi.org>
date Wed, 23 Jun 2010 00:23:26 +0800
parents 94011f553cd0
children 5458ac1380cc
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
33
b9bb5d8e0cc7 In-band-registration: data form 2 xml conversion
Goffi <goffi@goffi.org>
parents:
diff changeset
1 #!/usr/bin/python
b9bb5d8e0cc7 In-band-registration: data form 2 xml conversion
Goffi <goffi@goffi.org>
parents:
diff changeset
2 # -*- coding: utf-8 -*-
b9bb5d8e0cc7 In-band-registration: data form 2 xml conversion
Goffi <goffi@goffi.org>
parents:
diff changeset
3
b9bb5d8e0cc7 In-band-registration: data form 2 xml conversion
Goffi <goffi@goffi.org>
parents:
diff changeset
4 """
b9bb5d8e0cc7 In-band-registration: data form 2 xml conversion
Goffi <goffi@goffi.org>
parents:
diff changeset
5 SAT: a jabber client
57
a5b5fb5fc9fd updated README and copyright note
Goffi <goffi@goffi.org>
parents: 39
diff changeset
6 Copyright (C) 2009, 2010 Jérôme Poisson (goffi@goffi.org)
33
b9bb5d8e0cc7 In-band-registration: data form 2 xml conversion
Goffi <goffi@goffi.org>
parents:
diff changeset
7
b9bb5d8e0cc7 In-band-registration: data form 2 xml conversion
Goffi <goffi@goffi.org>
parents:
diff changeset
8 This program is free software: you can redistribute it and/or modify
b9bb5d8e0cc7 In-band-registration: data form 2 xml conversion
Goffi <goffi@goffi.org>
parents:
diff changeset
9 it under the terms of the GNU General Public License as published by
b9bb5d8e0cc7 In-band-registration: data form 2 xml conversion
Goffi <goffi@goffi.org>
parents:
diff changeset
10 the Free Software Foundation, either version 3 of the License, or
b9bb5d8e0cc7 In-band-registration: data form 2 xml conversion
Goffi <goffi@goffi.org>
parents:
diff changeset
11 (at your option) any later version.
b9bb5d8e0cc7 In-band-registration: data form 2 xml conversion
Goffi <goffi@goffi.org>
parents:
diff changeset
12
b9bb5d8e0cc7 In-band-registration: data form 2 xml conversion
Goffi <goffi@goffi.org>
parents:
diff changeset
13 This program is distributed in the hope that it will be useful,
b9bb5d8e0cc7 In-band-registration: data form 2 xml conversion
Goffi <goffi@goffi.org>
parents:
diff changeset
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
b9bb5d8e0cc7 In-band-registration: data form 2 xml conversion
Goffi <goffi@goffi.org>
parents:
diff changeset
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
b9bb5d8e0cc7 In-band-registration: data form 2 xml conversion
Goffi <goffi@goffi.org>
parents:
diff changeset
16 GNU General Public License for more details.
b9bb5d8e0cc7 In-band-registration: data form 2 xml conversion
Goffi <goffi@goffi.org>
parents:
diff changeset
17
b9bb5d8e0cc7 In-band-registration: data form 2 xml conversion
Goffi <goffi@goffi.org>
parents:
diff changeset
18 You should have received a copy of the GNU General Public License
b9bb5d8e0cc7 In-band-registration: data form 2 xml conversion
Goffi <goffi@goffi.org>
parents:
diff changeset
19 along with this program. If not, see <http://www.gnu.org/licenses/>.
b9bb5d8e0cc7 In-band-registration: data form 2 xml conversion
Goffi <goffi@goffi.org>
parents:
diff changeset
20 """
b9bb5d8e0cc7 In-band-registration: data form 2 xml conversion
Goffi <goffi@goffi.org>
parents:
diff changeset
21
b9bb5d8e0cc7 In-band-registration: data form 2 xml conversion
Goffi <goffi@goffi.org>
parents:
diff changeset
22 from logging import debug, info, error
b9bb5d8e0cc7 In-band-registration: data form 2 xml conversion
Goffi <goffi@goffi.org>
parents:
diff changeset
23 from xml.dom import minidom
37
a61beb21d16d Gateway registration, unregistration & edition
Goffi <goffi@goffi.org>
parents: 35
diff changeset
24 from wokkel import data_form
33
b9bb5d8e0cc7 In-band-registration: data form 2 xml conversion
Goffi <goffi@goffi.org>
parents:
diff changeset
25 import pdb
b9bb5d8e0cc7 In-band-registration: data form 2 xml conversion
Goffi <goffi@goffi.org>
parents:
diff changeset
26
102
94011f553cd0 misc bugfixes
Goffi <goffi@goffi.org>
parents: 95
diff changeset
27 """This library help manage XML used in SàT (parameters, registration, etc) """
33
b9bb5d8e0cc7 In-band-registration: data form 2 xml conversion
Goffi <goffi@goffi.org>
parents:
diff changeset
28
b9bb5d8e0cc7 In-band-registration: data form 2 xml conversion
Goffi <goffi@goffi.org>
parents:
diff changeset
29
102
94011f553cd0 misc bugfixes
Goffi <goffi@goffi.org>
parents: 95
diff changeset
30 def dataForm2xml(form):
94011f553cd0 misc bugfixes
Goffi <goffi@goffi.org>
parents: 95
diff changeset
31 """Take a data form (xep-0004, Wokkel's implementation) and convert it to a SàT xml"""
94011f553cd0 misc bugfixes
Goffi <goffi@goffi.org>
parents: 95
diff changeset
32
103
6be927a465ed XMLUI refactoring, step 1
Goffi <goffi@goffi.org>
parents: 102
diff changeset
33 form_panel = XMLUI("form", "vertical")
102
94011f553cd0 misc bugfixes
Goffi <goffi@goffi.org>
parents: 95
diff changeset
34
94011f553cd0 misc bugfixes
Goffi <goffi@goffi.org>
parents: 95
diff changeset
35 if form.instructions:
103
6be927a465ed XMLUI refactoring, step 1
Goffi <goffi@goffi.org>
parents: 102
diff changeset
36 form_panel.addText('\n'.join(form.instructions), 'instructions')
6be927a465ed XMLUI refactoring, step 1
Goffi <goffi@goffi.org>
parents: 102
diff changeset
37
6be927a465ed XMLUI refactoring, step 1
Goffi <goffi@goffi.org>
parents: 102
diff changeset
38 form_panel.changeLayout("pairs")
6be927a465ed XMLUI refactoring, step 1
Goffi <goffi@goffi.org>
parents: 102
diff changeset
39
102
94011f553cd0 misc bugfixes
Goffi <goffi@goffi.org>
parents: 95
diff changeset
40 for field in form.fieldList:
94011f553cd0 misc bugfixes
Goffi <goffi@goffi.org>
parents: 95
diff changeset
41 if field.fieldType == 'fixed':
94011f553cd0 misc bugfixes
Goffi <goffi@goffi.org>
parents: 95
diff changeset
42 __field_type = 'text'
94011f553cd0 misc bugfixes
Goffi <goffi@goffi.org>
parents: 95
diff changeset
43 elif field.fieldType == 'text-single':
94011f553cd0 misc bugfixes
Goffi <goffi@goffi.org>
parents: 95
diff changeset
44 __field_type = "string"
94011f553cd0 misc bugfixes
Goffi <goffi@goffi.org>
parents: 95
diff changeset
45 elif field.fieldType == 'text-private':
94011f553cd0 misc bugfixes
Goffi <goffi@goffi.org>
parents: 95
diff changeset
46 __field_type = "password"
94011f553cd0 misc bugfixes
Goffi <goffi@goffi.org>
parents: 95
diff changeset
47 elif field.fieldType == 'list-single':
94011f553cd0 misc bugfixes
Goffi <goffi@goffi.org>
parents: 95
diff changeset
48 __field_type = "list"
94011f553cd0 misc bugfixes
Goffi <goffi@goffi.org>
parents: 95
diff changeset
49 else:
94011f553cd0 misc bugfixes
Goffi <goffi@goffi.org>
parents: 95
diff changeset
50 error (u"FIXME FIXME FIXME: Type [%s] is not managed yet by SàT" % field.fieldType)
94011f553cd0 misc bugfixes
Goffi <goffi@goffi.org>
parents: 95
diff changeset
51 __field_type = "string"
35
c45deebb40a5 Wix: Registration form management (not finished yet)
Goffi <goffi@goffi.org>
parents: 33
diff changeset
52
103
6be927a465ed XMLUI refactoring, step 1
Goffi <goffi@goffi.org>
parents: 102
diff changeset
53 if field.label:
6be927a465ed XMLUI refactoring, step 1
Goffi <goffi@goffi.org>
parents: 102
diff changeset
54 form_panel.addLabel(field.label)
6be927a465ed XMLUI refactoring, step 1
Goffi <goffi@goffi.org>
parents: 102
diff changeset
55 else:
6be927a465ed XMLUI refactoring, step 1
Goffi <goffi@goffi.org>
parents: 102
diff changeset
56 form_panel.addEmpty()
6be927a465ed XMLUI refactoring, step 1
Goffi <goffi@goffi.org>
parents: 102
diff changeset
57
6be927a465ed XMLUI refactoring, step 1
Goffi <goffi@goffi.org>
parents: 102
diff changeset
58 elem = form_panel.addElement(__field_type, field.var, None, field.value, [option.value for option in field.options])
6be927a465ed XMLUI refactoring, step 1
Goffi <goffi@goffi.org>
parents: 102
diff changeset
59 return form_panel.toXml()
37
a61beb21d16d Gateway registration, unregistration & edition
Goffi <goffi@goffi.org>
parents: 35
diff changeset
60
102
94011f553cd0 misc bugfixes
Goffi <goffi@goffi.org>
parents: 95
diff changeset
61 def tupleList2dataForm(values):
94011f553cd0 misc bugfixes
Goffi <goffi@goffi.org>
parents: 95
diff changeset
62 """convert a list of tuples (name,value) to a wokkel submit data form"""
94011f553cd0 misc bugfixes
Goffi <goffi@goffi.org>
parents: 95
diff changeset
63 form = data_form.Form('submit')
94011f553cd0 misc bugfixes
Goffi <goffi@goffi.org>
parents: 95
diff changeset
64 for value in values:
94011f553cd0 misc bugfixes
Goffi <goffi@goffi.org>
parents: 95
diff changeset
65 field = data_form.Field(var=value[0], value=value[1])
94011f553cd0 misc bugfixes
Goffi <goffi@goffi.org>
parents: 95
diff changeset
66 form.addField(field)
37
a61beb21d16d Gateway registration, unregistration & edition
Goffi <goffi@goffi.org>
parents: 35
diff changeset
67
102
94011f553cd0 misc bugfixes
Goffi <goffi@goffi.org>
parents: 95
diff changeset
68 return form
103
6be927a465ed XMLUI refactoring, step 1
Goffi <goffi@goffi.org>
parents: 102
diff changeset
69
6be927a465ed XMLUI refactoring, step 1
Goffi <goffi@goffi.org>
parents: 102
diff changeset
70 class XMLUI:
6be927a465ed XMLUI refactoring, step 1
Goffi <goffi@goffi.org>
parents: 102
diff changeset
71 """This class is used to create a user interface (form/window/parameters/etc) using SàT XML"""
6be927a465ed XMLUI refactoring, step 1
Goffi <goffi@goffi.org>
parents: 102
diff changeset
72
6be927a465ed XMLUI refactoring, step 1
Goffi <goffi@goffi.org>
parents: 102
diff changeset
73 def __init__(self, panel_type, layout="vertical"):
6be927a465ed XMLUI refactoring, step 1
Goffi <goffi@goffi.org>
parents: 102
diff changeset
74 """Init SàT XML Panel
6be927a465ed XMLUI refactoring, step 1
Goffi <goffi@goffi.org>
parents: 102
diff changeset
75 @param panel_type: one of
6be927a465ed XMLUI refactoring, step 1
Goffi <goffi@goffi.org>
parents: 102
diff changeset
76 - window (new window)
6be927a465ed XMLUI refactoring, step 1
Goffi <goffi@goffi.org>
parents: 102
diff changeset
77 - form (formulaire, depend of the frontend, usually a panel with cancel/submit buttons)
6be927a465ed XMLUI refactoring, step 1
Goffi <goffi@goffi.org>
parents: 102
diff changeset
78 - param (parameters, presentatio depend of the frontend)
6be927a465ed XMLUI refactoring, step 1
Goffi <goffi@goffi.org>
parents: 102
diff changeset
79 @param layout: disposition of elements, one of:
6be927a465ed XMLUI refactoring, step 1
Goffi <goffi@goffi.org>
parents: 102
diff changeset
80 - VERTICAL: elements are disposed up to bottom
6be927a465ed XMLUI refactoring, step 1
Goffi <goffi@goffi.org>
parents: 102
diff changeset
81 - HORIZONTAL: elements are disposed left to right
6be927a465ed XMLUI refactoring, step 1
Goffi <goffi@goffi.org>
parents: 102
diff changeset
82 - PAIRS: elements come on two aligned columns
6be927a465ed XMLUI refactoring, step 1
Goffi <goffi@goffi.org>
parents: 102
diff changeset
83 (usually one for a label, the next for the element)
6be927a465ed XMLUI refactoring, step 1
Goffi <goffi@goffi.org>
parents: 102
diff changeset
84 """
6be927a465ed XMLUI refactoring, step 1
Goffi <goffi@goffi.org>
parents: 102
diff changeset
85 if not panel_type in ['window', 'form', 'param']:
6be927a465ed XMLUI refactoring, step 1
Goffi <goffi@goffi.org>
parents: 102
diff changeset
86 error(_("Unknown panel type [%s]") % panel_type)
6be927a465ed XMLUI refactoring, step 1
Goffi <goffi@goffi.org>
parents: 102
diff changeset
87 assert(False)
6be927a465ed XMLUI refactoring, step 1
Goffi <goffi@goffi.org>
parents: 102
diff changeset
88 self.type = panel_type
6be927a465ed XMLUI refactoring, step 1
Goffi <goffi@goffi.org>
parents: 102
diff changeset
89 impl = minidom.getDOMImplementation()
6be927a465ed XMLUI refactoring, step 1
Goffi <goffi@goffi.org>
parents: 102
diff changeset
90
6be927a465ed XMLUI refactoring, step 1
Goffi <goffi@goffi.org>
parents: 102
diff changeset
91 self.doc = impl.createDocument(None, "sat_xmlui", None)
6be927a465ed XMLUI refactoring, step 1
Goffi <goffi@goffi.org>
parents: 102
diff changeset
92 top_element = self.doc.documentElement
6be927a465ed XMLUI refactoring, step 1
Goffi <goffi@goffi.org>
parents: 102
diff changeset
93 top_element.setAttribute("type", panel_type)
6be927a465ed XMLUI refactoring, step 1
Goffi <goffi@goffi.org>
parents: 102
diff changeset
94 self.currentLayout = self.__createLayout(layout, top_element)
6be927a465ed XMLUI refactoring, step 1
Goffi <goffi@goffi.org>
parents: 102
diff changeset
95
6be927a465ed XMLUI refactoring, step 1
Goffi <goffi@goffi.org>
parents: 102
diff changeset
96 def __del__(self):
6be927a465ed XMLUI refactoring, step 1
Goffi <goffi@goffi.org>
parents: 102
diff changeset
97 self.doc.unlink()
37
a61beb21d16d Gateway registration, unregistration & edition
Goffi <goffi@goffi.org>
parents: 35
diff changeset
98
103
6be927a465ed XMLUI refactoring, step 1
Goffi <goffi@goffi.org>
parents: 102
diff changeset
99 def __createLayout(self, layout, parent=None):
6be927a465ed XMLUI refactoring, step 1
Goffi <goffi@goffi.org>
parents: 102
diff changeset
100 """Create a layout element
6be927a465ed XMLUI refactoring, step 1
Goffi <goffi@goffi.org>
parents: 102
diff changeset
101 @param type: layout type (cf init doc)
6be927a465ed XMLUI refactoring, step 1
Goffi <goffi@goffi.org>
parents: 102
diff changeset
102 @parent: parent element or None
6be927a465ed XMLUI refactoring, step 1
Goffi <goffi@goffi.org>
parents: 102
diff changeset
103 """
6be927a465ed XMLUI refactoring, step 1
Goffi <goffi@goffi.org>
parents: 102
diff changeset
104 if not layout in ['vertical', 'horizontal', 'pairs']:
6be927a465ed XMLUI refactoring, step 1
Goffi <goffi@goffi.org>
parents: 102
diff changeset
105 error (_("Unknown layout type [%s]") % layout)
6be927a465ed XMLUI refactoring, step 1
Goffi <goffi@goffi.org>
parents: 102
diff changeset
106 assert (False)
6be927a465ed XMLUI refactoring, step 1
Goffi <goffi@goffi.org>
parents: 102
diff changeset
107 layout_elt = self.doc.createElement('layout')
6be927a465ed XMLUI refactoring, step 1
Goffi <goffi@goffi.org>
parents: 102
diff changeset
108 layout_elt.setAttribute('type',layout)
6be927a465ed XMLUI refactoring, step 1
Goffi <goffi@goffi.org>
parents: 102
diff changeset
109 if parent != None:
6be927a465ed XMLUI refactoring, step 1
Goffi <goffi@goffi.org>
parents: 102
diff changeset
110 parent.appendChild(layout_elt)
6be927a465ed XMLUI refactoring, step 1
Goffi <goffi@goffi.org>
parents: 102
diff changeset
111 return layout_elt
37
a61beb21d16d Gateway registration, unregistration & edition
Goffi <goffi@goffi.org>
parents: 35
diff changeset
112
103
6be927a465ed XMLUI refactoring, step 1
Goffi <goffi@goffi.org>
parents: 102
diff changeset
113 def __createElem(self, type, name=None, parent = None):
6be927a465ed XMLUI refactoring, step 1
Goffi <goffi@goffi.org>
parents: 102
diff changeset
114 """Create an element
6be927a465ed XMLUI refactoring, step 1
Goffi <goffi@goffi.org>
parents: 102
diff changeset
115 @param type: one of
6be927a465ed XMLUI refactoring, step 1
Goffi <goffi@goffi.org>
parents: 102
diff changeset
116 - empty: empty element (usefull to skip something in a layout, e.g. skip first element in a PAIRS layout)
6be927a465ed XMLUI refactoring, step 1
Goffi <goffi@goffi.org>
parents: 102
diff changeset
117 - text: text to be displayed in an multi-line area, e.g. instructions
6be927a465ed XMLUI refactoring, step 1
Goffi <goffi@goffi.org>
parents: 102
diff changeset
118 @param name: name of the element or None
6be927a465ed XMLUI refactoring, step 1
Goffi <goffi@goffi.org>
parents: 102
diff changeset
119 @param parent: parent element or None
6be927a465ed XMLUI refactoring, step 1
Goffi <goffi@goffi.org>
parents: 102
diff changeset
120 """
6be927a465ed XMLUI refactoring, step 1
Goffi <goffi@goffi.org>
parents: 102
diff changeset
121 elem = self.doc.createElement('elem')
6be927a465ed XMLUI refactoring, step 1
Goffi <goffi@goffi.org>
parents: 102
diff changeset
122 if name:
6be927a465ed XMLUI refactoring, step 1
Goffi <goffi@goffi.org>
parents: 102
diff changeset
123 elem.setAttribute('name', name)
6be927a465ed XMLUI refactoring, step 1
Goffi <goffi@goffi.org>
parents: 102
diff changeset
124 elem.setAttribute('type', type)
6be927a465ed XMLUI refactoring, step 1
Goffi <goffi@goffi.org>
parents: 102
diff changeset
125 if parent != None:
6be927a465ed XMLUI refactoring, step 1
Goffi <goffi@goffi.org>
parents: 102
diff changeset
126 parent.appendChild(elem)
6be927a465ed XMLUI refactoring, step 1
Goffi <goffi@goffi.org>
parents: 102
diff changeset
127 return elem
6be927a465ed XMLUI refactoring, step 1
Goffi <goffi@goffi.org>
parents: 102
diff changeset
128
6be927a465ed XMLUI refactoring, step 1
Goffi <goffi@goffi.org>
parents: 102
diff changeset
129 def changeLayout(self, layout):
6be927a465ed XMLUI refactoring, step 1
Goffi <goffi@goffi.org>
parents: 102
diff changeset
130 """Change the current layout"""
6be927a465ed XMLUI refactoring, step 1
Goffi <goffi@goffi.org>
parents: 102
diff changeset
131 self.currentLayout = self.__createLayout(layout, self.doc.documentElement)
6be927a465ed XMLUI refactoring, step 1
Goffi <goffi@goffi.org>
parents: 102
diff changeset
132
6be927a465ed XMLUI refactoring, step 1
Goffi <goffi@goffi.org>
parents: 102
diff changeset
133
6be927a465ed XMLUI refactoring, step 1
Goffi <goffi@goffi.org>
parents: 102
diff changeset
134 def addEmpty(self, name=None):
6be927a465ed XMLUI refactoring, step 1
Goffi <goffi@goffi.org>
parents: 102
diff changeset
135 """Add a multi-lines text"""
6be927a465ed XMLUI refactoring, step 1
Goffi <goffi@goffi.org>
parents: 102
diff changeset
136 elem = self.__createElem('empty', name, self.currentLayout)
6be927a465ed XMLUI refactoring, step 1
Goffi <goffi@goffi.org>
parents: 102
diff changeset
137
6be927a465ed XMLUI refactoring, step 1
Goffi <goffi@goffi.org>
parents: 102
diff changeset
138 def addText(self, text, name=None):
6be927a465ed XMLUI refactoring, step 1
Goffi <goffi@goffi.org>
parents: 102
diff changeset
139 """Add a multi-lines text"""
6be927a465ed XMLUI refactoring, step 1
Goffi <goffi@goffi.org>
parents: 102
diff changeset
140 elem = self.__createElem('text', name, self.currentLayout)
6be927a465ed XMLUI refactoring, step 1
Goffi <goffi@goffi.org>
parents: 102
diff changeset
141 text = self.doc.createTextNode(text)
6be927a465ed XMLUI refactoring, step 1
Goffi <goffi@goffi.org>
parents: 102
diff changeset
142 elem.appendChild(text)
6be927a465ed XMLUI refactoring, step 1
Goffi <goffi@goffi.org>
parents: 102
diff changeset
143
6be927a465ed XMLUI refactoring, step 1
Goffi <goffi@goffi.org>
parents: 102
diff changeset
144 def addLabel(self, text, name=None):
6be927a465ed XMLUI refactoring, step 1
Goffi <goffi@goffi.org>
parents: 102
diff changeset
145 """Add a single line text, mainly useful as label before element"""
6be927a465ed XMLUI refactoring, step 1
Goffi <goffi@goffi.org>
parents: 102
diff changeset
146 elem = self.__createElem('label', name, self.currentLayout)
6be927a465ed XMLUI refactoring, step 1
Goffi <goffi@goffi.org>
parents: 102
diff changeset
147 elem.setAttribute('value', text)
6be927a465ed XMLUI refactoring, step 1
Goffi <goffi@goffi.org>
parents: 102
diff changeset
148
6be927a465ed XMLUI refactoring, step 1
Goffi <goffi@goffi.org>
parents: 102
diff changeset
149 def addString(self, name=None, value=None):
6be927a465ed XMLUI refactoring, step 1
Goffi <goffi@goffi.org>
parents: 102
diff changeset
150 """Add a string box"""
6be927a465ed XMLUI refactoring, step 1
Goffi <goffi@goffi.org>
parents: 102
diff changeset
151 elem = self.__createElem('string', name, self.currentLayout)
6be927a465ed XMLUI refactoring, step 1
Goffi <goffi@goffi.org>
parents: 102
diff changeset
152 if value:
6be927a465ed XMLUI refactoring, step 1
Goffi <goffi@goffi.org>
parents: 102
diff changeset
153 elem.setAttribute('value', value)
6be927a465ed XMLUI refactoring, step 1
Goffi <goffi@goffi.org>
parents: 102
diff changeset
154
6be927a465ed XMLUI refactoring, step 1
Goffi <goffi@goffi.org>
parents: 102
diff changeset
155 def addPassword(self, name=None, value=None):
6be927a465ed XMLUI refactoring, step 1
Goffi <goffi@goffi.org>
parents: 102
diff changeset
156 """Add a password box"""
6be927a465ed XMLUI refactoring, step 1
Goffi <goffi@goffi.org>
parents: 102
diff changeset
157 elem = self.__createElem('password', name, self.currentLayout)
6be927a465ed XMLUI refactoring, step 1
Goffi <goffi@goffi.org>
parents: 102
diff changeset
158 if value:
6be927a465ed XMLUI refactoring, step 1
Goffi <goffi@goffi.org>
parents: 102
diff changeset
159 elem.setAttribute('value', value)
6be927a465ed XMLUI refactoring, step 1
Goffi <goffi@goffi.org>
parents: 102
diff changeset
160
6be927a465ed XMLUI refactoring, step 1
Goffi <goffi@goffi.org>
parents: 102
diff changeset
161 def addList(self, options, name=None, value=None):
6be927a465ed XMLUI refactoring, step 1
Goffi <goffi@goffi.org>
parents: 102
diff changeset
162 """Add a list of choices"""
6be927a465ed XMLUI refactoring, step 1
Goffi <goffi@goffi.org>
parents: 102
diff changeset
163 assert (options)
6be927a465ed XMLUI refactoring, step 1
Goffi <goffi@goffi.org>
parents: 102
diff changeset
164 elem = self.__createElem('list', name, self.currentLayout)
6be927a465ed XMLUI refactoring, step 1
Goffi <goffi@goffi.org>
parents: 102
diff changeset
165 self.addOptions(options, elem)
6be927a465ed XMLUI refactoring, step 1
Goffi <goffi@goffi.org>
parents: 102
diff changeset
166 if value:
6be927a465ed XMLUI refactoring, step 1
Goffi <goffi@goffi.org>
parents: 102
diff changeset
167 elem.setAttribute('value', value)
6be927a465ed XMLUI refactoring, step 1
Goffi <goffi@goffi.org>
parents: 102
diff changeset
168
6be927a465ed XMLUI refactoring, step 1
Goffi <goffi@goffi.org>
parents: 102
diff changeset
169 def addElement(self, type, name = None, content = None, value = None, options = None):
6be927a465ed XMLUI refactoring, step 1
Goffi <goffi@goffi.org>
parents: 102
diff changeset
170 """Convenience method to add element, the params correspond to the ones in addSomething methods"""
6be927a465ed XMLUI refactoring, step 1
Goffi <goffi@goffi.org>
parents: 102
diff changeset
171 if type == 'empty':
6be927a465ed XMLUI refactoring, step 1
Goffi <goffi@goffi.org>
parents: 102
diff changeset
172 self.addEmpty(name)
6be927a465ed XMLUI refactoring, step 1
Goffi <goffi@goffi.org>
parents: 102
diff changeset
173 elif type == 'text':
6be927a465ed XMLUI refactoring, step 1
Goffi <goffi@goffi.org>
parents: 102
diff changeset
174 assert(content)
6be927a465ed XMLUI refactoring, step 1
Goffi <goffi@goffi.org>
parents: 102
diff changeset
175 self.addText(content, name)
6be927a465ed XMLUI refactoring, step 1
Goffi <goffi@goffi.org>
parents: 102
diff changeset
176 elif type == 'label':
6be927a465ed XMLUI refactoring, step 1
Goffi <goffi@goffi.org>
parents: 102
diff changeset
177 assert(value)
6be927a465ed XMLUI refactoring, step 1
Goffi <goffi@goffi.org>
parents: 102
diff changeset
178 elif type == 'string':
6be927a465ed XMLUI refactoring, step 1
Goffi <goffi@goffi.org>
parents: 102
diff changeset
179 self.addString(name, value)
6be927a465ed XMLUI refactoring, step 1
Goffi <goffi@goffi.org>
parents: 102
diff changeset
180 elif type == 'password':
6be927a465ed XMLUI refactoring, step 1
Goffi <goffi@goffi.org>
parents: 102
diff changeset
181 self.addPassword(name, value)
6be927a465ed XMLUI refactoring, step 1
Goffi <goffi@goffi.org>
parents: 102
diff changeset
182 elif type == 'list':
6be927a465ed XMLUI refactoring, step 1
Goffi <goffi@goffi.org>
parents: 102
diff changeset
183 self.addList(options, name, value)
6be927a465ed XMLUI refactoring, step 1
Goffi <goffi@goffi.org>
parents: 102
diff changeset
184
6be927a465ed XMLUI refactoring, step 1
Goffi <goffi@goffi.org>
parents: 102
diff changeset
185 def addOptions(self, options, parent):
6be927a465ed XMLUI refactoring, step 1
Goffi <goffi@goffi.org>
parents: 102
diff changeset
186 """Add options to a multi-values element (e.g. list)
6be927a465ed XMLUI refactoring, step 1
Goffi <goffi@goffi.org>
parents: 102
diff changeset
187 @param parent: multi-values element"""
6be927a465ed XMLUI refactoring, step 1
Goffi <goffi@goffi.org>
parents: 102
diff changeset
188 for option in options:
6be927a465ed XMLUI refactoring, step 1
Goffi <goffi@goffi.org>
parents: 102
diff changeset
189 opt = self.doc.createElement('option')
6be927a465ed XMLUI refactoring, step 1
Goffi <goffi@goffi.org>
parents: 102
diff changeset
190 opt.setAttribute('value', option)
6be927a465ed XMLUI refactoring, step 1
Goffi <goffi@goffi.org>
parents: 102
diff changeset
191 parent.appendChild(opt)
6be927a465ed XMLUI refactoring, step 1
Goffi <goffi@goffi.org>
parents: 102
diff changeset
192
6be927a465ed XMLUI refactoring, step 1
Goffi <goffi@goffi.org>
parents: 102
diff changeset
193 def toXml(self):
6be927a465ed XMLUI refactoring, step 1
Goffi <goffi@goffi.org>
parents: 102
diff changeset
194 """return the XML representation of the panel"""
6be927a465ed XMLUI refactoring, step 1
Goffi <goffi@goffi.org>
parents: 102
diff changeset
195 return self.doc.toxml()