Mercurial > libervia-backend
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 |
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 | 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 | 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 | 30 def dataForm2xml(form): |
31 """Take a data form (xep-0004, Wokkel's implementation) and convert it to a SàT xml""" | |
32 | |
103 | 33 form_panel = XMLUI("form", "vertical") |
102 | 34 |
35 if form.instructions: | |
103 | 36 form_panel.addText('\n'.join(form.instructions), 'instructions') |
37 | |
38 form_panel.changeLayout("pairs") | |
39 | |
102 | 40 for field in form.fieldList: |
41 if field.fieldType == 'fixed': | |
42 __field_type = 'text' | |
43 elif field.fieldType == 'text-single': | |
44 __field_type = "string" | |
45 elif field.fieldType == 'text-private': | |
46 __field_type = "password" | |
47 elif field.fieldType == 'list-single': | |
48 __field_type = "list" | |
49 else: | |
50 error (u"FIXME FIXME FIXME: Type [%s] is not managed yet by SàT" % field.fieldType) | |
51 __field_type = "string" | |
35
c45deebb40a5
Wix: Registration form management (not finished yet)
Goffi <goffi@goffi.org>
parents:
33
diff
changeset
|
52 |
103 | 53 if field.label: |
54 form_panel.addLabel(field.label) | |
55 else: | |
56 form_panel.addEmpty() | |
57 | |
58 elem = form_panel.addElement(__field_type, field.var, None, field.value, [option.value for option in field.options]) | |
59 return form_panel.toXml() | |
37
a61beb21d16d
Gateway registration, unregistration & edition
Goffi <goffi@goffi.org>
parents:
35
diff
changeset
|
60 |
102 | 61 def tupleList2dataForm(values): |
62 """convert a list of tuples (name,value) to a wokkel submit data form""" | |
63 form = data_form.Form('submit') | |
64 for value in values: | |
65 field = data_form.Field(var=value[0], value=value[1]) | |
66 form.addField(field) | |
37
a61beb21d16d
Gateway registration, unregistration & edition
Goffi <goffi@goffi.org>
parents:
35
diff
changeset
|
67 |
102 | 68 return form |
103 | 69 |
70 class XMLUI: | |
71 """This class is used to create a user interface (form/window/parameters/etc) using SàT XML""" | |
72 | |
73 def __init__(self, panel_type, layout="vertical"): | |
74 """Init SàT XML Panel | |
75 @param panel_type: one of | |
76 - window (new window) | |
77 - form (formulaire, depend of the frontend, usually a panel with cancel/submit buttons) | |
78 - param (parameters, presentatio depend of the frontend) | |
79 @param layout: disposition of elements, one of: | |
80 - VERTICAL: elements are disposed up to bottom | |
81 - HORIZONTAL: elements are disposed left to right | |
82 - PAIRS: elements come on two aligned columns | |
83 (usually one for a label, the next for the element) | |
84 """ | |
85 if not panel_type in ['window', 'form', 'param']: | |
86 error(_("Unknown panel type [%s]") % panel_type) | |
87 assert(False) | |
88 self.type = panel_type | |
89 impl = minidom.getDOMImplementation() | |
90 | |
91 self.doc = impl.createDocument(None, "sat_xmlui", None) | |
92 top_element = self.doc.documentElement | |
93 top_element.setAttribute("type", panel_type) | |
94 self.currentLayout = self.__createLayout(layout, top_element) | |
95 | |
96 def __del__(self): | |
97 self.doc.unlink() | |
37
a61beb21d16d
Gateway registration, unregistration & edition
Goffi <goffi@goffi.org>
parents:
35
diff
changeset
|
98 |
103 | 99 def __createLayout(self, layout, parent=None): |
100 """Create a layout element | |
101 @param type: layout type (cf init doc) | |
102 @parent: parent element or None | |
103 """ | |
104 if not layout in ['vertical', 'horizontal', 'pairs']: | |
105 error (_("Unknown layout type [%s]") % layout) | |
106 assert (False) | |
107 layout_elt = self.doc.createElement('layout') | |
108 layout_elt.setAttribute('type',layout) | |
109 if parent != None: | |
110 parent.appendChild(layout_elt) | |
111 return layout_elt | |
37
a61beb21d16d
Gateway registration, unregistration & edition
Goffi <goffi@goffi.org>
parents:
35
diff
changeset
|
112 |
103 | 113 def __createElem(self, type, name=None, parent = None): |
114 """Create an element | |
115 @param type: one of | |
116 - empty: empty element (usefull to skip something in a layout, e.g. skip first element in a PAIRS layout) | |
117 - text: text to be displayed in an multi-line area, e.g. instructions | |
118 @param name: name of the element or None | |
119 @param parent: parent element or None | |
120 """ | |
121 elem = self.doc.createElement('elem') | |
122 if name: | |
123 elem.setAttribute('name', name) | |
124 elem.setAttribute('type', type) | |
125 if parent != None: | |
126 parent.appendChild(elem) | |
127 return elem | |
128 | |
129 def changeLayout(self, layout): | |
130 """Change the current layout""" | |
131 self.currentLayout = self.__createLayout(layout, self.doc.documentElement) | |
132 | |
133 | |
134 def addEmpty(self, name=None): | |
135 """Add a multi-lines text""" | |
136 elem = self.__createElem('empty', name, self.currentLayout) | |
137 | |
138 def addText(self, text, name=None): | |
139 """Add a multi-lines text""" | |
140 elem = self.__createElem('text', name, self.currentLayout) | |
141 text = self.doc.createTextNode(text) | |
142 elem.appendChild(text) | |
143 | |
144 def addLabel(self, text, name=None): | |
145 """Add a single line text, mainly useful as label before element""" | |
146 elem = self.__createElem('label', name, self.currentLayout) | |
147 elem.setAttribute('value', text) | |
148 | |
149 def addString(self, name=None, value=None): | |
150 """Add a string box""" | |
151 elem = self.__createElem('string', name, self.currentLayout) | |
152 if value: | |
153 elem.setAttribute('value', value) | |
154 | |
155 def addPassword(self, name=None, value=None): | |
156 """Add a password box""" | |
157 elem = self.__createElem('password', name, self.currentLayout) | |
158 if value: | |
159 elem.setAttribute('value', value) | |
160 | |
161 def addList(self, options, name=None, value=None): | |
162 """Add a list of choices""" | |
163 assert (options) | |
164 elem = self.__createElem('list', name, self.currentLayout) | |
165 self.addOptions(options, elem) | |
166 if value: | |
167 elem.setAttribute('value', value) | |
168 | |
169 def addElement(self, type, name = None, content = None, value = None, options = None): | |
170 """Convenience method to add element, the params correspond to the ones in addSomething methods""" | |
171 if type == 'empty': | |
172 self.addEmpty(name) | |
173 elif type == 'text': | |
174 assert(content) | |
175 self.addText(content, name) | |
176 elif type == 'label': | |
177 assert(value) | |
178 elif type == 'string': | |
179 self.addString(name, value) | |
180 elif type == 'password': | |
181 self.addPassword(name, value) | |
182 elif type == 'list': | |
183 self.addList(options, name, value) | |
184 | |
185 def addOptions(self, options, parent): | |
186 """Add options to a multi-values element (e.g. list) | |
187 @param parent: multi-values element""" | |
188 for option in options: | |
189 opt = self.doc.createElement('option') | |
190 opt.setAttribute('value', option) | |
191 parent.appendChild(opt) | |
192 | |
193 def toXml(self): | |
194 """return the XML representation of the panel""" | |
195 return self.doc.toxml() |