annotate tools/xml_tools.py @ 90:4020931569b8

Tarot Game: session initialization
author Goffi <goffi@goffi.org>
date Sun, 23 May 2010 16:39:05 +0930
parents a5b5fb5fc9fd
children 39c672544593
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
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
a5b5fb5fc9fd updated README and copyright note
Goffi <goffi@goffi.org>
parents: 39
diff changeset
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
b9bb5d8e0cc7 In-band-registration: data form 2 xml conversion
Goffi <goffi@goffi.org>
parents:
diff changeset
27 class XMLTools:
b9bb5d8e0cc7 In-band-registration: data form 2 xml conversion
Goffi <goffi@goffi.org>
parents:
diff changeset
28 """This class help manage XML used in SàT (parameters, registration, etc) """
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
b9bb5d8e0cc7 In-band-registration: data form 2 xml conversion
Goffi <goffi@goffi.org>
parents:
diff changeset
31 @staticmethod
b9bb5d8e0cc7 In-band-registration: data form 2 xml conversion
Goffi <goffi@goffi.org>
parents:
diff changeset
32 def dataForm2xml(form):
b9bb5d8e0cc7 In-band-registration: data form 2 xml conversion
Goffi <goffi@goffi.org>
parents:
diff changeset
33 """Take a data form (xep-0004, Wokkel's implementation) and convert it to a SàT xml"""
35
c45deebb40a5 Wix: Registration form management (not finished yet)
Goffi <goffi@goffi.org>
parents: 33
diff changeset
34
c45deebb40a5 Wix: Registration form management (not finished yet)
Goffi <goffi@goffi.org>
parents: 33
diff changeset
35 impl = minidom.getDOMImplementation()
c45deebb40a5 Wix: Registration form management (not finished yet)
Goffi <goffi@goffi.org>
parents: 33
diff changeset
36
c45deebb40a5 Wix: Registration form management (not finished yet)
Goffi <goffi@goffi.org>
parents: 33
diff changeset
37 doc = impl.createDocument(None, "form", None)
c45deebb40a5 Wix: Registration form management (not finished yet)
Goffi <goffi@goffi.org>
parents: 33
diff changeset
38 top_element = doc.documentElement
c45deebb40a5 Wix: Registration form management (not finished yet)
Goffi <goffi@goffi.org>
parents: 33
diff changeset
39
c45deebb40a5 Wix: Registration form management (not finished yet)
Goffi <goffi@goffi.org>
parents: 33
diff changeset
40 #result_xml = ["<form>", "</form>"]
33
b9bb5d8e0cc7 In-band-registration: data form 2 xml conversion
Goffi <goffi@goffi.org>
parents:
diff changeset
41 if form.instructions:
35
c45deebb40a5 Wix: Registration form management (not finished yet)
Goffi <goffi@goffi.org>
parents: 33
diff changeset
42 elem = doc.createElement('elem')
c45deebb40a5 Wix: Registration form management (not finished yet)
Goffi <goffi@goffi.org>
parents: 33
diff changeset
43 elem.setAttribute('name','instructions')
c45deebb40a5 Wix: Registration form management (not finished yet)
Goffi <goffi@goffi.org>
parents: 33
diff changeset
44 elem.setAttribute('type','text')
c45deebb40a5 Wix: Registration form management (not finished yet)
Goffi <goffi@goffi.org>
parents: 33
diff changeset
45 text = doc.createTextNode('\n'.join(form.instructions))
c45deebb40a5 Wix: Registration form management (not finished yet)
Goffi <goffi@goffi.org>
parents: 33
diff changeset
46 elem.appendChild(text)
c45deebb40a5 Wix: Registration form management (not finished yet)
Goffi <goffi@goffi.org>
parents: 33
diff changeset
47 top_element.appendChild(elem)
33
b9bb5d8e0cc7 In-band-registration: data form 2 xml conversion
Goffi <goffi@goffi.org>
parents:
diff changeset
48 for field in form.fieldList:
b9bb5d8e0cc7 In-band-registration: data form 2 xml conversion
Goffi <goffi@goffi.org>
parents:
diff changeset
49 if field.fieldType == 'text-single':
b9bb5d8e0cc7 In-band-registration: data form 2 xml conversion
Goffi <goffi@goffi.org>
parents:
diff changeset
50 __field_type = "string"
b9bb5d8e0cc7 In-band-registration: data form 2 xml conversion
Goffi <goffi@goffi.org>
parents:
diff changeset
51 elif field.fieldType == 'text-private':
b9bb5d8e0cc7 In-band-registration: data form 2 xml conversion
Goffi <goffi@goffi.org>
parents:
diff changeset
52 __field_type = "password"
b9bb5d8e0cc7 In-band-registration: data form 2 xml conversion
Goffi <goffi@goffi.org>
parents:
diff changeset
53 else:
b9bb5d8e0cc7 In-band-registration: data form 2 xml conversion
Goffi <goffi@goffi.org>
parents:
diff changeset
54 error (u"FIXME FIXME FIXME: Type [%s] is not managed yet by SàT" % field.fieldType)
39
2e3411a6baad Wix: external server management in gateways manager, SàT: bug fixes in gateway management
Goffi <goffi@goffi.org>
parents: 37
diff changeset
55 __field_type = "string"
33
b9bb5d8e0cc7 In-band-registration: data form 2 xml conversion
Goffi <goffi@goffi.org>
parents:
diff changeset
56
35
c45deebb40a5 Wix: Registration form management (not finished yet)
Goffi <goffi@goffi.org>
parents: 33
diff changeset
57 elem = doc.createElement('elem')
c45deebb40a5 Wix: Registration form management (not finished yet)
Goffi <goffi@goffi.org>
parents: 33
diff changeset
58 elem.setAttribute('name', field.var)
c45deebb40a5 Wix: Registration form management (not finished yet)
Goffi <goffi@goffi.org>
parents: 33
diff changeset
59 elem.setAttribute('type', __field_type)
39
2e3411a6baad Wix: external server management in gateways manager, SàT: bug fixes in gateway management
Goffi <goffi@goffi.org>
parents: 37
diff changeset
60 elem.setAttribute('label', field.label or "")
37
a61beb21d16d Gateway registration, unregistration & edition
Goffi <goffi@goffi.org>
parents: 35
diff changeset
61 if field.value:
a61beb21d16d Gateway registration, unregistration & edition
Goffi <goffi@goffi.org>
parents: 35
diff changeset
62 text = doc.createTextNode(field.value)
a61beb21d16d Gateway registration, unregistration & edition
Goffi <goffi@goffi.org>
parents: 35
diff changeset
63 elem.appendChild(text)
35
c45deebb40a5 Wix: Registration form management (not finished yet)
Goffi <goffi@goffi.org>
parents: 33
diff changeset
64 top_element.appendChild(elem)
c45deebb40a5 Wix: Registration form management (not finished yet)
Goffi <goffi@goffi.org>
parents: 33
diff changeset
65 result = doc.toxml()
c45deebb40a5 Wix: Registration form management (not finished yet)
Goffi <goffi@goffi.org>
parents: 33
diff changeset
66 doc.unlink()
c45deebb40a5 Wix: Registration form management (not finished yet)
Goffi <goffi@goffi.org>
parents: 33
diff changeset
67 return result
37
a61beb21d16d Gateway registration, unregistration & edition
Goffi <goffi@goffi.org>
parents: 35
diff changeset
68
a61beb21d16d Gateway registration, unregistration & edition
Goffi <goffi@goffi.org>
parents: 35
diff changeset
69 @staticmethod
a61beb21d16d Gateway registration, unregistration & edition
Goffi <goffi@goffi.org>
parents: 35
diff changeset
70 def tupleList2dataForm(values):
a61beb21d16d Gateway registration, unregistration & edition
Goffi <goffi@goffi.org>
parents: 35
diff changeset
71 """convert a list of tuples (name,value) to a wokkel submit data form"""
a61beb21d16d Gateway registration, unregistration & edition
Goffi <goffi@goffi.org>
parents: 35
diff changeset
72 form = data_form.Form('submit')
a61beb21d16d Gateway registration, unregistration & edition
Goffi <goffi@goffi.org>
parents: 35
diff changeset
73 for value in values:
a61beb21d16d Gateway registration, unregistration & edition
Goffi <goffi@goffi.org>
parents: 35
diff changeset
74 field = data_form.Field(var=value[0], value=value[1])
a61beb21d16d Gateway registration, unregistration & edition
Goffi <goffi@goffi.org>
parents: 35
diff changeset
75 form.addField(field)
a61beb21d16d Gateway registration, unregistration & edition
Goffi <goffi@goffi.org>
parents: 35
diff changeset
76
a61beb21d16d Gateway registration, unregistration & edition
Goffi <goffi@goffi.org>
parents: 35
diff changeset
77 return form
a61beb21d16d Gateway registration, unregistration & edition
Goffi <goffi@goffi.org>
parents: 35
diff changeset
78
a61beb21d16d Gateway registration, unregistration & edition
Goffi <goffi@goffi.org>
parents: 35
diff changeset
79
a61beb21d16d Gateway registration, unregistration & edition
Goffi <goffi@goffi.org>
parents: 35
diff changeset
80