Mercurial > libervia-backend
annotate src/tools/xml_tools.py @ 411:b109a79ac72f
primitivus: urwid 1.0.0 update (crash was caused by using non unicode caption)
author | Goffi <goffi@goffi.org> |
---|---|
date | Sat, 08 Oct 2011 21:03:02 +0200 |
parents | e04ccf122bb6 |
children | cf005701624b |
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 |
228 | 6 Copyright (C) 2009, 2010, 2011 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 |
289
0e54b1b0a8c8
xml_tools: added ElementParser for raw xml -> domish.Element conversion
Goffi <goffi@goffi.org>
parents:
228
diff
changeset
|
26 from twisted.words.xish import domish |
33
b9bb5d8e0cc7
In-band-registration: data form 2 xml conversion
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
27 |
102 | 28 """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
|
29 |
b9bb5d8e0cc7
In-band-registration: data form 2 xml conversion
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
30 |
102 | 31 def dataForm2xml(form): |
32 """Take a data form (xep-0004, Wokkel's implementation) and convert it to a SàT xml""" | |
33 | |
105 | 34 form_ui = XMLUI("form", "vertical") |
104 | 35 |
102 | 36 if form.instructions: |
105 | 37 form_ui.addText('\n'.join(form.instructions), 'instructions') |
103 | 38 |
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 labels = filter(lambda field:field.label,form.fieldList) |
f494cba56a9e
xml tools: pairs layout is not used anymore if there is no label in dataForm2xml
Goffi <goffi@goffi.org>
parents:
146
diff
changeset
|
40 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
|
41 #if there is no label, we don't need to use pairs |
f494cba56a9e
xml tools: pairs layout is not used anymore if there is no label in dataForm2xml
Goffi <goffi@goffi.org>
parents:
146
diff
changeset
|
42 form_ui.changeLayout("pairs") |
103 | 43 |
102 | 44 for field in form.fieldList: |
45 if field.fieldType == 'fixed': | |
46 __field_type = 'text' | |
47 elif field.fieldType == 'text-single': | |
48 __field_type = "string" | |
49 elif field.fieldType == 'text-private': | |
50 __field_type = "password" | |
51 elif field.fieldType == 'list-single': | |
52 __field_type = "list" | |
53 else: | |
54 error (u"FIXME FIXME FIXME: Type [%s] is not managed yet by SàT" % field.fieldType) | |
55 __field_type = "string" | |
35
c45deebb40a5
Wix: Registration form management (not finished yet)
Goffi <goffi@goffi.org>
parents:
33
diff
changeset
|
56 |
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
|
57 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
|
58 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
|
59 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
|
60 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
|
61 form_ui.addEmpty() |
103 | 62 |
146
7c00c4b0a5c2
xml_tools: removed unused content parameter in addElement
Goffi <goffi@goffi.org>
parents:
107
diff
changeset
|
63 elem = form_ui.addElement(__field_type, field.var, field.value, [option.value for option in field.options]) |
105 | 64 return form_ui.toXml() |
37
a61beb21d16d
Gateway registration, unregistration & edition
Goffi <goffi@goffi.org>
parents:
35
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 |
105 | 75 def paramsXml2xmlUI(xml): |
76 """Convert the xml for parameter to a SàT XML User Interface""" | |
214 | 77 params_doc = minidom.parseString(xml.encode('utf-8')) |
105 | 78 top = params_doc.documentElement |
79 if top.nodeName != 'params': | |
80 error(_('INTERNAL ERROR: parameters xml not valid')) | |
81 assert(False) | |
82 param_ui = XMLUI("param", "tabs") | |
83 for category in top.getElementsByTagName("category"): | |
84 name = category.getAttribute('name') | |
183
9ee4a1d0d7fb
Added auto(dis)connect params + misc
Goffi <goffi@goffi.org>
parents:
160
diff
changeset
|
85 label = category.getAttribute('label') |
105 | 86 if not name: |
87 error(_('INTERNAL ERROR: params categories must have a name')) | |
88 assert(False) | |
183
9ee4a1d0d7fb
Added auto(dis)connect params + misc
Goffi <goffi@goffi.org>
parents:
160
diff
changeset
|
89 param_ui.addCategory(name, 'pairs', label=label) |
105 | 90 for param in category.getElementsByTagName("param"): |
91 name = param.getAttribute('name') | |
183
9ee4a1d0d7fb
Added auto(dis)connect params + misc
Goffi <goffi@goffi.org>
parents:
160
diff
changeset
|
92 label = param.getAttribute('label') |
105 | 93 if not name: |
94 error(_('INTERNAL ERROR: params must have a name')) | |
95 assert(False) | |
96 type = param.getAttribute('type') | |
97 value = param.getAttribute('value') or None | |
106 | 98 callback_id = param.getAttribute('callback_id') or None |
99 if type == "button": | |
100 param_ui.addEmpty() | |
101 else: | |
183
9ee4a1d0d7fb
Added auto(dis)connect params + misc
Goffi <goffi@goffi.org>
parents:
160
diff
changeset
|
102 param_ui.addLabel(label or name) |
106 | 103 param_ui.addElement(name=name, type=type, value=value, callback_id=callback_id) |
105 | 104 |
105 return param_ui.toXml() | |
106 | |
107 | |
108 | |
109 | |
103 | 110 class XMLUI: |
111 """This class is used to create a user interface (form/window/parameters/etc) using SàT XML""" | |
112 | |
107 | 113 def __init__(self, panel_type, layout="vertical", title=None): |
103 | 114 """Init SàT XML Panel |
115 @param panel_type: one of | |
116 - window (new window) | |
117 - form (formulaire, depend of the frontend, usually a panel with cancel/submit buttons) | |
118 - param (parameters, presentatio depend of the frontend) | |
119 @param layout: disposition of elements, one of: | |
107 | 120 - vertical: elements are disposed up to bottom |
121 - horizontal: elements are disposed left to right | |
122 - pairs: elements come on two aligned columns | |
103 | 123 (usually one for a label, the next for the element) |
107 | 124 - tabs: elemens are in categories with tabs (notebook) |
125 @param title: title or default if None | |
103 | 126 """ |
127 if not panel_type in ['window', 'form', 'param']: | |
128 error(_("Unknown panel type [%s]") % panel_type) | |
129 assert(False) | |
130 self.type = panel_type | |
131 impl = minidom.getDOMImplementation() | |
132 | |
133 self.doc = impl.createDocument(None, "sat_xmlui", None) | |
134 top_element = self.doc.documentElement | |
135 top_element.setAttribute("type", panel_type) | |
107 | 136 if title: |
137 top_element.setAttribute("title", title) | |
104 | 138 self.parentTabsLayout = None #used only we have 'tabs' layout |
139 self.currentCategory = None #used only we have 'tabs' layout | |
140 self.changeLayout(layout) | |
103 | 141 |
142 def __del__(self): | |
143 self.doc.unlink() | |
37
a61beb21d16d
Gateway registration, unregistration & edition
Goffi <goffi@goffi.org>
parents:
35
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']: |
103 | 151 error (_("Unknown layout type [%s]") % layout) |
152 assert (False) | |
153 layout_elt = self.doc.createElement('layout') | |
154 layout_elt.setAttribute('type',layout) | |
155 if parent != None: | |
156 parent.appendChild(layout_elt) | |
157 return layout_elt | |
37
a61beb21d16d
Gateway registration, unregistration & edition
Goffi <goffi@goffi.org>
parents:
35
diff
changeset
|
158 |
103 | 159 def __createElem(self, type, name=None, parent = None): |
160 """Create an element | |
161 @param type: one of | |
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) | |
170 elem.setAttribute('type', type) | |
171 if parent != None: | |
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 | |
182 def addEmpty(self, name=None): | |
183 """Add a multi-lines text""" | |
184 elem = self.__createElem('empty', name, self.currentLayout) | |
185 | |
186 def addText(self, text, name=None): | |
187 """Add a multi-lines text""" | |
188 elem = self.__createElem('text', name, self.currentLayout) | |
189 text = self.doc.createTextNode(text) | |
190 elem.appendChild(text) | |
191 | |
192 def addLabel(self, text, name=None): | |
193 """Add a single line text, mainly useful as label before element""" | |
194 elem = self.__createElem('label', name, self.currentLayout) | |
195 elem.setAttribute('value', text) | |
196 | |
197 def addString(self, name=None, value=None): | |
198 """Add a string box""" | |
199 elem = self.__createElem('string', name, self.currentLayout) | |
200 if value: | |
201 elem.setAttribute('value', value) | |
202 | |
203 def addPassword(self, name=None, value=None): | |
204 """Add a password box""" | |
205 elem = self.__createElem('password', name, self.currentLayout) | |
206 if value: | |
207 elem.setAttribute('value', value) | |
208 | |
107 | 209 def addTextBox(self, name=None, value=None): |
210 """Add a string box""" | |
211 elem = self.__createElem('textbox', name, self.currentLayout) | |
212 if value: | |
213 elem.setAttribute('value', value) | |
214 | |
183
9ee4a1d0d7fb
Added auto(dis)connect params + misc
Goffi <goffi@goffi.org>
parents:
160
diff
changeset
|
215 def addBool(self, name=None, value="true"): |
9ee4a1d0d7fb
Added auto(dis)connect params + misc
Goffi <goffi@goffi.org>
parents:
160
diff
changeset
|
216 """Add a string box""" |
9ee4a1d0d7fb
Added auto(dis)connect params + misc
Goffi <goffi@goffi.org>
parents:
160
diff
changeset
|
217 assert value in ["true","false"] |
9ee4a1d0d7fb
Added auto(dis)connect params + misc
Goffi <goffi@goffi.org>
parents:
160
diff
changeset
|
218 elem = self.__createElem('bool', name, self.currentLayout) |
9ee4a1d0d7fb
Added auto(dis)connect params + misc
Goffi <goffi@goffi.org>
parents:
160
diff
changeset
|
219 elem.setAttribute('value', value) |
9ee4a1d0d7fb
Added auto(dis)connect params + misc
Goffi <goffi@goffi.org>
parents:
160
diff
changeset
|
220 |
107 | 221 def addList(self, options, name=None, value=None, style=set()): |
103 | 222 """Add a list of choices""" |
107 | 223 styles = set(style) |
103 | 224 assert (options) |
107 | 225 assert (styles.issubset(['multi'])) |
103 | 226 elem = self.__createElem('list', name, self.currentLayout) |
227 self.addOptions(options, elem) | |
228 if value: | |
229 elem.setAttribute('value', value) | |
107 | 230 for style in styles: |
231 elem.setAttribute(style, 'yes') | |
105 | 232 |
107 | 233 def addButton(self, callback_id, name, value, fields_back=[]): |
105 | 234 """Add a button |
235 @param callback: callback which will be called if button is pressed | |
106 | 236 @param name: name |
107 | 237 @param value: label of the button |
238 @fields_back: list of names of field to give back when pushing the button""" | |
105 | 239 elem = self.__createElem('button', name, self.currentLayout) |
106 | 240 elem.setAttribute('callback_id', callback_id) |
107 | 241 elem.setAttribute('value', value) |
242 for field in fields_back: | |
243 fback_el = self.doc.createElement('field_back') | |
244 fback_el.setAttribute('name', field) | |
245 elem.appendChild(fback_el) | |
246 | |
105 | 247 |
103 | 248 |
146
7c00c4b0a5c2
xml_tools: removed unused content parameter in addElement
Goffi <goffi@goffi.org>
parents:
107
diff
changeset
|
249 def addElement(self, type, name = None, value = None, options = None, callback_id = None): |
103 | 250 """Convenience method to add element, the params correspond to the ones in addSomething methods""" |
251 if type == 'empty': | |
252 self.addEmpty(name) | |
253 elif type == 'text': | |
146
7c00c4b0a5c2
xml_tools: removed unused content parameter in addElement
Goffi <goffi@goffi.org>
parents:
107
diff
changeset
|
254 assert(value!=None) |
7c00c4b0a5c2
xml_tools: removed unused content parameter in addElement
Goffi <goffi@goffi.org>
parents:
107
diff
changeset
|
255 self.addText(value, name) |
103 | 256 elif type == 'label': |
257 assert(value) | |
146
7c00c4b0a5c2
xml_tools: removed unused content parameter in addElement
Goffi <goffi@goffi.org>
parents:
107
diff
changeset
|
258 self.addLabel(value) |
103 | 259 elif type == 'string': |
260 self.addString(name, value) | |
261 elif type == 'password': | |
262 self.addPassword(name, value) | |
107 | 263 elif type == 'textbox': |
264 self.addTextBox(name, value) | |
183
9ee4a1d0d7fb
Added auto(dis)connect params + misc
Goffi <goffi@goffi.org>
parents:
160
diff
changeset
|
265 elif type == 'bool': |
9ee4a1d0d7fb
Added auto(dis)connect params + misc
Goffi <goffi@goffi.org>
parents:
160
diff
changeset
|
266 if not value: |
9ee4a1d0d7fb
Added auto(dis)connect params + misc
Goffi <goffi@goffi.org>
parents:
160
diff
changeset
|
267 value = "true" |
9ee4a1d0d7fb
Added auto(dis)connect params + misc
Goffi <goffi@goffi.org>
parents:
160
diff
changeset
|
268 self.addBool(name, value) |
103 | 269 elif type == 'list': |
270 self.addList(options, name, value) | |
105 | 271 elif type == 'button': |
106 | 272 assert(callback_id and value) |
273 self.addButton(callback_id, name, value) | |
103 | 274 |
275 def addOptions(self, options, parent): | |
276 """Add options to a multi-values element (e.g. list) | |
277 @param parent: multi-values element""" | |
278 for option in options: | |
279 opt = self.doc.createElement('option') | |
280 opt.setAttribute('value', option) | |
281 parent.appendChild(opt) | |
282 | |
183
9ee4a1d0d7fb
Added auto(dis)connect params + misc
Goffi <goffi@goffi.org>
parents:
160
diff
changeset
|
283 def addCategory(self, name, layout, label=None): |
104 | 284 """Add a category to current layout (must be a tabs layout)""" |
285 assert(layout != 'tabs') | |
286 if not self.parentTabsLayout: | |
287 error(_("Trying to add a category without parent tabs layout")) | |
288 assert(False) | |
289 if self.parentTabsLayout.getAttribute('type') != 'tabs': | |
290 error(_("parent layout of a category is not tabs")) | |
291 assert(False) | |
183
9ee4a1d0d7fb
Added auto(dis)connect params + misc
Goffi <goffi@goffi.org>
parents:
160
diff
changeset
|
292 |
9ee4a1d0d7fb
Added auto(dis)connect params + misc
Goffi <goffi@goffi.org>
parents:
160
diff
changeset
|
293 if not label: |
9ee4a1d0d7fb
Added auto(dis)connect params + misc
Goffi <goffi@goffi.org>
parents:
160
diff
changeset
|
294 label = name |
104 | 295 self.currentCategory = cat = self.doc.createElement('category') |
296 cat.setAttribute('name', name) | |
183
9ee4a1d0d7fb
Added auto(dis)connect params + misc
Goffi <goffi@goffi.org>
parents:
160
diff
changeset
|
297 cat.setAttribute('label', label) |
104 | 298 self.changeLayout(layout) |
299 self.parentTabsLayout.appendChild(cat) | |
300 | |
103 | 301 def toXml(self): |
302 """return the XML representation of the panel""" | |
303 return self.doc.toxml() | |
289
0e54b1b0a8c8
xml_tools: added ElementParser for raw xml -> domish.Element conversion
Goffi <goffi@goffi.org>
parents:
228
diff
changeset
|
304 |
0e54b1b0a8c8
xml_tools: added ElementParser for raw xml -> domish.Element conversion
Goffi <goffi@goffi.org>
parents:
228
diff
changeset
|
305 |
0e54b1b0a8c8
xml_tools: added ElementParser for raw xml -> domish.Element conversion
Goffi <goffi@goffi.org>
parents:
228
diff
changeset
|
306 |
0e54b1b0a8c8
xml_tools: added ElementParser for raw xml -> domish.Element conversion
Goffi <goffi@goffi.org>
parents:
228
diff
changeset
|
307 class ElementParser(object): |
0e54b1b0a8c8
xml_tools: added ElementParser for raw xml -> domish.Element conversion
Goffi <goffi@goffi.org>
parents:
228
diff
changeset
|
308 """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
|
309 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
|
310 (c) Karl Anderson""" |
0e54b1b0a8c8
xml_tools: added ElementParser for raw xml -> domish.Element conversion
Goffi <goffi@goffi.org>
parents:
228
diff
changeset
|
311 |
0e54b1b0a8c8
xml_tools: added ElementParser for raw xml -> domish.Element conversion
Goffi <goffi@goffi.org>
parents:
228
diff
changeset
|
312 def __call__(self, s): |
0e54b1b0a8c8
xml_tools: added ElementParser for raw xml -> domish.Element conversion
Goffi <goffi@goffi.org>
parents:
228
diff
changeset
|
313 self.result = None |
0e54b1b0a8c8
xml_tools: added ElementParser for raw xml -> domish.Element conversion
Goffi <goffi@goffi.org>
parents:
228
diff
changeset
|
314 def onStart(el): |
0e54b1b0a8c8
xml_tools: added ElementParser for raw xml -> domish.Element conversion
Goffi <goffi@goffi.org>
parents:
228
diff
changeset
|
315 self.result = el |
0e54b1b0a8c8
xml_tools: added ElementParser for raw xml -> domish.Element conversion
Goffi <goffi@goffi.org>
parents:
228
diff
changeset
|
316 def onEnd(): |
0e54b1b0a8c8
xml_tools: added ElementParser for raw xml -> domish.Element conversion
Goffi <goffi@goffi.org>
parents:
228
diff
changeset
|
317 pass |
0e54b1b0a8c8
xml_tools: added ElementParser for raw xml -> domish.Element conversion
Goffi <goffi@goffi.org>
parents:
228
diff
changeset
|
318 def onElement(el): |
0e54b1b0a8c8
xml_tools: added ElementParser for raw xml -> domish.Element conversion
Goffi <goffi@goffi.org>
parents:
228
diff
changeset
|
319 self.result.addChild(el) |
0e54b1b0a8c8
xml_tools: added ElementParser for raw xml -> domish.Element conversion
Goffi <goffi@goffi.org>
parents:
228
diff
changeset
|
320 |
0e54b1b0a8c8
xml_tools: added ElementParser for raw xml -> domish.Element conversion
Goffi <goffi@goffi.org>
parents:
228
diff
changeset
|
321 parser = domish.elementStream() |
0e54b1b0a8c8
xml_tools: added ElementParser for raw xml -> domish.Element conversion
Goffi <goffi@goffi.org>
parents:
228
diff
changeset
|
322 parser.DocumentStartEvent = onStart |
0e54b1b0a8c8
xml_tools: added ElementParser for raw xml -> domish.Element conversion
Goffi <goffi@goffi.org>
parents:
228
diff
changeset
|
323 parser.ElementEvent = onElement |
0e54b1b0a8c8
xml_tools: added ElementParser for raw xml -> domish.Element conversion
Goffi <goffi@goffi.org>
parents:
228
diff
changeset
|
324 parser.DocumentEndEvent = onEnd |
0e54b1b0a8c8
xml_tools: added ElementParser for raw xml -> domish.Element conversion
Goffi <goffi@goffi.org>
parents:
228
diff
changeset
|
325 tmp = domish.Element(("", "s")) |
0e54b1b0a8c8
xml_tools: added ElementParser for raw xml -> domish.Element conversion
Goffi <goffi@goffi.org>
parents:
228
diff
changeset
|
326 tmp.addRawXml(s.replace('\n','').replace('\t','')) |
304 | 327 parser.parse(tmp.toXml().encode('utf-8')) |
289
0e54b1b0a8c8
xml_tools: added ElementParser for raw xml -> domish.Element conversion
Goffi <goffi@goffi.org>
parents:
228
diff
changeset
|
328 return self.result.firstChildElement() |