Mercurial > libervia-backend
annotate src/tools/xml_tools.py @ 590:56531f9e9ac7
Fix pep8 support in src/tools.
author | Emmanuel Gil Peyrot <linkmauve@linkmauve.fr> |
---|---|
date | Fri, 18 Jan 2013 17:55:35 +0100 |
parents | beaf6bec2fcd |
children | 84a6e83157c2 |
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 |
572 | 6 Copyright (C) 2009, 2010, 2011, 2012, 2013 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 |
480
2a072735e459
Licence modification: the full project is now under AGPL v3+ instead of GPL v3+
Goffi <goffi@goffi.org>
parents:
459
diff
changeset
|
9 it under the terms of the GNU Affero General Public License as published by |
33
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 |
480
2a072735e459
Licence modification: the full project is now under AGPL v3+ instead of GPL v3+
Goffi <goffi@goffi.org>
parents:
459
diff
changeset
|
16 GNU Affero General Public License for more details. |
33
b9bb5d8e0cc7
In-band-registration: data form 2 xml conversion
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
17 |
480
2a072735e459
Licence modification: the full project is now under AGPL v3+ instead of GPL v3+
Goffi <goffi@goffi.org>
parents:
459
diff
changeset
|
18 You should have received a copy of the GNU Affero General Public License |
33
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 |
289
0e54b1b0a8c8
xml_tools: added ElementParser for raw xml -> domish.Element conversion
Goffi <goffi@goffi.org>
parents:
228
diff
changeset
|
25 from twisted.words.xish import domish |
33
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 |
587
952322b1d490
Remove trailing whitespaces.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
572
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""" | |
587
952322b1d490
Remove trailing whitespaces.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
572
diff
changeset
|
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') |
587
952322b1d490
Remove trailing whitespaces.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
572
diff
changeset
|
37 |
590
56531f9e9ac7
Fix pep8 support in src/tools.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
588
diff
changeset
|
38 labels = [field for field in form.fieldList if field.label] |
160
f494cba56a9e
xml tools: pairs layout is not used anymore if there is no label in dataForm2xml
Goffi <goffi@goffi.org>
parents:
146
diff
changeset
|
39 if labels: |
590
56531f9e9ac7
Fix pep8 support in src/tools.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
588
diff
changeset
|
40 # if there is no label, we don't need to use pairs |
160
f494cba56a9e
xml tools: pairs layout is not used anymore if there is no label in dataForm2xml
Goffi <goffi@goffi.org>
parents:
146
diff
changeset
|
41 form_ui.changeLayout("pairs") |
587
952322b1d490
Remove trailing whitespaces.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
572
diff
changeset
|
42 |
102 | 43 for field in form.fieldList: |
44 if field.fieldType == 'fixed': | |
45 __field_type = 'text' | |
46 elif field.fieldType == 'text-single': | |
47 __field_type = "string" | |
48 elif field.fieldType == 'text-private': | |
49 __field_type = "password" | |
50 elif field.fieldType == 'list-single': | |
51 __field_type = "list" | |
52 else: | |
590
56531f9e9ac7
Fix pep8 support in src/tools.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
588
diff
changeset
|
53 error(u"FIXME FIXME FIXME: Type [%s] is not managed yet by SàT" % field.fieldType) |
102 | 54 __field_type = "string" |
587
952322b1d490
Remove trailing whitespaces.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
572
diff
changeset
|
55 |
160
f494cba56a9e
xml tools: pairs layout is not used anymore if there is no label in dataForm2xml
Goffi <goffi@goffi.org>
parents:
146
diff
changeset
|
56 if labels: |
f494cba56a9e
xml tools: pairs layout is not used anymore if there is no label in dataForm2xml
Goffi <goffi@goffi.org>
parents:
146
diff
changeset
|
57 if field.label: |
f494cba56a9e
xml tools: pairs layout is not used anymore if there is no label in dataForm2xml
Goffi <goffi@goffi.org>
parents:
146
diff
changeset
|
58 form_ui.addLabel(field.label) |
f494cba56a9e
xml tools: pairs layout is not used anymore if there is no label in dataForm2xml
Goffi <goffi@goffi.org>
parents:
146
diff
changeset
|
59 else: |
f494cba56a9e
xml tools: pairs layout is not used anymore if there is no label in dataForm2xml
Goffi <goffi@goffi.org>
parents:
146
diff
changeset
|
60 form_ui.addEmpty() |
103 | 61 |
590
56531f9e9ac7
Fix pep8 support in src/tools.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
588
diff
changeset
|
62 form_ui.addElement(__field_type, field.var, field.value, [option.value for option in field.options]) |
105 | 63 return form_ui.toXml() |
37
a61beb21d16d
Gateway registration, unregistration & edition
Goffi <goffi@goffi.org>
parents:
35
diff
changeset
|
64 |
590
56531f9e9ac7
Fix pep8 support in src/tools.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
588
diff
changeset
|
65 |
102 | 66 def tupleList2dataForm(values): |
67 """convert a list of tuples (name,value) to a wokkel submit data form""" | |
68 form = data_form.Form('submit') | |
69 for value in values: | |
70 field = data_form.Field(var=value[0], value=value[1]) | |
71 form.addField(field) | |
37
a61beb21d16d
Gateway registration, unregistration & edition
Goffi <goffi@goffi.org>
parents:
35
diff
changeset
|
72 |
102 | 73 return form |
103 | 74 |
590
56531f9e9ac7
Fix pep8 support in src/tools.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
588
diff
changeset
|
75 |
105 | 76 def paramsXml2xmlUI(xml): |
77 """Convert the xml for parameter to a SàT XML User Interface""" | |
214 | 78 params_doc = minidom.parseString(xml.encode('utf-8')) |
105 | 79 top = params_doc.documentElement |
80 if top.nodeName != 'params': | |
81 error(_('INTERNAL ERROR: parameters xml not valid')) | |
82 assert(False) | |
83 param_ui = XMLUI("param", "tabs") | |
84 for category in top.getElementsByTagName("category"): | |
85 name = category.getAttribute('name') | |
183
9ee4a1d0d7fb
Added auto(dis)connect params + misc
Goffi <goffi@goffi.org>
parents:
160
diff
changeset
|
86 label = category.getAttribute('label') |
105 | 87 if not name: |
88 error(_('INTERNAL ERROR: params categories must have a name')) | |
89 assert(False) | |
183
9ee4a1d0d7fb
Added auto(dis)connect params + misc
Goffi <goffi@goffi.org>
parents:
160
diff
changeset
|
90 param_ui.addCategory(name, 'pairs', label=label) |
105 | 91 for param in category.getElementsByTagName("param"): |
92 name = param.getAttribute('name') | |
183
9ee4a1d0d7fb
Added auto(dis)connect params + misc
Goffi <goffi@goffi.org>
parents:
160
diff
changeset
|
93 label = param.getAttribute('label') |
105 | 94 if not name: |
95 error(_('INTERNAL ERROR: params must have a name')) | |
96 assert(False) | |
590
56531f9e9ac7
Fix pep8 support in src/tools.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
588
diff
changeset
|
97 type_ = param.getAttribute('type') |
105 | 98 value = param.getAttribute('value') or None |
106 | 99 callback_id = param.getAttribute('callback_id') or None |
590
56531f9e9ac7
Fix pep8 support in src/tools.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
588
diff
changeset
|
100 if type_ == "button": |
106 | 101 param_ui.addEmpty() |
102 else: | |
183
9ee4a1d0d7fb
Added auto(dis)connect params + misc
Goffi <goffi@goffi.org>
parents:
160
diff
changeset
|
103 param_ui.addLabel(label or name) |
590
56531f9e9ac7
Fix pep8 support in src/tools.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
588
diff
changeset
|
104 param_ui.addElement(name=name, type_=type_, value=value, callback_id=callback_id) |
105 | 105 |
106 return param_ui.toXml() | |
107 | |
587
952322b1d490
Remove trailing whitespaces.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
572
diff
changeset
|
108 |
588
beaf6bec2fcd
Remove every old-style class.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
587
diff
changeset
|
109 class XMLUI(object): |
103 | 110 """This class is used to create a user interface (form/window/parameters/etc) using SàT XML""" |
111 | |
107 | 112 def __init__(self, panel_type, layout="vertical", title=None): |
103 | 113 """Init SàT XML Panel |
114 @param panel_type: one of | |
115 - window (new window) | |
116 - form (formulaire, depend of the frontend, usually a panel with cancel/submit buttons) | |
117 - param (parameters, presentatio depend of the frontend) | |
118 @param layout: disposition of elements, one of: | |
107 | 119 - vertical: elements are disposed up to bottom |
120 - horizontal: elements are disposed left to right | |
121 - pairs: elements come on two aligned columns | |
103 | 122 (usually one for a label, the next for the element) |
107 | 123 - tabs: elemens are in categories with tabs (notebook) |
124 @param title: title or default if None | |
103 | 125 """ |
126 if not panel_type in ['window', 'form', 'param']: | |
127 error(_("Unknown panel type [%s]") % panel_type) | |
128 assert(False) | |
590
56531f9e9ac7
Fix pep8 support in src/tools.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
588
diff
changeset
|
129 self.type_ = panel_type |
103 | 130 impl = minidom.getDOMImplementation() |
131 | |
132 self.doc = impl.createDocument(None, "sat_xmlui", None) | |
133 top_element = self.doc.documentElement | |
134 top_element.setAttribute("type", panel_type) | |
107 | 135 if title: |
136 top_element.setAttribute("title", title) | |
590
56531f9e9ac7
Fix pep8 support in src/tools.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
588
diff
changeset
|
137 self.parentTabsLayout = None # used only we have 'tabs' layout |
56531f9e9ac7
Fix pep8 support in src/tools.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
588
diff
changeset
|
138 self.currentCategory = None # used only we have 'tabs' layout |
56531f9e9ac7
Fix pep8 support in src/tools.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
588
diff
changeset
|
139 self.currentLayout = None |
104 | 140 self.changeLayout(layout) |
103 | 141 |
142 def __del__(self): | |
587
952322b1d490
Remove trailing whitespaces.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
572
diff
changeset
|
143 self.doc.unlink() |
952322b1d490
Remove trailing whitespaces.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
572
diff
changeset
|
144 |
103 | 145 def __createLayout(self, layout, parent=None): |
146 """Create a layout element | |
147 @param type: layout type (cf init doc) | |
148 @parent: parent element or None | |
149 """ | |
104 | 150 if not layout in ['vertical', 'horizontal', 'pairs', 'tabs']: |
590
56531f9e9ac7
Fix pep8 support in src/tools.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
588
diff
changeset
|
151 error(_("Unknown layout type [%s]") % layout) |
56531f9e9ac7
Fix pep8 support in src/tools.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
588
diff
changeset
|
152 assert False |
103 | 153 layout_elt = self.doc.createElement('layout') |
590
56531f9e9ac7
Fix pep8 support in src/tools.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
588
diff
changeset
|
154 layout_elt.setAttribute('type', layout) |
56531f9e9ac7
Fix pep8 support in src/tools.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
588
diff
changeset
|
155 if parent is not None: |
103 | 156 parent.appendChild(layout_elt) |
157 return layout_elt | |
37
a61beb21d16d
Gateway registration, unregistration & edition
Goffi <goffi@goffi.org>
parents:
35
diff
changeset
|
158 |
590
56531f9e9ac7
Fix pep8 support in src/tools.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
588
diff
changeset
|
159 def __createElem(self, type_, name=None, parent=None): |
103 | 160 """Create an element |
590
56531f9e9ac7
Fix pep8 support in src/tools.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
588
diff
changeset
|
161 @param type_: one of |
103 | 162 - empty: empty element (usefull to skip something in a layout, e.g. skip first element in a PAIRS layout) |
163 - text: text to be displayed in an multi-line area, e.g. instructions | |
164 @param name: name of the element or None | |
165 @param parent: parent element or None | |
166 """ | |
167 elem = self.doc.createElement('elem') | |
168 if name: | |
169 elem.setAttribute('name', name) | |
590
56531f9e9ac7
Fix pep8 support in src/tools.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
588
diff
changeset
|
170 elem.setAttribute('type', type_) |
56531f9e9ac7
Fix pep8 support in src/tools.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
588
diff
changeset
|
171 if parent is not None: |
103 | 172 parent.appendChild(elem) |
173 return elem | |
174 | |
175 def changeLayout(self, layout): | |
176 """Change the current layout""" | |
104 | 177 self.currentLayout = self.__createLayout(layout, self.currentCategory if self.currentCategory else self.doc.documentElement) |
178 if layout == "tabs": | |
179 self.parentTabsLayout = self.currentLayout | |
103 | 180 |
181 def addEmpty(self, name=None): | |
182 """Add a multi-lines text""" | |
590
56531f9e9ac7
Fix pep8 support in src/tools.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
588
diff
changeset
|
183 self.__createElem('empty', name, self.currentLayout) |
587
952322b1d490
Remove trailing whitespaces.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
572
diff
changeset
|
184 |
103 | 185 def addText(self, text, name=None): |
186 """Add a multi-lines text""" | |
187 elem = self.__createElem('text', name, self.currentLayout) | |
188 text = self.doc.createTextNode(text) | |
189 elem.appendChild(text) | |
190 | |
191 def addLabel(self, text, name=None): | |
192 """Add a single line text, mainly useful as label before element""" | |
193 elem = self.__createElem('label', name, self.currentLayout) | |
194 elem.setAttribute('value', text) | |
195 | |
196 def addString(self, name=None, value=None): | |
197 """Add a string box""" | |
198 elem = self.__createElem('string', name, self.currentLayout) | |
199 if value: | |
200 elem.setAttribute('value', value) | |
587
952322b1d490
Remove trailing whitespaces.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
572
diff
changeset
|
201 |
103 | 202 def addPassword(self, name=None, value=None): |
203 """Add a password box""" | |
204 elem = self.__createElem('password', name, self.currentLayout) | |
205 if value: | |
206 elem.setAttribute('value', value) | |
207 | |
107 | 208 def addTextBox(self, name=None, value=None): |
209 """Add a string box""" | |
210 elem = self.__createElem('textbox', name, self.currentLayout) | |
211 if value: | |
212 elem.setAttribute('value', value) | |
587
952322b1d490
Remove trailing whitespaces.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
572
diff
changeset
|
213 |
183
9ee4a1d0d7fb
Added auto(dis)connect params + misc
Goffi <goffi@goffi.org>
parents:
160
diff
changeset
|
214 def addBool(self, name=None, value="true"): |
9ee4a1d0d7fb
Added auto(dis)connect params + misc
Goffi <goffi@goffi.org>
parents:
160
diff
changeset
|
215 """Add a string box""" |
590
56531f9e9ac7
Fix pep8 support in src/tools.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
588
diff
changeset
|
216 assert value in ["true", "false"] |
183
9ee4a1d0d7fb
Added auto(dis)connect params + misc
Goffi <goffi@goffi.org>
parents:
160
diff
changeset
|
217 elem = self.__createElem('bool', name, self.currentLayout) |
9ee4a1d0d7fb
Added auto(dis)connect params + misc
Goffi <goffi@goffi.org>
parents:
160
diff
changeset
|
218 elem.setAttribute('value', value) |
587
952322b1d490
Remove trailing whitespaces.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
572
diff
changeset
|
219 |
107 | 220 def addList(self, options, name=None, value=None, style=set()): |
103 | 221 """Add a list of choices""" |
107 | 222 styles = set(style) |
590
56531f9e9ac7
Fix pep8 support in src/tools.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
588
diff
changeset
|
223 assert options |
56531f9e9ac7
Fix pep8 support in src/tools.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
588
diff
changeset
|
224 assert styles.issubset(['multi']) |
103 | 225 elem = self.__createElem('list', name, self.currentLayout) |
587
952322b1d490
Remove trailing whitespaces.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
572
diff
changeset
|
226 self.addOptions(options, elem) |
103 | 227 if value: |
228 elem.setAttribute('value', value) | |
107 | 229 for style in styles: |
230 elem.setAttribute(style, 'yes') | |
105 | 231 |
107 | 232 def addButton(self, callback_id, name, value, fields_back=[]): |
105 | 233 """Add a button |
234 @param callback: callback which will be called if button is pressed | |
106 | 235 @param name: name |
107 | 236 @param value: label of the button |
237 @fields_back: list of names of field to give back when pushing the button""" | |
105 | 238 elem = self.__createElem('button', name, self.currentLayout) |
106 | 239 elem.setAttribute('callback_id', callback_id) |
107 | 240 elem.setAttribute('value', value) |
241 for field in fields_back: | |
242 fback_el = self.doc.createElement('field_back') | |
243 fback_el.setAttribute('name', field) | |
244 elem.appendChild(fback_el) | |
245 | |
590
56531f9e9ac7
Fix pep8 support in src/tools.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
588
diff
changeset
|
246 def addElement(self, type_, name=None, value=None, options=None, callback_id=None): |
103 | 247 """Convenience method to add element, the params correspond to the ones in addSomething methods""" |
590
56531f9e9ac7
Fix pep8 support in src/tools.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
588
diff
changeset
|
248 if type_ == 'empty': |
103 | 249 self.addEmpty(name) |
590
56531f9e9ac7
Fix pep8 support in src/tools.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
588
diff
changeset
|
250 elif type_ == 'text': |
56531f9e9ac7
Fix pep8 support in src/tools.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
588
diff
changeset
|
251 assert value is not None |
146
7c00c4b0a5c2
xml_tools: removed unused content parameter in addElement
Goffi <goffi@goffi.org>
parents:
107
diff
changeset
|
252 self.addText(value, name) |
590
56531f9e9ac7
Fix pep8 support in src/tools.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
588
diff
changeset
|
253 elif type_ == 'label': |
103 | 254 assert(value) |
146
7c00c4b0a5c2
xml_tools: removed unused content parameter in addElement
Goffi <goffi@goffi.org>
parents:
107
diff
changeset
|
255 self.addLabel(value) |
590
56531f9e9ac7
Fix pep8 support in src/tools.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
588
diff
changeset
|
256 elif type_ == 'string': |
103 | 257 self.addString(name, value) |
590
56531f9e9ac7
Fix pep8 support in src/tools.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
588
diff
changeset
|
258 elif type_ == 'password': |
103 | 259 self.addPassword(name, value) |
590
56531f9e9ac7
Fix pep8 support in src/tools.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
588
diff
changeset
|
260 elif type_ == 'textbox': |
107 | 261 self.addTextBox(name, value) |
590
56531f9e9ac7
Fix pep8 support in src/tools.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
588
diff
changeset
|
262 elif type_ == 'bool': |
183
9ee4a1d0d7fb
Added auto(dis)connect params + misc
Goffi <goffi@goffi.org>
parents:
160
diff
changeset
|
263 if not value: |
9ee4a1d0d7fb
Added auto(dis)connect params + misc
Goffi <goffi@goffi.org>
parents:
160
diff
changeset
|
264 value = "true" |
9ee4a1d0d7fb
Added auto(dis)connect params + misc
Goffi <goffi@goffi.org>
parents:
160
diff
changeset
|
265 self.addBool(name, value) |
590
56531f9e9ac7
Fix pep8 support in src/tools.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
588
diff
changeset
|
266 elif type_ == 'list': |
103 | 267 self.addList(options, name, value) |
590
56531f9e9ac7
Fix pep8 support in src/tools.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
588
diff
changeset
|
268 elif type_ == 'button': |
106 | 269 assert(callback_id and value) |
270 self.addButton(callback_id, name, value) | |
103 | 271 |
272 def addOptions(self, options, parent): | |
273 """Add options to a multi-values element (e.g. list) | |
274 @param parent: multi-values element""" | |
275 for option in options: | |
276 opt = self.doc.createElement('option') | |
277 opt.setAttribute('value', option) | |
278 parent.appendChild(opt) | |
279 | |
183
9ee4a1d0d7fb
Added auto(dis)connect params + misc
Goffi <goffi@goffi.org>
parents:
160
diff
changeset
|
280 def addCategory(self, name, layout, label=None): |
104 | 281 """Add a category to current layout (must be a tabs layout)""" |
282 assert(layout != 'tabs') | |
283 if not self.parentTabsLayout: | |
284 error(_("Trying to add a category without parent tabs layout")) | |
285 assert(False) | |
286 if self.parentTabsLayout.getAttribute('type') != 'tabs': | |
287 error(_("parent layout of a category is not tabs")) | |
288 assert(False) | |
183
9ee4a1d0d7fb
Added auto(dis)connect params + misc
Goffi <goffi@goffi.org>
parents:
160
diff
changeset
|
289 |
9ee4a1d0d7fb
Added auto(dis)connect params + misc
Goffi <goffi@goffi.org>
parents:
160
diff
changeset
|
290 if not label: |
9ee4a1d0d7fb
Added auto(dis)connect params + misc
Goffi <goffi@goffi.org>
parents:
160
diff
changeset
|
291 label = name |
104 | 292 self.currentCategory = cat = self.doc.createElement('category') |
293 cat.setAttribute('name', name) | |
183
9ee4a1d0d7fb
Added auto(dis)connect params + misc
Goffi <goffi@goffi.org>
parents:
160
diff
changeset
|
294 cat.setAttribute('label', label) |
104 | 295 self.changeLayout(layout) |
296 self.parentTabsLayout.appendChild(cat) | |
297 | |
103 | 298 def toXml(self): |
587
952322b1d490
Remove trailing whitespaces.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
572
diff
changeset
|
299 """return the XML representation of the panel""" |
103 | 300 return self.doc.toxml() |
289
0e54b1b0a8c8
xml_tools: added ElementParser for raw xml -> domish.Element conversion
Goffi <goffi@goffi.org>
parents:
228
diff
changeset
|
301 |
0e54b1b0a8c8
xml_tools: added ElementParser for raw xml -> domish.Element conversion
Goffi <goffi@goffi.org>
parents:
228
diff
changeset
|
302 |
0e54b1b0a8c8
xml_tools: added ElementParser for raw xml -> domish.Element conversion
Goffi <goffi@goffi.org>
parents:
228
diff
changeset
|
303 class ElementParser(object): |
0e54b1b0a8c8
xml_tools: added ElementParser for raw xml -> domish.Element conversion
Goffi <goffi@goffi.org>
parents:
228
diff
changeset
|
304 """callable class to parse XML string into Element |
0e54b1b0a8c8
xml_tools: added ElementParser for raw xml -> domish.Element conversion
Goffi <goffi@goffi.org>
parents:
228
diff
changeset
|
305 Found at http://stackoverflow.com/questions/2093400/how-to-create-twisted-words-xish-domish-element-entirely-from-raw-xml/2095942#2095942 |
0e54b1b0a8c8
xml_tools: added ElementParser for raw xml -> domish.Element conversion
Goffi <goffi@goffi.org>
parents:
228
diff
changeset
|
306 (c) Karl Anderson""" |
0e54b1b0a8c8
xml_tools: added ElementParser for raw xml -> domish.Element conversion
Goffi <goffi@goffi.org>
parents:
228
diff
changeset
|
307 |
590
56531f9e9ac7
Fix pep8 support in src/tools.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
588
diff
changeset
|
308 def __call__(self, string): |
289
0e54b1b0a8c8
xml_tools: added ElementParser for raw xml -> domish.Element conversion
Goffi <goffi@goffi.org>
parents:
228
diff
changeset
|
309 self.result = None |
590
56531f9e9ac7
Fix pep8 support in src/tools.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
588
diff
changeset
|
310 |
56531f9e9ac7
Fix pep8 support in src/tools.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
588
diff
changeset
|
311 def onStart(elem): |
56531f9e9ac7
Fix pep8 support in src/tools.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
588
diff
changeset
|
312 self.result = elem |
56531f9e9ac7
Fix pep8 support in src/tools.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
588
diff
changeset
|
313 |
289
0e54b1b0a8c8
xml_tools: added ElementParser for raw xml -> domish.Element conversion
Goffi <goffi@goffi.org>
parents:
228
diff
changeset
|
314 def onEnd(): |
0e54b1b0a8c8
xml_tools: added ElementParser for raw xml -> domish.Element conversion
Goffi <goffi@goffi.org>
parents:
228
diff
changeset
|
315 pass |
590
56531f9e9ac7
Fix pep8 support in src/tools.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
588
diff
changeset
|
316 |
56531f9e9ac7
Fix pep8 support in src/tools.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
588
diff
changeset
|
317 def onElement(elem): |
56531f9e9ac7
Fix pep8 support in src/tools.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
588
diff
changeset
|
318 self.result.addChild(elem) |
289
0e54b1b0a8c8
xml_tools: added ElementParser for raw xml -> domish.Element conversion
Goffi <goffi@goffi.org>
parents:
228
diff
changeset
|
319 |
0e54b1b0a8c8
xml_tools: added ElementParser for raw xml -> domish.Element conversion
Goffi <goffi@goffi.org>
parents:
228
diff
changeset
|
320 parser = domish.elementStream() |
0e54b1b0a8c8
xml_tools: added ElementParser for raw xml -> domish.Element conversion
Goffi <goffi@goffi.org>
parents:
228
diff
changeset
|
321 parser.DocumentStartEvent = onStart |
0e54b1b0a8c8
xml_tools: added ElementParser for raw xml -> domish.Element conversion
Goffi <goffi@goffi.org>
parents:
228
diff
changeset
|
322 parser.ElementEvent = onElement |
0e54b1b0a8c8
xml_tools: added ElementParser for raw xml -> domish.Element conversion
Goffi <goffi@goffi.org>
parents:
228
diff
changeset
|
323 parser.DocumentEndEvent = onEnd |
536
a31abb97310d
core, plugins: fixed bad namespace in stanza creation
Goffi <goffi@goffi.org>
parents:
480
diff
changeset
|
324 tmp = domish.Element((None, "s")) |
590
56531f9e9ac7
Fix pep8 support in src/tools.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
588
diff
changeset
|
325 tmp.addRawXml(string.replace('\n', '').replace('\t', '')) |
304 | 326 parser.parse(tmp.toXml().encode('utf-8')) |
587
952322b1d490
Remove trailing whitespaces.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
572
diff
changeset
|
327 return self.result.firstChildElement() |