Mercurial > libervia-backend
annotate tools/xml_tools.py @ 106:138d82f64b6f
plugin CS: friends parsing
author | Goffi <goffi@goffi.org> |
---|---|
date | Sat, 26 Jun 2010 15:33:16 +0800 |
parents | d2630fba8dfd |
children | 5ae370c71803 |
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 | |
106 def __init__(self, panel_type, layout="vertical"): | |
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: | |
113 - VERTICAL: elements are disposed up to bottom | |
114 - HORIZONTAL: elements are disposed left to right | |
115 - PAIRS: elements come on two aligned columns | |
116 (usually one for a label, the next for the element) | |
117 """ | |
118 if not panel_type in ['window', 'form', 'param']: | |
119 error(_("Unknown panel type [%s]") % panel_type) | |
120 assert(False) | |
121 self.type = panel_type | |
122 impl = minidom.getDOMImplementation() | |
123 | |
124 self.doc = impl.createDocument(None, "sat_xmlui", None) | |
125 top_element = self.doc.documentElement | |
126 top_element.setAttribute("type", panel_type) | |
104 | 127 self.parentTabsLayout = None #used only we have 'tabs' layout |
128 self.currentCategory = None #used only we have 'tabs' layout | |
129 self.changeLayout(layout) | |
103 | 130 |
131 def __del__(self): | |
132 self.doc.unlink() | |
37
a61beb21d16d
Gateway registration, unregistration & edition
Goffi <goffi@goffi.org>
parents:
35
diff
changeset
|
133 |
103 | 134 def __createLayout(self, layout, parent=None): |
135 """Create a layout element | |
136 @param type: layout type (cf init doc) | |
137 @parent: parent element or None | |
138 """ | |
104 | 139 if not layout in ['vertical', 'horizontal', 'pairs', 'tabs']: |
103 | 140 error (_("Unknown layout type [%s]") % layout) |
141 assert (False) | |
142 layout_elt = self.doc.createElement('layout') | |
143 layout_elt.setAttribute('type',layout) | |
144 if parent != None: | |
145 parent.appendChild(layout_elt) | |
146 return layout_elt | |
37
a61beb21d16d
Gateway registration, unregistration & edition
Goffi <goffi@goffi.org>
parents:
35
diff
changeset
|
147 |
103 | 148 def __createElem(self, type, name=None, parent = None): |
149 """Create an element | |
150 @param type: one of | |
151 - empty: empty element (usefull to skip something in a layout, e.g. skip first element in a PAIRS layout) | |
152 - text: text to be displayed in an multi-line area, e.g. instructions | |
153 @param name: name of the element or None | |
154 @param parent: parent element or None | |
155 """ | |
156 elem = self.doc.createElement('elem') | |
157 if name: | |
158 elem.setAttribute('name', name) | |
159 elem.setAttribute('type', type) | |
160 if parent != None: | |
161 parent.appendChild(elem) | |
162 return elem | |
163 | |
164 def changeLayout(self, layout): | |
165 """Change the current layout""" | |
104 | 166 self.currentLayout = self.__createLayout(layout, self.currentCategory if self.currentCategory else self.doc.documentElement) |
167 if layout == "tabs": | |
168 self.parentTabsLayout = self.currentLayout | |
103 | 169 |
170 | |
171 def addEmpty(self, name=None): | |
172 """Add a multi-lines text""" | |
173 elem = self.__createElem('empty', name, self.currentLayout) | |
174 | |
175 def addText(self, text, name=None): | |
176 """Add a multi-lines text""" | |
177 elem = self.__createElem('text', name, self.currentLayout) | |
178 text = self.doc.createTextNode(text) | |
179 elem.appendChild(text) | |
180 | |
181 def addLabel(self, text, name=None): | |
182 """Add a single line text, mainly useful as label before element""" | |
183 elem = self.__createElem('label', name, self.currentLayout) | |
184 elem.setAttribute('value', text) | |
185 | |
186 def addString(self, name=None, value=None): | |
187 """Add a string box""" | |
188 elem = self.__createElem('string', name, self.currentLayout) | |
189 if value: | |
190 elem.setAttribute('value', value) | |
191 | |
192 def addPassword(self, name=None, value=None): | |
193 """Add a password box""" | |
194 elem = self.__createElem('password', name, self.currentLayout) | |
195 if value: | |
196 elem.setAttribute('value', value) | |
197 | |
198 def addList(self, options, name=None, value=None): | |
199 """Add a list of choices""" | |
200 assert (options) | |
201 elem = self.__createElem('list', name, self.currentLayout) | |
202 self.addOptions(options, elem) | |
203 if value: | |
204 elem.setAttribute('value', value) | |
105 | 205 |
106 | 206 def addButton(self, callback_id, name, value): |
105 | 207 """Add a button |
208 @param callback: callback which will be called if button is pressed | |
106 | 209 @param name: name |
210 @param value: label of the button""" | |
105 | 211 elem = self.__createElem('button', name, self.currentLayout) |
106 | 212 elem.setAttribute('callback_id', callback_id) |
213 if value: | |
214 elem.setAttribute('value', value) | |
105 | 215 |
103 | 216 |
106 | 217 def addElement(self, type, name = None, content = None, value = None, options = None, callback_id = None): |
103 | 218 """Convenience method to add element, the params correspond to the ones in addSomething methods""" |
219 if type == 'empty': | |
220 self.addEmpty(name) | |
221 elif type == 'text': | |
222 assert(content) | |
223 self.addText(content, name) | |
224 elif type == 'label': | |
225 assert(value) | |
226 elif type == 'string': | |
227 self.addString(name, value) | |
228 elif type == 'password': | |
229 self.addPassword(name, value) | |
230 elif type == 'list': | |
231 self.addList(options, name, value) | |
105 | 232 elif type == 'button': |
106 | 233 assert(callback_id and value) |
234 self.addButton(callback_id, name, value) | |
103 | 235 |
236 def addOptions(self, options, parent): | |
237 """Add options to a multi-values element (e.g. list) | |
238 @param parent: multi-values element""" | |
239 for option in options: | |
240 opt = self.doc.createElement('option') | |
241 opt.setAttribute('value', option) | |
242 parent.appendChild(opt) | |
243 | |
104 | 244 def addCategory(self, name, layout): |
245 """Add a category to current layout (must be a tabs layout)""" | |
246 assert(layout != 'tabs') | |
247 if not self.parentTabsLayout: | |
248 error(_("Trying to add a category without parent tabs layout")) | |
249 assert(False) | |
250 if self.parentTabsLayout.getAttribute('type') != 'tabs': | |
251 error(_("parent layout of a category is not tabs")) | |
252 assert(False) | |
253 self.currentCategory = cat = self.doc.createElement('category') | |
254 cat.setAttribute('name', name) | |
255 self.changeLayout(layout) | |
256 self.parentTabsLayout.appendChild(cat) | |
257 | |
103 | 258 def toXml(self): |
259 """return the XML representation of the panel""" | |
260 return self.doc.toxml() |