annotate src/tools/xml_tools.py @ 762:aed7d99276b8

core (xml_tools), frontends: added a prefix to XMLUI form names in result data, to avoid name conflicts (e.g.: if a form has name "submit", and this name is also used internally by SàT)
author Goffi <goffi@goffi.org>
date Tue, 24 Dec 2013 15:43:22 +0100
parents 2f8d72226bc0
children bfabeedbf32e
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
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
684
969562c4761b core (tools): small fix in using imported methods
souliane <souliane@mailoo.org>
parents: 660
diff changeset
20 from logging import debug, info, error, warning
33
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
645
17bd09cd1001 core: misc bug fixes
Goffi <goffi@goffi.org>
parents: 630
diff changeset
24 from sat.core import exceptions
33
b9bb5d8e0cc7 In-band-registration: data form 2 xml conversion
Goffi <goffi@goffi.org>
parents:
diff changeset
25
102
94011f553cd0 misc bugfixes
Goffi <goffi@goffi.org>
parents: 95
diff changeset
26 """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
27
762
aed7d99276b8 core (xml_tools), frontends: added a prefix to XMLUI form names in result data, to avoid name conflicts (e.g.: if a form has name "submit", and this name is also used internally by SàT)
Goffi <goffi@goffi.org>
parents: 761
diff changeset
28 SAT_FORM_PREFIX ="SAT_FORM_"
aed7d99276b8 core (xml_tools), frontends: added a prefix to XMLUI form names in result data, to avoid name conflicts (e.g.: if a form has name "submit", and this name is also used internally by SàT)
Goffi <goffi@goffi.org>
parents: 761
diff changeset
29
587
952322b1d490 Remove trailing whitespaces.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 572
diff changeset
30
761
2f8d72226bc0 core (xml_tools): dataForm*2XML renamed to dataForm*2XMLUI and now return XMLUI instead of raw XML + submit_id is managed, and session_id is returned if present
Goffi <goffi@goffi.org>
parents: 760
diff changeset
31 def dataForm2XMLUI(form, submit_id, session_id=None):
102
94011f553cd0 misc bugfixes
Goffi <goffi@goffi.org>
parents: 95
diff changeset
32 """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
33
761
2f8d72226bc0 core (xml_tools): dataForm*2XML renamed to dataForm*2XMLUI and now return XMLUI instead of raw XML + submit_id is managed, and session_id is returned if present
Goffi <goffi@goffi.org>
parents: 760
diff changeset
34 form_ui = XMLUI("form", "vertical", submit_id=submit_id, session_id=session_id)
104
5458ac1380cc XMLUI: added tabs layout
Goffi <goffi@goffi.org>
parents: 103
diff changeset
35
102
94011f553cd0 misc bugfixes
Goffi <goffi@goffi.org>
parents: 95
diff changeset
36 if form.instructions:
105
d2630fba8dfd params to XMLUI tools
Goffi <goffi@goffi.org>
parents: 104
diff changeset
37 form_ui.addText('\n'.join(form.instructions), 'instructions')
587
952322b1d490 Remove trailing whitespaces.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 572
diff changeset
38
590
56531f9e9ac7 Fix pep8 support in src/tools.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 588
diff changeset
39 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
40 if labels:
590
56531f9e9ac7 Fix pep8 support in src/tools.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 588
diff changeset
41 # 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
42 form_ui.changeLayout("pairs")
587
952322b1d490 Remove trailing whitespaces.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 572
diff changeset
43
102
94011f553cd0 misc bugfixes
Goffi <goffi@goffi.org>
parents: 95
diff changeset
44 for field in form.fieldList:
94011f553cd0 misc bugfixes
Goffi <goffi@goffi.org>
parents: 95
diff changeset
45 if field.fieldType == 'fixed':
94011f553cd0 misc bugfixes
Goffi <goffi@goffi.org>
parents: 95
diff changeset
46 __field_type = 'text'
94011f553cd0 misc bugfixes
Goffi <goffi@goffi.org>
parents: 95
diff changeset
47 elif field.fieldType == 'text-single':
94011f553cd0 misc bugfixes
Goffi <goffi@goffi.org>
parents: 95
diff changeset
48 __field_type = "string"
94011f553cd0 misc bugfixes
Goffi <goffi@goffi.org>
parents: 95
diff changeset
49 elif field.fieldType == 'text-private':
94011f553cd0 misc bugfixes
Goffi <goffi@goffi.org>
parents: 95
diff changeset
50 __field_type = "password"
94011f553cd0 misc bugfixes
Goffi <goffi@goffi.org>
parents: 95
diff changeset
51 elif field.fieldType == 'list-single':
94011f553cd0 misc bugfixes
Goffi <goffi@goffi.org>
parents: 95
diff changeset
52 __field_type = "list"
94011f553cd0 misc bugfixes
Goffi <goffi@goffi.org>
parents: 95
diff changeset
53 else:
590
56531f9e9ac7 Fix pep8 support in src/tools.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 588
diff changeset
54 error(u"FIXME FIXME FIXME: Type [%s] is not managed yet by SàT" % field.fieldType)
102
94011f553cd0 misc bugfixes
Goffi <goffi@goffi.org>
parents: 95
diff changeset
55 __field_type = "string"
587
952322b1d490 Remove trailing whitespaces.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 572
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
6be927a465ed XMLUI refactoring, step 1
Goffi <goffi@goffi.org>
parents: 102
diff changeset
62
590
56531f9e9ac7 Fix pep8 support in src/tools.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 588
diff changeset
63 form_ui.addElement(__field_type, field.var, field.value, [option.value for option in field.options])
761
2f8d72226bc0 core (xml_tools): dataForm*2XML renamed to dataForm*2XMLUI and now return XMLUI instead of raw XML + submit_id is managed, and session_id is returned if present
Goffi <goffi@goffi.org>
parents: 760
diff changeset
64 return form_ui
37
a61beb21d16d Gateway registration, unregistration & edition
Goffi <goffi@goffi.org>
parents: 35
diff changeset
65
630
0b914394e74f core: added advanced list to XMLUI (need improvment, very basic so far)
Goffi <goffi@goffi.org>
parents: 615
diff changeset
66 def dataFormResult2AdvancedList(form_ui, form_xml):
0b914394e74f core: added advanced list to XMLUI (need improvment, very basic so far)
Goffi <goffi@goffi.org>
parents: 615
diff changeset
67 """Take a raw data form (not parsed by XEP-0004) and convert it to an advanced list
0b914394e74f core: added advanced list to XMLUI (need improvment, very basic so far)
Goffi <goffi@goffi.org>
parents: 615
diff changeset
68 raw data form is used because Wokkel doesn't manage result items parsing yet
761
2f8d72226bc0 core (xml_tools): dataForm*2XML renamed to dataForm*2XMLUI and now return XMLUI instead of raw XML + submit_id is managed, and session_id is returned if present
Goffi <goffi@goffi.org>
parents: 760
diff changeset
69 @param form_ui: the XMLUI where the AdvancedList will be added
630
0b914394e74f core: added advanced list to XMLUI (need improvment, very basic so far)
Goffi <goffi@goffi.org>
parents: 615
diff changeset
70 @param form_xml: domish.Element of the data form
0b914394e74f core: added advanced list to XMLUI (need improvment, very basic so far)
Goffi <goffi@goffi.org>
parents: 615
diff changeset
71 @return: AdvancedList element
0b914394e74f core: added advanced list to XMLUI (need improvment, very basic so far)
Goffi <goffi@goffi.org>
parents: 615
diff changeset
72 """
0b914394e74f core: added advanced list to XMLUI (need improvment, very basic so far)
Goffi <goffi@goffi.org>
parents: 615
diff changeset
73 headers = []
0b914394e74f core: added advanced list to XMLUI (need improvment, very basic so far)
Goffi <goffi@goffi.org>
parents: 615
diff changeset
74 items = []
0b914394e74f core: added advanced list to XMLUI (need improvment, very basic so far)
Goffi <goffi@goffi.org>
parents: 615
diff changeset
75 try:
0b914394e74f core: added advanced list to XMLUI (need improvment, very basic so far)
Goffi <goffi@goffi.org>
parents: 615
diff changeset
76 reported_elt = form_xml.elements('jabber:x:data', 'reported').next()
0b914394e74f core: added advanced list to XMLUI (need improvment, very basic so far)
Goffi <goffi@goffi.org>
parents: 615
diff changeset
77 except StopIteration:
0b914394e74f core: added advanced list to XMLUI (need improvment, very basic so far)
Goffi <goffi@goffi.org>
parents: 615
diff changeset
78 raise exceptions.DataError("Couldn't find expected <reported> tag")
0b914394e74f core: added advanced list to XMLUI (need improvment, very basic so far)
Goffi <goffi@goffi.org>
parents: 615
diff changeset
79
660
69a8bfd266a5 core, plugins: fixed bad use of children instead of elements() for domish.Element instances.
Goffi <goffi@goffi.org>
parents: 655
diff changeset
80 for elt in reported_elt.elements():
630
0b914394e74f core: added advanced list to XMLUI (need improvment, very basic so far)
Goffi <goffi@goffi.org>
parents: 615
diff changeset
81 if elt.name != "field":
0b914394e74f core: added advanced list to XMLUI (need improvment, very basic so far)
Goffi <goffi@goffi.org>
parents: 615
diff changeset
82 raise exceptions.DataError("Unexpected tag")
0b914394e74f core: added advanced list to XMLUI (need improvment, very basic so far)
Goffi <goffi@goffi.org>
parents: 615
diff changeset
83 name = elt["var"]
0b914394e74f core: added advanced list to XMLUI (need improvment, very basic so far)
Goffi <goffi@goffi.org>
parents: 615
diff changeset
84 label = elt.attributes.get('label','')
0b914394e74f core: added advanced list to XMLUI (need improvment, very basic so far)
Goffi <goffi@goffi.org>
parents: 615
diff changeset
85 type_ = elt.attributes.get('type','') # TODO
0b914394e74f core: added advanced list to XMLUI (need improvment, very basic so far)
Goffi <goffi@goffi.org>
parents: 615
diff changeset
86 headers.append(Header(name, label))
0b914394e74f core: added advanced list to XMLUI (need improvment, very basic so far)
Goffi <goffi@goffi.org>
parents: 615
diff changeset
87
0b914394e74f core: added advanced list to XMLUI (need improvment, very basic so far)
Goffi <goffi@goffi.org>
parents: 615
diff changeset
88 if not headers:
684
969562c4761b core (tools): small fix in using imported methods
souliane <souliane@mailoo.org>
parents: 660
diff changeset
89 raise exceptions.DataError("No reported fields (see XEP-0004 §3.4)")
630
0b914394e74f core: added advanced list to XMLUI (need improvment, very basic so far)
Goffi <goffi@goffi.org>
parents: 615
diff changeset
90
0b914394e74f core: added advanced list to XMLUI (need improvment, very basic so far)
Goffi <goffi@goffi.org>
parents: 615
diff changeset
91 item_elts = form_xml.elements('jabber:x:data', 'item')
0b914394e74f core: added advanced list to XMLUI (need improvment, very basic so far)
Goffi <goffi@goffi.org>
parents: 615
diff changeset
92
0b914394e74f core: added advanced list to XMLUI (need improvment, very basic so far)
Goffi <goffi@goffi.org>
parents: 615
diff changeset
93 for item_elt in item_elts:
0b914394e74f core: added advanced list to XMLUI (need improvment, very basic so far)
Goffi <goffi@goffi.org>
parents: 615
diff changeset
94 fields = []
660
69a8bfd266a5 core, plugins: fixed bad use of children instead of elements() for domish.Element instances.
Goffi <goffi@goffi.org>
parents: 655
diff changeset
95 for elt in item_elt.elements():
630
0b914394e74f core: added advanced list to XMLUI (need improvment, very basic so far)
Goffi <goffi@goffi.org>
parents: 615
diff changeset
96 if elt.name != 'field':
0b914394e74f core: added advanced list to XMLUI (need improvment, very basic so far)
Goffi <goffi@goffi.org>
parents: 615
diff changeset
97 warning("Unexpected tag (%s)" % elt.name)
0b914394e74f core: added advanced list to XMLUI (need improvment, very basic so far)
Goffi <goffi@goffi.org>
parents: 615
diff changeset
98 continue
0b914394e74f core: added advanced list to XMLUI (need improvment, very basic so far)
Goffi <goffi@goffi.org>
parents: 615
diff changeset
99 name = elt['var']
0b914394e74f core: added advanced list to XMLUI (need improvment, very basic so far)
Goffi <goffi@goffi.org>
parents: 615
diff changeset
100 child_elt = elt.firstChildElement()
0b914394e74f core: added advanced list to XMLUI (need improvment, very basic so far)
Goffi <goffi@goffi.org>
parents: 615
diff changeset
101 if child_elt.name != "value":
684
969562c4761b core (tools): small fix in using imported methods
souliane <souliane@mailoo.org>
parents: 660
diff changeset
102 raise exceptions.DataError('Was expecting <value> tag')
630
0b914394e74f core: added advanced list to XMLUI (need improvment, very basic so far)
Goffi <goffi@goffi.org>
parents: 615
diff changeset
103 value = unicode(child_elt)
0b914394e74f core: added advanced list to XMLUI (need improvment, very basic so far)
Goffi <goffi@goffi.org>
parents: 615
diff changeset
104 fields.append(Field(name, value))
0b914394e74f core: added advanced list to XMLUI (need improvment, very basic so far)
Goffi <goffi@goffi.org>
parents: 615
diff changeset
105 items.append(Item(' | '.join((field.value for field in fields if field)), fields))
0b914394e74f core: added advanced list to XMLUI (need improvment, very basic so far)
Goffi <goffi@goffi.org>
parents: 615
diff changeset
106
0b914394e74f core: added advanced list to XMLUI (need improvment, very basic so far)
Goffi <goffi@goffi.org>
parents: 615
diff changeset
107 return form_ui.addAdvancedList(None, headers, items)
0b914394e74f core: added advanced list to XMLUI (need improvment, very basic so far)
Goffi <goffi@goffi.org>
parents: 615
diff changeset
108
0b914394e74f core: added advanced list to XMLUI (need improvment, very basic so far)
Goffi <goffi@goffi.org>
parents: 615
diff changeset
109
761
2f8d72226bc0 core (xml_tools): dataForm*2XML renamed to dataForm*2XMLUI and now return XMLUI instead of raw XML + submit_id is managed, and session_id is returned if present
Goffi <goffi@goffi.org>
parents: 760
diff changeset
110 def dataFormResult2XMLUI(form_xml, session_id=None):
630
0b914394e74f core: added advanced list to XMLUI (need improvment, very basic so far)
Goffi <goffi@goffi.org>
parents: 615
diff changeset
111 """Take a raw data form (not parsed by XEP-0004) and convert it to a SàT XMLUI
0b914394e74f core: added advanced list to XMLUI (need improvment, very basic so far)
Goffi <goffi@goffi.org>
parents: 615
diff changeset
112 raw data form is used because Wokkel doesn't manage result items parsing yet
0b914394e74f core: added advanced list to XMLUI (need improvment, very basic so far)
Goffi <goffi@goffi.org>
parents: 615
diff changeset
113 @param form_xml: domish.Element of the data form
0b914394e74f core: added advanced list to XMLUI (need improvment, very basic so far)
Goffi <goffi@goffi.org>
parents: 615
diff changeset
114 @return: XMLUI interface
0b914394e74f core: added advanced list to XMLUI (need improvment, very basic so far)
Goffi <goffi@goffi.org>
parents: 615
diff changeset
115 """
0b914394e74f core: added advanced list to XMLUI (need improvment, very basic so far)
Goffi <goffi@goffi.org>
parents: 615
diff changeset
116
761
2f8d72226bc0 core (xml_tools): dataForm*2XML renamed to dataForm*2XMLUI and now return XMLUI instead of raw XML + submit_id is managed, and session_id is returned if present
Goffi <goffi@goffi.org>
parents: 760
diff changeset
117 form_ui = XMLUI("window", "vertical", session_id=session_id)
630
0b914394e74f core: added advanced list to XMLUI (need improvment, very basic so far)
Goffi <goffi@goffi.org>
parents: 615
diff changeset
118 dataFormResult2AdvancedList(form_ui, form_xml)
761
2f8d72226bc0 core (xml_tools): dataForm*2XML renamed to dataForm*2XMLUI and now return XMLUI instead of raw XML + submit_id is managed, and session_id is returned if present
Goffi <goffi@goffi.org>
parents: 760
diff changeset
119 return form_ui
590
56531f9e9ac7 Fix pep8 support in src/tools.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 588
diff changeset
120
762
aed7d99276b8 core (xml_tools), frontends: added a prefix to XMLUI form names in result data, to avoid name conflicts (e.g.: if a form has name "submit", and this name is also used internally by SàT)
Goffi <goffi@goffi.org>
parents: 761
diff changeset
121 def XMLUIResult2DataFormResult(xmlui_data):
aed7d99276b8 core (xml_tools), frontends: added a prefix to XMLUI form names in result data, to avoid name conflicts (e.g.: if a form has name "submit", and this name is also used internally by SàT)
Goffi <goffi@goffi.org>
parents: 761
diff changeset
122 """ Extract form data from a XMLUI return
aed7d99276b8 core (xml_tools), frontends: added a prefix to XMLUI form names in result data, to avoid name conflicts (e.g.: if a form has name "submit", and this name is also used internally by SàT)
Goffi <goffi@goffi.org>
parents: 761
diff changeset
123 @xmlui_data: data returned by frontends for XMLUI form
aed7d99276b8 core (xml_tools), frontends: added a prefix to XMLUI form names in result data, to avoid name conflicts (e.g.: if a form has name "submit", and this name is also used internally by SàT)
Goffi <goffi@goffi.org>
parents: 761
diff changeset
124 @return: dict of data usable by Wokkel's dataform
aed7d99276b8 core (xml_tools), frontends: added a prefix to XMLUI form names in result data, to avoid name conflicts (e.g.: if a form has name "submit", and this name is also used internally by SàT)
Goffi <goffi@goffi.org>
parents: 761
diff changeset
125 """
aed7d99276b8 core (xml_tools), frontends: added a prefix to XMLUI form names in result data, to avoid name conflicts (e.g.: if a form has name "submit", and this name is also used internally by SàT)
Goffi <goffi@goffi.org>
parents: 761
diff changeset
126 return {key[len(SAT_FORM_PREFIX):]: value for key, value in xmlui_data.iteritems() if key.startswith(SAT_FORM_PREFIX)}
aed7d99276b8 core (xml_tools), frontends: added a prefix to XMLUI form names in result data, to avoid name conflicts (e.g.: if a form has name "submit", and this name is also used internally by SàT)
Goffi <goffi@goffi.org>
parents: 761
diff changeset
127
aed7d99276b8 core (xml_tools), frontends: added a prefix to XMLUI form names in result data, to avoid name conflicts (e.g.: if a form has name "submit", and this name is also used internally by SàT)
Goffi <goffi@goffi.org>
parents: 761
diff changeset
128 def XMLUIResultToElt(xmlui_data):
aed7d99276b8 core (xml_tools), frontends: added a prefix to XMLUI form names in result data, to avoid name conflicts (e.g.: if a form has name "submit", and this name is also used internally by SàT)
Goffi <goffi@goffi.org>
parents: 761
diff changeset
129 """ Construct result domish.Element from XMLUI result
aed7d99276b8 core (xml_tools), frontends: added a prefix to XMLUI form names in result data, to avoid name conflicts (e.g.: if a form has name "submit", and this name is also used internally by SàT)
Goffi <goffi@goffi.org>
parents: 761
diff changeset
130 @xmlui_data: data returned by frontends for XMLUI form
aed7d99276b8 core (xml_tools), frontends: added a prefix to XMLUI form names in result data, to avoid name conflicts (e.g.: if a form has name "submit", and this name is also used internally by SàT)
Goffi <goffi@goffi.org>
parents: 761
diff changeset
131 """
aed7d99276b8 core (xml_tools), frontends: added a prefix to XMLUI form names in result data, to avoid name conflicts (e.g.: if a form has name "submit", and this name is also used internally by SàT)
Goffi <goffi@goffi.org>
parents: 761
diff changeset
132 form = data_form.Form('result')
aed7d99276b8 core (xml_tools), frontends: added a prefix to XMLUI form names in result data, to avoid name conflicts (e.g.: if a form has name "submit", and this name is also used internally by SàT)
Goffi <goffi@goffi.org>
parents: 761
diff changeset
133 form.makeFields(XMLUIResult2DataFormResult(xmlui_data))
aed7d99276b8 core (xml_tools), frontends: added a prefix to XMLUI form names in result data, to avoid name conflicts (e.g.: if a form has name "submit", and this name is also used internally by SàT)
Goffi <goffi@goffi.org>
parents: 761
diff changeset
134 return form.toElement()
aed7d99276b8 core (xml_tools), frontends: added a prefix to XMLUI form names in result data, to avoid name conflicts (e.g.: if a form has name "submit", and this name is also used internally by SàT)
Goffi <goffi@goffi.org>
parents: 761
diff changeset
135
102
94011f553cd0 misc bugfixes
Goffi <goffi@goffi.org>
parents: 95
diff changeset
136 def tupleList2dataForm(values):
94011f553cd0 misc bugfixes
Goffi <goffi@goffi.org>
parents: 95
diff changeset
137 """convert a list of tuples (name,value) to a wokkel submit data form"""
94011f553cd0 misc bugfixes
Goffi <goffi@goffi.org>
parents: 95
diff changeset
138 form = data_form.Form('submit')
94011f553cd0 misc bugfixes
Goffi <goffi@goffi.org>
parents: 95
diff changeset
139 for value in values:
94011f553cd0 misc bugfixes
Goffi <goffi@goffi.org>
parents: 95
diff changeset
140 field = data_form.Field(var=value[0], value=value[1])
94011f553cd0 misc bugfixes
Goffi <goffi@goffi.org>
parents: 95
diff changeset
141 form.addField(field)
37
a61beb21d16d Gateway registration, unregistration & edition
Goffi <goffi@goffi.org>
parents: 35
diff changeset
142
102
94011f553cd0 misc bugfixes
Goffi <goffi@goffi.org>
parents: 95
diff changeset
143 return form
103
6be927a465ed XMLUI refactoring, step 1
Goffi <goffi@goffi.org>
parents: 102
diff changeset
144
105
d2630fba8dfd params to XMLUI tools
Goffi <goffi@goffi.org>
parents: 104
diff changeset
145 def paramsXml2xmlUI(xml):
d2630fba8dfd params to XMLUI tools
Goffi <goffi@goffi.org>
parents: 104
diff changeset
146 """Convert the xml for parameter to a SàT XML User Interface"""
214
e178e8f6d13a fixed some unicode issue
Goffi <goffi@goffi.org>
parents: 183
diff changeset
147 params_doc = minidom.parseString(xml.encode('utf-8'))
105
d2630fba8dfd params to XMLUI tools
Goffi <goffi@goffi.org>
parents: 104
diff changeset
148 top = params_doc.documentElement
d2630fba8dfd params to XMLUI tools
Goffi <goffi@goffi.org>
parents: 104
diff changeset
149 if top.nodeName != 'params':
d2630fba8dfd params to XMLUI tools
Goffi <goffi@goffi.org>
parents: 104
diff changeset
150 error(_('INTERNAL ERROR: parameters xml not valid'))
d2630fba8dfd params to XMLUI tools
Goffi <goffi@goffi.org>
parents: 104
diff changeset
151 assert(False)
d2630fba8dfd params to XMLUI tools
Goffi <goffi@goffi.org>
parents: 104
diff changeset
152 param_ui = XMLUI("param", "tabs")
d2630fba8dfd params to XMLUI tools
Goffi <goffi@goffi.org>
parents: 104
diff changeset
153 for category in top.getElementsByTagName("category"):
d2630fba8dfd params to XMLUI tools
Goffi <goffi@goffi.org>
parents: 104
diff changeset
154 name = category.getAttribute('name')
183
9ee4a1d0d7fb Added auto(dis)connect params + misc
Goffi <goffi@goffi.org>
parents: 160
diff changeset
155 label = category.getAttribute('label')
105
d2630fba8dfd params to XMLUI tools
Goffi <goffi@goffi.org>
parents: 104
diff changeset
156 if not name:
d2630fba8dfd params to XMLUI tools
Goffi <goffi@goffi.org>
parents: 104
diff changeset
157 error(_('INTERNAL ERROR: params categories must have a name'))
d2630fba8dfd params to XMLUI tools
Goffi <goffi@goffi.org>
parents: 104
diff changeset
158 assert(False)
183
9ee4a1d0d7fb Added auto(dis)connect params + misc
Goffi <goffi@goffi.org>
parents: 160
diff changeset
159 param_ui.addCategory(name, 'pairs', label=label)
105
d2630fba8dfd params to XMLUI tools
Goffi <goffi@goffi.org>
parents: 104
diff changeset
160 for param in category.getElementsByTagName("param"):
d2630fba8dfd params to XMLUI tools
Goffi <goffi@goffi.org>
parents: 104
diff changeset
161 name = param.getAttribute('name')
183
9ee4a1d0d7fb Added auto(dis)connect params + misc
Goffi <goffi@goffi.org>
parents: 160
diff changeset
162 label = param.getAttribute('label')
105
d2630fba8dfd params to XMLUI tools
Goffi <goffi@goffi.org>
parents: 104
diff changeset
163 if not name:
d2630fba8dfd params to XMLUI tools
Goffi <goffi@goffi.org>
parents: 104
diff changeset
164 error(_('INTERNAL ERROR: params must have a name'))
d2630fba8dfd params to XMLUI tools
Goffi <goffi@goffi.org>
parents: 104
diff changeset
165 assert(False)
590
56531f9e9ac7 Fix pep8 support in src/tools.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 588
diff changeset
166 type_ = param.getAttribute('type')
105
d2630fba8dfd params to XMLUI tools
Goffi <goffi@goffi.org>
parents: 104
diff changeset
167 value = param.getAttribute('value') or None
655
56f8a9c99194 core, primitivus: better support for parameter of type list
souliane <souliane@mailoo.org>
parents: 645
diff changeset
168 options = getOptions(param)
106
138d82f64b6f plugin CS: friends parsing
Goffi <goffi@goffi.org>
parents: 105
diff changeset
169 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
170 if type_ == "button":
106
138d82f64b6f plugin CS: friends parsing
Goffi <goffi@goffi.org>
parents: 105
diff changeset
171 param_ui.addEmpty()
138d82f64b6f plugin CS: friends parsing
Goffi <goffi@goffi.org>
parents: 105
diff changeset
172 else:
183
9ee4a1d0d7fb Added auto(dis)connect params + misc
Goffi <goffi@goffi.org>
parents: 160
diff changeset
173 param_ui.addLabel(label or name)
655
56f8a9c99194 core, primitivus: better support for parameter of type list
souliane <souliane@mailoo.org>
parents: 645
diff changeset
174 param_ui.addElement(name=name, type_=type_, value=value, options=options, callback_id=callback_id)
105
d2630fba8dfd params to XMLUI tools
Goffi <goffi@goffi.org>
parents: 104
diff changeset
175
d2630fba8dfd params to XMLUI tools
Goffi <goffi@goffi.org>
parents: 104
diff changeset
176 return param_ui.toXml()
d2630fba8dfd params to XMLUI tools
Goffi <goffi@goffi.org>
parents: 104
diff changeset
177
587
952322b1d490 Remove trailing whitespaces.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 572
diff changeset
178
655
56f8a9c99194 core, primitivus: better support for parameter of type list
souliane <souliane@mailoo.org>
parents: 645
diff changeset
179 def getOptions(param):
56f8a9c99194 core, primitivus: better support for parameter of type list
souliane <souliane@mailoo.org>
parents: 645
diff changeset
180 """Retrieve the options for list element. Allow listing the <option/>
56f8a9c99194 core, primitivus: better support for parameter of type list
souliane <souliane@mailoo.org>
parents: 645
diff changeset
181 tags directly in <param/> or in an intermediate <options/> tag."""
56f8a9c99194 core, primitivus: better support for parameter of type list
souliane <souliane@mailoo.org>
parents: 645
diff changeset
182 elems = param.getElementsByTagName("options")
56f8a9c99194 core, primitivus: better support for parameter of type list
souliane <souliane@mailoo.org>
parents: 645
diff changeset
183 if len(elems) == 0:
56f8a9c99194 core, primitivus: better support for parameter of type list
souliane <souliane@mailoo.org>
parents: 645
diff changeset
184 elems = param.getElementsByTagName("option")
56f8a9c99194 core, primitivus: better support for parameter of type list
souliane <souliane@mailoo.org>
parents: 645
diff changeset
185 else:
56f8a9c99194 core, primitivus: better support for parameter of type list
souliane <souliane@mailoo.org>
parents: 645
diff changeset
186 elems = elems.item(0).getElementsByTagName("option")
56f8a9c99194 core, primitivus: better support for parameter of type list
souliane <souliane@mailoo.org>
parents: 645
diff changeset
187 if len(elems) == 0:
56f8a9c99194 core, primitivus: better support for parameter of type list
souliane <souliane@mailoo.org>
parents: 645
diff changeset
188 return []
56f8a9c99194 core, primitivus: better support for parameter of type list
souliane <souliane@mailoo.org>
parents: 645
diff changeset
189 return [elem.getAttribute("value") for elem in elems]
56f8a9c99194 core, primitivus: better support for parameter of type list
souliane <souliane@mailoo.org>
parents: 645
diff changeset
190
56f8a9c99194 core, primitivus: better support for parameter of type list
souliane <souliane@mailoo.org>
parents: 645
diff changeset
191
630
0b914394e74f core: added advanced list to XMLUI (need improvment, very basic so far)
Goffi <goffi@goffi.org>
parents: 615
diff changeset
192 class Header(object):
0b914394e74f core: added advanced list to XMLUI (need improvment, very basic so far)
Goffi <goffi@goffi.org>
parents: 615
diff changeset
193 """AdvandeList's header"""
0b914394e74f core: added advanced list to XMLUI (need improvment, very basic so far)
Goffi <goffi@goffi.org>
parents: 615
diff changeset
194
0b914394e74f core: added advanced list to XMLUI (need improvment, very basic so far)
Goffi <goffi@goffi.org>
parents: 615
diff changeset
195 def __init__(self, field_name, label, description=None, type_=None):
0b914394e74f core: added advanced list to XMLUI (need improvment, very basic so far)
Goffi <goffi@goffi.org>
parents: 615
diff changeset
196 """
0b914394e74f core: added advanced list to XMLUI (need improvment, very basic so far)
Goffi <goffi@goffi.org>
parents: 615
diff changeset
197 @param field_name: name of the field referenced
0b914394e74f core: added advanced list to XMLUI (need improvment, very basic so far)
Goffi <goffi@goffi.org>
parents: 615
diff changeset
198 @param label: label to be displayed in columns
0b914394e74f core: added advanced list to XMLUI (need improvment, very basic so far)
Goffi <goffi@goffi.org>
parents: 615
diff changeset
199 @param description: long descriptive text
0b914394e74f core: added advanced list to XMLUI (need improvment, very basic so far)
Goffi <goffi@goffi.org>
parents: 615
diff changeset
200 @param type_: TODO
0b914394e74f core: added advanced list to XMLUI (need improvment, very basic so far)
Goffi <goffi@goffi.org>
parents: 615
diff changeset
201
0b914394e74f core: added advanced list to XMLUI (need improvment, very basic so far)
Goffi <goffi@goffi.org>
parents: 615
diff changeset
202 """
0b914394e74f core: added advanced list to XMLUI (need improvment, very basic so far)
Goffi <goffi@goffi.org>
parents: 615
diff changeset
203 self.field_name = field_name
0b914394e74f core: added advanced list to XMLUI (need improvment, very basic so far)
Goffi <goffi@goffi.org>
parents: 615
diff changeset
204 self.label = label
0b914394e74f core: added advanced list to XMLUI (need improvment, very basic so far)
Goffi <goffi@goffi.org>
parents: 615
diff changeset
205 self.description = description
0b914394e74f core: added advanced list to XMLUI (need improvment, very basic so far)
Goffi <goffi@goffi.org>
parents: 615
diff changeset
206 self.type = type_
0b914394e74f core: added advanced list to XMLUI (need improvment, very basic so far)
Goffi <goffi@goffi.org>
parents: 615
diff changeset
207 if type_ is not None:
0b914394e74f core: added advanced list to XMLUI (need improvment, very basic so far)
Goffi <goffi@goffi.org>
parents: 615
diff changeset
208 raise NotImplementedError # TODO:
0b914394e74f core: added advanced list to XMLUI (need improvment, very basic so far)
Goffi <goffi@goffi.org>
parents: 615
diff changeset
209
0b914394e74f core: added advanced list to XMLUI (need improvment, very basic so far)
Goffi <goffi@goffi.org>
parents: 615
diff changeset
210
0b914394e74f core: added advanced list to XMLUI (need improvment, very basic so far)
Goffi <goffi@goffi.org>
parents: 615
diff changeset
211 class Item(object):
0b914394e74f core: added advanced list to XMLUI (need improvment, very basic so far)
Goffi <goffi@goffi.org>
parents: 615
diff changeset
212 """Item used in AdvancedList"""
0b914394e74f core: added advanced list to XMLUI (need improvment, very basic so far)
Goffi <goffi@goffi.org>
parents: 615
diff changeset
213
0b914394e74f core: added advanced list to XMLUI (need improvment, very basic so far)
Goffi <goffi@goffi.org>
parents: 615
diff changeset
214 def __init__(self, text=None, fields=None):
0b914394e74f core: added advanced list to XMLUI (need improvment, very basic so far)
Goffi <goffi@goffi.org>
parents: 615
diff changeset
215 """
0b914394e74f core: added advanced list to XMLUI (need improvment, very basic so far)
Goffi <goffi@goffi.org>
parents: 615
diff changeset
216 @param text: Optional textual representation, when fields are not showed individually
0b914394e74f core: added advanced list to XMLUI (need improvment, very basic so far)
Goffi <goffi@goffi.org>
parents: 615
diff changeset
217 @param fields: list of Field instances²
0b914394e74f core: added advanced list to XMLUI (need improvment, very basic so far)
Goffi <goffi@goffi.org>
parents: 615
diff changeset
218 """
0b914394e74f core: added advanced list to XMLUI (need improvment, very basic so far)
Goffi <goffi@goffi.org>
parents: 615
diff changeset
219 self.text = text
0b914394e74f core: added advanced list to XMLUI (need improvment, very basic so far)
Goffi <goffi@goffi.org>
parents: 615
diff changeset
220 self.fields = fields if fields is not None else []
0b914394e74f core: added advanced list to XMLUI (need improvment, very basic so far)
Goffi <goffi@goffi.org>
parents: 615
diff changeset
221
0b914394e74f core: added advanced list to XMLUI (need improvment, very basic so far)
Goffi <goffi@goffi.org>
parents: 615
diff changeset
222
0b914394e74f core: added advanced list to XMLUI (need improvment, very basic so far)
Goffi <goffi@goffi.org>
parents: 615
diff changeset
223 class Field(object):
0b914394e74f core: added advanced list to XMLUI (need improvment, very basic so far)
Goffi <goffi@goffi.org>
parents: 615
diff changeset
224 """Field used in AdvancedList (in items)"""
0b914394e74f core: added advanced list to XMLUI (need improvment, very basic so far)
Goffi <goffi@goffi.org>
parents: 615
diff changeset
225
0b914394e74f core: added advanced list to XMLUI (need improvment, very basic so far)
Goffi <goffi@goffi.org>
parents: 615
diff changeset
226 def __init__(self, name, value):
0b914394e74f core: added advanced list to XMLUI (need improvment, very basic so far)
Goffi <goffi@goffi.org>
parents: 615
diff changeset
227 """
0b914394e74f core: added advanced list to XMLUI (need improvment, very basic so far)
Goffi <goffi@goffi.org>
parents: 615
diff changeset
228 @param name: name of the field, used to identify the field in headers
0b914394e74f core: added advanced list to XMLUI (need improvment, very basic so far)
Goffi <goffi@goffi.org>
parents: 615
diff changeset
229 @param value: actual content of the field
0b914394e74f core: added advanced list to XMLUI (need improvment, very basic so far)
Goffi <goffi@goffi.org>
parents: 615
diff changeset
230 """
0b914394e74f core: added advanced list to XMLUI (need improvment, very basic so far)
Goffi <goffi@goffi.org>
parents: 615
diff changeset
231 self.name = name
0b914394e74f core: added advanced list to XMLUI (need improvment, very basic so far)
Goffi <goffi@goffi.org>
parents: 615
diff changeset
232 self.value = value
0b914394e74f core: added advanced list to XMLUI (need improvment, very basic so far)
Goffi <goffi@goffi.org>
parents: 615
diff changeset
233
0b914394e74f core: added advanced list to XMLUI (need improvment, very basic so far)
Goffi <goffi@goffi.org>
parents: 615
diff changeset
234
0b914394e74f core: added advanced list to XMLUI (need improvment, very basic so far)
Goffi <goffi@goffi.org>
parents: 615
diff changeset
235
588
beaf6bec2fcd Remove every old-style class.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 587
diff changeset
236 class XMLUI(object):
103
6be927a465ed XMLUI refactoring, step 1
Goffi <goffi@goffi.org>
parents: 102
diff changeset
237 """This class is used to create a user interface (form/window/parameters/etc) using SàT XML"""
6be927a465ed XMLUI refactoring, step 1
Goffi <goffi@goffi.org>
parents: 102
diff changeset
238
760
73a0077f80cc backend, frontends: XMLUI refactoring:
Goffi <goffi@goffi.org>
parents: 759
diff changeset
239 def __init__(self, panel_type, layout="vertical", title=None, submit_id=None, session_id=None):
103
6be927a465ed XMLUI refactoring, step 1
Goffi <goffi@goffi.org>
parents: 102
diff changeset
240 """Init SàT XML Panel
6be927a465ed XMLUI refactoring, step 1
Goffi <goffi@goffi.org>
parents: 102
diff changeset
241 @param panel_type: one of
6be927a465ed XMLUI refactoring, step 1
Goffi <goffi@goffi.org>
parents: 102
diff changeset
242 - window (new window)
6be927a465ed XMLUI refactoring, step 1
Goffi <goffi@goffi.org>
parents: 102
diff changeset
243 - form (formulaire, depend of the frontend, usually a panel with cancel/submit buttons)
6be927a465ed XMLUI refactoring, step 1
Goffi <goffi@goffi.org>
parents: 102
diff changeset
244 - param (parameters, presentatio depend of the frontend)
6be927a465ed XMLUI refactoring, step 1
Goffi <goffi@goffi.org>
parents: 102
diff changeset
245 @param layout: disposition of elements, one of:
107
5ae370c71803 CS: message sending is now working
Goffi <goffi@goffi.org>
parents: 106
diff changeset
246 - vertical: elements are disposed up to bottom
5ae370c71803 CS: message sending is now working
Goffi <goffi@goffi.org>
parents: 106
diff changeset
247 - horizontal: elements are disposed left to right
5ae370c71803 CS: message sending is now working
Goffi <goffi@goffi.org>
parents: 106
diff changeset
248 - pairs: elements come on two aligned columns
103
6be927a465ed XMLUI refactoring, step 1
Goffi <goffi@goffi.org>
parents: 102
diff changeset
249 (usually one for a label, the next for the element)
107
5ae370c71803 CS: message sending is now working
Goffi <goffi@goffi.org>
parents: 106
diff changeset
250 - tabs: elemens are in categories with tabs (notebook)
5ae370c71803 CS: message sending is now working
Goffi <goffi@goffi.org>
parents: 106
diff changeset
251 @param title: title or default if None
759
93bd868b8fb6 backend, frontends: callbacks refactoring:
Goffi <goffi@goffi.org>
parents: 754
diff changeset
252 @param submit_id: callback id to call for panel_type we can submit (form, param)
103
6be927a465ed XMLUI refactoring, step 1
Goffi <goffi@goffi.org>
parents: 102
diff changeset
253 """
760
73a0077f80cc backend, frontends: XMLUI refactoring:
Goffi <goffi@goffi.org>
parents: 759
diff changeset
254 if panel_type not in ['window', 'form', 'param']:
73a0077f80cc backend, frontends: XMLUI refactoring:
Goffi <goffi@goffi.org>
parents: 759
diff changeset
255 raise exceptions.DataError(_("Unknown panel type [%s]") % panel_type)
73a0077f80cc backend, frontends: XMLUI refactoring:
Goffi <goffi@goffi.org>
parents: 759
diff changeset
256 if panel_type == 'form' and submit_id is None:
73a0077f80cc backend, frontends: XMLUI refactoring:
Goffi <goffi@goffi.org>
parents: 759
diff changeset
257 raise exceptions.DataError(_("form XMLUI need a submit_id"))
590
56531f9e9ac7 Fix pep8 support in src/tools.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 588
diff changeset
258 self.type_ = panel_type
103
6be927a465ed XMLUI refactoring, step 1
Goffi <goffi@goffi.org>
parents: 102
diff changeset
259 impl = minidom.getDOMImplementation()
6be927a465ed XMLUI refactoring, step 1
Goffi <goffi@goffi.org>
parents: 102
diff changeset
260
6be927a465ed XMLUI refactoring, step 1
Goffi <goffi@goffi.org>
parents: 102
diff changeset
261 self.doc = impl.createDocument(None, "sat_xmlui", None)
6be927a465ed XMLUI refactoring, step 1
Goffi <goffi@goffi.org>
parents: 102
diff changeset
262 top_element = self.doc.documentElement
6be927a465ed XMLUI refactoring, step 1
Goffi <goffi@goffi.org>
parents: 102
diff changeset
263 top_element.setAttribute("type", panel_type)
107
5ae370c71803 CS: message sending is now working
Goffi <goffi@goffi.org>
parents: 106
diff changeset
264 if title:
5ae370c71803 CS: message sending is now working
Goffi <goffi@goffi.org>
parents: 106
diff changeset
265 top_element.setAttribute("title", title)
759
93bd868b8fb6 backend, frontends: callbacks refactoring:
Goffi <goffi@goffi.org>
parents: 754
diff changeset
266 if submit_id:
93bd868b8fb6 backend, frontends: callbacks refactoring:
Goffi <goffi@goffi.org>
parents: 754
diff changeset
267 top_element.setAttribute("submit", submit_id)
760
73a0077f80cc backend, frontends: XMLUI refactoring:
Goffi <goffi@goffi.org>
parents: 759
diff changeset
268 if session_id is not None:
73a0077f80cc backend, frontends: XMLUI refactoring:
Goffi <goffi@goffi.org>
parents: 759
diff changeset
269 top_element.setAttribute("session_id", session_id)
590
56531f9e9ac7 Fix pep8 support in src/tools.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 588
diff changeset
270 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
271 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
272 self.currentLayout = None
104
5458ac1380cc XMLUI: added tabs layout
Goffi <goffi@goffi.org>
parents: 103
diff changeset
273 self.changeLayout(layout)
103
6be927a465ed XMLUI refactoring, step 1
Goffi <goffi@goffi.org>
parents: 102
diff changeset
274
6be927a465ed XMLUI refactoring, step 1
Goffi <goffi@goffi.org>
parents: 102
diff changeset
275 def __del__(self):
587
952322b1d490 Remove trailing whitespaces.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 572
diff changeset
276 self.doc.unlink()
952322b1d490 Remove trailing whitespaces.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 572
diff changeset
277
760
73a0077f80cc backend, frontends: XMLUI refactoring:
Goffi <goffi@goffi.org>
parents: 759
diff changeset
278 def setSessionId(self, session_id):
73a0077f80cc backend, frontends: XMLUI refactoring:
Goffi <goffi@goffi.org>
parents: 759
diff changeset
279 assert(session_id)
73a0077f80cc backend, frontends: XMLUI refactoring:
Goffi <goffi@goffi.org>
parents: 759
diff changeset
280 top_element = self.doc.documentElement
73a0077f80cc backend, frontends: XMLUI refactoring:
Goffi <goffi@goffi.org>
parents: 759
diff changeset
281 top_element.setAttribute("session_id", session_id)
73a0077f80cc backend, frontends: XMLUI refactoring:
Goffi <goffi@goffi.org>
parents: 759
diff changeset
282
73a0077f80cc backend, frontends: XMLUI refactoring:
Goffi <goffi@goffi.org>
parents: 759
diff changeset
283
630
0b914394e74f core: added advanced list to XMLUI (need improvment, very basic so far)
Goffi <goffi@goffi.org>
parents: 615
diff changeset
284 def _createLayout(self, layout, parent=None):
103
6be927a465ed XMLUI refactoring, step 1
Goffi <goffi@goffi.org>
parents: 102
diff changeset
285 """Create a layout element
6be927a465ed XMLUI refactoring, step 1
Goffi <goffi@goffi.org>
parents: 102
diff changeset
286 @param type: layout type (cf init doc)
6be927a465ed XMLUI refactoring, step 1
Goffi <goffi@goffi.org>
parents: 102
diff changeset
287 @parent: parent element or None
6be927a465ed XMLUI refactoring, step 1
Goffi <goffi@goffi.org>
parents: 102
diff changeset
288 """
104
5458ac1380cc XMLUI: added tabs layout
Goffi <goffi@goffi.org>
parents: 103
diff changeset
289 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
290 error(_("Unknown layout type [%s]") % layout)
56531f9e9ac7 Fix pep8 support in src/tools.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 588
diff changeset
291 assert False
103
6be927a465ed XMLUI refactoring, step 1
Goffi <goffi@goffi.org>
parents: 102
diff changeset
292 layout_elt = self.doc.createElement('layout')
590
56531f9e9ac7 Fix pep8 support in src/tools.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 588
diff changeset
293 layout_elt.setAttribute('type', layout)
56531f9e9ac7 Fix pep8 support in src/tools.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 588
diff changeset
294 if parent is not None:
103
6be927a465ed XMLUI refactoring, step 1
Goffi <goffi@goffi.org>
parents: 102
diff changeset
295 parent.appendChild(layout_elt)
6be927a465ed XMLUI refactoring, step 1
Goffi <goffi@goffi.org>
parents: 102
diff changeset
296 return layout_elt
37
a61beb21d16d Gateway registration, unregistration & edition
Goffi <goffi@goffi.org>
parents: 35
diff changeset
297
630
0b914394e74f core: added advanced list to XMLUI (need improvment, very basic so far)
Goffi <goffi@goffi.org>
parents: 615
diff changeset
298 def _createElem(self, type_, name=None, parent=None):
103
6be927a465ed XMLUI refactoring, step 1
Goffi <goffi@goffi.org>
parents: 102
diff changeset
299 """Create an element
590
56531f9e9ac7 Fix pep8 support in src/tools.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 588
diff changeset
300 @param type_: one of
103
6be927a465ed XMLUI refactoring, step 1
Goffi <goffi@goffi.org>
parents: 102
diff changeset
301 - empty: empty element (usefull to skip something in a layout, e.g. skip first element in a PAIRS layout)
6be927a465ed XMLUI refactoring, step 1
Goffi <goffi@goffi.org>
parents: 102
diff changeset
302 - text: text to be displayed in an multi-line area, e.g. instructions
6be927a465ed XMLUI refactoring, step 1
Goffi <goffi@goffi.org>
parents: 102
diff changeset
303 @param name: name of the element or None
6be927a465ed XMLUI refactoring, step 1
Goffi <goffi@goffi.org>
parents: 102
diff changeset
304 @param parent: parent element or None
630
0b914394e74f core: added advanced list to XMLUI (need improvment, very basic so far)
Goffi <goffi@goffi.org>
parents: 615
diff changeset
305 @return: created element
103
6be927a465ed XMLUI refactoring, step 1
Goffi <goffi@goffi.org>
parents: 102
diff changeset
306 """
6be927a465ed XMLUI refactoring, step 1
Goffi <goffi@goffi.org>
parents: 102
diff changeset
307 elem = self.doc.createElement('elem')
6be927a465ed XMLUI refactoring, step 1
Goffi <goffi@goffi.org>
parents: 102
diff changeset
308 if name:
6be927a465ed XMLUI refactoring, step 1
Goffi <goffi@goffi.org>
parents: 102
diff changeset
309 elem.setAttribute('name', name)
590
56531f9e9ac7 Fix pep8 support in src/tools.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 588
diff changeset
310 elem.setAttribute('type', type_)
56531f9e9ac7 Fix pep8 support in src/tools.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 588
diff changeset
311 if parent is not None:
103
6be927a465ed XMLUI refactoring, step 1
Goffi <goffi@goffi.org>
parents: 102
diff changeset
312 parent.appendChild(elem)
6be927a465ed XMLUI refactoring, step 1
Goffi <goffi@goffi.org>
parents: 102
diff changeset
313 return elem
6be927a465ed XMLUI refactoring, step 1
Goffi <goffi@goffi.org>
parents: 102
diff changeset
314
6be927a465ed XMLUI refactoring, step 1
Goffi <goffi@goffi.org>
parents: 102
diff changeset
315 def changeLayout(self, layout):
6be927a465ed XMLUI refactoring, step 1
Goffi <goffi@goffi.org>
parents: 102
diff changeset
316 """Change the current layout"""
630
0b914394e74f core: added advanced list to XMLUI (need improvment, very basic so far)
Goffi <goffi@goffi.org>
parents: 615
diff changeset
317 self.currentLayout = self._createLayout(layout, self.currentCategory if self.currentCategory else self.doc.documentElement)
104
5458ac1380cc XMLUI: added tabs layout
Goffi <goffi@goffi.org>
parents: 103
diff changeset
318 if layout == "tabs":
5458ac1380cc XMLUI: added tabs layout
Goffi <goffi@goffi.org>
parents: 103
diff changeset
319 self.parentTabsLayout = self.currentLayout
103
6be927a465ed XMLUI refactoring, step 1
Goffi <goffi@goffi.org>
parents: 102
diff changeset
320
6be927a465ed XMLUI refactoring, step 1
Goffi <goffi@goffi.org>
parents: 102
diff changeset
321 def addEmpty(self, name=None):
6be927a465ed XMLUI refactoring, step 1
Goffi <goffi@goffi.org>
parents: 102
diff changeset
322 """Add a multi-lines text"""
630
0b914394e74f core: added advanced list to XMLUI (need improvment, very basic so far)
Goffi <goffi@goffi.org>
parents: 615
diff changeset
323 return self._createElem('empty', name, self.currentLayout)
587
952322b1d490 Remove trailing whitespaces.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 572
diff changeset
324
103
6be927a465ed XMLUI refactoring, step 1
Goffi <goffi@goffi.org>
parents: 102
diff changeset
325 def addText(self, text, name=None):
6be927a465ed XMLUI refactoring, step 1
Goffi <goffi@goffi.org>
parents: 102
diff changeset
326 """Add a multi-lines text"""
630
0b914394e74f core: added advanced list to XMLUI (need improvment, very basic so far)
Goffi <goffi@goffi.org>
parents: 615
diff changeset
327 elem = self._createElem('text', name, self.currentLayout)
103
6be927a465ed XMLUI refactoring, step 1
Goffi <goffi@goffi.org>
parents: 102
diff changeset
328 text = self.doc.createTextNode(text)
6be927a465ed XMLUI refactoring, step 1
Goffi <goffi@goffi.org>
parents: 102
diff changeset
329 elem.appendChild(text)
630
0b914394e74f core: added advanced list to XMLUI (need improvment, very basic so far)
Goffi <goffi@goffi.org>
parents: 615
diff changeset
330 return elem
103
6be927a465ed XMLUI refactoring, step 1
Goffi <goffi@goffi.org>
parents: 102
diff changeset
331
6be927a465ed XMLUI refactoring, step 1
Goffi <goffi@goffi.org>
parents: 102
diff changeset
332 def addLabel(self, text, name=None):
6be927a465ed XMLUI refactoring, step 1
Goffi <goffi@goffi.org>
parents: 102
diff changeset
333 """Add a single line text, mainly useful as label before element"""
630
0b914394e74f core: added advanced list to XMLUI (need improvment, very basic so far)
Goffi <goffi@goffi.org>
parents: 615
diff changeset
334 elem = self._createElem('label', name, self.currentLayout)
103
6be927a465ed XMLUI refactoring, step 1
Goffi <goffi@goffi.org>
parents: 102
diff changeset
335 elem.setAttribute('value', text)
630
0b914394e74f core: added advanced list to XMLUI (need improvment, very basic so far)
Goffi <goffi@goffi.org>
parents: 615
diff changeset
336 return elem
103
6be927a465ed XMLUI refactoring, step 1
Goffi <goffi@goffi.org>
parents: 102
diff changeset
337
6be927a465ed XMLUI refactoring, step 1
Goffi <goffi@goffi.org>
parents: 102
diff changeset
338 def addString(self, name=None, value=None):
6be927a465ed XMLUI refactoring, step 1
Goffi <goffi@goffi.org>
parents: 102
diff changeset
339 """Add a string box"""
630
0b914394e74f core: added advanced list to XMLUI (need improvment, very basic so far)
Goffi <goffi@goffi.org>
parents: 615
diff changeset
340 elem = self._createElem('string', name, self.currentLayout)
103
6be927a465ed XMLUI refactoring, step 1
Goffi <goffi@goffi.org>
parents: 102
diff changeset
341 if value:
6be927a465ed XMLUI refactoring, step 1
Goffi <goffi@goffi.org>
parents: 102
diff changeset
342 elem.setAttribute('value', value)
630
0b914394e74f core: added advanced list to XMLUI (need improvment, very basic so far)
Goffi <goffi@goffi.org>
parents: 615
diff changeset
343 return elem
587
952322b1d490 Remove trailing whitespaces.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 572
diff changeset
344
103
6be927a465ed XMLUI refactoring, step 1
Goffi <goffi@goffi.org>
parents: 102
diff changeset
345 def addPassword(self, name=None, value=None):
6be927a465ed XMLUI refactoring, step 1
Goffi <goffi@goffi.org>
parents: 102
diff changeset
346 """Add a password box"""
630
0b914394e74f core: added advanced list to XMLUI (need improvment, very basic so far)
Goffi <goffi@goffi.org>
parents: 615
diff changeset
347 elem = self._createElem('password', name, self.currentLayout)
103
6be927a465ed XMLUI refactoring, step 1
Goffi <goffi@goffi.org>
parents: 102
diff changeset
348 if value:
6be927a465ed XMLUI refactoring, step 1
Goffi <goffi@goffi.org>
parents: 102
diff changeset
349 elem.setAttribute('value', value)
630
0b914394e74f core: added advanced list to XMLUI (need improvment, very basic so far)
Goffi <goffi@goffi.org>
parents: 615
diff changeset
350 return elem
103
6be927a465ed XMLUI refactoring, step 1
Goffi <goffi@goffi.org>
parents: 102
diff changeset
351
107
5ae370c71803 CS: message sending is now working
Goffi <goffi@goffi.org>
parents: 106
diff changeset
352 def addTextBox(self, name=None, value=None):
5ae370c71803 CS: message sending is now working
Goffi <goffi@goffi.org>
parents: 106
diff changeset
353 """Add a string box"""
630
0b914394e74f core: added advanced list to XMLUI (need improvment, very basic so far)
Goffi <goffi@goffi.org>
parents: 615
diff changeset
354 elem = self._createElem('textbox', name, self.currentLayout)
107
5ae370c71803 CS: message sending is now working
Goffi <goffi@goffi.org>
parents: 106
diff changeset
355 if value:
5ae370c71803 CS: message sending is now working
Goffi <goffi@goffi.org>
parents: 106
diff changeset
356 elem.setAttribute('value', value)
630
0b914394e74f core: added advanced list to XMLUI (need improvment, very basic so far)
Goffi <goffi@goffi.org>
parents: 615
diff changeset
357 return elem
587
952322b1d490 Remove trailing whitespaces.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 572
diff changeset
358
183
9ee4a1d0d7fb Added auto(dis)connect params + misc
Goffi <goffi@goffi.org>
parents: 160
diff changeset
359 def addBool(self, name=None, value="true"):
9ee4a1d0d7fb Added auto(dis)connect params + misc
Goffi <goffi@goffi.org>
parents: 160
diff changeset
360 """Add a string box"""
590
56531f9e9ac7 Fix pep8 support in src/tools.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 588
diff changeset
361 assert value in ["true", "false"]
630
0b914394e74f core: added advanced list to XMLUI (need improvment, very basic so far)
Goffi <goffi@goffi.org>
parents: 615
diff changeset
362 elem = self._createElem('bool', name, self.currentLayout)
183
9ee4a1d0d7fb Added auto(dis)connect params + misc
Goffi <goffi@goffi.org>
parents: 160
diff changeset
363 elem.setAttribute('value', value)
630
0b914394e74f core: added advanced list to XMLUI (need improvment, very basic so far)
Goffi <goffi@goffi.org>
parents: 615
diff changeset
364 return elem
587
952322b1d490 Remove trailing whitespaces.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 572
diff changeset
365
754
f021bf27a557 core (xml tools): bad default parameter in XMLUI.addList
Goffi <goffi@goffi.org>
parents: 684
diff changeset
366 def addList(self, options, name=None, value=None, style=None):
103
6be927a465ed XMLUI refactoring, step 1
Goffi <goffi@goffi.org>
parents: 102
diff changeset
367 """Add a list of choices"""
754
f021bf27a557 core (xml tools): bad default parameter in XMLUI.addList
Goffi <goffi@goffi.org>
parents: 684
diff changeset
368 if style is None:
f021bf27a557 core (xml tools): bad default parameter in XMLUI.addList
Goffi <goffi@goffi.org>
parents: 684
diff changeset
369 style = set()
107
5ae370c71803 CS: message sending is now working
Goffi <goffi@goffi.org>
parents: 106
diff changeset
370 styles = set(style)
590
56531f9e9ac7 Fix pep8 support in src/tools.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 588
diff changeset
371 assert options
56531f9e9ac7 Fix pep8 support in src/tools.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 588
diff changeset
372 assert styles.issubset(['multi'])
630
0b914394e74f core: added advanced list to XMLUI (need improvment, very basic so far)
Goffi <goffi@goffi.org>
parents: 615
diff changeset
373 elem = self._createElem('list', name, self.currentLayout)
587
952322b1d490 Remove trailing whitespaces.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 572
diff changeset
374 self.addOptions(options, elem)
103
6be927a465ed XMLUI refactoring, step 1
Goffi <goffi@goffi.org>
parents: 102
diff changeset
375 if value:
6be927a465ed XMLUI refactoring, step 1
Goffi <goffi@goffi.org>
parents: 102
diff changeset
376 elem.setAttribute('value', value)
107
5ae370c71803 CS: message sending is now working
Goffi <goffi@goffi.org>
parents: 106
diff changeset
377 for style in styles:
5ae370c71803 CS: message sending is now working
Goffi <goffi@goffi.org>
parents: 106
diff changeset
378 elem.setAttribute(style, 'yes')
630
0b914394e74f core: added advanced list to XMLUI (need improvment, very basic so far)
Goffi <goffi@goffi.org>
parents: 615
diff changeset
379 return elem
0b914394e74f core: added advanced list to XMLUI (need improvment, very basic so far)
Goffi <goffi@goffi.org>
parents: 615
diff changeset
380
0b914394e74f core: added advanced list to XMLUI (need improvment, very basic so far)
Goffi <goffi@goffi.org>
parents: 615
diff changeset
381 def addAdvancedList(self, name=None, headers=None, items=None):
0b914394e74f core: added advanced list to XMLUI (need improvment, very basic so far)
Goffi <goffi@goffi.org>
parents: 615
diff changeset
382 """Create an advanced list
0b914394e74f core: added advanced list to XMLUI (need improvment, very basic so far)
Goffi <goffi@goffi.org>
parents: 615
diff changeset
383 @param headers: optional headers informations
0b914394e74f core: added advanced list to XMLUI (need improvment, very basic so far)
Goffi <goffi@goffi.org>
parents: 615
diff changeset
384 @param items: list of Item instances
0b914394e74f core: added advanced list to XMLUI (need improvment, very basic so far)
Goffi <goffi@goffi.org>
parents: 615
diff changeset
385 @return: created element
0b914394e74f core: added advanced list to XMLUI (need improvment, very basic so far)
Goffi <goffi@goffi.org>
parents: 615
diff changeset
386 """
0b914394e74f core: added advanced list to XMLUI (need improvment, very basic so far)
Goffi <goffi@goffi.org>
parents: 615
diff changeset
387 elem = self._createElem('advanced_list', name, self.currentLayout)
0b914394e74f core: added advanced list to XMLUI (need improvment, very basic so far)
Goffi <goffi@goffi.org>
parents: 615
diff changeset
388 self.addHeaders(headers, elem)
0b914394e74f core: added advanced list to XMLUI (need improvment, very basic so far)
Goffi <goffi@goffi.org>
parents: 615
diff changeset
389 if items:
0b914394e74f core: added advanced list to XMLUI (need improvment, very basic so far)
Goffi <goffi@goffi.org>
parents: 615
diff changeset
390 self.addItems(items, elem)
0b914394e74f core: added advanced list to XMLUI (need improvment, very basic so far)
Goffi <goffi@goffi.org>
parents: 615
diff changeset
391 return elem
105
d2630fba8dfd params to XMLUI tools
Goffi <goffi@goffi.org>
parents: 104
diff changeset
392
107
5ae370c71803 CS: message sending is now working
Goffi <goffi@goffi.org>
parents: 106
diff changeset
393 def addButton(self, callback_id, name, value, fields_back=[]):
105
d2630fba8dfd params to XMLUI tools
Goffi <goffi@goffi.org>
parents: 104
diff changeset
394 """Add a button
d2630fba8dfd params to XMLUI tools
Goffi <goffi@goffi.org>
parents: 104
diff changeset
395 @param callback: callback which will be called if button is pressed
106
138d82f64b6f plugin CS: friends parsing
Goffi <goffi@goffi.org>
parents: 105
diff changeset
396 @param name: name
107
5ae370c71803 CS: message sending is now working
Goffi <goffi@goffi.org>
parents: 106
diff changeset
397 @param value: label of the button
630
0b914394e74f core: added advanced list to XMLUI (need improvment, very basic so far)
Goffi <goffi@goffi.org>
parents: 615
diff changeset
398 @fields_back: list of names of field to give back when pushing the button
0b914394e74f core: added advanced list to XMLUI (need improvment, very basic so far)
Goffi <goffi@goffi.org>
parents: 615
diff changeset
399 """
0b914394e74f core: added advanced list to XMLUI (need improvment, very basic so far)
Goffi <goffi@goffi.org>
parents: 615
diff changeset
400 elem = self._createElem('button', name, self.currentLayout)
759
93bd868b8fb6 backend, frontends: callbacks refactoring:
Goffi <goffi@goffi.org>
parents: 754
diff changeset
401 elem.setAttribute('callback', callback_id)
107
5ae370c71803 CS: message sending is now working
Goffi <goffi@goffi.org>
parents: 106
diff changeset
402 elem.setAttribute('value', value)
5ae370c71803 CS: message sending is now working
Goffi <goffi@goffi.org>
parents: 106
diff changeset
403 for field in fields_back:
5ae370c71803 CS: message sending is now working
Goffi <goffi@goffi.org>
parents: 106
diff changeset
404 fback_el = self.doc.createElement('field_back')
5ae370c71803 CS: message sending is now working
Goffi <goffi@goffi.org>
parents: 106
diff changeset
405 fback_el.setAttribute('name', field)
5ae370c71803 CS: message sending is now working
Goffi <goffi@goffi.org>
parents: 106
diff changeset
406 elem.appendChild(fback_el)
630
0b914394e74f core: added advanced list to XMLUI (need improvment, very basic so far)
Goffi <goffi@goffi.org>
parents: 615
diff changeset
407 return elem
107
5ae370c71803 CS: message sending is now working
Goffi <goffi@goffi.org>
parents: 106
diff changeset
408
630
0b914394e74f core: added advanced list to XMLUI (need improvment, very basic so far)
Goffi <goffi@goffi.org>
parents: 615
diff changeset
409 def addElement(self, type_, name=None, value=None, options=None, callback_id=None, headers=None, available=None):
103
6be927a465ed XMLUI refactoring, step 1
Goffi <goffi@goffi.org>
parents: 102
diff changeset
410 """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
411 if type_ == 'empty':
630
0b914394e74f core: added advanced list to XMLUI (need improvment, very basic so far)
Goffi <goffi@goffi.org>
parents: 615
diff changeset
412 return self.addEmpty(name)
590
56531f9e9ac7 Fix pep8 support in src/tools.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 588
diff changeset
413 elif type_ == 'text':
56531f9e9ac7 Fix pep8 support in src/tools.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 588
diff changeset
414 assert value is not None
630
0b914394e74f core: added advanced list to XMLUI (need improvment, very basic so far)
Goffi <goffi@goffi.org>
parents: 615
diff changeset
415 return self.addText(value, name)
590
56531f9e9ac7 Fix pep8 support in src/tools.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 588
diff changeset
416 elif type_ == 'label':
103
6be927a465ed XMLUI refactoring, step 1
Goffi <goffi@goffi.org>
parents: 102
diff changeset
417 assert(value)
630
0b914394e74f core: added advanced list to XMLUI (need improvment, very basic so far)
Goffi <goffi@goffi.org>
parents: 615
diff changeset
418 return self.addLabel(value)
590
56531f9e9ac7 Fix pep8 support in src/tools.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 588
diff changeset
419 elif type_ == 'string':
630
0b914394e74f core: added advanced list to XMLUI (need improvment, very basic so far)
Goffi <goffi@goffi.org>
parents: 615
diff changeset
420 return self.addString(name, value)
590
56531f9e9ac7 Fix pep8 support in src/tools.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 588
diff changeset
421 elif type_ == 'password':
630
0b914394e74f core: added advanced list to XMLUI (need improvment, very basic so far)
Goffi <goffi@goffi.org>
parents: 615
diff changeset
422 return self.addPassword(name, value)
590
56531f9e9ac7 Fix pep8 support in src/tools.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 588
diff changeset
423 elif type_ == 'textbox':
630
0b914394e74f core: added advanced list to XMLUI (need improvment, very basic so far)
Goffi <goffi@goffi.org>
parents: 615
diff changeset
424 return self.addTextBox(name, value)
590
56531f9e9ac7 Fix pep8 support in src/tools.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 588
diff changeset
425 elif type_ == 'bool':
183
9ee4a1d0d7fb Added auto(dis)connect params + misc
Goffi <goffi@goffi.org>
parents: 160
diff changeset
426 if not value:
9ee4a1d0d7fb Added auto(dis)connect params + misc
Goffi <goffi@goffi.org>
parents: 160
diff changeset
427 value = "true"
630
0b914394e74f core: added advanced list to XMLUI (need improvment, very basic so far)
Goffi <goffi@goffi.org>
parents: 615
diff changeset
428 return self.addBool(name, value)
590
56531f9e9ac7 Fix pep8 support in src/tools.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 588
diff changeset
429 elif type_ == 'list':
630
0b914394e74f core: added advanced list to XMLUI (need improvment, very basic so far)
Goffi <goffi@goffi.org>
parents: 615
diff changeset
430 return self.addList(options, name, value)
0b914394e74f core: added advanced list to XMLUI (need improvment, very basic so far)
Goffi <goffi@goffi.org>
parents: 615
diff changeset
431 elif type_ == 'advancedlist':
0b914394e74f core: added advanced list to XMLUI (need improvment, very basic so far)
Goffi <goffi@goffi.org>
parents: 615
diff changeset
432 return self.addAdvancedList(name, headers, available)
590
56531f9e9ac7 Fix pep8 support in src/tools.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 588
diff changeset
433 elif type_ == 'button':
106
138d82f64b6f plugin CS: friends parsing
Goffi <goffi@goffi.org>
parents: 105
diff changeset
434 assert(callback_id and value)
630
0b914394e74f core: added advanced list to XMLUI (need improvment, very basic so far)
Goffi <goffi@goffi.org>
parents: 615
diff changeset
435 return self.addButton(callback_id, name, value)
0b914394e74f core: added advanced list to XMLUI (need improvment, very basic so far)
Goffi <goffi@goffi.org>
parents: 615
diff changeset
436
0b914394e74f core: added advanced list to XMLUI (need improvment, very basic so far)
Goffi <goffi@goffi.org>
parents: 615
diff changeset
437 # List
103
6be927a465ed XMLUI refactoring, step 1
Goffi <goffi@goffi.org>
parents: 102
diff changeset
438
6be927a465ed XMLUI refactoring, step 1
Goffi <goffi@goffi.org>
parents: 102
diff changeset
439 def addOptions(self, options, parent):
6be927a465ed XMLUI refactoring, step 1
Goffi <goffi@goffi.org>
parents: 102
diff changeset
440 """Add options to a multi-values element (e.g. list)
6be927a465ed XMLUI refactoring, step 1
Goffi <goffi@goffi.org>
parents: 102
diff changeset
441 @param parent: multi-values element"""
6be927a465ed XMLUI refactoring, step 1
Goffi <goffi@goffi.org>
parents: 102
diff changeset
442 for option in options:
6be927a465ed XMLUI refactoring, step 1
Goffi <goffi@goffi.org>
parents: 102
diff changeset
443 opt = self.doc.createElement('option')
760
73a0077f80cc backend, frontends: XMLUI refactoring:
Goffi <goffi@goffi.org>
parents: 759
diff changeset
444 if isinstance(option, basestring):
73a0077f80cc backend, frontends: XMLUI refactoring:
Goffi <goffi@goffi.org>
parents: 759
diff changeset
445 value, label = option, option
73a0077f80cc backend, frontends: XMLUI refactoring:
Goffi <goffi@goffi.org>
parents: 759
diff changeset
446 elif isinstance(option, tuple):
73a0077f80cc backend, frontends: XMLUI refactoring:
Goffi <goffi@goffi.org>
parents: 759
diff changeset
447 value, label = option
73a0077f80cc backend, frontends: XMLUI refactoring:
Goffi <goffi@goffi.org>
parents: 759
diff changeset
448 opt.setAttribute('value', value)
73a0077f80cc backend, frontends: XMLUI refactoring:
Goffi <goffi@goffi.org>
parents: 759
diff changeset
449 opt.setAttribute('label', label)
103
6be927a465ed XMLUI refactoring, step 1
Goffi <goffi@goffi.org>
parents: 102
diff changeset
450 parent.appendChild(opt)
6be927a465ed XMLUI refactoring, step 1
Goffi <goffi@goffi.org>
parents: 102
diff changeset
451
630
0b914394e74f core: added advanced list to XMLUI (need improvment, very basic so far)
Goffi <goffi@goffi.org>
parents: 615
diff changeset
452 # Advanced list
0b914394e74f core: added advanced list to XMLUI (need improvment, very basic so far)
Goffi <goffi@goffi.org>
parents: 615
diff changeset
453
0b914394e74f core: added advanced list to XMLUI (need improvment, very basic so far)
Goffi <goffi@goffi.org>
parents: 615
diff changeset
454 def addHeaders(self, headers, parent):
0b914394e74f core: added advanced list to XMLUI (need improvment, very basic so far)
Goffi <goffi@goffi.org>
parents: 615
diff changeset
455 headers_elt = self.doc.createElement('headers')
0b914394e74f core: added advanced list to XMLUI (need improvment, very basic so far)
Goffi <goffi@goffi.org>
parents: 615
diff changeset
456 for header in headers:
0b914394e74f core: added advanced list to XMLUI (need improvment, very basic so far)
Goffi <goffi@goffi.org>
parents: 615
diff changeset
457 field_elt = self.doc.createElement('field')
0b914394e74f core: added advanced list to XMLUI (need improvment, very basic so far)
Goffi <goffi@goffi.org>
parents: 615
diff changeset
458 field_elt.setAttribute('field_name', header.field_name)
0b914394e74f core: added advanced list to XMLUI (need improvment, very basic so far)
Goffi <goffi@goffi.org>
parents: 615
diff changeset
459 field_elt.setAttribute('label', header.label)
0b914394e74f core: added advanced list to XMLUI (need improvment, very basic so far)
Goffi <goffi@goffi.org>
parents: 615
diff changeset
460 if header.description:
0b914394e74f core: added advanced list to XMLUI (need improvment, very basic so far)
Goffi <goffi@goffi.org>
parents: 615
diff changeset
461 field_elt.setAttribute('description', header.description)
0b914394e74f core: added advanced list to XMLUI (need improvment, very basic so far)
Goffi <goffi@goffi.org>
parents: 615
diff changeset
462 if header.type:
0b914394e74f core: added advanced list to XMLUI (need improvment, very basic so far)
Goffi <goffi@goffi.org>
parents: 615
diff changeset
463 field_elt.setAttribute('type', header.type)
0b914394e74f core: added advanced list to XMLUI (need improvment, very basic so far)
Goffi <goffi@goffi.org>
parents: 615
diff changeset
464 headers_elt.appendChild(field_elt)
0b914394e74f core: added advanced list to XMLUI (need improvment, very basic so far)
Goffi <goffi@goffi.org>
parents: 615
diff changeset
465 parent.appendChild(headers_elt)
0b914394e74f core: added advanced list to XMLUI (need improvment, very basic so far)
Goffi <goffi@goffi.org>
parents: 615
diff changeset
466
0b914394e74f core: added advanced list to XMLUI (need improvment, very basic so far)
Goffi <goffi@goffi.org>
parents: 615
diff changeset
467 def addItems(self, items, parent):
0b914394e74f core: added advanced list to XMLUI (need improvment, very basic so far)
Goffi <goffi@goffi.org>
parents: 615
diff changeset
468 """Add items to an AdvancedList
0b914394e74f core: added advanced list to XMLUI (need improvment, very basic so far)
Goffi <goffi@goffi.org>
parents: 615
diff changeset
469 @param items: list of Item instances
0b914394e74f core: added advanced list to XMLUI (need improvment, very basic so far)
Goffi <goffi@goffi.org>
parents: 615
diff changeset
470 @param parent: parent element (should be addAdvancedList)
0b914394e74f core: added advanced list to XMLUI (need improvment, very basic so far)
Goffi <goffi@goffi.org>
parents: 615
diff changeset
471
0b914394e74f core: added advanced list to XMLUI (need improvment, very basic so far)
Goffi <goffi@goffi.org>
parents: 615
diff changeset
472 """
0b914394e74f core: added advanced list to XMLUI (need improvment, very basic so far)
Goffi <goffi@goffi.org>
parents: 615
diff changeset
473 items_elt = self.doc.createElement('items')
0b914394e74f core: added advanced list to XMLUI (need improvment, very basic so far)
Goffi <goffi@goffi.org>
parents: 615
diff changeset
474 for item in items:
0b914394e74f core: added advanced list to XMLUI (need improvment, very basic so far)
Goffi <goffi@goffi.org>
parents: 615
diff changeset
475 item_elt = self.doc.createElement('item')
0b914394e74f core: added advanced list to XMLUI (need improvment, very basic so far)
Goffi <goffi@goffi.org>
parents: 615
diff changeset
476 if item.text is not None:
0b914394e74f core: added advanced list to XMLUI (need improvment, very basic so far)
Goffi <goffi@goffi.org>
parents: 615
diff changeset
477 text_elt = self.doc.createElement('text')
0b914394e74f core: added advanced list to XMLUI (need improvment, very basic so far)
Goffi <goffi@goffi.org>
parents: 615
diff changeset
478 text_elt.appendChild(self.doc.createTextNode(item.text))
0b914394e74f core: added advanced list to XMLUI (need improvment, very basic so far)
Goffi <goffi@goffi.org>
parents: 615
diff changeset
479 item_elt.appendChild(text_elt)
0b914394e74f core: added advanced list to XMLUI (need improvment, very basic so far)
Goffi <goffi@goffi.org>
parents: 615
diff changeset
480
0b914394e74f core: added advanced list to XMLUI (need improvment, very basic so far)
Goffi <goffi@goffi.org>
parents: 615
diff changeset
481 for field in item.fields:
0b914394e74f core: added advanced list to XMLUI (need improvment, very basic so far)
Goffi <goffi@goffi.org>
parents: 615
diff changeset
482 field_elt = self.doc.createElement('field')
0b914394e74f core: added advanced list to XMLUI (need improvment, very basic so far)
Goffi <goffi@goffi.org>
parents: 615
diff changeset
483 field_elt.setAttribute('name', field.name)
0b914394e74f core: added advanced list to XMLUI (need improvment, very basic so far)
Goffi <goffi@goffi.org>
parents: 615
diff changeset
484 field_elt.setAttribute('value', field.value)
0b914394e74f core: added advanced list to XMLUI (need improvment, very basic so far)
Goffi <goffi@goffi.org>
parents: 615
diff changeset
485 item_elt.appendChild(field_elt)
0b914394e74f core: added advanced list to XMLUI (need improvment, very basic so far)
Goffi <goffi@goffi.org>
parents: 615
diff changeset
486
0b914394e74f core: added advanced list to XMLUI (need improvment, very basic so far)
Goffi <goffi@goffi.org>
parents: 615
diff changeset
487 items_elt.appendChild(item_elt)
0b914394e74f core: added advanced list to XMLUI (need improvment, very basic so far)
Goffi <goffi@goffi.org>
parents: 615
diff changeset
488
0b914394e74f core: added advanced list to XMLUI (need improvment, very basic so far)
Goffi <goffi@goffi.org>
parents: 615
diff changeset
489 parent.appendChild(items_elt)
0b914394e74f core: added advanced list to XMLUI (need improvment, very basic so far)
Goffi <goffi@goffi.org>
parents: 615
diff changeset
490
0b914394e74f core: added advanced list to XMLUI (need improvment, very basic so far)
Goffi <goffi@goffi.org>
parents: 615
diff changeset
491 # Tabs
0b914394e74f core: added advanced list to XMLUI (need improvment, very basic so far)
Goffi <goffi@goffi.org>
parents: 615
diff changeset
492
183
9ee4a1d0d7fb Added auto(dis)connect params + misc
Goffi <goffi@goffi.org>
parents: 160
diff changeset
493 def addCategory(self, name, layout, label=None):
104
5458ac1380cc XMLUI: added tabs layout
Goffi <goffi@goffi.org>
parents: 103
diff changeset
494 """Add a category to current layout (must be a tabs layout)"""
5458ac1380cc XMLUI: added tabs layout
Goffi <goffi@goffi.org>
parents: 103
diff changeset
495 assert(layout != 'tabs')
5458ac1380cc XMLUI: added tabs layout
Goffi <goffi@goffi.org>
parents: 103
diff changeset
496 if not self.parentTabsLayout:
5458ac1380cc XMLUI: added tabs layout
Goffi <goffi@goffi.org>
parents: 103
diff changeset
497 error(_("Trying to add a category without parent tabs layout"))
5458ac1380cc XMLUI: added tabs layout
Goffi <goffi@goffi.org>
parents: 103
diff changeset
498 assert(False)
5458ac1380cc XMLUI: added tabs layout
Goffi <goffi@goffi.org>
parents: 103
diff changeset
499 if self.parentTabsLayout.getAttribute('type') != 'tabs':
5458ac1380cc XMLUI: added tabs layout
Goffi <goffi@goffi.org>
parents: 103
diff changeset
500 error(_("parent layout of a category is not tabs"))
5458ac1380cc XMLUI: added tabs layout
Goffi <goffi@goffi.org>
parents: 103
diff changeset
501 assert(False)
183
9ee4a1d0d7fb Added auto(dis)connect params + misc
Goffi <goffi@goffi.org>
parents: 160
diff changeset
502
9ee4a1d0d7fb Added auto(dis)connect params + misc
Goffi <goffi@goffi.org>
parents: 160
diff changeset
503 if not label:
9ee4a1d0d7fb Added auto(dis)connect params + misc
Goffi <goffi@goffi.org>
parents: 160
diff changeset
504 label = name
104
5458ac1380cc XMLUI: added tabs layout
Goffi <goffi@goffi.org>
parents: 103
diff changeset
505 self.currentCategory = cat = self.doc.createElement('category')
5458ac1380cc XMLUI: added tabs layout
Goffi <goffi@goffi.org>
parents: 103
diff changeset
506 cat.setAttribute('name', name)
183
9ee4a1d0d7fb Added auto(dis)connect params + misc
Goffi <goffi@goffi.org>
parents: 160
diff changeset
507 cat.setAttribute('label', label)
104
5458ac1380cc XMLUI: added tabs layout
Goffi <goffi@goffi.org>
parents: 103
diff changeset
508 self.changeLayout(layout)
5458ac1380cc XMLUI: added tabs layout
Goffi <goffi@goffi.org>
parents: 103
diff changeset
509 self.parentTabsLayout.appendChild(cat)
5458ac1380cc XMLUI: added tabs layout
Goffi <goffi@goffi.org>
parents: 103
diff changeset
510
103
6be927a465ed XMLUI refactoring, step 1
Goffi <goffi@goffi.org>
parents: 102
diff changeset
511 def toXml(self):
587
952322b1d490 Remove trailing whitespaces.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 572
diff changeset
512 """return the XML representation of the panel"""
103
6be927a465ed XMLUI refactoring, step 1
Goffi <goffi@goffi.org>
parents: 102
diff changeset
513 return self.doc.toxml()
289
0e54b1b0a8c8 xml_tools: added ElementParser for raw xml -> domish.Element conversion
Goffi <goffi@goffi.org>
parents: 228
diff changeset
514
0e54b1b0a8c8 xml_tools: added ElementParser for raw xml -> domish.Element conversion
Goffi <goffi@goffi.org>
parents: 228
diff changeset
515
0e54b1b0a8c8 xml_tools: added ElementParser for raw xml -> domish.Element conversion
Goffi <goffi@goffi.org>
parents: 228
diff changeset
516 class ElementParser(object):
0e54b1b0a8c8 xml_tools: added ElementParser for raw xml -> domish.Element conversion
Goffi <goffi@goffi.org>
parents: 228
diff changeset
517 """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
518 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
519 (c) Karl Anderson"""
0e54b1b0a8c8 xml_tools: added ElementParser for raw xml -> domish.Element conversion
Goffi <goffi@goffi.org>
parents: 228
diff changeset
520
590
56531f9e9ac7 Fix pep8 support in src/tools.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 588
diff changeset
521 def __call__(self, string):
289
0e54b1b0a8c8 xml_tools: added ElementParser for raw xml -> domish.Element conversion
Goffi <goffi@goffi.org>
parents: 228
diff changeset
522 self.result = None
590
56531f9e9ac7 Fix pep8 support in src/tools.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 588
diff changeset
523
56531f9e9ac7 Fix pep8 support in src/tools.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 588
diff changeset
524 def onStart(elem):
56531f9e9ac7 Fix pep8 support in src/tools.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 588
diff changeset
525 self.result = elem
56531f9e9ac7 Fix pep8 support in src/tools.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 588
diff changeset
526
289
0e54b1b0a8c8 xml_tools: added ElementParser for raw xml -> domish.Element conversion
Goffi <goffi@goffi.org>
parents: 228
diff changeset
527 def onEnd():
0e54b1b0a8c8 xml_tools: added ElementParser for raw xml -> domish.Element conversion
Goffi <goffi@goffi.org>
parents: 228
diff changeset
528 pass
590
56531f9e9ac7 Fix pep8 support in src/tools.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 588
diff changeset
529
56531f9e9ac7 Fix pep8 support in src/tools.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 588
diff changeset
530 def onElement(elem):
56531f9e9ac7 Fix pep8 support in src/tools.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 588
diff changeset
531 self.result.addChild(elem)
289
0e54b1b0a8c8 xml_tools: added ElementParser for raw xml -> domish.Element conversion
Goffi <goffi@goffi.org>
parents: 228
diff changeset
532
0e54b1b0a8c8 xml_tools: added ElementParser for raw xml -> domish.Element conversion
Goffi <goffi@goffi.org>
parents: 228
diff changeset
533 parser = domish.elementStream()
0e54b1b0a8c8 xml_tools: added ElementParser for raw xml -> domish.Element conversion
Goffi <goffi@goffi.org>
parents: 228
diff changeset
534 parser.DocumentStartEvent = onStart
0e54b1b0a8c8 xml_tools: added ElementParser for raw xml -> domish.Element conversion
Goffi <goffi@goffi.org>
parents: 228
diff changeset
535 parser.ElementEvent = onElement
0e54b1b0a8c8 xml_tools: added ElementParser for raw xml -> domish.Element conversion
Goffi <goffi@goffi.org>
parents: 228
diff changeset
536 parser.DocumentEndEvent = onEnd
536
a31abb97310d core, plugins: fixed bad namespace in stanza creation
Goffi <goffi@goffi.org>
parents: 480
diff changeset
537 tmp = domish.Element((None, "s"))
615
6f4c31192c7c plugins XEP-0060, XEP-0277, groupblog: comments implementation (first draft, not finished yet):
Goffi <goffi@goffi.org>
parents: 609
diff changeset
538 tmp.addRawXml(string.replace('\n', ' ').replace('\t', ' '))
304
e04ccf122bb6 microblog sending
Goffi <goffi@goffi.org>
parents: 289
diff changeset
539 parser.parse(tmp.toXml().encode('utf-8'))
587
952322b1d490 Remove trailing whitespaces.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 572
diff changeset
540 return self.result.firstChildElement()