Mercurial > libervia-backend
annotate tools/xml_tools.py @ 107:5ae370c71803
CS: message sending is now working
- xmltools/xmlui: buttons can now send fields of the ui when used
- xmltools/xmlui: new textbox element, to write a large text (used for messages in CS plugin)
- xmltools/xmlui: list have now an attribute multi for selecting several options
- xmltools/xmlui: window title can now be specified in the xml (attribute title)
- CS_plugin: message sending interface & management
author | Goffi <goffi@goffi.org> |
---|---|
date | Mon, 28 Jun 2010 15:18:59 +0800 |
parents | 138d82f64b6f |
children | 7c00c4b0a5c2 |
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 | |
105 | 33 form_ui = XMLUI("form", "vertical") |
104 | 34 |
102 | 35 if form.instructions: |
105 | 36 form_ui.addText('\n'.join(form.instructions), 'instructions') |
103 | 37 |
105 | 38 form_ui.changeLayout("pairs") |
103 | 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: |
105 | 54 form_ui.addLabel(field.label) |
103 | 55 else: |
105 | 56 form_ui.addEmpty() |
103 | 57 |
105 | 58 elem = form_ui.addElement(__field_type, field.var, None, field.value, [option.value for option in field.options]) |
59 return form_ui.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 |
105 | 70 def paramsXml2xmlUI(xml): |
71 """Convert the xml for parameter to a SàT XML User Interface""" | |
72 params_doc = minidom.parseString(xml) | |
73 top = params_doc.documentElement | |
74 if top.nodeName != 'params': | |
75 error(_('INTERNAL ERROR: parameters xml not valid')) | |
76 assert(False) | |
77 param_ui = XMLUI("param", "tabs") | |
78 for category in top.getElementsByTagName("category"): | |
79 name = category.getAttribute('name') | |
80 if not name: | |
81 error(_('INTERNAL ERROR: params categories must have a name')) | |
82 assert(False) | |
83 param_ui.addCategory(name, 'pairs') | |
84 for param in category.getElementsByTagName("param"): | |
85 name = param.getAttribute('name') | |
86 if not name: | |
87 error(_('INTERNAL ERROR: params must have a name')) | |
88 assert(False) | |
89 type = param.getAttribute('type') | |
90 value = param.getAttribute('value') or None | |
106 | 91 callback_id = param.getAttribute('callback_id') or None |
92 if type == "button": | |
93 param_ui.addEmpty() | |
94 else: | |
95 param_ui.addLabel(name) | |
96 param_ui.addElement(name=name, type=type, value=value, callback_id=callback_id) | |
105 | 97 |
98 return param_ui.toXml() | |
99 | |
100 | |
101 | |
102 | |
103 | 103 class XMLUI: |
104 """This class is used to create a user interface (form/window/parameters/etc) using SàT XML""" | |
105 | |
107 | 106 def __init__(self, panel_type, layout="vertical", title=None): |
103 | 107 """Init SàT XML Panel |
108 @param panel_type: one of | |
109 - window (new window) | |
110 - form (formulaire, depend of the frontend, usually a panel with cancel/submit buttons) | |
111 - param (parameters, presentatio depend of the frontend) | |
112 @param layout: disposition of elements, one of: | |
107 | 113 - vertical: elements are disposed up to bottom |
114 - horizontal: elements are disposed left to right | |
115 - pairs: elements come on two aligned columns | |
103 | 116 (usually one for a label, the next for the element) |
107 | 117 - tabs: elemens are in categories with tabs (notebook) |
118 @param title: title or default if None | |
103 | 119 """ |
120 if not panel_type in ['window', 'form', 'param']: | |
121 error(_("Unknown panel type [%s]") % panel_type) | |
122 assert(False) | |
123 self.type = panel_type | |
124 impl = minidom.getDOMImplementation() | |
125 | |
126 self.doc = impl.createDocument(None, "sat_xmlui", None) | |
127 top_element = self.doc.documentElement | |
128 top_element.setAttribute("type", panel_type) | |
107 | 129 if title: |
130 top_element.setAttribute("title", title) | |
104 | 131 self.parentTabsLayout = None #used only we have 'tabs' layout |
132 self.currentCategory = None #used only we have 'tabs' layout | |
133 self.changeLayout(layout) | |
103 | 134 |
135 def __del__(self): | |
136 self.doc.unlink() | |
37
a61beb21d16d
Gateway registration, unregistration & edition
Goffi <goffi@goffi.org>
parents:
35
diff
changeset
|
137 |
103 | 138 def __createLayout(self, layout, parent=None): |
139 """Create a layout element | |
140 @param type: layout type (cf init doc) | |
141 @parent: parent element or None | |
142 """ | |
104 | 143 if not layout in ['vertical', 'horizontal', 'pairs', 'tabs']: |
103 | 144 error (_("Unknown layout type [%s]") % layout) |
145 assert (False) | |
146 layout_elt = self.doc.createElement('layout') | |
147 layout_elt.setAttribute('type',layout) | |
148 if parent != None: | |
149 parent.appendChild(layout_elt) | |
150 return layout_elt | |
37
a61beb21d16d
Gateway registration, unregistration & edition
Goffi <goffi@goffi.org>
parents:
35
diff
changeset
|
151 |
103 | 152 def __createElem(self, type, name=None, parent = None): |
153 """Create an element | |
154 @param type: one of | |
155 - empty: empty element (usefull to skip something in a layout, e.g. skip first element in a PAIRS layout) | |
156 - text: text to be displayed in an multi-line area, e.g. instructions | |
157 @param name: name of the element or None | |
158 @param parent: parent element or None | |
159 """ | |
160 elem = self.doc.createElement('elem') | |
161 if name: | |
162 elem.setAttribute('name', name) | |
163 elem.setAttribute('type', type) | |
164 if parent != None: | |
165 parent.appendChild(elem) | |
166 return elem | |
167 | |
168 def changeLayout(self, layout): | |
169 """Change the current layout""" | |
104 | 170 self.currentLayout = self.__createLayout(layout, self.currentCategory if self.currentCategory else self.doc.documentElement) |
171 if layout == "tabs": | |
172 self.parentTabsLayout = self.currentLayout | |
103 | 173 |
174 | |
175 def addEmpty(self, name=None): | |
176 """Add a multi-lines text""" | |
177 elem = self.__createElem('empty', name, self.currentLayout) | |
178 | |
179 def addText(self, text, name=None): | |
180 """Add a multi-lines text""" | |
181 elem = self.__createElem('text', name, self.currentLayout) | |
182 text = self.doc.createTextNode(text) | |
183 elem.appendChild(text) | |
184 | |
185 def addLabel(self, text, name=None): | |
186 """Add a single line text, mainly useful as label before element""" | |
187 elem = self.__createElem('label', name, self.currentLayout) | |
188 elem.setAttribute('value', text) | |
189 | |
190 def addString(self, name=None, value=None): | |
191 """Add a string box""" | |
192 elem = self.__createElem('string', name, self.currentLayout) | |
193 if value: | |
194 elem.setAttribute('value', value) | |
195 | |
196 def addPassword(self, name=None, value=None): | |
197 """Add a password box""" | |
198 elem = self.__createElem('password', name, self.currentLayout) | |
199 if value: | |
200 elem.setAttribute('value', value) | |
201 | |
107 | 202 def addTextBox(self, name=None, value=None): |
203 """Add a string box""" | |
204 elem = self.__createElem('textbox', name, self.currentLayout) | |
205 if value: | |
206 elem.setAttribute('value', value) | |
207 | |
208 def addList(self, options, name=None, value=None, style=set()): | |
103 | 209 """Add a list of choices""" |
107 | 210 styles = set(style) |
103 | 211 assert (options) |
107 | 212 assert (styles.issubset(['multi'])) |
103 | 213 elem = self.__createElem('list', name, self.currentLayout) |
214 self.addOptions(options, elem) | |
215 if value: | |
216 elem.setAttribute('value', value) | |
107 | 217 for style in styles: |
218 elem.setAttribute(style, 'yes') | |
105 | 219 |
107 | 220 def addButton(self, callback_id, name, value, fields_back=[]): |
105 | 221 """Add a button |
222 @param callback: callback which will be called if button is pressed | |
106 | 223 @param name: name |
107 | 224 @param value: label of the button |
225 @fields_back: list of names of field to give back when pushing the button""" | |
105 | 226 elem = self.__createElem('button', name, self.currentLayout) |
106 | 227 elem.setAttribute('callback_id', callback_id) |
107 | 228 elem.setAttribute('value', value) |
229 for field in fields_back: | |
230 fback_el = self.doc.createElement('field_back') | |
231 fback_el.setAttribute('name', field) | |
232 elem.appendChild(fback_el) | |
233 | |
105 | 234 |
103 | 235 |
106 | 236 def addElement(self, type, name = None, content = None, value = None, options = None, callback_id = None): |
103 | 237 """Convenience method to add element, the params correspond to the ones in addSomething methods""" |
238 if type == 'empty': | |
239 self.addEmpty(name) | |
240 elif type == 'text': | |
241 assert(content) | |
242 self.addText(content, name) | |
243 elif type == 'label': | |
244 assert(value) | |
245 elif type == 'string': | |
246 self.addString(name, value) | |
247 elif type == 'password': | |
248 self.addPassword(name, value) | |
107 | 249 elif type == 'textbox': |
250 self.addTextBox(name, value) | |
103 | 251 elif type == 'list': |
252 self.addList(options, name, value) | |
105 | 253 elif type == 'button': |
106 | 254 assert(callback_id and value) |
255 self.addButton(callback_id, name, value) | |
103 | 256 |
257 def addOptions(self, options, parent): | |
258 """Add options to a multi-values element (e.g. list) | |
259 @param parent: multi-values element""" | |
260 for option in options: | |
261 opt = self.doc.createElement('option') | |
262 opt.setAttribute('value', option) | |
263 parent.appendChild(opt) | |
264 | |
104 | 265 def addCategory(self, name, layout): |
266 """Add a category to current layout (must be a tabs layout)""" | |
267 assert(layout != 'tabs') | |
268 if not self.parentTabsLayout: | |
269 error(_("Trying to add a category without parent tabs layout")) | |
270 assert(False) | |
271 if self.parentTabsLayout.getAttribute('type') != 'tabs': | |
272 error(_("parent layout of a category is not tabs")) | |
273 assert(False) | |
274 self.currentCategory = cat = self.doc.createElement('category') | |
275 cat.setAttribute('name', name) | |
276 self.changeLayout(layout) | |
277 self.parentTabsLayout.appendChild(cat) | |
278 | |
103 | 279 def toXml(self): |
280 """return the XML representation of the panel""" | |
281 return self.doc.toxml() |