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