comparison src/tools/xml_tools.py @ 587:952322b1d490

Remove trailing whitespaces.
author Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
date Fri, 18 Jan 2013 17:55:34 +0100
parents ca13633d3b6b
children beaf6bec2fcd
comparison
equal deleted inserted replaced
586:6a718ede8be1 587:952322b1d490
25 import pdb 25 import pdb
26 from twisted.words.xish import domish 26 from twisted.words.xish import domish
27 27
28 """This library help manage XML used in SàT (parameters, registration, etc) """ 28 """This library help manage XML used in SàT (parameters, registration, etc) """
29 29
30 30
31 def dataForm2xml(form): 31 def dataForm2xml(form):
32 """Take a data form (xep-0004, Wokkel's implementation) and convert it to a SàT xml""" 32 """Take a data form (xep-0004, Wokkel's implementation) and convert it to a SàT xml"""
33 33
34 form_ui = XMLUI("form", "vertical") 34 form_ui = XMLUI("form", "vertical")
35 35
36 if form.instructions: 36 if form.instructions:
37 form_ui.addText('\n'.join(form.instructions), 'instructions') 37 form_ui.addText('\n'.join(form.instructions), 'instructions')
38 38
39 labels = filter(lambda field:field.label,form.fieldList) 39 labels = filter(lambda field:field.label,form.fieldList)
40 if labels: 40 if labels:
41 #if there is no label, we don't need to use pairs 41 #if there is no label, we don't need to use pairs
42 form_ui.changeLayout("pairs") 42 form_ui.changeLayout("pairs")
43 43
44 for field in form.fieldList: 44 for field in form.fieldList:
45 if field.fieldType == 'fixed': 45 if field.fieldType == 'fixed':
46 __field_type = 'text' 46 __field_type = 'text'
47 elif field.fieldType == 'text-single': 47 elif field.fieldType == 'text-single':
48 __field_type = "string" 48 __field_type = "string"
51 elif field.fieldType == 'list-single': 51 elif field.fieldType == 'list-single':
52 __field_type = "list" 52 __field_type = "list"
53 else: 53 else:
54 error (u"FIXME FIXME FIXME: Type [%s] is not managed yet by SàT" % field.fieldType) 54 error (u"FIXME FIXME FIXME: Type [%s] is not managed yet by SàT" % field.fieldType)
55 __field_type = "string" 55 __field_type = "string"
56 56
57 if labels: 57 if labels:
58 if field.label: 58 if field.label:
59 form_ui.addLabel(field.label) 59 form_ui.addLabel(field.label)
60 else: 60 else:
61 form_ui.addEmpty() 61 form_ui.addEmpty()
62 62
63 elem = form_ui.addElement(__field_type, field.var, field.value, [option.value for option in field.options]) 63 elem = form_ui.addElement(__field_type, field.var, field.value, [option.value for option in field.options])
64 return form_ui.toXml() 64 return form_ui.toXml()
65 65
66 def tupleList2dataForm(values): 66 def tupleList2dataForm(values):
67 """convert a list of tuples (name,value) to a wokkel submit data form""" 67 """convert a list of tuples (name,value) to a wokkel submit data form"""
68 form = data_form.Form('submit') 68 form = data_form.Form('submit')
102 param_ui.addLabel(label or name) 102 param_ui.addLabel(label or name)
103 param_ui.addElement(name=name, type=type, value=value, callback_id=callback_id) 103 param_ui.addElement(name=name, type=type, value=value, callback_id=callback_id)
104 104
105 return param_ui.toXml() 105 return param_ui.toXml()
106 106
107 107
108 108
109 109
110 class XMLUI: 110 class XMLUI:
111 """This class is used to create a user interface (form/window/parameters/etc) using SàT XML""" 111 """This class is used to create a user interface (form/window/parameters/etc) using SàT XML"""
112 112
138 self.parentTabsLayout = None #used only we have 'tabs' layout 138 self.parentTabsLayout = None #used only we have 'tabs' layout
139 self.currentCategory = None #used only we have 'tabs' layout 139 self.currentCategory = None #used only we have 'tabs' layout
140 self.changeLayout(layout) 140 self.changeLayout(layout)
141 141
142 def __del__(self): 142 def __del__(self):
143 self.doc.unlink() 143 self.doc.unlink()
144 144
145 def __createLayout(self, layout, parent=None): 145 def __createLayout(self, layout, parent=None):
146 """Create a layout element 146 """Create a layout element
147 @param type: layout type (cf init doc) 147 @param type: layout type (cf init doc)
148 @parent: parent element or None 148 @parent: parent element or None
149 """ 149 """
180 180
181 181
182 def addEmpty(self, name=None): 182 def addEmpty(self, name=None):
183 """Add a multi-lines text""" 183 """Add a multi-lines text"""
184 elem = self.__createElem('empty', name, self.currentLayout) 184 elem = self.__createElem('empty', name, self.currentLayout)
185 185
186 def addText(self, text, name=None): 186 def addText(self, text, name=None):
187 """Add a multi-lines text""" 187 """Add a multi-lines text"""
188 elem = self.__createElem('text', name, self.currentLayout) 188 elem = self.__createElem('text', name, self.currentLayout)
189 text = self.doc.createTextNode(text) 189 text = self.doc.createTextNode(text)
190 elem.appendChild(text) 190 elem.appendChild(text)
197 def addString(self, name=None, value=None): 197 def addString(self, name=None, value=None):
198 """Add a string box""" 198 """Add a string box"""
199 elem = self.__createElem('string', name, self.currentLayout) 199 elem = self.__createElem('string', name, self.currentLayout)
200 if value: 200 if value:
201 elem.setAttribute('value', value) 201 elem.setAttribute('value', value)
202 202
203 def addPassword(self, name=None, value=None): 203 def addPassword(self, name=None, value=None):
204 """Add a password box""" 204 """Add a password box"""
205 elem = self.__createElem('password', name, self.currentLayout) 205 elem = self.__createElem('password', name, self.currentLayout)
206 if value: 206 if value:
207 elem.setAttribute('value', value) 207 elem.setAttribute('value', value)
209 def addTextBox(self, name=None, value=None): 209 def addTextBox(self, name=None, value=None):
210 """Add a string box""" 210 """Add a string box"""
211 elem = self.__createElem('textbox', name, self.currentLayout) 211 elem = self.__createElem('textbox', name, self.currentLayout)
212 if value: 212 if value:
213 elem.setAttribute('value', value) 213 elem.setAttribute('value', value)
214 214
215 def addBool(self, name=None, value="true"): 215 def addBool(self, name=None, value="true"):
216 """Add a string box""" 216 """Add a string box"""
217 assert value in ["true","false"] 217 assert value in ["true","false"]
218 elem = self.__createElem('bool', name, self.currentLayout) 218 elem = self.__createElem('bool', name, self.currentLayout)
219 elem.setAttribute('value', value) 219 elem.setAttribute('value', value)
220 220
221 def addList(self, options, name=None, value=None, style=set()): 221 def addList(self, options, name=None, value=None, style=set()):
222 """Add a list of choices""" 222 """Add a list of choices"""
223 styles = set(style) 223 styles = set(style)
224 assert (options) 224 assert (options)
225 assert (styles.issubset(['multi'])) 225 assert (styles.issubset(['multi']))
226 elem = self.__createElem('list', name, self.currentLayout) 226 elem = self.__createElem('list', name, self.currentLayout)
227 self.addOptions(options, elem) 227 self.addOptions(options, elem)
228 if value: 228 if value:
229 elem.setAttribute('value', value) 229 elem.setAttribute('value', value)
230 for style in styles: 230 for style in styles:
231 elem.setAttribute(style, 'yes') 231 elem.setAttribute(style, 'yes')
232 232
243 fback_el = self.doc.createElement('field_back') 243 fback_el = self.doc.createElement('field_back')
244 fback_el.setAttribute('name', field) 244 fback_el.setAttribute('name', field)
245 elem.appendChild(fback_el) 245 elem.appendChild(fback_el)
246 246
247 247
248 248
249 def addElement(self, type, name = None, value = None, options = None, callback_id = None): 249 def addElement(self, type, name = None, value = None, options = None, callback_id = None):
250 """Convenience method to add element, the params correspond to the ones in addSomething methods""" 250 """Convenience method to add element, the params correspond to the ones in addSomething methods"""
251 if type == 'empty': 251 if type == 'empty':
252 self.addEmpty(name) 252 self.addEmpty(name)
253 elif type == 'text': 253 elif type == 'text':
297 cat.setAttribute('label', label) 297 cat.setAttribute('label', label)
298 self.changeLayout(layout) 298 self.changeLayout(layout)
299 self.parentTabsLayout.appendChild(cat) 299 self.parentTabsLayout.appendChild(cat)
300 300
301 def toXml(self): 301 def toXml(self):
302 """return the XML representation of the panel""" 302 """return the XML representation of the panel"""
303 return self.doc.toxml() 303 return self.doc.toxml()
304 304
305 305
306 306
307 class ElementParser(object): 307 class ElementParser(object):
323 parser.ElementEvent = onElement 323 parser.ElementEvent = onElement
324 parser.DocumentEndEvent = onEnd 324 parser.DocumentEndEvent = onEnd
325 tmp = domish.Element((None, "s")) 325 tmp = domish.Element((None, "s"))
326 tmp.addRawXml(s.replace('\n','').replace('\t','')) 326 tmp.addRawXml(s.replace('\n','').replace('\t',''))
327 parser.parse(tmp.toXml().encode('utf-8')) 327 parser.parse(tmp.toXml().encode('utf-8'))
328 return self.result.firstChildElement() 328 return self.result.firstChildElement()