annotate libervia/backend/tools/xml_tools.py @ 4340:ea72364131d5 default tip @

doc (components): Update Email Gateway documentation: A section has been added to explain how attachments are handled. fix 453
author Goffi <goffi@goffi.org>
date Tue, 03 Dec 2024 00:53:18 +0100
parents d2deddd6df44
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2958
diff changeset
1 #!/usr/bin/env python3
3137
559a625a236b fixed shebangs
Goffi <goffi@goffi.org>
parents: 3136
diff changeset
2
609
84a6e83157c2 fixed licences in docstrings (they are now in comments)
Goffi <goffi@goffi.org>
parents: 590
diff changeset
3 # SAT: a jabber client
3479
be6d91572633 date update
Goffi <goffi@goffi.org>
parents: 3472
diff changeset
4 # Copyright (C) 2009-2021 Jérôme Poisson (goffi@goffi.org)
33
b9bb5d8e0cc7 In-band-registration: data form 2 xml conversion
Goffi <goffi@goffi.org>
parents:
diff changeset
5
609
84a6e83157c2 fixed licences in docstrings (they are now in comments)
Goffi <goffi@goffi.org>
parents: 590
diff changeset
6 # 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
7 # 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
8 # 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
9 # (at your option) any later version.
33
b9bb5d8e0cc7 In-band-registration: data form 2 xml conversion
Goffi <goffi@goffi.org>
parents:
diff changeset
10
609
84a6e83157c2 fixed licences in docstrings (they are now in comments)
Goffi <goffi@goffi.org>
parents: 590
diff changeset
11 # 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
12 # 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
13 # 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
14 # 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
15
609
84a6e83157c2 fixed licences in docstrings (they are now in comments)
Goffi <goffi@goffi.org>
parents: 590
diff changeset
16 # 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
17 # 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
18
3491
2bd75fc2555d tools (xml_tools): new findAncestor method:
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
19
3955
323017a4e4d2 tools (xml_tools): `domish_elt_2_et_elt` and `et_elt_2_domish_elt` now handle lxml.etree
Goffi <goffi@goffi.org>
parents: 3914
diff changeset
20 from collections import OrderedDict
3491
2bd75fc2555d tools (xml_tools): new findAncestor method:
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
21 import html.entities
3955
323017a4e4d2 tools (xml_tools): `domish_elt_2_et_elt` and `et_elt_2_domish_elt` now handle lxml.etree
Goffi <goffi@goffi.org>
parents: 3914
diff changeset
22 import re
4010
818db4ca3717 tools (xml_tools): accept several namespaces in `findAncestor`
Goffi <goffi@goffi.org>
parents: 3967
diff changeset
23 from typing import Dict, Optional, Tuple, Union, Literal, overload, Iterable
3955
323017a4e4d2 tools (xml_tools): `domish_elt_2_et_elt` and `et_elt_2_domish_elt` now handle lxml.etree
Goffi <goffi@goffi.org>
parents: 3914
diff changeset
24 from xml.dom import NotFoundErr, minidom
323017a4e4d2 tools (xml_tools): `domish_elt_2_et_elt` and `et_elt_2_domish_elt` now handle lxml.etree
Goffi <goffi@goffi.org>
parents: 3914
diff changeset
25 import xml.etree.ElementTree as ET
323017a4e4d2 tools (xml_tools): `domish_elt_2_et_elt` and `et_elt_2_domish_elt` now handle lxml.etree
Goffi <goffi@goffi.org>
parents: 3914
diff changeset
26 from lxml import etree
323017a4e4d2 tools (xml_tools): `domish_elt_2_et_elt` and `et_elt_2_domish_elt` now handle lxml.etree
Goffi <goffi@goffi.org>
parents: 3914
diff changeset
27
323017a4e4d2 tools (xml_tools): `domish_elt_2_et_elt` and `et_elt_2_domish_elt` now handle lxml.etree
Goffi <goffi@goffi.org>
parents: 3914
diff changeset
28 from twisted.internet import defer
3491
2bd75fc2555d tools (xml_tools): new findAncestor method:
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
29 from twisted.words.protocols.jabber import jid
3955
323017a4e4d2 tools (xml_tools): `domish_elt_2_et_elt` and `et_elt_2_domish_elt` now handle lxml.etree
Goffi <goffi@goffi.org>
parents: 3914
diff changeset
30 from twisted.words.xish import domish
3491
2bd75fc2555d tools (xml_tools): new findAncestor method:
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
31 from wokkel import data_form
3955
323017a4e4d2 tools (xml_tools): `domish_elt_2_et_elt` and `et_elt_2_domish_elt` now handle lxml.etree
Goffi <goffi@goffi.org>
parents: 3914
diff changeset
32
4071
4b842c1fb686 refactoring: renamed `sat` package to `libervia.backend`
Goffi <goffi@goffi.org>
parents: 4041
diff changeset
33 from libervia.backend.core import exceptions
4b842c1fb686 refactoring: renamed `sat` package to `libervia.backend`
Goffi <goffi@goffi.org>
parents: 4041
diff changeset
34 from libervia.backend.core.constants import Const as C
4b842c1fb686 refactoring: renamed `sat` package to `libervia.backend`
Goffi <goffi@goffi.org>
parents: 4041
diff changeset
35 from libervia.backend.core.i18n import _
4b842c1fb686 refactoring: renamed `sat` package to `libervia.backend`
Goffi <goffi@goffi.org>
parents: 4041
diff changeset
36 from libervia.backend.core.log import getLogger
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
37
3491
2bd75fc2555d tools (xml_tools): new findAncestor method:
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
38
993
301b342c697a core: use of the new core.log module:
Goffi <goffi@goffi.org>
parents: 981
diff changeset
39 log = getLogger(__name__)
1444
8ce9924fa92c tools (xml_tools): better PEP-8 compliance
souliane <souliane@mailoo.org>
parents: 1409
diff changeset
40
8ce9924fa92c tools (xml_tools): better PEP-8 compliance
souliane <souliane@mailoo.org>
parents: 1409
diff changeset
41 """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
42
796
46aa5ada61bf core, frontends: XMLUI refactoring:
Goffi <goffi@goffi.org>
parents: 771
diff changeset
43 SAT_FORM_PREFIX = "SAT_FORM_"
1444
8ce9924fa92c tools (xml_tools): better PEP-8 compliance
souliane <souliane@mailoo.org>
parents: 1409
diff changeset
44 SAT_PARAM_SEPARATOR = "_XMLUI_PARAM_" # used to have unique elements names
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
45 html_entity_re = re.compile(r"&([a-zA-Z]+?);")
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
46 XML_ENTITIES = ("quot", "amp", "apos", "lt", "gt")
587
952322b1d490 Remove trailing whitespaces.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 572
diff changeset
47
2782
b17e6fa1e607 core (XMLUI): new XHTMLBox widget:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
48 # method to clean XHTML, receive raw unsecure XML or HTML, must return cleaned raw XHTML
b17e6fa1e607 core (XMLUI): new XHTMLBox widget:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
49 # this method must be set during runtime
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 4032
diff changeset
50 clean_xhtml = None
2782
b17e6fa1e607 core (XMLUI): new XHTMLBox widget:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
51
1903
29564cec913f plugin XEP-0060: service, node and uri are added to metadata when doing a getItems
Goffi <goffi@goffi.org>
parents: 1819
diff changeset
52 # TODO: move XMLUI stuff in a separate module
2364
918e38622a48 core (xmlui): added method to convert data form result to XMLUI:
Goffi <goffi@goffi.org>
parents: 2361
diff changeset
53 # TODO: rewrite this with lxml or ElementTree or domish.Element: it's complicated and difficult to maintain with current minidom implementation
802
9007bb133009 core, frontends: XMLUI refactoring:
Goffi <goffi@goffi.org>
parents: 800
diff changeset
54
9007bb133009 core, frontends: XMLUI refactoring:
Goffi <goffi@goffi.org>
parents: 800
diff changeset
55 # Helper functions
9007bb133009 core, frontends: XMLUI refactoring:
Goffi <goffi@goffi.org>
parents: 800
diff changeset
56
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
57
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 4032
diff changeset
58 def _data_form_field_2_xmlui_data(field, read_only=False):
1444
8ce9924fa92c tools (xml_tools): better PEP-8 compliance
souliane <souliane@mailoo.org>
parents: 1409
diff changeset
59 """Get data needed to create an XMLUI's Widget from Wokkel's data_form's Field.
8ce9924fa92c tools (xml_tools): better PEP-8 compliance
souliane <souliane@mailoo.org>
parents: 1409
diff changeset
60
8ce9924fa92c tools (xml_tools): better PEP-8 compliance
souliane <souliane@mailoo.org>
parents: 1409
diff changeset
61 The attribute field can be modified (if it's fixed and it has no value).
2625
a55a14c3cbf4 plugin XEP-0070: use a confirm dialog instead of a form + simplified a bit the code
Goffi <goffi@goffi.org>
parents: 2624
diff changeset
62 @param field (data_form.Field): a field with attributes "value", "fieldType",
a55a14c3cbf4 plugin XEP-0070: use a confirm dialog instead of a form + simplified a bit the code
Goffi <goffi@goffi.org>
parents: 2624
diff changeset
63 "label" and "var"
1444
8ce9924fa92c tools (xml_tools): better PEP-8 compliance
souliane <souliane@mailoo.org>
parents: 1409
diff changeset
64 @param read_only (bool): if True and it makes sense, create a read only input widget
8ce9924fa92c tools (xml_tools): better PEP-8 compliance
souliane <souliane@mailoo.org>
parents: 1409
diff changeset
65 @return: a tuple (widget_type, widget_args, widget_kwargs)
802
9007bb133009 core, frontends: XMLUI refactoring:
Goffi <goffi@goffi.org>
parents: 800
diff changeset
66 """
3716
d33da3fe34a5 tools (xml_tools): implement `list-multi` in `_dataFormField2XMLUIData`:
Goffi <goffi@goffi.org>
parents: 3531
diff changeset
67 widget_args = field.values or [None]
802
9007bb133009 core, frontends: XMLUI refactoring:
Goffi <goffi@goffi.org>
parents: 800
diff changeset
68 widget_kwargs = {}
2782
b17e6fa1e607 core (XMLUI): new XHTMLBox widget:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
69 if field.fieldType is None and field.ext_type is not None:
b17e6fa1e607 core (XMLUI): new XHTMLBox widget:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
70 # we have an extended field
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2958
diff changeset
71 if field.ext_type == "xml":
2782
b17e6fa1e607 core (XMLUI): new XHTMLBox widget:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
72 element = field.value
b17e6fa1e607 core (XMLUI): new XHTMLBox widget:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
73 if element.uri == C.NS_XHTML:
b17e6fa1e607 core (XMLUI): new XHTMLBox widget:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
74 widget_type = "xhtmlbox"
b17e6fa1e607 core (XMLUI): new XHTMLBox widget:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
75 widget_args[0] = element.toXml()
b17e6fa1e607 core (XMLUI): new XHTMLBox widget:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
76 widget_kwargs["read_only"] = read_only
b17e6fa1e607 core (XMLUI): new XHTMLBox widget:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
77 else:
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2958
diff changeset
78 log.warning("unknown XML element, falling back to textbox")
2782
b17e6fa1e607 core (XMLUI): new XHTMLBox widget:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
79 widget_type = "textbox"
b17e6fa1e607 core (XMLUI): new XHTMLBox widget:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
80 widget_args[0] = element.toXml()
b17e6fa1e607 core (XMLUI): new XHTMLBox widget:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
81 widget_kwargs["read_only"] = read_only
b17e6fa1e607 core (XMLUI): new XHTMLBox widget:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
82 else:
3716
d33da3fe34a5 tools (xml_tools): implement `list-multi` in `_dataFormField2XMLUIData`:
Goffi <goffi@goffi.org>
parents: 3531
diff changeset
83
4270
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4110
diff changeset
84 raise exceptions.DataError(
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4110
diff changeset
85 "unknown extended type {ext_type}".format(ext_type=field.ext_type)
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4110
diff changeset
86 )
2782
b17e6fa1e607 core (XMLUI): new XHTMLBox widget:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
87
b17e6fa1e607 core (XMLUI): new XHTMLBox widget:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
88 elif field.fieldType == "fixed" or field.fieldType is None:
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
89 widget_type = "text"
863
97ee7594c788 core (xmlui): fixed types with empty value are now correctly managed in _dataFormField2XMLUIData
Goffi <goffi@goffi.org>
parents: 820
diff changeset
90 if field.value is None:
97ee7594c788 core (xmlui): fixed types with empty value are now correctly managed in _dataFormField2XMLUIData
Goffi <goffi@goffi.org>
parents: 820
diff changeset
91 if field.label is None:
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2958
diff changeset
92 log.warning(_("Fixed field has neither value nor label, ignoring it"))
863
97ee7594c788 core (xmlui): fixed types with empty value are now correctly managed in _dataFormField2XMLUIData
Goffi <goffi@goffi.org>
parents: 820
diff changeset
93 field.value = ""
97ee7594c788 core (xmlui): fixed types with empty value are now correctly managed in _dataFormField2XMLUIData
Goffi <goffi@goffi.org>
parents: 820
diff changeset
94 else:
97ee7594c788 core (xmlui): fixed types with empty value are now correctly managed in _dataFormField2XMLUIData
Goffi <goffi@goffi.org>
parents: 820
diff changeset
95 field.value = field.label
97ee7594c788 core (xmlui): fixed types with empty value are now correctly managed in _dataFormField2XMLUIData
Goffi <goffi@goffi.org>
parents: 820
diff changeset
96 field.label = None
3732
0fac164ff2d8 tools (xml_tools): fix `widget_args` modification in `_dataFormField2XMLUIData`:
Goffi <goffi@goffi.org>
parents: 3716
diff changeset
97 widget_args = [field.value]
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
98 elif field.fieldType == "text-single":
802
9007bb133009 core, frontends: XMLUI refactoring:
Goffi <goffi@goffi.org>
parents: 800
diff changeset
99 widget_type = "string"
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
100 widget_kwargs["read_only"] = read_only
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
101 elif field.fieldType == "jid-single":
1473
675e0e9f1653 core , privmitivus (xmlui): add jidInput handling, with a simple String for now
Goffi <goffi@goffi.org>
parents: 1472
diff changeset
102 widget_type = "jid_input"
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
103 widget_kwargs["read_only"] = read_only
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
104 elif field.fieldType == "text-multi":
1078
bb4517e69957 tools (xmlui): data form 'text-multi' element must be converted to xmlui 'textbox' (not 'text')
souliane <souliane@mailoo.org>
parents: 1070
diff changeset
105 widget_type = "textbox"
3732
0fac164ff2d8 tools (xml_tools): fix `widget_args` modification in `_dataFormField2XMLUIData`:
Goffi <goffi@goffi.org>
parents: 3716
diff changeset
106 widget_args = ["\n".join(field.values)]
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
107 widget_kwargs["read_only"] = read_only
2668
c274201cea94 core, frontends (xmlui): added "hidden" widget, to specify a value to be returned unmodified
Goffi <goffi@goffi.org>
parents: 2625
diff changeset
108 elif field.fieldType == "hidden":
c274201cea94 core, frontends (xmlui): added "hidden" widget, to specify a value to be returned unmodified
Goffi <goffi@goffi.org>
parents: 2625
diff changeset
109 widget_type = "hidden"
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
110 elif field.fieldType == "text-private":
802
9007bb133009 core, frontends: XMLUI refactoring:
Goffi <goffi@goffi.org>
parents: 800
diff changeset
111 widget_type = "password"
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
112 widget_kwargs["read_only"] = read_only
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
113 elif field.fieldType == "boolean":
802
9007bb133009 core, frontends: XMLUI refactoring:
Goffi <goffi@goffi.org>
parents: 800
diff changeset
114 widget_type = "bool"
9007bb133009 core, frontends: XMLUI refactoring:
Goffi <goffi@goffi.org>
parents: 800
diff changeset
115 if widget_args[0] is None:
3732
0fac164ff2d8 tools (xml_tools): fix `widget_args` modification in `_dataFormField2XMLUIData`:
Goffi <goffi@goffi.org>
parents: 3716
diff changeset
116 widget_args = ["false"]
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
117 widget_kwargs["read_only"] = read_only
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
118 elif field.fieldType == "integer":
1220
f91e7028e2c3 memory (params), tools (xml_tools), plugins, frontends: add "int" parameter type with "min" and "max" attributes
souliane <souliane@mailoo.org>
parents: 1199
diff changeset
119 widget_type = "integer"
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
120 widget_kwargs["read_only"] = read_only
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
121 elif field.fieldType == "list-single":
802
9007bb133009 core, frontends: XMLUI refactoring:
Goffi <goffi@goffi.org>
parents: 800
diff changeset
122 widget_type = "list"
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
123 widget_kwargs["options"] = [
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
124 (option.value, option.label or option.value) for option in field.options
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
125 ]
1067
f7f15d44fdfa tools (xml_tools): dataForm2XMLUI handles the default value for "list-single" fields
souliane <souliane@mailoo.org>
parents: 1036
diff changeset
126 widget_kwargs["selected"] = widget_args
f7f15d44fdfa tools (xml_tools): dataForm2XMLUI handles the default value for "list-single" fields
souliane <souliane@mailoo.org>
parents: 1036
diff changeset
127 widget_args = []
3716
d33da3fe34a5 tools (xml_tools): implement `list-multi` in `_dataFormField2XMLUIData`:
Goffi <goffi@goffi.org>
parents: 3531
diff changeset
128 elif field.fieldType == "list-multi":
d33da3fe34a5 tools (xml_tools): implement `list-multi` in `_dataFormField2XMLUIData`:
Goffi <goffi@goffi.org>
parents: 3531
diff changeset
129 widget_type = "list"
d33da3fe34a5 tools (xml_tools): implement `list-multi` in `_dataFormField2XMLUIData`:
Goffi <goffi@goffi.org>
parents: 3531
diff changeset
130 widget_kwargs["options"] = [
d33da3fe34a5 tools (xml_tools): implement `list-multi` in `_dataFormField2XMLUIData`:
Goffi <goffi@goffi.org>
parents: 3531
diff changeset
131 (option.value, option.label or option.value) for option in field.options
d33da3fe34a5 tools (xml_tools): implement `list-multi` in `_dataFormField2XMLUIData`:
Goffi <goffi@goffi.org>
parents: 3531
diff changeset
132 ]
d33da3fe34a5 tools (xml_tools): implement `list-multi` in `_dataFormField2XMLUIData`:
Goffi <goffi@goffi.org>
parents: 3531
diff changeset
133 widget_kwargs["selected"] = widget_args
4270
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4110
diff changeset
134 widget_kwargs["styles"] = ["multi"]
3716
d33da3fe34a5 tools (xml_tools): implement `list-multi` in `_dataFormField2XMLUIData`:
Goffi <goffi@goffi.org>
parents: 3531
diff changeset
135 widget_args = []
802
9007bb133009 core, frontends: XMLUI refactoring:
Goffi <goffi@goffi.org>
parents: 800
diff changeset
136 else:
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
137 log.error(
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2958
diff changeset
138 "FIXME FIXME FIXME: Type [%s] is not managed yet by SàT" % field.fieldType
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
139 )
802
9007bb133009 core, frontends: XMLUI refactoring:
Goffi <goffi@goffi.org>
parents: 800
diff changeset
140 widget_type = "string"
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
141 widget_kwargs["read_only"] = read_only
802
9007bb133009 core, frontends: XMLUI refactoring:
Goffi <goffi@goffi.org>
parents: 800
diff changeset
142
9007bb133009 core, frontends: XMLUI refactoring:
Goffi <goffi@goffi.org>
parents: 800
diff changeset
143 if field.var:
9007bb133009 core, frontends: XMLUI refactoring:
Goffi <goffi@goffi.org>
parents: 800
diff changeset
144 widget_kwargs["name"] = field.var
9007bb133009 core, frontends: XMLUI refactoring:
Goffi <goffi@goffi.org>
parents: 800
diff changeset
145
9007bb133009 core, frontends: XMLUI refactoring:
Goffi <goffi@goffi.org>
parents: 800
diff changeset
146 return widget_type, widget_args, widget_kwargs
9007bb133009 core, frontends: XMLUI refactoring:
Goffi <goffi@goffi.org>
parents: 800
diff changeset
147
4270
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4110
diff changeset
148
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 4032
diff changeset
149 def data_form_2_widgets(form_ui, form, read_only=False, prepend=None, filters=None):
1444
8ce9924fa92c tools (xml_tools): better PEP-8 compliance
souliane <souliane@mailoo.org>
parents: 1409
diff changeset
150 """Complete an existing XMLUI with widget converted from XEP-0004 data forms.
802
9007bb133009 core, frontends: XMLUI refactoring:
Goffi <goffi@goffi.org>
parents: 800
diff changeset
151
1444
8ce9924fa92c tools (xml_tools): better PEP-8 compliance
souliane <souliane@mailoo.org>
parents: 1409
diff changeset
152 @param form_ui (XMLUI): XMLUI instance
8ce9924fa92c tools (xml_tools): better PEP-8 compliance
souliane <souliane@mailoo.org>
parents: 1409
diff changeset
153 @param form (data_form.Form): Wokkel's implementation of data form
8ce9924fa92c tools (xml_tools): better PEP-8 compliance
souliane <souliane@mailoo.org>
parents: 1409
diff changeset
154 @param read_only (bool): if True and it makes sense, create a read only input widget
2380
59636c4db2d0 core (xmlui): new "prepend" argument for dataForm2Widgets and dataFormResult2XMLUI:
Goffi <goffi@goffi.org>
parents: 2365
diff changeset
155 @param prepend(iterable, None): widgets to prepend to main LabelContainer
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 4032
diff changeset
156 if not None, must be an iterable of *args for add_widget. Those widgets will
2380
59636c4db2d0 core (xmlui): new "prepend" argument for dataForm2Widgets and dataFormResult2XMLUI:
Goffi <goffi@goffi.org>
parents: 2365
diff changeset
157 be added first to the container.
2398
3ff9d7a7fe71 core (XMLUI): filters can now be used when converting data form to XMLUI:
Goffi <goffi@goffi.org>
parents: 2397
diff changeset
158 @param filters(dict, None): if not None, a dictionary of callable:
3ff9d7a7fe71 core (XMLUI): filters can now be used when converting data form to XMLUI:
Goffi <goffi@goffi.org>
parents: 2397
diff changeset
159 key is the name of the widget to filter
2429
941fdf81958c core (XMLUI): form's XMLUI is use as first arguments for filters in dataForm2Widgets
Goffi <goffi@goffi.org>
parents: 2428
diff changeset
160 the value is a callable, it will get form's XMLUI, widget's type, args and kwargs
941fdf81958c core (XMLUI): form's XMLUI is use as first arguments for filters in dataForm2Widgets
Goffi <goffi@goffi.org>
parents: 2428
diff changeset
161 and must return widget's type, args and kwargs (which can be modified)
2398
3ff9d7a7fe71 core (XMLUI): filters can now be used when converting data form to XMLUI:
Goffi <goffi@goffi.org>
parents: 2397
diff changeset
162 This is especially useful to modify well known fields
1444
8ce9924fa92c tools (xml_tools): better PEP-8 compliance
souliane <souliane@mailoo.org>
parents: 1409
diff changeset
163 @return: the completed XMLUI instance
802
9007bb133009 core, frontends: XMLUI refactoring:
Goffi <goffi@goffi.org>
parents: 800
diff changeset
164 """
2405
f4b6176eb65f core (xmlui): fixed data_form => XMLUI conversion when filters is None
Goffi <goffi@goffi.org>
parents: 2398
diff changeset
165 if filters is None:
f4b6176eb65f core (xmlui): fixed data_form => XMLUI conversion when filters is None
Goffi <goffi@goffi.org>
parents: 2398
diff changeset
166 filters = {}
102
94011f553cd0 misc bugfixes
Goffi <goffi@goffi.org>
parents: 95
diff changeset
167 if form.instructions:
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
168 form_ui.addText("\n".join(form.instructions), "instructions")
587
952322b1d490 Remove trailing whitespaces.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 572
diff changeset
169
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 4032
diff changeset
170 form_ui.change_container("label")
587
952322b1d490 Remove trailing whitespaces.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 572
diff changeset
171
2380
59636c4db2d0 core (xmlui): new "prepend" argument for dataForm2Widgets and dataFormResult2XMLUI:
Goffi <goffi@goffi.org>
parents: 2365
diff changeset
172 if prepend is not None:
59636c4db2d0 core (xmlui): new "prepend" argument for dataForm2Widgets and dataFormResult2XMLUI:
Goffi <goffi@goffi.org>
parents: 2365
diff changeset
173 for widget_args in prepend:
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 4032
diff changeset
174 form_ui.add_widget(*widget_args)
2380
59636c4db2d0 core (xmlui): new "prepend" argument for dataForm2Widgets and dataFormResult2XMLUI:
Goffi <goffi@goffi.org>
parents: 2365
diff changeset
175
102
94011f553cd0 misc bugfixes
Goffi <goffi@goffi.org>
parents: 95
diff changeset
176 for field in form.fieldList:
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 4032
diff changeset
177 widget_type, widget_args, widget_kwargs = _data_form_field_2_xmlui_data(
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
178 field, read_only
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
179 )
2398
3ff9d7a7fe71 core (XMLUI): filters can now be used when converting data form to XMLUI:
Goffi <goffi@goffi.org>
parents: 2397
diff changeset
180 try:
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
181 widget_filter = filters[widget_kwargs["name"]]
2398
3ff9d7a7fe71 core (XMLUI): filters can now be used when converting data form to XMLUI:
Goffi <goffi@goffi.org>
parents: 2397
diff changeset
182 except KeyError:
3ff9d7a7fe71 core (XMLUI): filters can now be used when converting data form to XMLUI:
Goffi <goffi@goffi.org>
parents: 2397
diff changeset
183 pass
3ff9d7a7fe71 core (XMLUI): filters can now be used when converting data form to XMLUI:
Goffi <goffi@goffi.org>
parents: 2397
diff changeset
184 else:
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
185 widget_type, widget_args, widget_kwargs = widget_filter(
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
186 form_ui, widget_type, widget_args, widget_kwargs
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
187 )
2668
c274201cea94 core, frontends (xmlui): added "hidden" widget, to specify a value to be returned unmodified
Goffi <goffi@goffi.org>
parents: 2625
diff changeset
188 if widget_type != "hidden":
c274201cea94 core, frontends (xmlui): added "hidden" widget, to specify a value to be returned unmodified
Goffi <goffi@goffi.org>
parents: 2625
diff changeset
189 label = field.label or field.var
c274201cea94 core, frontends (xmlui): added "hidden" widget, to specify a value to be returned unmodified
Goffi <goffi@goffi.org>
parents: 2625
diff changeset
190 if label:
c274201cea94 core, frontends (xmlui): added "hidden" widget, to specify a value to be returned unmodified
Goffi <goffi@goffi.org>
parents: 2625
diff changeset
191 form_ui.addLabel(label)
c274201cea94 core, frontends (xmlui): added "hidden" widget, to specify a value to be returned unmodified
Goffi <goffi@goffi.org>
parents: 2625
diff changeset
192 else:
c274201cea94 core, frontends (xmlui): added "hidden" widget, to specify a value to be returned unmodified
Goffi <goffi@goffi.org>
parents: 2625
diff changeset
193 form_ui.addEmpty()
103
6be927a465ed XMLUI refactoring, step 1
Goffi <goffi@goffi.org>
parents: 102
diff changeset
194
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 4032
diff changeset
195 form_ui.add_widget(widget_type, *widget_args, **widget_kwargs)
802
9007bb133009 core, frontends: XMLUI refactoring:
Goffi <goffi@goffi.org>
parents: 800
diff changeset
196
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
197 return form_ui
37
a61beb21d16d Gateway registration, unregistration & edition
Goffi <goffi@goffi.org>
parents: 35
diff changeset
198
1444
8ce9924fa92c tools (xml_tools): better PEP-8 compliance
souliane <souliane@mailoo.org>
parents: 1409
diff changeset
199
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 4032
diff changeset
200 def data_form_2_xmlui(form, submit_id, session_id=None, read_only=False):
1487
0df627d0b4ca tools (xmlui): changeContainer do not create a new one when the current one already fits
souliane <souliane@mailoo.org>
parents: 1486
diff changeset
201 """Take a data form (Wokkel's XEP-0004 implementation) and convert it to a SàT XMLUI.
1069
8e1f30aa3975 core (XMLUI): data form result now manage generic data set
Goffi <goffi@goffi.org>
parents: 1067
diff changeset
202
1444
8ce9924fa92c tools (xml_tools): better PEP-8 compliance
souliane <souliane@mailoo.org>
parents: 1409
diff changeset
203 @param form (data_form.Form): a Form instance
8ce9924fa92c tools (xml_tools): better PEP-8 compliance
souliane <souliane@mailoo.org>
parents: 1409
diff changeset
204 @param submit_id (unicode): callback id to call when submitting form
8ce9924fa92c tools (xml_tools): better PEP-8 compliance
souliane <souliane@mailoo.org>
parents: 1409
diff changeset
205 @param session_id (unicode): session id to return with the data
8ce9924fa92c tools (xml_tools): better PEP-8 compliance
souliane <souliane@mailoo.org>
parents: 1409
diff changeset
206 @param read_only (bool): if True and it makes sense, create a read only input widget
8ce9924fa92c tools (xml_tools): better PEP-8 compliance
souliane <souliane@mailoo.org>
parents: 1409
diff changeset
207 @return: XMLUI instance
1069
8e1f30aa3975 core (XMLUI): data form result now manage generic data set
Goffi <goffi@goffi.org>
parents: 1067
diff changeset
208 """
8e1f30aa3975 core (XMLUI): data form result now manage generic data set
Goffi <goffi@goffi.org>
parents: 1067
diff changeset
209 form_ui = XMLUI("form", "vertical", submit_id=submit_id, session_id=session_id)
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 4032
diff changeset
210 return data_form_2_widgets(form_ui, form, read_only=read_only)
1069
8e1f30aa3975 core (XMLUI): data form result now manage generic data set
Goffi <goffi@goffi.org>
parents: 1067
diff changeset
211
1444
8ce9924fa92c tools (xml_tools): better PEP-8 compliance
souliane <souliane@mailoo.org>
parents: 1409
diff changeset
212
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 4032
diff changeset
213 def data_form_2_data_dict(form: data_form.Form) -> dict:
3472
e12e9e1535d3 tools (xml_tools): new `dataForm2dataDict` and `dataDict2dataForm`:
Goffi <goffi@goffi.org>
parents: 3466
diff changeset
214 """Convert data form to a simple dict, easily serialisable
e12e9e1535d3 tools (xml_tools): new `dataForm2dataDict` and `dataDict2dataForm`:
Goffi <goffi@goffi.org>
parents: 3466
diff changeset
215
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 4032
diff changeset
216 see data_dict_2_data_form for a description of the format
3472
e12e9e1535d3 tools (xml_tools): new `dataForm2dataDict` and `dataDict2dataForm`:
Goffi <goffi@goffi.org>
parents: 3466
diff changeset
217 """
e12e9e1535d3 tools (xml_tools): new `dataForm2dataDict` and `dataDict2dataForm`:
Goffi <goffi@goffi.org>
parents: 3466
diff changeset
218 fields = []
4270
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4110
diff changeset
219 data_dict = {"fields": fields}
3472
e12e9e1535d3 tools (xml_tools): new `dataForm2dataDict` and `dataDict2dataForm`:
Goffi <goffi@goffi.org>
parents: 3466
diff changeset
220 if form.formNamespace:
e12e9e1535d3 tools (xml_tools): new `dataForm2dataDict` and `dataDict2dataForm`:
Goffi <goffi@goffi.org>
parents: 3466
diff changeset
221 data_dict["namespace"] = form.formNamespace
e12e9e1535d3 tools (xml_tools): new `dataForm2dataDict` and `dataDict2dataForm`:
Goffi <goffi@goffi.org>
parents: 3466
diff changeset
222 for form_field in form.fieldList:
e12e9e1535d3 tools (xml_tools): new `dataForm2dataDict` and `dataDict2dataForm`:
Goffi <goffi@goffi.org>
parents: 3466
diff changeset
223 field = {"type": form_field.fieldType}
e12e9e1535d3 tools (xml_tools): new `dataForm2dataDict` and `dataDict2dataForm`:
Goffi <goffi@goffi.org>
parents: 3466
diff changeset
224 fields.append(field)
e12e9e1535d3 tools (xml_tools): new `dataForm2dataDict` and `dataDict2dataForm`:
Goffi <goffi@goffi.org>
parents: 3466
diff changeset
225 for src_name, dest_name in (
4270
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4110
diff changeset
226 ("var", "name"),
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4110
diff changeset
227 ("label", "label"),
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4110
diff changeset
228 ("value", "value"),
4032
bb211f80c3e6 tools (xml_tools): add fields's `values` when converting data form to dict
Goffi <goffi@goffi.org>
parents: 4010
diff changeset
229 # FIXME: we probably should have only "values"
4270
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4110
diff changeset
230 ("values", "values"),
3472
e12e9e1535d3 tools (xml_tools): new `dataForm2dataDict` and `dataDict2dataForm`:
Goffi <goffi@goffi.org>
parents: 3466
diff changeset
231 ):
e12e9e1535d3 tools (xml_tools): new `dataForm2dataDict` and `dataDict2dataForm`:
Goffi <goffi@goffi.org>
parents: 3466
diff changeset
232 value = getattr(form_field, src_name, None)
e12e9e1535d3 tools (xml_tools): new `dataForm2dataDict` and `dataDict2dataForm`:
Goffi <goffi@goffi.org>
parents: 3466
diff changeset
233 if value:
e12e9e1535d3 tools (xml_tools): new `dataForm2dataDict` and `dataDict2dataForm`:
Goffi <goffi@goffi.org>
parents: 3466
diff changeset
234 field[dest_name] = value
e12e9e1535d3 tools (xml_tools): new `dataForm2dataDict` and `dataDict2dataForm`:
Goffi <goffi@goffi.org>
parents: 3466
diff changeset
235 if form_field.options:
e12e9e1535d3 tools (xml_tools): new `dataForm2dataDict` and `dataDict2dataForm`:
Goffi <goffi@goffi.org>
parents: 3466
diff changeset
236 options = field["options"] = []
e12e9e1535d3 tools (xml_tools): new `dataForm2dataDict` and `dataDict2dataForm`:
Goffi <goffi@goffi.org>
parents: 3466
diff changeset
237 for form_opt in form_field.options:
e12e9e1535d3 tools (xml_tools): new `dataForm2dataDict` and `dataDict2dataForm`:
Goffi <goffi@goffi.org>
parents: 3466
diff changeset
238 opt = {"value": form_opt.value}
e12e9e1535d3 tools (xml_tools): new `dataForm2dataDict` and `dataDict2dataForm`:
Goffi <goffi@goffi.org>
parents: 3466
diff changeset
239 if form_opt.label:
e12e9e1535d3 tools (xml_tools): new `dataForm2dataDict` and `dataDict2dataForm`:
Goffi <goffi@goffi.org>
parents: 3466
diff changeset
240 opt["label"] = form_opt.label
e12e9e1535d3 tools (xml_tools): new `dataForm2dataDict` and `dataDict2dataForm`:
Goffi <goffi@goffi.org>
parents: 3466
diff changeset
241 options.append(opt)
e12e9e1535d3 tools (xml_tools): new `dataForm2dataDict` and `dataDict2dataForm`:
Goffi <goffi@goffi.org>
parents: 3466
diff changeset
242
e12e9e1535d3 tools (xml_tools): new `dataForm2dataDict` and `dataDict2dataForm`:
Goffi <goffi@goffi.org>
parents: 3466
diff changeset
243 if form_field.fieldType is None and form_field.ext_type == "xml":
e12e9e1535d3 tools (xml_tools): new `dataForm2dataDict` and `dataDict2dataForm`:
Goffi <goffi@goffi.org>
parents: 3466
diff changeset
244 if isinstance(form_field.value, domish.Element):
4270
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4110
diff changeset
245 if form_field.value.uri == C.NS_XHTML and form_field.value.name == "div":
3472
e12e9e1535d3 tools (xml_tools): new `dataForm2dataDict` and `dataDict2dataForm`:
Goffi <goffi@goffi.org>
parents: 3466
diff changeset
246 field["type"] = "xhtml"
e12e9e1535d3 tools (xml_tools): new `dataForm2dataDict` and `dataDict2dataForm`:
Goffi <goffi@goffi.org>
parents: 3466
diff changeset
247 if form_field.value.children:
e12e9e1535d3 tools (xml_tools): new `dataForm2dataDict` and `dataDict2dataForm`:
Goffi <goffi@goffi.org>
parents: 3466
diff changeset
248 log.warning(
e12e9e1535d3 tools (xml_tools): new `dataForm2dataDict` and `dataDict2dataForm`:
Goffi <goffi@goffi.org>
parents: 3466
diff changeset
249 "children are not managed for XHTML fields: "
e12e9e1535d3 tools (xml_tools): new `dataForm2dataDict` and `dataDict2dataForm`:
Goffi <goffi@goffi.org>
parents: 3466
diff changeset
250 f"{form_field.value.toXml()}"
e12e9e1535d3 tools (xml_tools): new `dataForm2dataDict` and `dataDict2dataForm`:
Goffi <goffi@goffi.org>
parents: 3466
diff changeset
251 )
e12e9e1535d3 tools (xml_tools): new `dataForm2dataDict` and `dataDict2dataForm`:
Goffi <goffi@goffi.org>
parents: 3466
diff changeset
252 return data_dict
e12e9e1535d3 tools (xml_tools): new `dataForm2dataDict` and `dataDict2dataForm`:
Goffi <goffi@goffi.org>
parents: 3466
diff changeset
253
e12e9e1535d3 tools (xml_tools): new `dataForm2dataDict` and `dataDict2dataForm`:
Goffi <goffi@goffi.org>
parents: 3466
diff changeset
254
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 4032
diff changeset
255 def data_dict_2_data_form(data_dict):
3472
e12e9e1535d3 tools (xml_tools): new `dataForm2dataDict` and `dataDict2dataForm`:
Goffi <goffi@goffi.org>
parents: 3466
diff changeset
256 """Convert serialisable dict of data to a data form
e12e9e1535d3 tools (xml_tools): new `dataForm2dataDict` and `dataDict2dataForm`:
Goffi <goffi@goffi.org>
parents: 3466
diff changeset
257
e12e9e1535d3 tools (xml_tools): new `dataForm2dataDict` and `dataDict2dataForm`:
Goffi <goffi@goffi.org>
parents: 3466
diff changeset
258 The format of the dict is as follow:
e12e9e1535d3 tools (xml_tools): new `dataForm2dataDict` and `dataDict2dataForm`:
Goffi <goffi@goffi.org>
parents: 3466
diff changeset
259 - an optional "namespace" key with form namespace
e12e9e1535d3 tools (xml_tools): new `dataForm2dataDict` and `dataDict2dataForm`:
Goffi <goffi@goffi.org>
parents: 3466
diff changeset
260 - a mandatory "fields" key with list of fields as follow:
e12e9e1535d3 tools (xml_tools): new `dataForm2dataDict` and `dataDict2dataForm`:
Goffi <goffi@goffi.org>
parents: 3466
diff changeset
261 - "type" is mostly the same as data_form.Field.fieldType
3531
a8259a1f89b2 component file sharing (management): add values of `quota` and `used` size on result form
Goffi <goffi@goffi.org>
parents: 3491
diff changeset
262 - "name" is used to set the "var" attribute of data_form.Field
a8259a1f89b2 component file sharing (management): add values of `quota` and `used` size on result form
Goffi <goffi@goffi.org>
parents: 3491
diff changeset
263 - "label", and "value" follow same attribude in data_form.Field
3472
e12e9e1535d3 tools (xml_tools): new `dataForm2dataDict` and `dataDict2dataForm`:
Goffi <goffi@goffi.org>
parents: 3466
diff changeset
264 - "xhtml" is used for "xml" fields with child in the C.NS_XHTML namespace
e12e9e1535d3 tools (xml_tools): new `dataForm2dataDict` and `dataDict2dataForm`:
Goffi <goffi@goffi.org>
parents: 3466
diff changeset
265 - "options" are list of dict with optional "label" and mandatory "value"
e12e9e1535d3 tools (xml_tools): new `dataForm2dataDict` and `dataDict2dataForm`:
Goffi <goffi@goffi.org>
parents: 3466
diff changeset
266 following suitable attributes from data_form.Option
3903
384b7e6c2dbf tools (xml_tools): handle `required` field in `dataDict2dataForm`:
Goffi <goffi@goffi.org>
parents: 3732
diff changeset
267 - "required" is the same as data_form.Field.required
3472
e12e9e1535d3 tools (xml_tools): new `dataForm2dataDict` and `dataDict2dataForm`:
Goffi <goffi@goffi.org>
parents: 3466
diff changeset
268 """
e12e9e1535d3 tools (xml_tools): new `dataForm2dataDict` and `dataDict2dataForm`:
Goffi <goffi@goffi.org>
parents: 3466
diff changeset
269 # TODO: describe format
e12e9e1535d3 tools (xml_tools): new `dataForm2dataDict` and `dataDict2dataForm`:
Goffi <goffi@goffi.org>
parents: 3466
diff changeset
270 fields = []
e12e9e1535d3 tools (xml_tools): new `dataForm2dataDict` and `dataDict2dataForm`:
Goffi <goffi@goffi.org>
parents: 3466
diff changeset
271 for field_data in data_dict["fields"]:
4270
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4110
diff changeset
272 field_type = field_data.get("type", "text-single")
3472
e12e9e1535d3 tools (xml_tools): new `dataForm2dataDict` and `dataDict2dataForm`:
Goffi <goffi@goffi.org>
parents: 3466
diff changeset
273 kwargs = {
e12e9e1535d3 tools (xml_tools): new `dataForm2dataDict` and `dataDict2dataForm`:
Goffi <goffi@goffi.org>
parents: 3466
diff changeset
274 "fieldType": field_type,
e12e9e1535d3 tools (xml_tools): new `dataForm2dataDict` and `dataDict2dataForm`:
Goffi <goffi@goffi.org>
parents: 3466
diff changeset
275 "var": field_data["name"],
4270
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4110
diff changeset
276 "label": field_data.get("label"),
3472
e12e9e1535d3 tools (xml_tools): new `dataForm2dataDict` and `dataDict2dataForm`:
Goffi <goffi@goffi.org>
parents: 3466
diff changeset
277 "value": field_data.get("value"),
4270
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4110
diff changeset
278 "required": field_data.get("required"),
3472
e12e9e1535d3 tools (xml_tools): new `dataForm2dataDict` and `dataDict2dataForm`:
Goffi <goffi@goffi.org>
parents: 3466
diff changeset
279 }
e12e9e1535d3 tools (xml_tools): new `dataForm2dataDict` and `dataDict2dataForm`:
Goffi <goffi@goffi.org>
parents: 3466
diff changeset
280 if field_type == "xhtml":
4270
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4110
diff changeset
281 kwargs.update(
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4110
diff changeset
282 {
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4110
diff changeset
283 "fieldType": None,
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4110
diff changeset
284 "ext_type": "xml",
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4110
diff changeset
285 }
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4110
diff changeset
286 )
3472
e12e9e1535d3 tools (xml_tools): new `dataForm2dataDict` and `dataDict2dataForm`:
Goffi <goffi@goffi.org>
parents: 3466
diff changeset
287 if kwargs["value"] is None:
e12e9e1535d3 tools (xml_tools): new `dataForm2dataDict` and `dataDict2dataForm`:
Goffi <goffi@goffi.org>
parents: 3466
diff changeset
288 kwargs["value"] = domish.Element((C.NS_XHTML, "div"))
e12e9e1535d3 tools (xml_tools): new `dataForm2dataDict` and `dataDict2dataForm`:
Goffi <goffi@goffi.org>
parents: 3466
diff changeset
289 elif "options" in field_data:
e12e9e1535d3 tools (xml_tools): new `dataForm2dataDict` and `dataDict2dataForm`:
Goffi <goffi@goffi.org>
parents: 3466
diff changeset
290 kwargs["options"] = [
e12e9e1535d3 tools (xml_tools): new `dataForm2dataDict` and `dataDict2dataForm`:
Goffi <goffi@goffi.org>
parents: 3466
diff changeset
291 data_form.Option(o["value"], o.get("label"))
e12e9e1535d3 tools (xml_tools): new `dataForm2dataDict` and `dataDict2dataForm`:
Goffi <goffi@goffi.org>
parents: 3466
diff changeset
292 for o in field_data["options"]
e12e9e1535d3 tools (xml_tools): new `dataForm2dataDict` and `dataDict2dataForm`:
Goffi <goffi@goffi.org>
parents: 3466
diff changeset
293 ]
e12e9e1535d3 tools (xml_tools): new `dataForm2dataDict` and `dataDict2dataForm`:
Goffi <goffi@goffi.org>
parents: 3466
diff changeset
294 field = data_form.Field(**kwargs)
e12e9e1535d3 tools (xml_tools): new `dataForm2dataDict` and `dataDict2dataForm`:
Goffi <goffi@goffi.org>
parents: 3466
diff changeset
295 fields.append(field)
4270
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4110
diff changeset
296 return data_form.Form("form", formNamespace=data_dict.get("namespace"), fields=fields)
3472
e12e9e1535d3 tools (xml_tools): new `dataForm2dataDict` and `dataDict2dataForm`:
Goffi <goffi@goffi.org>
parents: 3466
diff changeset
297
e12e9e1535d3 tools (xml_tools): new `dataForm2dataDict` and `dataDict2dataForm`:
Goffi <goffi@goffi.org>
parents: 3466
diff changeset
298
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 4032
diff changeset
299 def data_form_elt_result_2_xmlui_data(form_xml):
1507
84250128e425 tools (xml_tools): split dataFormResult2AdvancedList in dataFormResult2XMLUIData and XMLUIData2AdvancedList
souliane <souliane@mailoo.org>
parents: 1504
diff changeset
300 """Parse a data form result (not parsed by Wokkel's XEP-0004 implementation).
1444
8ce9924fa92c tools (xml_tools): better PEP-8 compliance
souliane <souliane@mailoo.org>
parents: 1409
diff changeset
301
8ce9924fa92c tools (xml_tools): better PEP-8 compliance
souliane <souliane@mailoo.org>
parents: 1409
diff changeset
302 The raw data form is used because Wokkel doesn't manage result items parsing yet.
8ce9924fa92c tools (xml_tools): better PEP-8 compliance
souliane <souliane@mailoo.org>
parents: 1409
diff changeset
303 @param form_xml (domish.Element): element of the data form
1507
84250128e425 tools (xml_tools): split dataFormResult2AdvancedList in dataFormResult2XMLUIData and XMLUIData2AdvancedList
souliane <souliane@mailoo.org>
parents: 1504
diff changeset
304 @return: a couple (headers, result_list):
84250128e425 tools (xml_tools): split dataFormResult2AdvancedList in dataFormResult2XMLUIData and XMLUIData2AdvancedList
souliane <souliane@mailoo.org>
parents: 1504
diff changeset
305 - headers (dict{unicode: unicode}): form headers (field labels and types)
84250128e425 tools (xml_tools): split dataFormResult2AdvancedList in dataFormResult2XMLUIData and XMLUIData2AdvancedList
souliane <souliane@mailoo.org>
parents: 1504
diff changeset
306 - xmlui_data (list[tuple]): list of (widget_type, widget_args, widget_kwargs)
630
0b914394e74f core: added advanced list to XMLUI (need improvment, very basic so far)
Goffi <goffi@goffi.org>
parents: 615
diff changeset
307 """
1500
c7c872a40e56 tools (xml_tools): keeps data form result headers sorted
souliane <souliane@mailoo.org>
parents: 1499
diff changeset
308 headers = OrderedDict()
630
0b914394e74f core: added advanced list to XMLUI (need improvment, very basic so far)
Goffi <goffi@goffi.org>
parents: 615
diff changeset
309 try:
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2958
diff changeset
310 reported_elt = next(form_xml.elements("jabber:x:data", "reported"))
630
0b914394e74f core: added advanced list to XMLUI (need improvment, very basic so far)
Goffi <goffi@goffi.org>
parents: 615
diff changeset
311 except StopIteration:
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
312 raise exceptions.DataError(
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
313 "Couldn't find expected <reported> tag in %s" % form_xml.toXml()
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
314 )
630
0b914394e74f core: added advanced list to XMLUI (need improvment, very basic so far)
Goffi <goffi@goffi.org>
parents: 615
diff changeset
315
660
69a8bfd266a5 core, plugins: fixed bad use of children instead of elements() for domish.Element instances.
Goffi <goffi@goffi.org>
parents: 655
diff changeset
316 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
317 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
318 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
319 name = elt["var"]
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
320 label = elt.attributes.get("label", "")
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
321 type_ = elt.attributes.get("type")
802
9007bb133009 core, frontends: XMLUI refactoring:
Goffi <goffi@goffi.org>
parents: 800
diff changeset
322 headers[name] = (label, type_)
630
0b914394e74f core: added advanced list to XMLUI (need improvment, very basic so far)
Goffi <goffi@goffi.org>
parents: 615
diff changeset
323
0b914394e74f core: added advanced list to XMLUI (need improvment, very basic so far)
Goffi <goffi@goffi.org>
parents: 615
diff changeset
324 if not headers:
684
969562c4761b core (tools): small fix in using imported methods
souliane <souliane@mailoo.org>
parents: 660
diff changeset
325 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
326
1507
84250128e425 tools (xml_tools): split dataFormResult2AdvancedList in dataFormResult2XMLUIData and XMLUIData2AdvancedList
souliane <souliane@mailoo.org>
parents: 1504
diff changeset
327 xmlui_data = []
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
328 item_elts = form_xml.elements("jabber:x:data", "item")
630
0b914394e74f core: added advanced list to XMLUI (need improvment, very basic so far)
Goffi <goffi@goffi.org>
parents: 615
diff changeset
329
0b914394e74f core: added advanced list to XMLUI (need improvment, very basic so far)
Goffi <goffi@goffi.org>
parents: 615
diff changeset
330 for item_elt in item_elts:
660
69a8bfd266a5 core, plugins: fixed bad use of children instead of elements() for domish.Element instances.
Goffi <goffi@goffi.org>
parents: 655
diff changeset
331 for elt in item_elt.elements():
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
332 if elt.name != "field":
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2958
diff changeset
333 log.warning("Unexpected tag (%s)" % elt.name)
630
0b914394e74f core: added advanced list to XMLUI (need improvment, very basic so far)
Goffi <goffi@goffi.org>
parents: 615
diff changeset
334 continue
802
9007bb133009 core, frontends: XMLUI refactoring:
Goffi <goffi@goffi.org>
parents: 800
diff changeset
335 field = data_form.Field.fromElement(elt)
630
0b914394e74f core: added advanced list to XMLUI (need improvment, very basic so far)
Goffi <goffi@goffi.org>
parents: 615
diff changeset
336
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 4032
diff changeset
337 xmlui_data.append(_data_form_field_2_xmlui_data(field))
1507
84250128e425 tools (xml_tools): split dataFormResult2AdvancedList in dataFormResult2XMLUIData and XMLUIData2AdvancedList
souliane <souliane@mailoo.org>
parents: 1504
diff changeset
338
84250128e425 tools (xml_tools): split dataFormResult2AdvancedList in dataFormResult2XMLUIData and XMLUIData2AdvancedList
souliane <souliane@mailoo.org>
parents: 1504
diff changeset
339 return headers, xmlui_data
84250128e425 tools (xml_tools): split dataFormResult2AdvancedList in dataFormResult2XMLUIData and XMLUIData2AdvancedList
souliane <souliane@mailoo.org>
parents: 1504
diff changeset
340
84250128e425 tools (xml_tools): split dataFormResult2AdvancedList in dataFormResult2XMLUIData and XMLUIData2AdvancedList
souliane <souliane@mailoo.org>
parents: 1504
diff changeset
341
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 4032
diff changeset
342 def xmlui_data_2_advanced_list(xmlui, headers, xmlui_data):
1507
84250128e425 tools (xml_tools): split dataFormResult2AdvancedList in dataFormResult2XMLUIData and XMLUIData2AdvancedList
souliane <souliane@mailoo.org>
parents: 1504
diff changeset
343 """Take a raw data form result (not parsed by Wokkel's XEP-0004 implementation) and convert it to an advanced list.
84250128e425 tools (xml_tools): split dataFormResult2AdvancedList in dataFormResult2XMLUIData and XMLUIData2AdvancedList
souliane <souliane@mailoo.org>
parents: 1504
diff changeset
344
84250128e425 tools (xml_tools): split dataFormResult2AdvancedList in dataFormResult2XMLUIData and XMLUIData2AdvancedList
souliane <souliane@mailoo.org>
parents: 1504
diff changeset
345 The raw data form is used because Wokkel doesn't manage result items parsing yet.
84250128e425 tools (xml_tools): split dataFormResult2AdvancedList in dataFormResult2XMLUIData and XMLUIData2AdvancedList
souliane <souliane@mailoo.org>
parents: 1504
diff changeset
346 @param xmlui (XMLUI): the XMLUI where the AdvancedList will be added
84250128e425 tools (xml_tools): split dataFormResult2AdvancedList in dataFormResult2XMLUIData and XMLUIData2AdvancedList
souliane <souliane@mailoo.org>
parents: 1504
diff changeset
347 @param headers (dict{unicode: unicode}): form headers (field labels and types)
84250128e425 tools (xml_tools): split dataFormResult2AdvancedList in dataFormResult2XMLUIData and XMLUIData2AdvancedList
souliane <souliane@mailoo.org>
parents: 1504
diff changeset
348 @param xmlui_data (list[tuple]): list of (widget_type, widget_args, widget_kwargs)
84250128e425 tools (xml_tools): split dataFormResult2AdvancedList in dataFormResult2XMLUIData and XMLUIData2AdvancedList
souliane <souliane@mailoo.org>
parents: 1504
diff changeset
349 @return: the completed XMLUI instance
84250128e425 tools (xml_tools): split dataFormResult2AdvancedList in dataFormResult2XMLUIData and XMLUIData2AdvancedList
souliane <souliane@mailoo.org>
parents: 1504
diff changeset
350 """
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
351 adv_list = AdvancedListContainer(
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
352 xmlui, headers=headers, columns=len(headers), parent=xmlui.current_container
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
353 )
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 4032
diff changeset
354 xmlui.change_container(adv_list)
1507
84250128e425 tools (xml_tools): split dataFormResult2AdvancedList in dataFormResult2XMLUIData and XMLUIData2AdvancedList
souliane <souliane@mailoo.org>
parents: 1504
diff changeset
355
84250128e425 tools (xml_tools): split dataFormResult2AdvancedList in dataFormResult2XMLUIData and XMLUIData2AdvancedList
souliane <souliane@mailoo.org>
parents: 1504
diff changeset
356 for widget_type, widget_args, widget_kwargs in xmlui_data:
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 4032
diff changeset
357 xmlui.add_widget(widget_type, *widget_args, **widget_kwargs)
630
0b914394e74f core: added advanced list to XMLUI (need improvment, very basic so far)
Goffi <goffi@goffi.org>
parents: 615
diff changeset
358
803
f100fd8d279f core, frontends: implementation of AdvancedListContainer first draft + misc:
Goffi <goffi@goffi.org>
parents: 802
diff changeset
359 return xmlui
630
0b914394e74f core: added advanced list to XMLUI (need improvment, very basic so far)
Goffi <goffi@goffi.org>
parents: 615
diff changeset
360
1444
8ce9924fa92c tools (xml_tools): better PEP-8 compliance
souliane <souliane@mailoo.org>
parents: 1409
diff changeset
361
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 4032
diff changeset
362 def data_form_result_2_advanced_list(xmlui, form_xml):
1507
84250128e425 tools (xml_tools): split dataFormResult2AdvancedList in dataFormResult2XMLUIData and XMLUIData2AdvancedList
souliane <souliane@mailoo.org>
parents: 1504
diff changeset
363 """Take a raw data form result (not parsed by Wokkel's XEP-0004 implementation) and convert it to an advanced list.
84250128e425 tools (xml_tools): split dataFormResult2AdvancedList in dataFormResult2XMLUIData and XMLUIData2AdvancedList
souliane <souliane@mailoo.org>
parents: 1504
diff changeset
364
84250128e425 tools (xml_tools): split dataFormResult2AdvancedList in dataFormResult2XMLUIData and XMLUIData2AdvancedList
souliane <souliane@mailoo.org>
parents: 1504
diff changeset
365 The raw data form is used because Wokkel doesn't manage result items parsing yet.
84250128e425 tools (xml_tools): split dataFormResult2AdvancedList in dataFormResult2XMLUIData and XMLUIData2AdvancedList
souliane <souliane@mailoo.org>
parents: 1504
diff changeset
366 @param xmlui (XMLUI): the XMLUI where the AdvancedList will be added
84250128e425 tools (xml_tools): split dataFormResult2AdvancedList in dataFormResult2XMLUIData and XMLUIData2AdvancedList
souliane <souliane@mailoo.org>
parents: 1504
diff changeset
367 @param form_xml (domish.Element): element of the data form
84250128e425 tools (xml_tools): split dataFormResult2AdvancedList in dataFormResult2XMLUIData and XMLUIData2AdvancedList
souliane <souliane@mailoo.org>
parents: 1504
diff changeset
368 @return: the completed XMLUI instance
84250128e425 tools (xml_tools): split dataFormResult2AdvancedList in dataFormResult2XMLUIData and XMLUIData2AdvancedList
souliane <souliane@mailoo.org>
parents: 1504
diff changeset
369 """
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 4032
diff changeset
370 headers, xmlui_data = data_form_elt_result_2_xmlui_data(form_xml)
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 4032
diff changeset
371 xmlui_data_2_advanced_list(xmlui, headers, xmlui_data)
1507
84250128e425 tools (xml_tools): split dataFormResult2AdvancedList in dataFormResult2XMLUIData and XMLUIData2AdvancedList
souliane <souliane@mailoo.org>
parents: 1504
diff changeset
372
84250128e425 tools (xml_tools): split dataFormResult2AdvancedList in dataFormResult2XMLUIData and XMLUIData2AdvancedList
souliane <souliane@mailoo.org>
parents: 1504
diff changeset
373
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 4032
diff changeset
374 def data_form_elt_result_2_xmlui(form_elt, session_id=None):
1444
8ce9924fa92c tools (xml_tools): better PEP-8 compliance
souliane <souliane@mailoo.org>
parents: 1409
diff changeset
375 """Take a raw data form (not parsed by XEP-0004) and convert it to a SàT XMLUI.
8ce9924fa92c tools (xml_tools): better PEP-8 compliance
souliane <souliane@mailoo.org>
parents: 1409
diff changeset
376
8ce9924fa92c tools (xml_tools): better PEP-8 compliance
souliane <souliane@mailoo.org>
parents: 1409
diff changeset
377 The raw data form is used because Wokkel doesn't manage result items parsing yet.
8ce9924fa92c tools (xml_tools): better PEP-8 compliance
souliane <souliane@mailoo.org>
parents: 1409
diff changeset
378 @param form_elt (domish.Element): element of the data form
8ce9924fa92c tools (xml_tools): better PEP-8 compliance
souliane <souliane@mailoo.org>
parents: 1409
diff changeset
379 @param session_id (unicode): session id to return with the data
8ce9924fa92c tools (xml_tools): better PEP-8 compliance
souliane <souliane@mailoo.org>
parents: 1409
diff changeset
380 @return: XMLUI instance
630
0b914394e74f core: added advanced list to XMLUI (need improvment, very basic so far)
Goffi <goffi@goffi.org>
parents: 615
diff changeset
381 """
1069
8e1f30aa3975 core (XMLUI): data form result now manage generic data set
Goffi <goffi@goffi.org>
parents: 1067
diff changeset
382 xml_ui = XMLUI("window", "vertical", session_id=session_id)
8e1f30aa3975 core (XMLUI): data form result now manage generic data set
Goffi <goffi@goffi.org>
parents: 1067
diff changeset
383 try:
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 4032
diff changeset
384 data_form_result_2_advanced_list(xml_ui, form_elt)
1069
8e1f30aa3975 core (XMLUI): data form result now manage generic data set
Goffi <goffi@goffi.org>
parents: 1067
diff changeset
385 except exceptions.DataError:
8e1f30aa3975 core (XMLUI): data form result now manage generic data set
Goffi <goffi@goffi.org>
parents: 1067
diff changeset
386 parsed_form = data_form.Form.fromElement(form_elt)
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 4032
diff changeset
387 data_form_2_widgets(xml_ui, parsed_form, read_only=True)
1069
8e1f30aa3975 core (XMLUI): data form result now manage generic data set
Goffi <goffi@goffi.org>
parents: 1067
diff changeset
388 return xml_ui
590
56531f9e9ac7 Fix pep8 support in src/tools.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 588
diff changeset
389
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
390
4270
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4110
diff changeset
391 def data_form_result_2_xmlui(
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4110
diff changeset
392 result_form, base_form, session_id=None, prepend=None, filters=None, read_only=True
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4110
diff changeset
393 ):
2364
918e38622a48 core (xmlui): added method to convert data form result to XMLUI:
Goffi <goffi@goffi.org>
parents: 2361
diff changeset
394 """Convert data form result to SàT XMLUI.
918e38622a48 core (xmlui): added method to convert data form result to XMLUI:
Goffi <goffi@goffi.org>
parents: 2361
diff changeset
395
918e38622a48 core (xmlui): added method to convert data form result to XMLUI:
Goffi <goffi@goffi.org>
parents: 2361
diff changeset
396 @param result_form (data_form.Form): result form to convert
918e38622a48 core (xmlui): added method to convert data form result to XMLUI:
Goffi <goffi@goffi.org>
parents: 2361
diff changeset
397 @param base_form (data_form.Form): initial form (i.e. of form type "form")
918e38622a48 core (xmlui): added method to convert data form result to XMLUI:
Goffi <goffi@goffi.org>
parents: 2361
diff changeset
398 this one is necessary to reconstruct options when needed (e.g. list elements)
918e38622a48 core (xmlui): added method to convert data form result to XMLUI:
Goffi <goffi@goffi.org>
parents: 2361
diff changeset
399 @param session_id (unicode): session id to return with the data
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 4032
diff changeset
400 @param prepend: same as for [data_form_2_widgets]
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 4032
diff changeset
401 @param filters: same as for [data_form_2_widgets]
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 4032
diff changeset
402 @param read_only: same as for [data_form_2_widgets]
2364
918e38622a48 core (xmlui): added method to convert data form result to XMLUI:
Goffi <goffi@goffi.org>
parents: 2361
diff changeset
403 @return: XMLUI instance
918e38622a48 core (xmlui): added method to convert data form result to XMLUI:
Goffi <goffi@goffi.org>
parents: 2361
diff changeset
404 """
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 4032
diff changeset
405 # we deepcopy the form because _data_form_field_2_xmlui_data can modify the value
2762
5a51c7fc74a5 core (XMLUI): small optimisation: introspection is done once at module loading instead of on each XMLUI instantiation
Goffi <goffi@goffi.org>
parents: 2741
diff changeset
406 # FIXME: check if it's really important, the only modified value seems to be
5a51c7fc74a5 core (XMLUI): small optimisation: introspection is done once at module loading instead of on each XMLUI instantiation
Goffi <goffi@goffi.org>
parents: 2741
diff changeset
407 # the replacement of None by "" on fixed fields
4270
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4110
diff changeset
408 # form = deepcopy(result_form)
2762
5a51c7fc74a5 core (XMLUI): small optimisation: introspection is done once at module loading instead of on each XMLUI instantiation
Goffi <goffi@goffi.org>
parents: 2741
diff changeset
409 form = result_form
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2958
diff changeset
410 for name, field in form.fields.items():
2364
918e38622a48 core (xmlui): added method to convert data form result to XMLUI:
Goffi <goffi@goffi.org>
parents: 2361
diff changeset
411 try:
918e38622a48 core (xmlui): added method to convert data form result to XMLUI:
Goffi <goffi@goffi.org>
parents: 2361
diff changeset
412 base_field = base_form.fields[name]
918e38622a48 core (xmlui): added method to convert data form result to XMLUI:
Goffi <goffi@goffi.org>
parents: 2361
diff changeset
413 except KeyError:
918e38622a48 core (xmlui): added method to convert data form result to XMLUI:
Goffi <goffi@goffi.org>
parents: 2361
diff changeset
414 continue
2386
2e05921df16a plugin schema, core(xmlui): deep copy base form when reused to avoid accidental modification
Goffi <goffi@goffi.org>
parents: 2380
diff changeset
415 field.options = base_field.options[:]
2364
918e38622a48 core (xmlui): added method to convert data form result to XMLUI:
Goffi <goffi@goffi.org>
parents: 2361
diff changeset
416 xml_ui = XMLUI("window", "vertical", session_id=session_id)
4270
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4110
diff changeset
417 data_form_2_widgets(
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4110
diff changeset
418 xml_ui, form, read_only=read_only, prepend=prepend, filters=filters
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4110
diff changeset
419 )
2364
918e38622a48 core (xmlui): added method to convert data form result to XMLUI:
Goffi <goffi@goffi.org>
parents: 2361
diff changeset
420 return xml_ui
918e38622a48 core (xmlui): added method to convert data form result to XMLUI:
Goffi <goffi@goffi.org>
parents: 2361
diff changeset
421
1444
8ce9924fa92c tools (xml_tools): better PEP-8 compliance
souliane <souliane@mailoo.org>
parents: 1409
diff changeset
422
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 4032
diff changeset
423 def _clean_value(value):
1444
8ce9924fa92c tools (xml_tools): better PEP-8 compliance
souliane <souliane@mailoo.org>
parents: 1409
diff changeset
424 """Workaround method to avoid DBus types with D-Bus bridge.
981
58a57ce5932a core (XMLUI): _cleanValue method to avoid issues with DBus types (must be deleted in the future)
Goffi <goffi@goffi.org>
parents: 977
diff changeset
425
58a57ce5932a core (XMLUI): _cleanValue method to avoid issues with DBus types (must be deleted in the future)
Goffi <goffi@goffi.org>
parents: 977
diff changeset
426 @param value: value to clean
58a57ce5932a core (XMLUI): _cleanValue method to avoid issues with DBus types (must be deleted in the future)
Goffi <goffi@goffi.org>
parents: 977
diff changeset
427 @return: value in a non DBus type (only clean string yet)
58a57ce5932a core (XMLUI): _cleanValue method to avoid issues with DBus types (must be deleted in the future)
Goffi <goffi@goffi.org>
parents: 977
diff changeset
428 """
58a57ce5932a core (XMLUI): _cleanValue method to avoid issues with DBus types (must be deleted in the future)
Goffi <goffi@goffi.org>
parents: 977
diff changeset
429 # XXX: must be removed when DBus types will no cause problems anymore
58a57ce5932a core (XMLUI): _cleanValue method to avoid issues with DBus types (must be deleted in the future)
Goffi <goffi@goffi.org>
parents: 977
diff changeset
430 # FIXME: should be cleaned inside D-Bus bridge itself
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2958
diff changeset
431 if isinstance(value, str):
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2958
diff changeset
432 return str(value)
981
58a57ce5932a core (XMLUI): _cleanValue method to avoid issues with DBus types (must be deleted in the future)
Goffi <goffi@goffi.org>
parents: 977
diff changeset
433 return value
58a57ce5932a core (XMLUI): _cleanValue method to avoid issues with DBus types (must be deleted in the future)
Goffi <goffi@goffi.org>
parents: 977
diff changeset
434
1444
8ce9924fa92c tools (xml_tools): better PEP-8 compliance
souliane <souliane@mailoo.org>
parents: 1409
diff changeset
435
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 4032
diff changeset
436 def xmlui_result_2_data_form_result(xmlui_data):
4270
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4110
diff changeset
437 """Extract form data from a XMLUI return.
1444
8ce9924fa92c tools (xml_tools): better PEP-8 compliance
souliane <souliane@mailoo.org>
parents: 1409
diff changeset
438
8ce9924fa92c tools (xml_tools): better PEP-8 compliance
souliane <souliane@mailoo.org>
parents: 1409
diff changeset
439 @param xmlui_data (dict): data returned by frontends for XMLUI form
8ce9924fa92c tools (xml_tools): better PEP-8 compliance
souliane <souliane@mailoo.org>
parents: 1409
diff changeset
440 @return: dict of data usable by Wokkel's data form
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
441 """
2938
48985ef7682f tools (xml_tools): split new lines for multi-lines text in XMLUIResult2DataFormResult to be correctly handled in data form
Goffi <goffi@goffi.org>
parents: 2814
diff changeset
442 ret = {}
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2958
diff changeset
443 for key, value in xmlui_data.items():
2938
48985ef7682f tools (xml_tools): split new lines for multi-lines text in XMLUIResult2DataFormResult to be correctly handled in data form
Goffi <goffi@goffi.org>
parents: 2814
diff changeset
444 if not key.startswith(SAT_FORM_PREFIX):
48985ef7682f tools (xml_tools): split new lines for multi-lines text in XMLUIResult2DataFormResult to be correctly handled in data form
Goffi <goffi@goffi.org>
parents: 2814
diff changeset
445 continue
3716
d33da3fe34a5 tools (xml_tools): implement `list-multi` in `_dataFormField2XMLUIData`:
Goffi <goffi@goffi.org>
parents: 3531
diff changeset
446 if isinstance(value, str):
d33da3fe34a5 tools (xml_tools): implement `list-multi` in `_dataFormField2XMLUIData`:
Goffi <goffi@goffi.org>
parents: 3531
diff changeset
447 if "\n" in value:
d33da3fe34a5 tools (xml_tools): implement `list-multi` in `_dataFormField2XMLUIData`:
Goffi <goffi@goffi.org>
parents: 3531
diff changeset
448 # data form expects multi-lines text to be in separated values
4270
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4110
diff changeset
449 value = value.split("\n")
3716
d33da3fe34a5 tools (xml_tools): implement `list-multi` in `_dataFormField2XMLUIData`:
Goffi <goffi@goffi.org>
parents: 3531
diff changeset
450 elif "\t" in value:
d33da3fe34a5 tools (xml_tools): implement `list-multi` in `_dataFormField2XMLUIData`:
Goffi <goffi@goffi.org>
parents: 3531
diff changeset
451 # FIXME: workaround to handle multiple values. Proper serialisation must
d33da3fe34a5 tools (xml_tools): implement `list-multi` in `_dataFormField2XMLUIData`:
Goffi <goffi@goffi.org>
parents: 3531
diff changeset
452 # be done in XMLUI
d33da3fe34a5 tools (xml_tools): implement `list-multi` in `_dataFormField2XMLUIData`:
Goffi <goffi@goffi.org>
parents: 3531
diff changeset
453 value = value.split("\t")
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 4032
diff changeset
454 ret[key[len(SAT_FORM_PREFIX) :]] = _clean_value(value)
2938
48985ef7682f tools (xml_tools): split new lines for multi-lines text in XMLUIResult2DataFormResult to be correctly handled in data form
Goffi <goffi@goffi.org>
parents: 2814
diff changeset
455 return ret
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
456
1444
8ce9924fa92c tools (xml_tools): better PEP-8 compliance
souliane <souliane@mailoo.org>
parents: 1409
diff changeset
457
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 4032
diff changeset
458 def form_escape(name):
1444
8ce9924fa92c tools (xml_tools): better PEP-8 compliance
souliane <souliane@mailoo.org>
parents: 1409
diff changeset
459 """Return escaped name for forms.
8ce9924fa92c tools (xml_tools): better PEP-8 compliance
souliane <souliane@mailoo.org>
parents: 1409
diff changeset
460
8ce9924fa92c tools (xml_tools): better PEP-8 compliance
souliane <souliane@mailoo.org>
parents: 1409
diff changeset
461 @param name (unicode): form name
8ce9924fa92c tools (xml_tools): better PEP-8 compliance
souliane <souliane@mailoo.org>
parents: 1409
diff changeset
462 @return: unicode
8ce9924fa92c tools (xml_tools): better PEP-8 compliance
souliane <souliane@mailoo.org>
parents: 1409
diff changeset
463 """
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2958
diff changeset
464 return "%s%s" % (SAT_FORM_PREFIX, name)
1444
8ce9924fa92c tools (xml_tools): better PEP-8 compliance
souliane <souliane@mailoo.org>
parents: 1409
diff changeset
465
805
7c05c39156a2 core (XMLUI), frontends: advancedListContainer part 2:
Goffi <goffi@goffi.org>
parents: 804
diff changeset
466
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 4032
diff changeset
467 def is_xmlui_cancelled(raw_xmlui):
2741
1797671827b9 tools (xml_tools): isXMLUICancelled helper function to check if an XMLUI has been cancelled from raw XML.
Goffi <goffi@goffi.org>
parents: 2737
diff changeset
468 """Tell if an XMLUI has been cancelled by checking raw XML"""
4270
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4110
diff changeset
469 return C.bool(raw_xmlui.get("cancelled", C.BOOL_FALSE))
2741
1797671827b9 tools (xml_tools): isXMLUICancelled helper function to check if an XMLUI has been cancelled from raw XML.
Goffi <goffi@goffi.org>
parents: 2737
diff changeset
470
1797671827b9 tools (xml_tools): isXMLUICancelled helper function to check if an XMLUI has been cancelled from raw XML.
Goffi <goffi@goffi.org>
parents: 2737
diff changeset
471
4299
d2deddd6df44 tools (xml_tools): add `namespace` argument to `xmlui_result_to_elt`:
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
472 def xmlui_result_to_elt(xmlui_data: dict, namespace: str | None = None):
1444
8ce9924fa92c tools (xml_tools): better PEP-8 compliance
souliane <souliane@mailoo.org>
parents: 1409
diff changeset
473 """Construct result domish.Element from XMLUI result.
8ce9924fa92c tools (xml_tools): better PEP-8 compliance
souliane <souliane@mailoo.org>
parents: 1409
diff changeset
474
4299
d2deddd6df44 tools (xml_tools): add `namespace` argument to `xmlui_result_to_elt`:
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
475 @param xmlui_data: data returned by frontends for XMLUI form
d2deddd6df44 tools (xml_tools): add `namespace` argument to `xmlui_result_to_elt`:
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
476 @param namespace: namespace to use for the form.
1444
8ce9924fa92c tools (xml_tools): better PEP-8 compliance
souliane <souliane@mailoo.org>
parents: 1409
diff changeset
477 @return: domish.Element
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
478 """
4299
d2deddd6df44 tools (xml_tools): add `namespace` argument to `xmlui_result_to_elt`:
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
479 form = data_form.Form("submit", formNamespace=namespace)
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 4032
diff changeset
480 form.makeFields(xmlui_result_2_data_form_result(xmlui_data))
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
481 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
482
1444
8ce9924fa92c tools (xml_tools): better PEP-8 compliance
souliane <souliane@mailoo.org>
parents: 1409
diff changeset
483
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 4032
diff changeset
484 def tuple_list_2_data_form(values):
1444
8ce9924fa92c tools (xml_tools): better PEP-8 compliance
souliane <souliane@mailoo.org>
parents: 1409
diff changeset
485 """Convert a list of tuples (name, value) to a wokkel submit data form.
8ce9924fa92c tools (xml_tools): better PEP-8 compliance
souliane <souliane@mailoo.org>
parents: 1409
diff changeset
486
1472
c005c212b538 xmlui: minor docstring fixes
Goffi <goffi@goffi.org>
parents: 1456
diff changeset
487 @param values (list): list of tuples
1444
8ce9924fa92c tools (xml_tools): better PEP-8 compliance
souliane <souliane@mailoo.org>
parents: 1409
diff changeset
488 @return: data_form.Form
8ce9924fa92c tools (xml_tools): better PEP-8 compliance
souliane <souliane@mailoo.org>
parents: 1409
diff changeset
489 """
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
490 form = data_form.Form("submit")
102
94011f553cd0 misc bugfixes
Goffi <goffi@goffi.org>
parents: 95
diff changeset
491 for value in values:
94011f553cd0 misc bugfixes
Goffi <goffi@goffi.org>
parents: 95
diff changeset
492 field = data_form.Field(var=value[0], value=value[1])
94011f553cd0 misc bugfixes
Goffi <goffi@goffi.org>
parents: 95
diff changeset
493 form.addField(field)
37
a61beb21d16d Gateway registration, unregistration & edition
Goffi <goffi@goffi.org>
parents: 35
diff changeset
494
102
94011f553cd0 misc bugfixes
Goffi <goffi@goffi.org>
parents: 95
diff changeset
495 return form
103
6be927a465ed XMLUI refactoring, step 1
Goffi <goffi@goffi.org>
parents: 102
diff changeset
496
1444
8ce9924fa92c tools (xml_tools): better PEP-8 compliance
souliane <souliane@mailoo.org>
parents: 1409
diff changeset
497
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 4032
diff changeset
498 def params_xml_2_xmlui(xml):
1444
8ce9924fa92c tools (xml_tools): better PEP-8 compliance
souliane <souliane@mailoo.org>
parents: 1409
diff changeset
499 """Convert the XML for parameter to a SàT XML User Interface.
8ce9924fa92c tools (xml_tools): better PEP-8 compliance
souliane <souliane@mailoo.org>
parents: 1409
diff changeset
500
8ce9924fa92c tools (xml_tools): better PEP-8 compliance
souliane <souliane@mailoo.org>
parents: 1409
diff changeset
501 @param xml (unicode)
8ce9924fa92c tools (xml_tools): better PEP-8 compliance
souliane <souliane@mailoo.org>
parents: 1409
diff changeset
502 @return: XMLUI
8ce9924fa92c tools (xml_tools): better PEP-8 compliance
souliane <souliane@mailoo.org>
parents: 1409
diff changeset
503 """
1476
48706f4ff19c core (xmlui): added JidsListWidget to manage editable list of jids:
Goffi <goffi@goffi.org>
parents: 1475
diff changeset
504 # TODO: refactor params and use Twisted directly to parse XML
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
505 params_doc = minidom.parseString(xml.encode("utf-8"))
105
d2630fba8dfd params to XMLUI tools
Goffi <goffi@goffi.org>
parents: 104
diff changeset
506 top = params_doc.documentElement
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
507 if top.nodeName != "params":
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
508 raise exceptions.DataError(_("INTERNAL ERROR: parameters xml not valid"))
802
9007bb133009 core, frontends: XMLUI refactoring:
Goffi <goffi@goffi.org>
parents: 800
diff changeset
509
105
d2630fba8dfd params to XMLUI tools
Goffi <goffi@goffi.org>
parents: 104
diff changeset
510 param_ui = XMLUI("param", "tabs")
802
9007bb133009 core, frontends: XMLUI refactoring:
Goffi <goffi@goffi.org>
parents: 800
diff changeset
511 tabs_cont = param_ui.current_container
9007bb133009 core, frontends: XMLUI refactoring:
Goffi <goffi@goffi.org>
parents: 800
diff changeset
512
105
d2630fba8dfd params to XMLUI tools
Goffi <goffi@goffi.org>
parents: 104
diff changeset
513 for category in top.getElementsByTagName("category"):
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
514 category_name = category.getAttribute("name")
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
515 label = category.getAttribute("label")
796
46aa5ada61bf core, frontends: XMLUI refactoring:
Goffi <goffi@goffi.org>
parents: 771
diff changeset
516 if not category_name:
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
517 raise exceptions.DataError(
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
518 _("INTERNAL ERROR: params categories must have a name")
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
519 )
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 4032
diff changeset
520 tabs_cont.add_tab(category_name, label=label, container=LabelContainer)
105
d2630fba8dfd params to XMLUI tools
Goffi <goffi@goffi.org>
parents: 104
diff changeset
521 for param in category.getElementsByTagName("param"):
802
9007bb133009 core, frontends: XMLUI refactoring:
Goffi <goffi@goffi.org>
parents: 800
diff changeset
522 widget_kwargs = {}
9007bb133009 core, frontends: XMLUI refactoring:
Goffi <goffi@goffi.org>
parents: 800
diff changeset
523
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
524 param_name = param.getAttribute("name")
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
525 param_label = param.getAttribute("label")
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
526 type_ = param.getAttribute("type")
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
527 if not param_name and type_ != "text":
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
528 raise exceptions.DataError(_("INTERNAL ERROR: params must have a name"))
802
9007bb133009 core, frontends: XMLUI refactoring:
Goffi <goffi@goffi.org>
parents: 800
diff changeset
529
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
530 value = param.getAttribute("value") or None
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
531 callback_id = param.getAttribute("callback_id") or None
802
9007bb133009 core, frontends: XMLUI refactoring:
Goffi <goffi@goffi.org>
parents: 800
diff changeset
532
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
533 if type_ == "list":
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 4032
diff changeset
534 options, selected = _params_get_list_options(param)
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
535 widget_kwargs["options"] = options
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
536 widget_kwargs["selected"] = selected
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
537 widget_kwargs["styles"] = ["extensible"]
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
538 elif type_ == "jids_list":
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 4032
diff changeset
539 widget_kwargs["jids"] = _params_get_list_jids(param)
802
9007bb133009 core, frontends: XMLUI refactoring:
Goffi <goffi@goffi.org>
parents: 800
diff changeset
540
865
3ee2ec7ec010 core (xmlui), frontends: handling of "text" type in params xml + bug fixes
Goffi <goffi@goffi.org>
parents: 863
diff changeset
541 if type_ in ("button", "text"):
106
138d82f64b6f plugin CS: friends parsing
Goffi <goffi@goffi.org>
parents: 105
diff changeset
542 param_ui.addEmpty()
803
f100fd8d279f core, frontends: implementation of AdvancedListContainer first draft + misc:
Goffi <goffi@goffi.org>
parents: 802
diff changeset
543 value = param_label
106
138d82f64b6f plugin CS: friends parsing
Goffi <goffi@goffi.org>
parents: 105
diff changeset
544 else:
802
9007bb133009 core, frontends: XMLUI refactoring:
Goffi <goffi@goffi.org>
parents: 800
diff changeset
545 param_ui.addLabel(param_label or param_name)
9007bb133009 core, frontends: XMLUI refactoring:
Goffi <goffi@goffi.org>
parents: 800
diff changeset
546
9007bb133009 core, frontends: XMLUI refactoring:
Goffi <goffi@goffi.org>
parents: 800
diff changeset
547 if value:
9007bb133009 core, frontends: XMLUI refactoring:
Goffi <goffi@goffi.org>
parents: 800
diff changeset
548 widget_kwargs["value"] = value
9007bb133009 core, frontends: XMLUI refactoring:
Goffi <goffi@goffi.org>
parents: 800
diff changeset
549
9007bb133009 core, frontends: XMLUI refactoring:
Goffi <goffi@goffi.org>
parents: 800
diff changeset
550 if callback_id:
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
551 widget_kwargs["callback_id"] = callback_id
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
552 others = [
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
553 "%s%s%s"
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
554 % (category_name, SAT_PARAM_SEPARATOR, other.getAttribute("name"))
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
555 for other in category.getElementsByTagName("param")
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
556 if other.getAttribute("type") != "button"
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
557 ]
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
558 widget_kwargs["fields_back"] = others
802
9007bb133009 core, frontends: XMLUI refactoring:
Goffi <goffi@goffi.org>
parents: 800
diff changeset
559
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
560 widget_kwargs["name"] = "%s%s%s" % (
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
561 category_name,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
562 SAT_PARAM_SEPARATOR,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
563 param_name,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
564 )
802
9007bb133009 core, frontends: XMLUI refactoring:
Goffi <goffi@goffi.org>
parents: 800
diff changeset
565
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 4032
diff changeset
566 param_ui.add_widget(type_, **widget_kwargs)
105
d2630fba8dfd params to XMLUI tools
Goffi <goffi@goffi.org>
parents: 104
diff changeset
567
d2630fba8dfd params to XMLUI tools
Goffi <goffi@goffi.org>
parents: 104
diff changeset
568 return param_ui.toXml()
d2630fba8dfd params to XMLUI tools
Goffi <goffi@goffi.org>
parents: 104
diff changeset
569
587
952322b1d490 Remove trailing whitespaces.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 572
diff changeset
570
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 4032
diff changeset
571 def _params_get_list_options(param):
1444
8ce9924fa92c tools (xml_tools): better PEP-8 compliance
souliane <souliane@mailoo.org>
parents: 1409
diff changeset
572 """Retrieve the options for list element.
8ce9924fa92c tools (xml_tools): better PEP-8 compliance
souliane <souliane@mailoo.org>
parents: 1409
diff changeset
573
8ce9924fa92c tools (xml_tools): better PEP-8 compliance
souliane <souliane@mailoo.org>
parents: 1409
diff changeset
574 The <option/> tags must be direct children of <param/>.
8ce9924fa92c tools (xml_tools): better PEP-8 compliance
souliane <souliane@mailoo.org>
parents: 1409
diff changeset
575 @param param (domish.Element): element
8ce9924fa92c tools (xml_tools): better PEP-8 compliance
souliane <souliane@mailoo.org>
parents: 1409
diff changeset
576 @return: a tuple (options, selected_value)
8ce9924fa92c tools (xml_tools): better PEP-8 compliance
souliane <souliane@mailoo.org>
parents: 1409
diff changeset
577 """
968
75f3b3b430ff tools, frontends, memory: param definition and XMLUI handle multi-selection for list widgets:
souliane <souliane@mailoo.org>
parents: 910
diff changeset
578 if len(param.getElementsByTagName("options")) > 0:
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
579 raise exceptions.DataError(
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
580 _("The 'options' tag is not allowed in parameter of type 'list'!")
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
581 )
968
75f3b3b430ff tools, frontends, memory: param definition and XMLUI handle multi-selection for list widgets:
souliane <souliane@mailoo.org>
parents: 910
diff changeset
582 elems = param.getElementsByTagName("option")
655
56f8a9c99194 core, primitivus: better support for parameter of type list
souliane <souliane@mailoo.org>
parents: 645
diff changeset
583 if len(elems) == 0:
56f8a9c99194 core, primitivus: better support for parameter of type list
souliane <souliane@mailoo.org>
parents: 645
diff changeset
584 return []
3233
8770397f8f82 tools (xl_tools): fixed handling of list options labels
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
585 options = []
8770397f8f82 tools (xl_tools): fixed handling of list options labels
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
586 for elem in elems:
8770397f8f82 tools (xl_tools): fixed handling of list options labels
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
587 value = elem.getAttribute("value")
8770397f8f82 tools (xl_tools): fixed handling of list options labels
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
588 if not value:
8770397f8f82 tools (xl_tools): fixed handling of list options labels
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
589 raise exceptions.InternalError("list option must have a value")
8770397f8f82 tools (xl_tools): fixed handling of list options labels
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
590 label = elem.getAttribute("label")
8770397f8f82 tools (xl_tools): fixed handling of list options labels
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
591 if label:
8770397f8f82 tools (xl_tools): fixed handling of list options labels
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
592 options.append((value, label))
8770397f8f82 tools (xl_tools): fixed handling of list options labels
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
593 else:
8770397f8f82 tools (xl_tools): fixed handling of list options labels
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
594 options.append(value)
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
595 selected = [
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
596 elem.getAttribute("value")
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
597 for elem in elems
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
598 if elem.getAttribute("selected") == "true"
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
599 ]
1444
8ce9924fa92c tools (xml_tools): better PEP-8 compliance
souliane <souliane@mailoo.org>
parents: 1409
diff changeset
600 return (options, selected)
655
56f8a9c99194 core, primitivus: better support for parameter of type list
souliane <souliane@mailoo.org>
parents: 645
diff changeset
601
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
602
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 4032
diff changeset
603 def _params_get_list_jids(param):
1476
48706f4ff19c core (xmlui): added JidsListWidget to manage editable list of jids:
Goffi <goffi@goffi.org>
parents: 1475
diff changeset
604 """Retrive jids from a jids_list element.
655
56f8a9c99194 core, primitivus: better support for parameter of type list
souliane <souliane@mailoo.org>
parents: 645
diff changeset
605
1476
48706f4ff19c core (xmlui): added JidsListWidget to manage editable list of jids:
Goffi <goffi@goffi.org>
parents: 1475
diff changeset
606 the <jid/> tags must be direct children of <param/>
48706f4ff19c core (xmlui): added JidsListWidget to manage editable list of jids:
Goffi <goffi@goffi.org>
parents: 1475
diff changeset
607 @param param (domish.Element): element
48706f4ff19c core (xmlui): added JidsListWidget to manage editable list of jids:
Goffi <goffi@goffi.org>
parents: 1475
diff changeset
608 @return: a list of jids
48706f4ff19c core (xmlui): added JidsListWidget to manage editable list of jids:
Goffi <goffi@goffi.org>
parents: 1475
diff changeset
609 """
48706f4ff19c core (xmlui): added JidsListWidget to manage editable list of jids:
Goffi <goffi@goffi.org>
parents: 1475
diff changeset
610 elems = param.getElementsByTagName("jid")
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
611 jids = [
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
612 elem.firstChild.data
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
613 for elem in elems
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
614 if elem.firstChild is not None and elem.firstChild.nodeType == elem.TEXT_NODE
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
615 ]
1476
48706f4ff19c core (xmlui): added JidsListWidget to manage editable list of jids:
Goffi <goffi@goffi.org>
parents: 1475
diff changeset
616 return jids
48706f4ff19c core (xmlui): added JidsListWidget to manage editable list of jids:
Goffi <goffi@goffi.org>
parents: 1475
diff changeset
617
48706f4ff19c core (xmlui): added JidsListWidget to manage editable list of jids:
Goffi <goffi@goffi.org>
parents: 1475
diff changeset
618
48706f4ff19c core (xmlui): added JidsListWidget to manage editable list of jids:
Goffi <goffi@goffi.org>
parents: 1475
diff changeset
619 ### XMLUI Elements ###
802
9007bb133009 core, frontends: XMLUI refactoring:
Goffi <goffi@goffi.org>
parents: 800
diff changeset
620
9007bb133009 core, frontends: XMLUI refactoring:
Goffi <goffi@goffi.org>
parents: 800
diff changeset
621
9007bb133009 core, frontends: XMLUI refactoring:
Goffi <goffi@goffi.org>
parents: 800
diff changeset
622 class Element(object):
4270
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4110
diff changeset
623 """Base XMLUI element"""
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
624
802
9007bb133009 core, frontends: XMLUI refactoring:
Goffi <goffi@goffi.org>
parents: 800
diff changeset
625 type = None
9007bb133009 core, frontends: XMLUI refactoring:
Goffi <goffi@goffi.org>
parents: 800
diff changeset
626
9007bb133009 core, frontends: XMLUI refactoring:
Goffi <goffi@goffi.org>
parents: 800
diff changeset
627 def __init__(self, xmlui, parent=None):
9007bb133009 core, frontends: XMLUI refactoring:
Goffi <goffi@goffi.org>
parents: 800
diff changeset
628 """Create a container element
1444
8ce9924fa92c tools (xml_tools): better PEP-8 compliance
souliane <souliane@mailoo.org>
parents: 1409
diff changeset
629
802
9007bb133009 core, frontends: XMLUI refactoring:
Goffi <goffi@goffi.org>
parents: 800
diff changeset
630 @param xmlui: XMLUI instance
9007bb133009 core, frontends: XMLUI refactoring:
Goffi <goffi@goffi.org>
parents: 800
diff changeset
631 @parent: parent element
9007bb133009 core, frontends: XMLUI refactoring:
Goffi <goffi@goffi.org>
parents: 800
diff changeset
632 """
1475
7ac073d2e7e0 core(xmlui): removed useless parenthesis around assert
Goffi <goffi@goffi.org>
parents: 1473
diff changeset
633 assert self.type is not None
2365
318f0434d830 core (xmlui): container now keep a list of children
Goffi <goffi@goffi.org>
parents: 2364
diff changeset
634 self.children = []
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
635 if not hasattr(self, "elem"):
802
9007bb133009 core, frontends: XMLUI refactoring:
Goffi <goffi@goffi.org>
parents: 800
diff changeset
636 self.elem = parent.xmlui.doc.createElement(self.type)
9007bb133009 core, frontends: XMLUI refactoring:
Goffi <goffi@goffi.org>
parents: 800
diff changeset
637 self.xmlui = xmlui
9007bb133009 core, frontends: XMLUI refactoring:
Goffi <goffi@goffi.org>
parents: 800
diff changeset
638 if parent is not None:
9007bb133009 core, frontends: XMLUI refactoring:
Goffi <goffi@goffi.org>
parents: 800
diff changeset
639 parent.append(self)
803
f100fd8d279f core, frontends: implementation of AdvancedListContainer first draft + misc:
Goffi <goffi@goffi.org>
parents: 802
diff changeset
640 self.parent = parent
802
9007bb133009 core, frontends: XMLUI refactoring:
Goffi <goffi@goffi.org>
parents: 800
diff changeset
641
9007bb133009 core, frontends: XMLUI refactoring:
Goffi <goffi@goffi.org>
parents: 800
diff changeset
642 def append(self, child):
1487
0df627d0b4ca tools (xmlui): changeContainer do not create a new one when the current one already fits
souliane <souliane@mailoo.org>
parents: 1486
diff changeset
643 """Append a child to this element.
1490
55cff13b1f10 core (xmlui): reverted change from 039d96e131be: we should be able to recreate the same container with changeContainer
Goffi <goffi@goffi.org>
parents: 1487
diff changeset
644
1487
0df627d0b4ca tools (xmlui): changeContainer do not create a new one when the current one already fits
souliane <souliane@mailoo.org>
parents: 1486
diff changeset
645 @param child (Element): child element
0df627d0b4ca tools (xmlui): changeContainer do not create a new one when the current one already fits
souliane <souliane@mailoo.org>
parents: 1486
diff changeset
646 @return: the added child Element
0df627d0b4ca tools (xmlui): changeContainer do not create a new one when the current one already fits
souliane <souliane@mailoo.org>
parents: 1486
diff changeset
647 """
802
9007bb133009 core, frontends: XMLUI refactoring:
Goffi <goffi@goffi.org>
parents: 800
diff changeset
648 self.elem.appendChild(child.elem)
9007bb133009 core, frontends: XMLUI refactoring:
Goffi <goffi@goffi.org>
parents: 800
diff changeset
649 child.parent = self
2365
318f0434d830 core (xmlui): container now keep a list of children
Goffi <goffi@goffi.org>
parents: 2364
diff changeset
650 self.children.append(child)
1487
0df627d0b4ca tools (xmlui): changeContainer do not create a new one when the current one already fits
souliane <souliane@mailoo.org>
parents: 1486
diff changeset
651 return child
802
9007bb133009 core, frontends: XMLUI refactoring:
Goffi <goffi@goffi.org>
parents: 800
diff changeset
652
9007bb133009 core, frontends: XMLUI refactoring:
Goffi <goffi@goffi.org>
parents: 800
diff changeset
653
9007bb133009 core, frontends: XMLUI refactoring:
Goffi <goffi@goffi.org>
parents: 800
diff changeset
654 class TopElement(Element):
4270
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4110
diff changeset
655 """Main XML Element"""
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
656
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
657 type = "top"
802
9007bb133009 core, frontends: XMLUI refactoring:
Goffi <goffi@goffi.org>
parents: 800
diff changeset
658
9007bb133009 core, frontends: XMLUI refactoring:
Goffi <goffi@goffi.org>
parents: 800
diff changeset
659 def __init__(self, xmlui):
9007bb133009 core, frontends: XMLUI refactoring:
Goffi <goffi@goffi.org>
parents: 800
diff changeset
660 self.elem = xmlui.doc.documentElement
9007bb133009 core, frontends: XMLUI refactoring:
Goffi <goffi@goffi.org>
parents: 800
diff changeset
661 super(TopElement, self).__init__(xmlui)
9007bb133009 core, frontends: XMLUI refactoring:
Goffi <goffi@goffi.org>
parents: 800
diff changeset
662
9007bb133009 core, frontends: XMLUI refactoring:
Goffi <goffi@goffi.org>
parents: 800
diff changeset
663
9007bb133009 core, frontends: XMLUI refactoring:
Goffi <goffi@goffi.org>
parents: 800
diff changeset
664 class TabElement(Element):
4270
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4110
diff changeset
665 """Used by TabsContainer to give name and label to tabs."""
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
666
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
667 type = "tab"
630
0b914394e74f core: added advanced list to XMLUI (need improvment, very basic so far)
Goffi <goffi@goffi.org>
parents: 615
diff changeset
668
1486
a77217511afd tools, frontends (xmlui): allow to select a tab when adding it
souliane <souliane@mailoo.org>
parents: 1476
diff changeset
669 def __init__(self, parent, name, label, selected=False):
a77217511afd tools, frontends (xmlui): allow to select a tab when adding it
souliane <souliane@mailoo.org>
parents: 1476
diff changeset
670 """
a77217511afd tools, frontends (xmlui): allow to select a tab when adding it
souliane <souliane@mailoo.org>
parents: 1476
diff changeset
671
a77217511afd tools, frontends (xmlui): allow to select a tab when adding it
souliane <souliane@mailoo.org>
parents: 1476
diff changeset
672 @param parent (TabsContainer): parent container
a77217511afd tools, frontends (xmlui): allow to select a tab when adding it
souliane <souliane@mailoo.org>
parents: 1476
diff changeset
673 @param name (unicode): tab name
a77217511afd tools, frontends (xmlui): allow to select a tab when adding it
souliane <souliane@mailoo.org>
parents: 1476
diff changeset
674 @param label (unicode): tab label
a77217511afd tools, frontends (xmlui): allow to select a tab when adding it
souliane <souliane@mailoo.org>
parents: 1476
diff changeset
675 @param selected (bool): set to True to select this tab
a77217511afd tools, frontends (xmlui): allow to select a tab when adding it
souliane <souliane@mailoo.org>
parents: 1476
diff changeset
676 """
802
9007bb133009 core, frontends: XMLUI refactoring:
Goffi <goffi@goffi.org>
parents: 800
diff changeset
677 if not isinstance(parent, TabsContainer):
9007bb133009 core, frontends: XMLUI refactoring:
Goffi <goffi@goffi.org>
parents: 800
diff changeset
678 raise exceptions.DataError(_("TabElement must be a child of TabsContainer"))
9007bb133009 core, frontends: XMLUI refactoring:
Goffi <goffi@goffi.org>
parents: 800
diff changeset
679 super(TabElement, self).__init__(parent.xmlui, parent)
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
680 self.elem.setAttribute("name", name)
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
681 self.elem.setAttribute("label", label)
1486
a77217511afd tools, frontends (xmlui): allow to select a tab when adding it
souliane <souliane@mailoo.org>
parents: 1476
diff changeset
682 if selected:
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 4032
diff changeset
683 self.set_selected(selected)
1486
a77217511afd tools, frontends (xmlui): allow to select a tab when adding it
souliane <souliane@mailoo.org>
parents: 1476
diff changeset
684
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 4032
diff changeset
685 def set_selected(self, selected=False):
1486
a77217511afd tools, frontends (xmlui): allow to select a tab when adding it
souliane <souliane@mailoo.org>
parents: 1476
diff changeset
686 """Set the tab selected.
a77217511afd tools, frontends (xmlui): allow to select a tab when adding it
souliane <souliane@mailoo.org>
parents: 1476
diff changeset
687
a77217511afd tools, frontends (xmlui): allow to select a tab when adding it
souliane <souliane@mailoo.org>
parents: 1476
diff changeset
688 @param selected (bool): set to True to select this tab
a77217511afd tools, frontends (xmlui): allow to select a tab when adding it
souliane <souliane@mailoo.org>
parents: 1476
diff changeset
689 """
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
690 self.elem.setAttribute("selected", "true" if selected else "false")
802
9007bb133009 core, frontends: XMLUI refactoring:
Goffi <goffi@goffi.org>
parents: 800
diff changeset
691
9007bb133009 core, frontends: XMLUI refactoring:
Goffi <goffi@goffi.org>
parents: 800
diff changeset
692
9007bb133009 core, frontends: XMLUI refactoring:
Goffi <goffi@goffi.org>
parents: 800
diff changeset
693 class FieldBackElement(Element):
4270
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4110
diff changeset
694 """Used by ButtonWidget to indicate which field have to be sent back"""
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
695
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
696 type = "field_back"
802
9007bb133009 core, frontends: XMLUI refactoring:
Goffi <goffi@goffi.org>
parents: 800
diff changeset
697
9007bb133009 core, frontends: XMLUI refactoring:
Goffi <goffi@goffi.org>
parents: 800
diff changeset
698 def __init__(self, parent, name):
1475
7ac073d2e7e0 core(xmlui): removed useless parenthesis around assert
Goffi <goffi@goffi.org>
parents: 1473
diff changeset
699 assert isinstance(parent, ButtonWidget)
802
9007bb133009 core, frontends: XMLUI refactoring:
Goffi <goffi@goffi.org>
parents: 800
diff changeset
700 super(FieldBackElement, self).__init__(parent.xmlui, parent)
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
701 self.elem.setAttribute("name", name)
802
9007bb133009 core, frontends: XMLUI refactoring:
Goffi <goffi@goffi.org>
parents: 800
diff changeset
702
9007bb133009 core, frontends: XMLUI refactoring:
Goffi <goffi@goffi.org>
parents: 800
diff changeset
703
977
d2e612a45e76 tools, frontends (xmlui): add Widget.setInternalCallback:
souliane <souliane@mailoo.org>
parents: 975
diff changeset
704 class InternalFieldElement(Element):
4270
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4110
diff changeset
705 """Used by internal callbacks to indicate which fields are manipulated"""
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
706
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
707 type = "internal_field"
977
d2e612a45e76 tools, frontends (xmlui): add Widget.setInternalCallback:
souliane <souliane@mailoo.org>
parents: 975
diff changeset
708
d2e612a45e76 tools, frontends (xmlui): add Widget.setInternalCallback:
souliane <souliane@mailoo.org>
parents: 975
diff changeset
709 def __init__(self, parent, name):
d2e612a45e76 tools, frontends (xmlui): add Widget.setInternalCallback:
souliane <souliane@mailoo.org>
parents: 975
diff changeset
710 super(InternalFieldElement, self).__init__(parent.xmlui, parent)
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
711 self.elem.setAttribute("name", name)
977
d2e612a45e76 tools, frontends (xmlui): add Widget.setInternalCallback:
souliane <souliane@mailoo.org>
parents: 975
diff changeset
712
d2e612a45e76 tools, frontends (xmlui): add Widget.setInternalCallback:
souliane <souliane@mailoo.org>
parents: 975
diff changeset
713
d2e612a45e76 tools, frontends (xmlui): add Widget.setInternalCallback:
souliane <souliane@mailoo.org>
parents: 975
diff changeset
714 class InternalDataElement(Element):
4270
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4110
diff changeset
715 """Used by internal callbacks to retrieve extra data"""
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
716
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
717 type = "internal_data"
977
d2e612a45e76 tools, frontends (xmlui): add Widget.setInternalCallback:
souliane <souliane@mailoo.org>
parents: 975
diff changeset
718
d2e612a45e76 tools, frontends (xmlui): add Widget.setInternalCallback:
souliane <souliane@mailoo.org>
parents: 975
diff changeset
719 def __init__(self, parent, children):
d2e612a45e76 tools, frontends (xmlui): add Widget.setInternalCallback:
souliane <souliane@mailoo.org>
parents: 975
diff changeset
720 super(InternalDataElement, self).__init__(parent.xmlui, parent)
1475
7ac073d2e7e0 core(xmlui): removed useless parenthesis around assert
Goffi <goffi@goffi.org>
parents: 1473
diff changeset
721 assert isinstance(children, list)
977
d2e612a45e76 tools, frontends (xmlui): add Widget.setInternalCallback:
souliane <souliane@mailoo.org>
parents: 975
diff changeset
722 for child in children:
d2e612a45e76 tools, frontends (xmlui): add Widget.setInternalCallback:
souliane <souliane@mailoo.org>
parents: 975
diff changeset
723 self.elem.childNodes.append(child)
d2e612a45e76 tools, frontends (xmlui): add Widget.setInternalCallback:
souliane <souliane@mailoo.org>
parents: 975
diff changeset
724
d2e612a45e76 tools, frontends (xmlui): add Widget.setInternalCallback:
souliane <souliane@mailoo.org>
parents: 975
diff changeset
725
802
9007bb133009 core, frontends: XMLUI refactoring:
Goffi <goffi@goffi.org>
parents: 800
diff changeset
726 class OptionElement(Element):
4270
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4110
diff changeset
727 """ " Used by ListWidget to specify options"""
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
728
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
729 type = "option"
802
9007bb133009 core, frontends: XMLUI refactoring:
Goffi <goffi@goffi.org>
parents: 800
diff changeset
730
968
75f3b3b430ff tools, frontends, memory: param definition and XMLUI handle multi-selection for list widgets:
souliane <souliane@mailoo.org>
parents: 910
diff changeset
731 def __init__(self, parent, option, selected=False):
75f3b3b430ff tools, frontends, memory: param definition and XMLUI handle multi-selection for list widgets:
souliane <souliane@mailoo.org>
parents: 910
diff changeset
732 """
75f3b3b430ff tools, frontends, memory: param definition and XMLUI handle multi-selection for list widgets:
souliane <souliane@mailoo.org>
parents: 910
diff changeset
733
75f3b3b430ff tools, frontends, memory: param definition and XMLUI handle multi-selection for list widgets:
souliane <souliane@mailoo.org>
parents: 910
diff changeset
734 @param parent
75f3b3b430ff tools, frontends, memory: param definition and XMLUI handle multi-selection for list widgets:
souliane <souliane@mailoo.org>
parents: 910
diff changeset
735 @param option (string, tuple)
75f3b3b430ff tools, frontends, memory: param definition and XMLUI handle multi-selection for list widgets:
souliane <souliane@mailoo.org>
parents: 910
diff changeset
736 @param selected (boolean)
75f3b3b430ff tools, frontends, memory: param definition and XMLUI handle multi-selection for list widgets:
souliane <souliane@mailoo.org>
parents: 910
diff changeset
737 """
1475
7ac073d2e7e0 core(xmlui): removed useless parenthesis around assert
Goffi <goffi@goffi.org>
parents: 1473
diff changeset
738 assert isinstance(parent, ListWidget)
802
9007bb133009 core, frontends: XMLUI refactoring:
Goffi <goffi@goffi.org>
parents: 800
diff changeset
739 super(OptionElement, self).__init__(parent.xmlui, parent)
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2958
diff changeset
740 if isinstance(option, str):
802
9007bb133009 core, frontends: XMLUI refactoring:
Goffi <goffi@goffi.org>
parents: 800
diff changeset
741 value, label = option, option
9007bb133009 core, frontends: XMLUI refactoring:
Goffi <goffi@goffi.org>
parents: 800
diff changeset
742 elif isinstance(option, tuple):
9007bb133009 core, frontends: XMLUI refactoring:
Goffi <goffi@goffi.org>
parents: 800
diff changeset
743 value, label = option
1067
f7f15d44fdfa tools (xml_tools): dataForm2XMLUI handles the default value for "list-single" fields
souliane <souliane@mailoo.org>
parents: 1036
diff changeset
744 else:
f7f15d44fdfa tools (xml_tools): dataForm2XMLUI handles the default value for "list-single" fields
souliane <souliane@mailoo.org>
parents: 1036
diff changeset
745 raise NotImplementedError
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
746 self.elem.setAttribute("value", value)
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
747 self.elem.setAttribute("label", label)
968
75f3b3b430ff tools, frontends, memory: param definition and XMLUI handle multi-selection for list widgets:
souliane <souliane@mailoo.org>
parents: 910
diff changeset
748 if selected:
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
749 self.elem.setAttribute("selected", "true")
802
9007bb133009 core, frontends: XMLUI refactoring:
Goffi <goffi@goffi.org>
parents: 800
diff changeset
750
9007bb133009 core, frontends: XMLUI refactoring:
Goffi <goffi@goffi.org>
parents: 800
diff changeset
751
1476
48706f4ff19c core (xmlui): added JidsListWidget to manage editable list of jids:
Goffi <goffi@goffi.org>
parents: 1475
diff changeset
752 class JidElement(Element):
4270
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4110
diff changeset
753 """ " Used by JidsListWidget to specify jids"""
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
754
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
755 type = "jid"
1476
48706f4ff19c core (xmlui): added JidsListWidget to manage editable list of jids:
Goffi <goffi@goffi.org>
parents: 1475
diff changeset
756
48706f4ff19c core (xmlui): added JidsListWidget to manage editable list of jids:
Goffi <goffi@goffi.org>
parents: 1475
diff changeset
757 def __init__(self, parent, jid_):
48706f4ff19c core (xmlui): added JidsListWidget to manage editable list of jids:
Goffi <goffi@goffi.org>
parents: 1475
diff changeset
758 """
48706f4ff19c core (xmlui): added JidsListWidget to manage editable list of jids:
Goffi <goffi@goffi.org>
parents: 1475
diff changeset
759 @param jid_(jid.JID, unicode): jid to append
48706f4ff19c core (xmlui): added JidsListWidget to manage editable list of jids:
Goffi <goffi@goffi.org>
parents: 1475
diff changeset
760 """
48706f4ff19c core (xmlui): added JidsListWidget to manage editable list of jids:
Goffi <goffi@goffi.org>
parents: 1475
diff changeset
761 assert isinstance(parent, JidsListWidget)
48706f4ff19c core (xmlui): added JidsListWidget to manage editable list of jids:
Goffi <goffi@goffi.org>
parents: 1475
diff changeset
762 super(JidElement, self).__init__(parent.xmlui, parent)
48706f4ff19c core (xmlui): added JidsListWidget to manage editable list of jids:
Goffi <goffi@goffi.org>
parents: 1475
diff changeset
763 if isinstance(jid_, jid.JID):
1499
adc72c39f032 tools (xml_tools): fixes JidElement.__init__
souliane <souliane@mailoo.org>
parents: 1490
diff changeset
764 value = jid_.full()
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2958
diff changeset
765 elif isinstance(jid_, str):
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2958
diff changeset
766 value = str(jid_)
1476
48706f4ff19c core (xmlui): added JidsListWidget to manage editable list of jids:
Goffi <goffi@goffi.org>
parents: 1475
diff changeset
767 else:
48706f4ff19c core (xmlui): added JidsListWidget to manage editable list of jids:
Goffi <goffi@goffi.org>
parents: 1475
diff changeset
768 raise NotImplementedError
48706f4ff19c core (xmlui): added JidsListWidget to manage editable list of jids:
Goffi <goffi@goffi.org>
parents: 1475
diff changeset
769 jid_txt = self.xmlui.doc.createTextNode(value)
48706f4ff19c core (xmlui): added JidsListWidget to manage editable list of jids:
Goffi <goffi@goffi.org>
parents: 1475
diff changeset
770 self.elem.appendChild(jid_txt)
48706f4ff19c core (xmlui): added JidsListWidget to manage editable list of jids:
Goffi <goffi@goffi.org>
parents: 1475
diff changeset
771
48706f4ff19c core (xmlui): added JidsListWidget to manage editable list of jids:
Goffi <goffi@goffi.org>
parents: 1475
diff changeset
772
802
9007bb133009 core, frontends: XMLUI refactoring:
Goffi <goffi@goffi.org>
parents: 800
diff changeset
773 class RowElement(Element):
4270
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4110
diff changeset
774 """ " Used by AdvancedListContainer"""
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
775
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
776 type = "row"
802
9007bb133009 core, frontends: XMLUI refactoring:
Goffi <goffi@goffi.org>
parents: 800
diff changeset
777
9007bb133009 core, frontends: XMLUI refactoring:
Goffi <goffi@goffi.org>
parents: 800
diff changeset
778 def __init__(self, parent):
1475
7ac073d2e7e0 core(xmlui): removed useless parenthesis around assert
Goffi <goffi@goffi.org>
parents: 1473
diff changeset
779 assert isinstance(parent, AdvancedListContainer)
802
9007bb133009 core, frontends: XMLUI refactoring:
Goffi <goffi@goffi.org>
parents: 800
diff changeset
780 super(RowElement, self).__init__(parent.xmlui, parent)
805
7c05c39156a2 core (XMLUI), frontends: advancedListContainer part 2:
Goffi <goffi@goffi.org>
parents: 804
diff changeset
781 if parent.next_row_idx is not None:
7c05c39156a2 core (XMLUI), frontends: advancedListContainer part 2:
Goffi <goffi@goffi.org>
parents: 804
diff changeset
782 if parent.auto_index:
7c05c39156a2 core (XMLUI), frontends: advancedListContainer part 2:
Goffi <goffi@goffi.org>
parents: 804
diff changeset
783 raise exceptions.DataError(_("Can't set row index if auto_index is True"))
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
784 self.elem.setAttribute("index", parent.next_row_idx)
805
7c05c39156a2 core (XMLUI), frontends: advancedListContainer part 2:
Goffi <goffi@goffi.org>
parents: 804
diff changeset
785 parent.next_row_idx = None
802
9007bb133009 core, frontends: XMLUI refactoring:
Goffi <goffi@goffi.org>
parents: 800
diff changeset
786
9007bb133009 core, frontends: XMLUI refactoring:
Goffi <goffi@goffi.org>
parents: 800
diff changeset
787
9007bb133009 core, frontends: XMLUI refactoring:
Goffi <goffi@goffi.org>
parents: 800
diff changeset
788 class HeaderElement(Element):
4270
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4110
diff changeset
789 """ " Used by AdvancedListContainer"""
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
790
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
791 type = "header"
802
9007bb133009 core, frontends: XMLUI refactoring:
Goffi <goffi@goffi.org>
parents: 800
diff changeset
792
876
65bf1bc70f6b tools, plugin XEP-0077: small fixes
souliane <souliane@mailoo.org>
parents: 865
diff changeset
793 def __init__(self, parent, name=None, label=None, description=None):
630
0b914394e74f core: added advanced list to XMLUI (need improvment, very basic so far)
Goffi <goffi@goffi.org>
parents: 615
diff changeset
794 """
802
9007bb133009 core, frontends: XMLUI refactoring:
Goffi <goffi@goffi.org>
parents: 800
diff changeset
795 @param parent: AdvancedListContainer instance
9007bb133009 core, frontends: XMLUI refactoring:
Goffi <goffi@goffi.org>
parents: 800
diff changeset
796 @param name: name of the container
630
0b914394e74f core: added advanced list to XMLUI (need improvment, very basic so far)
Goffi <goffi@goffi.org>
parents: 615
diff changeset
797 @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
798 @param description: long descriptive text
802
9007bb133009 core, frontends: XMLUI refactoring:
Goffi <goffi@goffi.org>
parents: 800
diff changeset
799 """
1475
7ac073d2e7e0 core(xmlui): removed useless parenthesis around assert
Goffi <goffi@goffi.org>
parents: 1473
diff changeset
800 assert isinstance(parent, AdvancedListContainer)
802
9007bb133009 core, frontends: XMLUI refactoring:
Goffi <goffi@goffi.org>
parents: 800
diff changeset
801 super(HeaderElement, self).__init__(parent.xmlui, parent)
9007bb133009 core, frontends: XMLUI refactoring:
Goffi <goffi@goffi.org>
parents: 800
diff changeset
802 if name:
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
803 self.elem.setAttribute("name", name)
802
9007bb133009 core, frontends: XMLUI refactoring:
Goffi <goffi@goffi.org>
parents: 800
diff changeset
804 if label:
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
805 self.elem.setAttribute("label", label)
802
9007bb133009 core, frontends: XMLUI refactoring:
Goffi <goffi@goffi.org>
parents: 800
diff changeset
806 if description:
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
807 self.elem.setAttribute("description", description)
802
9007bb133009 core, frontends: XMLUI refactoring:
Goffi <goffi@goffi.org>
parents: 800
diff changeset
808
9007bb133009 core, frontends: XMLUI refactoring:
Goffi <goffi@goffi.org>
parents: 800
diff changeset
809
1476
48706f4ff19c core (xmlui): added JidsListWidget to manage editable list of jids:
Goffi <goffi@goffi.org>
parents: 1475
diff changeset
810 ## Containers ##
48706f4ff19c core (xmlui): added JidsListWidget to manage editable list of jids:
Goffi <goffi@goffi.org>
parents: 1475
diff changeset
811
48706f4ff19c core (xmlui): added JidsListWidget to manage editable list of jids:
Goffi <goffi@goffi.org>
parents: 1475
diff changeset
812
802
9007bb133009 core, frontends: XMLUI refactoring:
Goffi <goffi@goffi.org>
parents: 800
diff changeset
813 class Container(Element):
4270
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4110
diff changeset
814 """And Element which contains other ones and has a layout"""
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
815
802
9007bb133009 core, frontends: XMLUI refactoring:
Goffi <goffi@goffi.org>
parents: 800
diff changeset
816 type = None
9007bb133009 core, frontends: XMLUI refactoring:
Goffi <goffi@goffi.org>
parents: 800
diff changeset
817
9007bb133009 core, frontends: XMLUI refactoring:
Goffi <goffi@goffi.org>
parents: 800
diff changeset
818 def __init__(self, xmlui, parent=None):
9007bb133009 core, frontends: XMLUI refactoring:
Goffi <goffi@goffi.org>
parents: 800
diff changeset
819 """Create a container element
1472
c005c212b538 xmlui: minor docstring fixes
Goffi <goffi@goffi.org>
parents: 1456
diff changeset
820
802
9007bb133009 core, frontends: XMLUI refactoring:
Goffi <goffi@goffi.org>
parents: 800
diff changeset
821 @param xmlui: XMLUI instance
9007bb133009 core, frontends: XMLUI refactoring:
Goffi <goffi@goffi.org>
parents: 800
diff changeset
822 @parent: parent element or None
9007bb133009 core, frontends: XMLUI refactoring:
Goffi <goffi@goffi.org>
parents: 800
diff changeset
823 """
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
824 self.elem = xmlui.doc.createElement("container")
802
9007bb133009 core, frontends: XMLUI refactoring:
Goffi <goffi@goffi.org>
parents: 800
diff changeset
825 super(Container, self).__init__(xmlui, parent)
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
826 self.elem.setAttribute("type", self.type)
802
9007bb133009 core, frontends: XMLUI refactoring:
Goffi <goffi@goffi.org>
parents: 800
diff changeset
827
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 4032
diff changeset
828 def get_parent_container(self):
4270
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4110
diff changeset
829 """Return first parent container
1472
c005c212b538 xmlui: minor docstring fixes
Goffi <goffi@goffi.org>
parents: 1456
diff changeset
830
802
9007bb133009 core, frontends: XMLUI refactoring:
Goffi <goffi@goffi.org>
parents: 800
diff changeset
831 @return: parent container or None
630
0b914394e74f core: added advanced list to XMLUI (need improvment, very basic so far)
Goffi <goffi@goffi.org>
parents: 615
diff changeset
832 """
802
9007bb133009 core, frontends: XMLUI refactoring:
Goffi <goffi@goffi.org>
parents: 800
diff changeset
833 current = self.parent
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
834 while not isinstance(current, (Container)) and current is not None:
802
9007bb133009 core, frontends: XMLUI refactoring:
Goffi <goffi@goffi.org>
parents: 800
diff changeset
835 current = current.parent
9007bb133009 core, frontends: XMLUI refactoring:
Goffi <goffi@goffi.org>
parents: 800
diff changeset
836 return current
9007bb133009 core, frontends: XMLUI refactoring:
Goffi <goffi@goffi.org>
parents: 800
diff changeset
837
1444
8ce9924fa92c tools (xml_tools): better PEP-8 compliance
souliane <souliane@mailoo.org>
parents: 1409
diff changeset
838
802
9007bb133009 core, frontends: XMLUI refactoring:
Goffi <goffi@goffi.org>
parents: 800
diff changeset
839 class VerticalContainer(Container):
9007bb133009 core, frontends: XMLUI refactoring:
Goffi <goffi@goffi.org>
parents: 800
diff changeset
840 type = "vertical"
9007bb133009 core, frontends: XMLUI refactoring:
Goffi <goffi@goffi.org>
parents: 800
diff changeset
841
9007bb133009 core, frontends: XMLUI refactoring:
Goffi <goffi@goffi.org>
parents: 800
diff changeset
842
9007bb133009 core, frontends: XMLUI refactoring:
Goffi <goffi@goffi.org>
parents: 800
diff changeset
843 class HorizontalContainer(Container):
9007bb133009 core, frontends: XMLUI refactoring:
Goffi <goffi@goffi.org>
parents: 800
diff changeset
844 type = "horizontal"
9007bb133009 core, frontends: XMLUI refactoring:
Goffi <goffi@goffi.org>
parents: 800
diff changeset
845
9007bb133009 core, frontends: XMLUI refactoring:
Goffi <goffi@goffi.org>
parents: 800
diff changeset
846
9007bb133009 core, frontends: XMLUI refactoring:
Goffi <goffi@goffi.org>
parents: 800
diff changeset
847 class PairsContainer(Container):
2737
5c2ed8a5ae22 frontends (XMLUI): TabsContainer can now be the main container (Primitivus keep the old behaviour and always use VerticalContainer as main container)
Goffi <goffi@goffi.org>
parents: 2668
diff changeset
848 """Container with series of 2 elements"""
4270
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4110
diff changeset
849
802
9007bb133009 core, frontends: XMLUI refactoring:
Goffi <goffi@goffi.org>
parents: 800
diff changeset
850 type = "pairs"
9007bb133009 core, frontends: XMLUI refactoring:
Goffi <goffi@goffi.org>
parents: 800
diff changeset
851
9007bb133009 core, frontends: XMLUI refactoring:
Goffi <goffi@goffi.org>
parents: 800
diff changeset
852
2361
5defafc8ede6 core, frontends (xmlui): new LabelContainer:
Goffi <goffi@goffi.org>
parents: 2108
diff changeset
853 class LabelContainer(Container):
2737
5c2ed8a5ae22 frontends (XMLUI): TabsContainer can now be the main container (Primitivus keep the old behaviour and always use VerticalContainer as main container)
Goffi <goffi@goffi.org>
parents: 2668
diff changeset
854 """Like PairsContainer, but first element can only be a label"""
4270
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4110
diff changeset
855
2361
5defafc8ede6 core, frontends (xmlui): new LabelContainer:
Goffi <goffi@goffi.org>
parents: 2108
diff changeset
856 type = "label"
5defafc8ede6 core, frontends (xmlui): new LabelContainer:
Goffi <goffi@goffi.org>
parents: 2108
diff changeset
857
5defafc8ede6 core, frontends (xmlui): new LabelContainer:
Goffi <goffi@goffi.org>
parents: 2108
diff changeset
858
802
9007bb133009 core, frontends: XMLUI refactoring:
Goffi <goffi@goffi.org>
parents: 800
diff changeset
859 class TabsContainer(Container):
9007bb133009 core, frontends: XMLUI refactoring:
Goffi <goffi@goffi.org>
parents: 800
diff changeset
860 type = "tabs"
9007bb133009 core, frontends: XMLUI refactoring:
Goffi <goffi@goffi.org>
parents: 800
diff changeset
861
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 4032
diff changeset
862 def add_tab(self, name, label=None, selected=None, container=VerticalContainer):
1486
a77217511afd tools, frontends (xmlui): allow to select a tab when adding it
souliane <souliane@mailoo.org>
parents: 1476
diff changeset
863 """Add a tab.
a77217511afd tools, frontends (xmlui): allow to select a tab when adding it
souliane <souliane@mailoo.org>
parents: 1476
diff changeset
864
a77217511afd tools, frontends (xmlui): allow to select a tab when adding it
souliane <souliane@mailoo.org>
parents: 1476
diff changeset
865 @param name (unicode): tab name
a77217511afd tools, frontends (xmlui): allow to select a tab when adding it
souliane <souliane@mailoo.org>
parents: 1476
diff changeset
866 @param label (unicode): tab label
a77217511afd tools, frontends (xmlui): allow to select a tab when adding it
souliane <souliane@mailoo.org>
parents: 1476
diff changeset
867 @param selected (bool): set to True to select this tab
1490
55cff13b1f10 core (xmlui): reverted change from 039d96e131be: we should be able to recreate the same container with changeContainer
Goffi <goffi@goffi.org>
parents: 1487
diff changeset
868 @param container (class): container class, inheriting from Container
1486
a77217511afd tools, frontends (xmlui): allow to select a tab when adding it
souliane <souliane@mailoo.org>
parents: 1476
diff changeset
869 @return: the container for the new tab
a77217511afd tools, frontends (xmlui): allow to select a tab when adding it
souliane <souliane@mailoo.org>
parents: 1476
diff changeset
870 """
802
9007bb133009 core, frontends: XMLUI refactoring:
Goffi <goffi@goffi.org>
parents: 800
diff changeset
871 if not label:
9007bb133009 core, frontends: XMLUI refactoring:
Goffi <goffi@goffi.org>
parents: 800
diff changeset
872 label = name
1486
a77217511afd tools, frontends (xmlui): allow to select a tab when adding it
souliane <souliane@mailoo.org>
parents: 1476
diff changeset
873 tab_elt = TabElement(self, name, label, selected)
802
9007bb133009 core, frontends: XMLUI refactoring:
Goffi <goffi@goffi.org>
parents: 800
diff changeset
874 new_container = container(self.xmlui, tab_elt)
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 4032
diff changeset
875 return self.xmlui.change_container(new_container)
802
9007bb133009 core, frontends: XMLUI refactoring:
Goffi <goffi@goffi.org>
parents: 800
diff changeset
876
9007bb133009 core, frontends: XMLUI refactoring:
Goffi <goffi@goffi.org>
parents: 800
diff changeset
877 def end(self):
4270
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4110
diff changeset
878 """Called when we have finished tabs
1472
c005c212b538 xmlui: minor docstring fixes
Goffi <goffi@goffi.org>
parents: 1456
diff changeset
879
802
9007bb133009 core, frontends: XMLUI refactoring:
Goffi <goffi@goffi.org>
parents: 800
diff changeset
880 change current container to first container parent
9007bb133009 core, frontends: XMLUI refactoring:
Goffi <goffi@goffi.org>
parents: 800
diff changeset
881 """
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 4032
diff changeset
882 parent_container = self.get_parent_container()
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 4032
diff changeset
883 self.xmlui.change_container(parent_container)
630
0b914394e74f core: added advanced list to XMLUI (need improvment, very basic so far)
Goffi <goffi@goffi.org>
parents: 615
diff changeset
884
0b914394e74f core: added advanced list to XMLUI (need improvment, very basic so far)
Goffi <goffi@goffi.org>
parents: 615
diff changeset
885
802
9007bb133009 core, frontends: XMLUI refactoring:
Goffi <goffi@goffi.org>
parents: 800
diff changeset
886 class AdvancedListContainer(Container):
1472
c005c212b538 xmlui: minor docstring fixes
Goffi <goffi@goffi.org>
parents: 1456
diff changeset
887 """A list which can contain other widgets, headers, etc"""
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
888
802
9007bb133009 core, frontends: XMLUI refactoring:
Goffi <goffi@goffi.org>
parents: 800
diff changeset
889 type = "advanced_list"
630
0b914394e74f core: added advanced list to XMLUI (need improvment, very basic so far)
Goffi <goffi@goffi.org>
parents: 615
diff changeset
890
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
891 def __init__(
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
892 self,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
893 xmlui,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
894 callback_id=None,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
895 name=None,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
896 headers=None,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
897 items=None,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
898 columns=None,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
899 selectable="no",
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
900 auto_index=False,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
901 parent=None,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
902 ):
802
9007bb133009 core, frontends: XMLUI refactoring:
Goffi <goffi@goffi.org>
parents: 800
diff changeset
903 """Create an advanced list
1472
c005c212b538 xmlui: minor docstring fixes
Goffi <goffi@goffi.org>
parents: 1456
diff changeset
904
1199
96fb74a4714d misc: fixed some typos
Matteo Cypriani <mcy@lm7.fr>
parents: 1142
diff changeset
905 @param headers: optional headers information
805
7c05c39156a2 core (XMLUI), frontends: advancedListContainer part 2:
Goffi <goffi@goffi.org>
parents: 804
diff changeset
906 @param callback_id: id of the method to call when selection is done
7c05c39156a2 core (XMLUI), frontends: advancedListContainer part 2:
Goffi <goffi@goffi.org>
parents: 804
diff changeset
907 @param items: list of widgets to add (just the first row)
7c05c39156a2 core (XMLUI), frontends: advancedListContainer part 2:
Goffi <goffi@goffi.org>
parents: 804
diff changeset
908 @param columns: number of columns in this table, or None to autodetect
7c05c39156a2 core (XMLUI), frontends: advancedListContainer part 2:
Goffi <goffi@goffi.org>
parents: 804
diff changeset
909 @param selectable: one of:
7c05c39156a2 core (XMLUI), frontends: advancedListContainer part 2:
Goffi <goffi@goffi.org>
parents: 804
diff changeset
910 'no': nothing is done
7c05c39156a2 core (XMLUI), frontends: advancedListContainer part 2:
Goffi <goffi@goffi.org>
parents: 804
diff changeset
911 'single': one row can be selected
2625
a55a14c3cbf4 plugin XEP-0070: use a confirm dialog instead of a form + simplified a bit the code
Goffi <goffi@goffi.org>
parents: 2624
diff changeset
912 @param auto_index: if True, indexes will be generated by frontends,
a55a14c3cbf4 plugin XEP-0070: use a confirm dialog instead of a form + simplified a bit the code
Goffi <goffi@goffi.org>
parents: 2624
diff changeset
913 starting from 0
802
9007bb133009 core, frontends: XMLUI refactoring:
Goffi <goffi@goffi.org>
parents: 800
diff changeset
914 @return: created element
630
0b914394e74f core: added advanced list to XMLUI (need improvment, very basic so far)
Goffi <goffi@goffi.org>
parents: 615
diff changeset
915 """
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
916 assert selectable in ("no", "single")
802
9007bb133009 core, frontends: XMLUI refactoring:
Goffi <goffi@goffi.org>
parents: 800
diff changeset
917 if not items and columns is None:
876
65bf1bc70f6b tools, plugin XEP-0077: small fixes
souliane <souliane@mailoo.org>
parents: 865
diff changeset
918 raise exceptions.DataError(_("either items or columns need do be filled"))
802
9007bb133009 core, frontends: XMLUI refactoring:
Goffi <goffi@goffi.org>
parents: 800
diff changeset
919 if headers is None:
9007bb133009 core, frontends: XMLUI refactoring:
Goffi <goffi@goffi.org>
parents: 800
diff changeset
920 headers = []
9007bb133009 core, frontends: XMLUI refactoring:
Goffi <goffi@goffi.org>
parents: 800
diff changeset
921 if items is None:
9007bb133009 core, frontends: XMLUI refactoring:
Goffi <goffi@goffi.org>
parents: 800
diff changeset
922 items = []
9007bb133009 core, frontends: XMLUI refactoring:
Goffi <goffi@goffi.org>
parents: 800
diff changeset
923 super(AdvancedListContainer, self).__init__(xmlui, parent)
9007bb133009 core, frontends: XMLUI refactoring:
Goffi <goffi@goffi.org>
parents: 800
diff changeset
924 if columns is None:
9007bb133009 core, frontends: XMLUI refactoring:
Goffi <goffi@goffi.org>
parents: 800
diff changeset
925 columns = len(items[0])
9007bb133009 core, frontends: XMLUI refactoring:
Goffi <goffi@goffi.org>
parents: 800
diff changeset
926 self._columns = columns
803
f100fd8d279f core, frontends: implementation of AdvancedListContainer first draft + misc:
Goffi <goffi@goffi.org>
parents: 802
diff changeset
927 self._item_idx = 0
802
9007bb133009 core, frontends: XMLUI refactoring:
Goffi <goffi@goffi.org>
parents: 800
diff changeset
928 self.current_row = None
9007bb133009 core, frontends: XMLUI refactoring:
Goffi <goffi@goffi.org>
parents: 800
diff changeset
929 if headers:
9007bb133009 core, frontends: XMLUI refactoring:
Goffi <goffi@goffi.org>
parents: 800
diff changeset
930 if len(headers) != self._columns:
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
931 raise exceptions.DataError(
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
932 _("Headers lenght doesn't correspond to columns")
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
933 )
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 4032
diff changeset
934 self.add_headers(headers)
802
9007bb133009 core, frontends: XMLUI refactoring:
Goffi <goffi@goffi.org>
parents: 800
diff changeset
935 if items:
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 4032
diff changeset
936 self.add_items(items)
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
937 self.elem.setAttribute("columns", str(self._columns))
805
7c05c39156a2 core (XMLUI), frontends: advancedListContainer part 2:
Goffi <goffi@goffi.org>
parents: 804
diff changeset
938 if callback_id is not None:
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
939 self.elem.setAttribute("callback", callback_id)
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
940 self.elem.setAttribute("selectable", selectable)
805
7c05c39156a2 core (XMLUI), frontends: advancedListContainer part 2:
Goffi <goffi@goffi.org>
parents: 804
diff changeset
941 self.auto_index = auto_index
7c05c39156a2 core (XMLUI), frontends: advancedListContainer part 2:
Goffi <goffi@goffi.org>
parents: 804
diff changeset
942 if auto_index:
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
943 self.elem.setAttribute("auto_index", "true")
805
7c05c39156a2 core (XMLUI), frontends: advancedListContainer part 2:
Goffi <goffi@goffi.org>
parents: 804
diff changeset
944 self.next_row_idx = None
802
9007bb133009 core, frontends: XMLUI refactoring:
Goffi <goffi@goffi.org>
parents: 800
diff changeset
945
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 4032
diff changeset
946 def add_headers(self, headers):
802
9007bb133009 core, frontends: XMLUI refactoring:
Goffi <goffi@goffi.org>
parents: 800
diff changeset
947 for header in headers:
9007bb133009 core, frontends: XMLUI refactoring:
Goffi <goffi@goffi.org>
parents: 800
diff changeset
948 self.addHeader(header)
9007bb133009 core, frontends: XMLUI refactoring:
Goffi <goffi@goffi.org>
parents: 800
diff changeset
949
9007bb133009 core, frontends: XMLUI refactoring:
Goffi <goffi@goffi.org>
parents: 800
diff changeset
950 def addHeader(self, header):
1444
8ce9924fa92c tools (xml_tools): better PEP-8 compliance
souliane <souliane@mailoo.org>
parents: 1409
diff changeset
951 pass # TODO
802
9007bb133009 core, frontends: XMLUI refactoring:
Goffi <goffi@goffi.org>
parents: 800
diff changeset
952
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 4032
diff changeset
953 def add_items(self, items):
802
9007bb133009 core, frontends: XMLUI refactoring:
Goffi <goffi@goffi.org>
parents: 800
diff changeset
954 for item in items:
803
f100fd8d279f core, frontends: implementation of AdvancedListContainer first draft + misc:
Goffi <goffi@goffi.org>
parents: 802
diff changeset
955 self.append(item)
802
9007bb133009 core, frontends: XMLUI refactoring:
Goffi <goffi@goffi.org>
parents: 800
diff changeset
956
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 4032
diff changeset
957 def set_row_index(self, idx):
4270
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4110
diff changeset
958 """Set index for next row
1472
c005c212b538 xmlui: minor docstring fixes
Goffi <goffi@goffi.org>
parents: 1456
diff changeset
959
805
7c05c39156a2 core (XMLUI), frontends: advancedListContainer part 2:
Goffi <goffi@goffi.org>
parents: 804
diff changeset
960 index are returned when a row is selected, in data's "index" key
7c05c39156a2 core (XMLUI), frontends: advancedListContainer part 2:
Goffi <goffi@goffi.org>
parents: 804
diff changeset
961 @param idx: string index to associate to the next row
7c05c39156a2 core (XMLUI), frontends: advancedListContainer part 2:
Goffi <goffi@goffi.org>
parents: 804
diff changeset
962 """
7c05c39156a2 core (XMLUI), frontends: advancedListContainer part 2:
Goffi <goffi@goffi.org>
parents: 804
diff changeset
963 self.next_row_idx = idx
7c05c39156a2 core (XMLUI), frontends: advancedListContainer part 2:
Goffi <goffi@goffi.org>
parents: 804
diff changeset
964
803
f100fd8d279f core, frontends: implementation of AdvancedListContainer first draft + misc:
Goffi <goffi@goffi.org>
parents: 802
diff changeset
965 def append(self, child):
f100fd8d279f core, frontends: implementation of AdvancedListContainer first draft + misc:
Goffi <goffi@goffi.org>
parents: 802
diff changeset
966 if isinstance(child, RowElement):
f100fd8d279f core, frontends: implementation of AdvancedListContainer first draft + misc:
Goffi <goffi@goffi.org>
parents: 802
diff changeset
967 return super(AdvancedListContainer, self).append(child)
f100fd8d279f core, frontends: implementation of AdvancedListContainer first draft + misc:
Goffi <goffi@goffi.org>
parents: 802
diff changeset
968 if self._item_idx % self._columns == 0:
802
9007bb133009 core, frontends: XMLUI refactoring:
Goffi <goffi@goffi.org>
parents: 800
diff changeset
969 self.current_row = RowElement(self)
803
f100fd8d279f core, frontends: implementation of AdvancedListContainer first draft + misc:
Goffi <goffi@goffi.org>
parents: 802
diff changeset
970 self.current_row.append(child)
f100fd8d279f core, frontends: implementation of AdvancedListContainer first draft + misc:
Goffi <goffi@goffi.org>
parents: 802
diff changeset
971 self._item_idx += 1
802
9007bb133009 core, frontends: XMLUI refactoring:
Goffi <goffi@goffi.org>
parents: 800
diff changeset
972
9007bb133009 core, frontends: XMLUI refactoring:
Goffi <goffi@goffi.org>
parents: 800
diff changeset
973 def end(self):
4270
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4110
diff changeset
974 """Called when we have finished list
1472
c005c212b538 xmlui: minor docstring fixes
Goffi <goffi@goffi.org>
parents: 1456
diff changeset
975
802
9007bb133009 core, frontends: XMLUI refactoring:
Goffi <goffi@goffi.org>
parents: 800
diff changeset
976 change current container to first container parent
630
0b914394e74f core: added advanced list to XMLUI (need improvment, very basic so far)
Goffi <goffi@goffi.org>
parents: 615
diff changeset
977 """
804
5174657b3378 XMLUI (core, frontends): added JidWidget and DividerWidget + popup type + some bugfixes:
Goffi <goffi@goffi.org>
parents: 803
diff changeset
978 if self._item_idx % self._columns != 0:
802
9007bb133009 core, frontends: XMLUI refactoring:
Goffi <goffi@goffi.org>
parents: 800
diff changeset
979 raise exceptions.DataError(_("Incorrect number of items in list"))
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 4032
diff changeset
980 parent_container = self.get_parent_container()
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 4032
diff changeset
981 self.xmlui.change_container(parent_container)
802
9007bb133009 core, frontends: XMLUI refactoring:
Goffi <goffi@goffi.org>
parents: 800
diff changeset
982
9007bb133009 core, frontends: XMLUI refactoring:
Goffi <goffi@goffi.org>
parents: 800
diff changeset
983
1476
48706f4ff19c core (xmlui): added JidsListWidget to manage editable list of jids:
Goffi <goffi@goffi.org>
parents: 1475
diff changeset
984 ## Widgets ##
48706f4ff19c core (xmlui): added JidsListWidget to manage editable list of jids:
Goffi <goffi@goffi.org>
parents: 1475
diff changeset
985
48706f4ff19c core (xmlui): added JidsListWidget to manage editable list of jids:
Goffi <goffi@goffi.org>
parents: 1475
diff changeset
986
802
9007bb133009 core, frontends: XMLUI refactoring:
Goffi <goffi@goffi.org>
parents: 800
diff changeset
987 class Widget(Element):
9007bb133009 core, frontends: XMLUI refactoring:
Goffi <goffi@goffi.org>
parents: 800
diff changeset
988 type = None
9007bb133009 core, frontends: XMLUI refactoring:
Goffi <goffi@goffi.org>
parents: 800
diff changeset
989
9007bb133009 core, frontends: XMLUI refactoring:
Goffi <goffi@goffi.org>
parents: 800
diff changeset
990 def __init__(self, xmlui, name=None, parent=None):
9007bb133009 core, frontends: XMLUI refactoring:
Goffi <goffi@goffi.org>
parents: 800
diff changeset
991 """Create an element
1472
c005c212b538 xmlui: minor docstring fixes
Goffi <goffi@goffi.org>
parents: 1456
diff changeset
992
802
9007bb133009 core, frontends: XMLUI refactoring:
Goffi <goffi@goffi.org>
parents: 800
diff changeset
993 @param xmlui: XMLUI instance
9007bb133009 core, frontends: XMLUI refactoring:
Goffi <goffi@goffi.org>
parents: 800
diff changeset
994 @param name: name of the element or None
9007bb133009 core, frontends: XMLUI refactoring:
Goffi <goffi@goffi.org>
parents: 800
diff changeset
995 @param parent: parent element or None
9007bb133009 core, frontends: XMLUI refactoring:
Goffi <goffi@goffi.org>
parents: 800
diff changeset
996 """
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
997 self.elem = xmlui.doc.createElement("widget")
802
9007bb133009 core, frontends: XMLUI refactoring:
Goffi <goffi@goffi.org>
parents: 800
diff changeset
998 super(Widget, self).__init__(xmlui, parent)
9007bb133009 core, frontends: XMLUI refactoring:
Goffi <goffi@goffi.org>
parents: 800
diff changeset
999 if name:
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
1000 self.elem.setAttribute("name", name)
2398
3ff9d7a7fe71 core (XMLUI): filters can now be used when converting data form to XMLUI:
Goffi <goffi@goffi.org>
parents: 2397
diff changeset
1001 if name in xmlui.named_widgets:
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
1002 raise exceptions.ConflictError(
4270
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4110
diff changeset
1003 _('A widget with the name "{name}" already exists.').format(name=name)
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
1004 )
2398
3ff9d7a7fe71 core (XMLUI): filters can now be used when converting data form to XMLUI:
Goffi <goffi@goffi.org>
parents: 2397
diff changeset
1005 xmlui.named_widgets[name] = self
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
1006 self.elem.setAttribute("type", self.type)
802
9007bb133009 core, frontends: XMLUI refactoring:
Goffi <goffi@goffi.org>
parents: 800
diff changeset
1007
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 4032
diff changeset
1008 def set_internal_callback(self, callback, fields, data_elts=None):
977
d2e612a45e76 tools, frontends (xmlui): add Widget.setInternalCallback:
souliane <souliane@mailoo.org>
parents: 975
diff changeset
1009 """Set an internal UI callback when the widget value is changed.
d2e612a45e76 tools, frontends (xmlui): add Widget.setInternalCallback:
souliane <souliane@mailoo.org>
parents: 975
diff changeset
1010
d2e612a45e76 tools, frontends (xmlui): add Widget.setInternalCallback:
souliane <souliane@mailoo.org>
parents: 975
diff changeset
1011 The internal callbacks are NO callback ids, they are strings from
d2e612a45e76 tools, frontends (xmlui): add Widget.setInternalCallback:
souliane <souliane@mailoo.org>
parents: 975
diff changeset
1012 a predefined set of actions that are running in the scope of XMLUI.
d2e612a45e76 tools, frontends (xmlui): add Widget.setInternalCallback:
souliane <souliane@mailoo.org>
parents: 975
diff changeset
1013 @param callback (string): a value from:
d2e612a45e76 tools, frontends (xmlui): add Widget.setInternalCallback:
souliane <souliane@mailoo.org>
parents: 975
diff changeset
1014 - 'copy': process the widgets given in 'fields' two by two, by
d2e612a45e76 tools, frontends (xmlui): add Widget.setInternalCallback:
souliane <souliane@mailoo.org>
parents: 975
diff changeset
1015 copying the values of one widget to the other. Target widgets
d2e612a45e76 tools, frontends (xmlui): add Widget.setInternalCallback:
souliane <souliane@mailoo.org>
parents: 975
diff changeset
1016 of type List do not accept the empty value.
d2e612a45e76 tools, frontends (xmlui): add Widget.setInternalCallback:
souliane <souliane@mailoo.org>
parents: 975
diff changeset
1017 - 'move': same than copy but moves the values if the source widget
d2e612a45e76 tools, frontends (xmlui): add Widget.setInternalCallback:
souliane <souliane@mailoo.org>
parents: 975
diff changeset
1018 is not a List.
d2e612a45e76 tools, frontends (xmlui): add Widget.setInternalCallback:
souliane <souliane@mailoo.org>
parents: 975
diff changeset
1019 - 'groups_of_contact': process the widgets two by two, assume A is
d2e612a45e76 tools, frontends (xmlui): add Widget.setInternalCallback:
souliane <souliane@mailoo.org>
parents: 975
diff changeset
1020 is a list of JID and B a list of groups, select in B the groups
d2e612a45e76 tools, frontends (xmlui): add Widget.setInternalCallback:
souliane <souliane@mailoo.org>
parents: 975
diff changeset
1021 to which the JID selected in A belongs.
d2e612a45e76 tools, frontends (xmlui): add Widget.setInternalCallback:
souliane <souliane@mailoo.org>
parents: 975
diff changeset
1022 - more operation to be added when necessary...
d2e612a45e76 tools, frontends (xmlui): add Widget.setInternalCallback:
souliane <souliane@mailoo.org>
parents: 975
diff changeset
1023 @param fields (list): a list of widget names (string)
d2e612a45e76 tools, frontends (xmlui): add Widget.setInternalCallback:
souliane <souliane@mailoo.org>
parents: 975
diff changeset
1024 @param data_elts (list[Element]): extra data elements
d2e612a45e76 tools, frontends (xmlui): add Widget.setInternalCallback:
souliane <souliane@mailoo.org>
parents: 975
diff changeset
1025 """
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
1026 self.elem.setAttribute("internal_callback", callback)
977
d2e612a45e76 tools, frontends (xmlui): add Widget.setInternalCallback:
souliane <souliane@mailoo.org>
parents: 975
diff changeset
1027 if fields:
d2e612a45e76 tools, frontends (xmlui): add Widget.setInternalCallback:
souliane <souliane@mailoo.org>
parents: 975
diff changeset
1028 for field in fields:
d2e612a45e76 tools, frontends (xmlui): add Widget.setInternalCallback:
souliane <souliane@mailoo.org>
parents: 975
diff changeset
1029 InternalFieldElement(self, field)
d2e612a45e76 tools, frontends (xmlui): add Widget.setInternalCallback:
souliane <souliane@mailoo.org>
parents: 975
diff changeset
1030 if data_elts:
d2e612a45e76 tools, frontends (xmlui): add Widget.setInternalCallback:
souliane <souliane@mailoo.org>
parents: 975
diff changeset
1031 InternalDataElement(self, data_elts)
d2e612a45e76 tools, frontends (xmlui): add Widget.setInternalCallback:
souliane <souliane@mailoo.org>
parents: 975
diff changeset
1032
802
9007bb133009 core, frontends: XMLUI refactoring:
Goffi <goffi@goffi.org>
parents: 800
diff changeset
1033
9007bb133009 core, frontends: XMLUI refactoring:
Goffi <goffi@goffi.org>
parents: 800
diff changeset
1034 class EmptyWidget(Widget):
1084
03dcb6ca7e49 core (XMLUI): created a read_only attribute for InputWidget + new JidInputWidget
Goffi <goffi@goffi.org>
parents: 1078
diff changeset
1035 """Place holder widget"""
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
1036
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
1037 type = "empty"
630
0b914394e74f core: added advanced list to XMLUI (need improvment, very basic so far)
Goffi <goffi@goffi.org>
parents: 615
diff changeset
1038
0b914394e74f core: added advanced list to XMLUI (need improvment, very basic so far)
Goffi <goffi@goffi.org>
parents: 615
diff changeset
1039
802
9007bb133009 core, frontends: XMLUI refactoring:
Goffi <goffi@goffi.org>
parents: 800
diff changeset
1040 class TextWidget(Widget):
1084
03dcb6ca7e49 core (XMLUI): created a read_only attribute for InputWidget + new JidInputWidget
Goffi <goffi@goffi.org>
parents: 1078
diff changeset
1041 """Used for blob of text"""
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
1042
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
1043 type = "text"
802
9007bb133009 core, frontends: XMLUI refactoring:
Goffi <goffi@goffi.org>
parents: 800
diff changeset
1044
865
3ee2ec7ec010 core (xmlui), frontends: handling of "text" type in params xml + bug fixes
Goffi <goffi@goffi.org>
parents: 863
diff changeset
1045 def __init__(self, xmlui, value, name=None, parent=None):
802
9007bb133009 core, frontends: XMLUI refactoring:
Goffi <goffi@goffi.org>
parents: 800
diff changeset
1046 super(TextWidget, self).__init__(xmlui, name, parent)
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
1047 value_elt = self.xmlui.doc.createElement("value")
865
3ee2ec7ec010 core (xmlui), frontends: handling of "text" type in params xml + bug fixes
Goffi <goffi@goffi.org>
parents: 863
diff changeset
1048 text = self.xmlui.doc.createTextNode(value)
1086
2cb30f46e560 core/frontends (XMLUI): value can now be inserted as a <value/> element, if not present value attribute is tested, else empty string is used.
Goffi <goffi@goffi.org>
parents: 1084
diff changeset
1049 value_elt.appendChild(text)
2cb30f46e560 core/frontends (XMLUI): value can now be inserted as a <value/> element, if not present value attribute is tested, else empty string is used.
Goffi <goffi@goffi.org>
parents: 1084
diff changeset
1050 self.elem.appendChild(value_elt)
802
9007bb133009 core, frontends: XMLUI refactoring:
Goffi <goffi@goffi.org>
parents: 800
diff changeset
1051
2428
c2d88fe05303 core (XMLUI): added a "value" property in TextWidget
Goffi <goffi@goffi.org>
parents: 2414
diff changeset
1052 @property
c2d88fe05303 core (XMLUI): added a "value" property in TextWidget
Goffi <goffi@goffi.org>
parents: 2414
diff changeset
1053 def value(self):
c2d88fe05303 core (XMLUI): added a "value" property in TextWidget
Goffi <goffi@goffi.org>
parents: 2414
diff changeset
1054 return self.elem.firstChild.firstChild.wholeText
c2d88fe05303 core (XMLUI): added a "value" property in TextWidget
Goffi <goffi@goffi.org>
parents: 2414
diff changeset
1055
802
9007bb133009 core, frontends: XMLUI refactoring:
Goffi <goffi@goffi.org>
parents: 800
diff changeset
1056
9007bb133009 core, frontends: XMLUI refactoring:
Goffi <goffi@goffi.org>
parents: 800
diff changeset
1057 class LabelWidget(Widget):
1472
c005c212b538 xmlui: minor docstring fixes
Goffi <goffi@goffi.org>
parents: 1456
diff changeset
1058 """One line blob of text
c005c212b538 xmlui: minor docstring fixes
Goffi <goffi@goffi.org>
parents: 1456
diff changeset
1059
c005c212b538 xmlui: minor docstring fixes
Goffi <goffi@goffi.org>
parents: 1456
diff changeset
1060 used most of time to display the desciption or name of the next widget
1084
03dcb6ca7e49 core (XMLUI): created a read_only attribute for InputWidget + new JidInputWidget
Goffi <goffi@goffi.org>
parents: 1078
diff changeset
1061 """
4270
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4110
diff changeset
1062
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
1063 type = "label"
802
9007bb133009 core, frontends: XMLUI refactoring:
Goffi <goffi@goffi.org>
parents: 800
diff changeset
1064
9007bb133009 core, frontends: XMLUI refactoring:
Goffi <goffi@goffi.org>
parents: 800
diff changeset
1065 def __init__(self, xmlui, label, name=None, parent=None):
9007bb133009 core, frontends: XMLUI refactoring:
Goffi <goffi@goffi.org>
parents: 800
diff changeset
1066 super(LabelWidget, self).__init__(xmlui, name, parent)
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
1067 self.elem.setAttribute("value", label)
802
9007bb133009 core, frontends: XMLUI refactoring:
Goffi <goffi@goffi.org>
parents: 800
diff changeset
1068
9007bb133009 core, frontends: XMLUI refactoring:
Goffi <goffi@goffi.org>
parents: 800
diff changeset
1069
2668
c274201cea94 core, frontends (xmlui): added "hidden" widget, to specify a value to be returned unmodified
Goffi <goffi@goffi.org>
parents: 2625
diff changeset
1070 class HiddenWidget(Widget):
c274201cea94 core, frontends (xmlui): added "hidden" widget, to specify a value to be returned unmodified
Goffi <goffi@goffi.org>
parents: 2625
diff changeset
1071 """Not displayed widget, frontends will just copy the value(s)"""
4270
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4110
diff changeset
1072
2668
c274201cea94 core, frontends (xmlui): added "hidden" widget, to specify a value to be returned unmodified
Goffi <goffi@goffi.org>
parents: 2625
diff changeset
1073 type = "hidden"
c274201cea94 core, frontends (xmlui): added "hidden" widget, to specify a value to be returned unmodified
Goffi <goffi@goffi.org>
parents: 2625
diff changeset
1074
c274201cea94 core, frontends (xmlui): added "hidden" widget, to specify a value to be returned unmodified
Goffi <goffi@goffi.org>
parents: 2625
diff changeset
1075 def __init__(self, xmlui, value, name, parent=None):
c274201cea94 core, frontends (xmlui): added "hidden" widget, to specify a value to be returned unmodified
Goffi <goffi@goffi.org>
parents: 2625
diff changeset
1076 super(HiddenWidget, self).__init__(xmlui, name, parent)
c274201cea94 core, frontends (xmlui): added "hidden" widget, to specify a value to be returned unmodified
Goffi <goffi@goffi.org>
parents: 2625
diff changeset
1077 value_elt = self.xmlui.doc.createElement("value")
c274201cea94 core, frontends (xmlui): added "hidden" widget, to specify a value to be returned unmodified
Goffi <goffi@goffi.org>
parents: 2625
diff changeset
1078 text = self.xmlui.doc.createTextNode(value)
c274201cea94 core, frontends (xmlui): added "hidden" widget, to specify a value to be returned unmodified
Goffi <goffi@goffi.org>
parents: 2625
diff changeset
1079 value_elt.appendChild(text)
c274201cea94 core, frontends (xmlui): added "hidden" widget, to specify a value to be returned unmodified
Goffi <goffi@goffi.org>
parents: 2625
diff changeset
1080 self.elem.appendChild(value_elt)
c274201cea94 core, frontends (xmlui): added "hidden" widget, to specify a value to be returned unmodified
Goffi <goffi@goffi.org>
parents: 2625
diff changeset
1081
c274201cea94 core, frontends (xmlui): added "hidden" widget, to specify a value to be returned unmodified
Goffi <goffi@goffi.org>
parents: 2625
diff changeset
1082 @property
c274201cea94 core, frontends (xmlui): added "hidden" widget, to specify a value to be returned unmodified
Goffi <goffi@goffi.org>
parents: 2625
diff changeset
1083 def value(self):
c274201cea94 core, frontends (xmlui): added "hidden" widget, to specify a value to be returned unmodified
Goffi <goffi@goffi.org>
parents: 2625
diff changeset
1084 return self.elem.firstChild.firstChild.wholeText
c274201cea94 core, frontends (xmlui): added "hidden" widget, to specify a value to be returned unmodified
Goffi <goffi@goffi.org>
parents: 2625
diff changeset
1085
c274201cea94 core, frontends (xmlui): added "hidden" widget, to specify a value to be returned unmodified
Goffi <goffi@goffi.org>
parents: 2625
diff changeset
1086
804
5174657b3378 XMLUI (core, frontends): added JidWidget and DividerWidget + popup type + some bugfixes:
Goffi <goffi@goffi.org>
parents: 803
diff changeset
1087 class JidWidget(Widget):
1084
03dcb6ca7e49 core (XMLUI): created a read_only attribute for InputWidget + new JidInputWidget
Goffi <goffi@goffi.org>
parents: 1078
diff changeset
1088 """Used to display a Jabber ID, some specific methods can be added"""
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
1089
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
1090 type = "jid"
804
5174657b3378 XMLUI (core, frontends): added JidWidget and DividerWidget + popup type + some bugfixes:
Goffi <goffi@goffi.org>
parents: 803
diff changeset
1091
5174657b3378 XMLUI (core, frontends): added JidWidget and DividerWidget + popup type + some bugfixes:
Goffi <goffi@goffi.org>
parents: 803
diff changeset
1092 def __init__(self, xmlui, jid, name=None, parent=None):
5174657b3378 XMLUI (core, frontends): added JidWidget and DividerWidget + popup type + some bugfixes:
Goffi <goffi@goffi.org>
parents: 803
diff changeset
1093 super(JidWidget, self).__init__(xmlui, name, parent)
5174657b3378 XMLUI (core, frontends): added JidWidget and DividerWidget + popup type + some bugfixes:
Goffi <goffi@goffi.org>
parents: 803
diff changeset
1094 try:
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
1095 self.elem.setAttribute("value", jid.full())
804
5174657b3378 XMLUI (core, frontends): added JidWidget and DividerWidget + popup type + some bugfixes:
Goffi <goffi@goffi.org>
parents: 803
diff changeset
1096 except AttributeError:
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2958
diff changeset
1097 self.elem.setAttribute("value", str(jid))
804
5174657b3378 XMLUI (core, frontends): added JidWidget and DividerWidget + popup type + some bugfixes:
Goffi <goffi@goffi.org>
parents: 803
diff changeset
1098
3448
987198910e56 tools (xml_tools): add missing `value` property to JidWidget
Goffi <goffi@goffi.org>
parents: 3396
diff changeset
1099 @property
987198910e56 tools (xml_tools): add missing `value` property to JidWidget
Goffi <goffi@goffi.org>
parents: 3396
diff changeset
1100 def value(self):
987198910e56 tools (xml_tools): add missing `value` property to JidWidget
Goffi <goffi@goffi.org>
parents: 3396
diff changeset
1101 return self.elem.getAttribute("value")
987198910e56 tools (xml_tools): add missing `value` property to JidWidget
Goffi <goffi@goffi.org>
parents: 3396
diff changeset
1102
804
5174657b3378 XMLUI (core, frontends): added JidWidget and DividerWidget + popup type + some bugfixes:
Goffi <goffi@goffi.org>
parents: 803
diff changeset
1103
5174657b3378 XMLUI (core, frontends): added JidWidget and DividerWidget + popup type + some bugfixes:
Goffi <goffi@goffi.org>
parents: 803
diff changeset
1104 class DividerWidget(Widget):
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
1105 type = "divider"
804
5174657b3378 XMLUI (core, frontends): added JidWidget and DividerWidget + popup type + some bugfixes:
Goffi <goffi@goffi.org>
parents: 803
diff changeset
1106
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
1107 def __init__(self, xmlui, style="line", name=None, parent=None):
4270
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4110
diff changeset
1108 """Create a divider
1472
c005c212b538 xmlui: minor docstring fixes
Goffi <goffi@goffi.org>
parents: 1456
diff changeset
1109
804
5174657b3378 XMLUI (core, frontends): added JidWidget and DividerWidget + popup type + some bugfixes:
Goffi <goffi@goffi.org>
parents: 803
diff changeset
1110 @param xmlui: XMLUI instance
5174657b3378 XMLUI (core, frontends): added JidWidget and DividerWidget + popup type + some bugfixes:
Goffi <goffi@goffi.org>
parents: 803
diff changeset
1111 @param style: one of:
5174657b3378 XMLUI (core, frontends): added JidWidget and DividerWidget + popup type + some bugfixes:
Goffi <goffi@goffi.org>
parents: 803
diff changeset
1112 - line: a simple line
5174657b3378 XMLUI (core, frontends): added JidWidget and DividerWidget + popup type + some bugfixes:
Goffi <goffi@goffi.org>
parents: 803
diff changeset
1113 - dot: a line of dots
5174657b3378 XMLUI (core, frontends): added JidWidget and DividerWidget + popup type + some bugfixes:
Goffi <goffi@goffi.org>
parents: 803
diff changeset
1114 - dash: a line of dashes
5174657b3378 XMLUI (core, frontends): added JidWidget and DividerWidget + popup type + some bugfixes:
Goffi <goffi@goffi.org>
parents: 803
diff changeset
1115 - plain: a full thick line
5174657b3378 XMLUI (core, frontends): added JidWidget and DividerWidget + popup type + some bugfixes:
Goffi <goffi@goffi.org>
parents: 803
diff changeset
1116 - blank: a blank line/space
5174657b3378 XMLUI (core, frontends): added JidWidget and DividerWidget + popup type + some bugfixes:
Goffi <goffi@goffi.org>
parents: 803
diff changeset
1117 @param name: name of the widget
5174657b3378 XMLUI (core, frontends): added JidWidget and DividerWidget + popup type + some bugfixes:
Goffi <goffi@goffi.org>
parents: 803
diff changeset
1118 @param parent: parent container
5174657b3378 XMLUI (core, frontends): added JidWidget and DividerWidget + popup type + some bugfixes:
Goffi <goffi@goffi.org>
parents: 803
diff changeset
1119
5174657b3378 XMLUI (core, frontends): added JidWidget and DividerWidget + popup type + some bugfixes:
Goffi <goffi@goffi.org>
parents: 803
diff changeset
1120 """
5174657b3378 XMLUI (core, frontends): added JidWidget and DividerWidget + popup type + some bugfixes:
Goffi <goffi@goffi.org>
parents: 803
diff changeset
1121 super(DividerWidget, self).__init__(xmlui, name, parent)
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
1122 self.elem.setAttribute("style", style)
804
5174657b3378 XMLUI (core, frontends): added JidWidget and DividerWidget + popup type + some bugfixes:
Goffi <goffi@goffi.org>
parents: 803
diff changeset
1123
5174657b3378 XMLUI (core, frontends): added JidWidget and DividerWidget + popup type + some bugfixes:
Goffi <goffi@goffi.org>
parents: 803
diff changeset
1124
1084
03dcb6ca7e49 core (XMLUI): created a read_only attribute for InputWidget + new JidInputWidget
Goffi <goffi@goffi.org>
parents: 1078
diff changeset
1125 ### Inputs ###
03dcb6ca7e49 core (XMLUI): created a read_only attribute for InputWidget + new JidInputWidget
Goffi <goffi@goffi.org>
parents: 1078
diff changeset
1126
03dcb6ca7e49 core (XMLUI): created a read_only attribute for InputWidget + new JidInputWidget
Goffi <goffi@goffi.org>
parents: 1078
diff changeset
1127
03dcb6ca7e49 core (XMLUI): created a read_only attribute for InputWidget + new JidInputWidget
Goffi <goffi@goffi.org>
parents: 1078
diff changeset
1128 class InputWidget(Widget):
1472
c005c212b538 xmlui: minor docstring fixes
Goffi <goffi@goffi.org>
parents: 1456
diff changeset
1129 """Widget which can accept user inputs
c005c212b538 xmlui: minor docstring fixes
Goffi <goffi@goffi.org>
parents: 1456
diff changeset
1130
1084
03dcb6ca7e49 core (XMLUI): created a read_only attribute for InputWidget + new JidInputWidget
Goffi <goffi@goffi.org>
parents: 1078
diff changeset
1131 used mainly in forms
03dcb6ca7e49 core (XMLUI): created a read_only attribute for InputWidget + new JidInputWidget
Goffi <goffi@goffi.org>
parents: 1078
diff changeset
1132 """
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
1133
1084
03dcb6ca7e49 core (XMLUI): created a read_only attribute for InputWidget + new JidInputWidget
Goffi <goffi@goffi.org>
parents: 1078
diff changeset
1134 def __init__(self, xmlui, name=None, parent=None, read_only=False):
03dcb6ca7e49 core (XMLUI): created a read_only attribute for InputWidget + new JidInputWidget
Goffi <goffi@goffi.org>
parents: 1078
diff changeset
1135 super(InputWidget, self).__init__(xmlui, name, parent)
03dcb6ca7e49 core (XMLUI): created a read_only attribute for InputWidget + new JidInputWidget
Goffi <goffi@goffi.org>
parents: 1078
diff changeset
1136 if read_only:
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
1137 self.elem.setAttribute("read_only", "true")
1084
03dcb6ca7e49 core (XMLUI): created a read_only attribute for InputWidget + new JidInputWidget
Goffi <goffi@goffi.org>
parents: 1078
diff changeset
1138
03dcb6ca7e49 core (XMLUI): created a read_only attribute for InputWidget + new JidInputWidget
Goffi <goffi@goffi.org>
parents: 1078
diff changeset
1139
802
9007bb133009 core, frontends: XMLUI refactoring:
Goffi <goffi@goffi.org>
parents: 800
diff changeset
1140 class StringWidget(InputWidget):
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
1141 type = "string"
802
9007bb133009 core, frontends: XMLUI refactoring:
Goffi <goffi@goffi.org>
parents: 800
diff changeset
1142
1084
03dcb6ca7e49 core (XMLUI): created a read_only attribute for InputWidget + new JidInputWidget
Goffi <goffi@goffi.org>
parents: 1078
diff changeset
1143 def __init__(self, xmlui, value=None, name=None, parent=None, read_only=False):
03dcb6ca7e49 core (XMLUI): created a read_only attribute for InputWidget + new JidInputWidget
Goffi <goffi@goffi.org>
parents: 1078
diff changeset
1144 super(StringWidget, self).__init__(xmlui, name, parent, read_only=read_only)
802
9007bb133009 core, frontends: XMLUI refactoring:
Goffi <goffi@goffi.org>
parents: 800
diff changeset
1145 if value:
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
1146 value_elt = self.xmlui.doc.createElement("value")
1086
2cb30f46e560 core/frontends (XMLUI): value can now be inserted as a <value/> element, if not present value attribute is tested, else empty string is used.
Goffi <goffi@goffi.org>
parents: 1084
diff changeset
1147 text = self.xmlui.doc.createTextNode(value)
2cb30f46e560 core/frontends (XMLUI): value can now be inserted as a <value/> element, if not present value attribute is tested, else empty string is used.
Goffi <goffi@goffi.org>
parents: 1084
diff changeset
1148 value_elt.appendChild(text)
2cb30f46e560 core/frontends (XMLUI): value can now be inserted as a <value/> element, if not present value attribute is tested, else empty string is used.
Goffi <goffi@goffi.org>
parents: 1084
diff changeset
1149 self.elem.appendChild(value_elt)
802
9007bb133009 core, frontends: XMLUI refactoring:
Goffi <goffi@goffi.org>
parents: 800
diff changeset
1150
2446
bfd1e9d737c4 core (XMLUI): added value property for StringWidget and ListWidget
Goffi <goffi@goffi.org>
parents: 2429
diff changeset
1151 @property
bfd1e9d737c4 core (XMLUI): added value property for StringWidget and ListWidget
Goffi <goffi@goffi.org>
parents: 2429
diff changeset
1152 def value(self):
bfd1e9d737c4 core (XMLUI): added value property for StringWidget and ListWidget
Goffi <goffi@goffi.org>
parents: 2429
diff changeset
1153 return self.elem.firstChild.firstChild.wholeText
bfd1e9d737c4 core (XMLUI): added value property for StringWidget and ListWidget
Goffi <goffi@goffi.org>
parents: 2429
diff changeset
1154
802
9007bb133009 core, frontends: XMLUI refactoring:
Goffi <goffi@goffi.org>
parents: 800
diff changeset
1155
9007bb133009 core, frontends: XMLUI refactoring:
Goffi <goffi@goffi.org>
parents: 800
diff changeset
1156 class PasswordWidget(StringWidget):
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
1157 type = "password"
630
0b914394e74f core: added advanced list to XMLUI (need improvment, very basic so far)
Goffi <goffi@goffi.org>
parents: 615
diff changeset
1158
802
9007bb133009 core, frontends: XMLUI refactoring:
Goffi <goffi@goffi.org>
parents: 800
diff changeset
1159
9007bb133009 core, frontends: XMLUI refactoring:
Goffi <goffi@goffi.org>
parents: 800
diff changeset
1160 class TextBoxWidget(StringWidget):
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
1161 type = "textbox"
802
9007bb133009 core, frontends: XMLUI refactoring:
Goffi <goffi@goffi.org>
parents: 800
diff changeset
1162
9007bb133009 core, frontends: XMLUI refactoring:
Goffi <goffi@goffi.org>
parents: 800
diff changeset
1163
2782
b17e6fa1e607 core (XMLUI): new XHTMLBox widget:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
1164 class XHTMLBoxWidget(StringWidget):
b17e6fa1e607 core (XMLUI): new XHTMLBox widget:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
1165 """Specialized textbox to manipulate XHTML"""
4270
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4110
diff changeset
1166
2782
b17e6fa1e607 core (XMLUI): new XHTMLBox widget:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
1167 type = "xhtmlbox"
b17e6fa1e607 core (XMLUI): new XHTMLBox widget:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
1168
b17e6fa1e607 core (XMLUI): new XHTMLBox widget:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
1169 def __init__(self, xmlui, value, name=None, parent=None, read_only=False, clean=True):
b17e6fa1e607 core (XMLUI): new XHTMLBox widget:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
1170 """
b17e6fa1e607 core (XMLUI): new XHTMLBox widget:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
1171 @param clean(bool): if True, the XHTML is considered insecure and will be cleaned
b17e6fa1e607 core (XMLUI): new XHTMLBox widget:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
1172 Only set to False if you are absolutely sure that the XHTML is safe (in other
b17e6fa1e607 core (XMLUI): new XHTMLBox widget:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
1173 word, set to False only if you made the XHTML yourself)
b17e6fa1e607 core (XMLUI): new XHTMLBox widget:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
1174 """
b17e6fa1e607 core (XMLUI): new XHTMLBox widget:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
1175 if clean:
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 4032
diff changeset
1176 if clean_xhtml is None:
4270
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4110
diff changeset
1177 raise exceptions.NotFound("No cleaning method set, can't clean the XHTML")
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 4032
diff changeset
1178 value = clean_xhtml(value)
2782
b17e6fa1e607 core (XMLUI): new XHTMLBox widget:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
1179
b17e6fa1e607 core (XMLUI): new XHTMLBox widget:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
1180 super(XHTMLBoxWidget, self).__init__(
4270
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4110
diff changeset
1181 xmlui, value=value, name=name, parent=parent, read_only=read_only
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4110
diff changeset
1182 )
2782
b17e6fa1e607 core (XMLUI): new XHTMLBox widget:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
1183
b17e6fa1e607 core (XMLUI): new XHTMLBox widget:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
1184
1084
03dcb6ca7e49 core (XMLUI): created a read_only attribute for InputWidget + new JidInputWidget
Goffi <goffi@goffi.org>
parents: 1078
diff changeset
1185 class JidInputWidget(StringWidget):
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
1186 type = "jid_input"
1084
03dcb6ca7e49 core (XMLUI): created a read_only attribute for InputWidget + new JidInputWidget
Goffi <goffi@goffi.org>
parents: 1078
diff changeset
1187
03dcb6ca7e49 core (XMLUI): created a read_only attribute for InputWidget + new JidInputWidget
Goffi <goffi@goffi.org>
parents: 1078
diff changeset
1188
1220
f91e7028e2c3 memory (params), tools (xml_tools), plugins, frontends: add "int" parameter type with "min" and "max" attributes
souliane <souliane@mailoo.org>
parents: 1199
diff changeset
1189 # TODO handle min and max values
f91e7028e2c3 memory (params), tools (xml_tools), plugins, frontends: add "int" parameter type with "min" and "max" attributes
souliane <souliane@mailoo.org>
parents: 1199
diff changeset
1190 class IntWidget(StringWidget):
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
1191 type = "int"
1220
f91e7028e2c3 memory (params), tools (xml_tools), plugins, frontends: add "int" parameter type with "min" and "max" attributes
souliane <souliane@mailoo.org>
parents: 1199
diff changeset
1192
f91e7028e2c3 memory (params), tools (xml_tools), plugins, frontends: add "int" parameter type with "min" and "max" attributes
souliane <souliane@mailoo.org>
parents: 1199
diff changeset
1193 def __init__(self, xmlui, value=0, name=None, parent=None, read_only=False):
f91e7028e2c3 memory (params), tools (xml_tools), plugins, frontends: add "int" parameter type with "min" and "max" attributes
souliane <souliane@mailoo.org>
parents: 1199
diff changeset
1194 try:
f91e7028e2c3 memory (params), tools (xml_tools), plugins, frontends: add "int" parameter type with "min" and "max" attributes
souliane <souliane@mailoo.org>
parents: 1199
diff changeset
1195 int(value)
f91e7028e2c3 memory (params), tools (xml_tools), plugins, frontends: add "int" parameter type with "min" and "max" attributes
souliane <souliane@mailoo.org>
parents: 1199
diff changeset
1196 except ValueError:
f91e7028e2c3 memory (params), tools (xml_tools), plugins, frontends: add "int" parameter type with "min" and "max" attributes
souliane <souliane@mailoo.org>
parents: 1199
diff changeset
1197 raise exceptions.DataError(_("Value must be an integer"))
f91e7028e2c3 memory (params), tools (xml_tools), plugins, frontends: add "int" parameter type with "min" and "max" attributes
souliane <souliane@mailoo.org>
parents: 1199
diff changeset
1198 super(IntWidget, self).__init__(xmlui, value, name, parent, read_only=read_only)
f91e7028e2c3 memory (params), tools (xml_tools), plugins, frontends: add "int" parameter type with "min" and "max" attributes
souliane <souliane@mailoo.org>
parents: 1199
diff changeset
1199
f91e7028e2c3 memory (params), tools (xml_tools), plugins, frontends: add "int" parameter type with "min" and "max" attributes
souliane <souliane@mailoo.org>
parents: 1199
diff changeset
1200
802
9007bb133009 core, frontends: XMLUI refactoring:
Goffi <goffi@goffi.org>
parents: 800
diff changeset
1201 class BoolWidget(InputWidget):
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
1202 type = "bool"
802
9007bb133009 core, frontends: XMLUI refactoring:
Goffi <goffi@goffi.org>
parents: 800
diff changeset
1203
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
1204 def __init__(self, xmlui, value="false", name=None, parent=None, read_only=False):
910
9754c0ebadba core (XMLUI): BoolWidget now accept real boolean for value
Goffi <goffi@goffi.org>
parents: 905
diff changeset
1205 if isinstance(value, bool):
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
1206 value = "true" if value else "false"
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
1207 elif value == "0":
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
1208 value = "false"
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
1209 elif value == "1":
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
1210 value = "true"
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
1211 if value not in ("true", "false"):
802
9007bb133009 core, frontends: XMLUI refactoring:
Goffi <goffi@goffi.org>
parents: 800
diff changeset
1212 raise exceptions.DataError(_("Value must be 0, 1, false or true"))
1084
03dcb6ca7e49 core (XMLUI): created a read_only attribute for InputWidget + new JidInputWidget
Goffi <goffi@goffi.org>
parents: 1078
diff changeset
1213 super(BoolWidget, self).__init__(xmlui, name, parent, read_only=read_only)
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
1214 self.elem.setAttribute("value", value)
802
9007bb133009 core, frontends: XMLUI refactoring:
Goffi <goffi@goffi.org>
parents: 800
diff changeset
1215
9007bb133009 core, frontends: XMLUI refactoring:
Goffi <goffi@goffi.org>
parents: 800
diff changeset
1216
804
5174657b3378 XMLUI (core, frontends): added JidWidget and DividerWidget + popup type + some bugfixes:
Goffi <goffi@goffi.org>
parents: 803
diff changeset
1217 class ButtonWidget(Widget):
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
1218 type = "button"
802
9007bb133009 core, frontends: XMLUI refactoring:
Goffi <goffi@goffi.org>
parents: 800
diff changeset
1219
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
1220 def __init__(
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
1221 self, xmlui, callback_id, value=None, fields_back=None, name=None, parent=None
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
1222 ):
802
9007bb133009 core, frontends: XMLUI refactoring:
Goffi <goffi@goffi.org>
parents: 800
diff changeset
1223 """Add a button
1472
c005c212b538 xmlui: minor docstring fixes
Goffi <goffi@goffi.org>
parents: 1456
diff changeset
1224
802
9007bb133009 core, frontends: XMLUI refactoring:
Goffi <goffi@goffi.org>
parents: 800
diff changeset
1225 @param callback_id: callback which will be called if button is pressed
9007bb133009 core, frontends: XMLUI refactoring:
Goffi <goffi@goffi.org>
parents: 800
diff changeset
1226 @param value: label of the button
1487
0df627d0b4ca tools (xmlui): changeContainer do not create a new one when the current one already fits
souliane <souliane@mailoo.org>
parents: 1486
diff changeset
1227 @param fields_back: list of names of field to give back when pushing the button
802
9007bb133009 core, frontends: XMLUI refactoring:
Goffi <goffi@goffi.org>
parents: 800
diff changeset
1228 @param name: name
9007bb133009 core, frontends: XMLUI refactoring:
Goffi <goffi@goffi.org>
parents: 800
diff changeset
1229 @param parent: parent container
630
0b914394e74f core: added advanced list to XMLUI (need improvment, very basic so far)
Goffi <goffi@goffi.org>
parents: 615
diff changeset
1230 """
802
9007bb133009 core, frontends: XMLUI refactoring:
Goffi <goffi@goffi.org>
parents: 800
diff changeset
1231 if fields_back is None:
9007bb133009 core, frontends: XMLUI refactoring:
Goffi <goffi@goffi.org>
parents: 800
diff changeset
1232 fields_back = []
9007bb133009 core, frontends: XMLUI refactoring:
Goffi <goffi@goffi.org>
parents: 800
diff changeset
1233 super(ButtonWidget, self).__init__(xmlui, name, parent)
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
1234 self.elem.setAttribute("callback", callback_id)
802
9007bb133009 core, frontends: XMLUI refactoring:
Goffi <goffi@goffi.org>
parents: 800
diff changeset
1235 if value:
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
1236 self.elem.setAttribute("value", value)
802
9007bb133009 core, frontends: XMLUI refactoring:
Goffi <goffi@goffi.org>
parents: 800
diff changeset
1237 for field in fields_back:
968
75f3b3b430ff tools, frontends, memory: param definition and XMLUI handle multi-selection for list widgets:
souliane <souliane@mailoo.org>
parents: 910
diff changeset
1238 FieldBackElement(self, field)
802
9007bb133009 core, frontends: XMLUI refactoring:
Goffi <goffi@goffi.org>
parents: 800
diff changeset
1239
9007bb133009 core, frontends: XMLUI refactoring:
Goffi <goffi@goffi.org>
parents: 800
diff changeset
1240
9007bb133009 core, frontends: XMLUI refactoring:
Goffi <goffi@goffi.org>
parents: 800
diff changeset
1241 class ListWidget(InputWidget):
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
1242 type = "list"
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2958
diff changeset
1243 STYLES = ("multi", "noselect", "extensible", "reducible", "inline")
630
0b914394e74f core: added advanced list to XMLUI (need improvment, very basic so far)
Goffi <goffi@goffi.org>
parents: 615
diff changeset
1244
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
1245 def __init__(
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
1246 self, xmlui, options, selected=None, styles=None, name=None, parent=None
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
1247 ):
968
75f3b3b430ff tools, frontends, memory: param definition and XMLUI handle multi-selection for list widgets:
souliane <souliane@mailoo.org>
parents: 910
diff changeset
1248 """
75f3b3b430ff tools, frontends, memory: param definition and XMLUI handle multi-selection for list widgets:
souliane <souliane@mailoo.org>
parents: 910
diff changeset
1249
75f3b3b430ff tools, frontends, memory: param definition and XMLUI handle multi-selection for list widgets:
souliane <souliane@mailoo.org>
parents: 910
diff changeset
1250 @param xmlui
1141
7fcafc3206b1 plugin OTR: authentication management + fixed a bug in setState (due to a wrong docstring in potr.context.getCurrentTrust)
Goffi <goffi@goffi.org>
parents: 1105
diff changeset
1251 @param options (list[option]): each option can be given as:
7fcafc3206b1 plugin OTR: authentication management + fixed a bug in setState (due to a wrong docstring in potr.context.getCurrentTrust)
Goffi <goffi@goffi.org>
parents: 1105
diff changeset
1252 - a single string if the label and the value are the same
2625
a55a14c3cbf4 plugin XEP-0070: use a confirm dialog instead of a form + simplified a bit the code
Goffi <goffi@goffi.org>
parents: 2624
diff changeset
1253 - a tuple with a couple of string (value,label) if the label and the
a55a14c3cbf4 plugin XEP-0070: use a confirm dialog instead of a form + simplified a bit the code
Goffi <goffi@goffi.org>
parents: 2624
diff changeset
1254 value differ
968
75f3b3b430ff tools, frontends, memory: param definition and XMLUI handle multi-selection for list widgets:
souliane <souliane@mailoo.org>
parents: 910
diff changeset
1255 @param selected (list[string]): list of the selected values
1476
48706f4ff19c core (xmlui): added JidsListWidget to manage editable list of jids:
Goffi <goffi@goffi.org>
parents: 1475
diff changeset
1256 @param styles (iterable[string]): flags to set the behaviour of the list
2071
c2fdee1bd908 core (stdui): fixed cancellation of ContactList forms
Goffi <goffi@goffi.org>
parents: 2044
diff changeset
1257 can be:
2397
7fff98d64ab5 core (XMLUI), template(XMLUI): added flags to ListWidget:
Goffi <goffi@goffi.org>
parents: 2386
diff changeset
1258 - multi: multiple selection is allowed
7fff98d64ab5 core (XMLUI), template(XMLUI): added flags to ListWidget:
Goffi <goffi@goffi.org>
parents: 2386
diff changeset
1259 - noselect: no selection is allowed
7fff98d64ab5 core (XMLUI), template(XMLUI): added flags to ListWidget:
Goffi <goffi@goffi.org>
parents: 2386
diff changeset
1260 useful when only the list itself is needed
7fff98d64ab5 core (XMLUI), template(XMLUI): added flags to ListWidget:
Goffi <goffi@goffi.org>
parents: 2386
diff changeset
1261 - extensible: can be extended by user (i.e. new options can be added)
7fff98d64ab5 core (XMLUI), template(XMLUI): added flags to ListWidget:
Goffi <goffi@goffi.org>
parents: 2386
diff changeset
1262 - reducible: can be reduced by user (i.e. options can be removed)
2625
a55a14c3cbf4 plugin XEP-0070: use a confirm dialog instead of a form + simplified a bit the code
Goffi <goffi@goffi.org>
parents: 2624
diff changeset
1263 - inline: hint that this list should be displayed on a single line
a55a14c3cbf4 plugin XEP-0070: use a confirm dialog instead of a form + simplified a bit the code
Goffi <goffi@goffi.org>
parents: 2624
diff changeset
1264 (e.g. list of labels)
968
75f3b3b430ff tools, frontends, memory: param definition and XMLUI handle multi-selection for list widgets:
souliane <souliane@mailoo.org>
parents: 910
diff changeset
1265 @param name (string)
75f3b3b430ff tools, frontends, memory: param definition and XMLUI handle multi-selection for list widgets:
souliane <souliane@mailoo.org>
parents: 910
diff changeset
1266 @param parent
75f3b3b430ff tools, frontends, memory: param definition and XMLUI handle multi-selection for list widgets:
souliane <souliane@mailoo.org>
parents: 910
diff changeset
1267 """
1476
48706f4ff19c core (xmlui): added JidsListWidget to manage editable list of jids:
Goffi <goffi@goffi.org>
parents: 1475
diff changeset
1268 styles = set() if styles is None else set(styles)
48706f4ff19c core (xmlui): added JidsListWidget to manage editable list of jids:
Goffi <goffi@goffi.org>
parents: 1475
diff changeset
1269 if styles is None:
48706f4ff19c core (xmlui): added JidsListWidget to manage editable list of jids:
Goffi <goffi@goffi.org>
parents: 1475
diff changeset
1270 styles = set()
48706f4ff19c core (xmlui): added JidsListWidget to manage editable list of jids:
Goffi <goffi@goffi.org>
parents: 1475
diff changeset
1271 else:
48706f4ff19c core (xmlui): added JidsListWidget to manage editable list of jids:
Goffi <goffi@goffi.org>
parents: 1475
diff changeset
1272 styles = set(styles)
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2958
diff changeset
1273 if "noselect" in styles and ("multi" in styles or selected):
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
1274 raise exceptions.DataError(
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
1275 _(
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2958
diff changeset
1276 '"multi" flag and "selected" option are not compatible with '
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2958
diff changeset
1277 '"noselect" flag'
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
1278 )
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
1279 )
820
462f2052af26 core(XMLUI): empty options in ListWidget now just show a warning message, no more assert
Goffi <goffi@goffi.org>
parents: 811
diff changeset
1280 if not options:
2364
918e38622a48 core (xmlui): added method to convert data form result to XMLUI:
Goffi <goffi@goffi.org>
parents: 2361
diff changeset
1281 # we can have no options if we get a submitted data form
918e38622a48 core (xmlui): added method to convert data form result to XMLUI:
Goffi <goffi@goffi.org>
parents: 2361
diff changeset
1282 # but we can't use submitted values directly,
918e38622a48 core (xmlui): added method to convert data form result to XMLUI:
Goffi <goffi@goffi.org>
parents: 2361
diff changeset
1283 # because we would not have the labels
993
301b342c697a core: use of the new core.log module:
Goffi <goffi@goffi.org>
parents: 981
diff changeset
1284 log.warning(_('empty "options" list'))
802
9007bb133009 core, frontends: XMLUI refactoring:
Goffi <goffi@goffi.org>
parents: 800
diff changeset
1285 super(ListWidget, self).__init__(xmlui, name, parent)
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 4032
diff changeset
1286 self.add_options(options, selected)
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 4032
diff changeset
1287 self.set_styles(styles)
802
9007bb133009 core, frontends: XMLUI refactoring:
Goffi <goffi@goffi.org>
parents: 800
diff changeset
1288
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 4032
diff changeset
1289 def add_options(self, options, selected=None):
4270
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4110
diff changeset
1290 """Add options to a multi-values element (e.g. list)"""
968
75f3b3b430ff tools, frontends, memory: param definition and XMLUI handle multi-selection for list widgets:
souliane <souliane@mailoo.org>
parents: 910
diff changeset
1291 if selected:
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2958
diff changeset
1292 if isinstance(selected, str):
968
75f3b3b430ff tools, frontends, memory: param definition and XMLUI handle multi-selection for list widgets:
souliane <souliane@mailoo.org>
parents: 910
diff changeset
1293 selected = [selected]
75f3b3b430ff tools, frontends, memory: param definition and XMLUI handle multi-selection for list widgets:
souliane <souliane@mailoo.org>
parents: 910
diff changeset
1294 else:
75f3b3b430ff tools, frontends, memory: param definition and XMLUI handle multi-selection for list widgets:
souliane <souliane@mailoo.org>
parents: 910
diff changeset
1295 selected = []
802
9007bb133009 core, frontends: XMLUI refactoring:
Goffi <goffi@goffi.org>
parents: 800
diff changeset
1296 for option in options:
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2958
diff changeset
1297 assert isinstance(option, str) or isinstance(option, tuple)
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2958
diff changeset
1298 value = option if isinstance(option, str) else option[0]
1067
f7f15d44fdfa tools (xml_tools): dataForm2XMLUI handles the default value for "list-single" fields
souliane <souliane@mailoo.org>
parents: 1036
diff changeset
1299 OptionElement(self, option, value in selected)
802
9007bb133009 core, frontends: XMLUI refactoring:
Goffi <goffi@goffi.org>
parents: 800
diff changeset
1300
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 4032
diff changeset
1301 def set_styles(self, styles):
2397
7fff98d64ab5 core (XMLUI), template(XMLUI): added flags to ListWidget:
Goffi <goffi@goffi.org>
parents: 2386
diff changeset
1302 if not styles.issubset(self.STYLES):
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2958
diff changeset
1303 raise exceptions.DataError(_("invalid styles"))
2397
7fff98d64ab5 core (XMLUI), template(XMLUI): added flags to ListWidget:
Goffi <goffi@goffi.org>
parents: 2386
diff changeset
1304 for style in styles:
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
1305 self.elem.setAttribute(style, "yes")
2397
7fff98d64ab5 core (XMLUI), template(XMLUI): added flags to ListWidget:
Goffi <goffi@goffi.org>
parents: 2386
diff changeset
1306 # TODO: check flags incompatibily (noselect and multi) like in __init__
7fff98d64ab5 core (XMLUI), template(XMLUI): added flags to ListWidget:
Goffi <goffi@goffi.org>
parents: 2386
diff changeset
1307
7fff98d64ab5 core (XMLUI), template(XMLUI): added flags to ListWidget:
Goffi <goffi@goffi.org>
parents: 2386
diff changeset
1308 def setStyle(self, style):
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 4032
diff changeset
1309 self.set_styles([style])
2397
7fff98d64ab5 core (XMLUI), template(XMLUI): added flags to ListWidget:
Goffi <goffi@goffi.org>
parents: 2386
diff changeset
1310
2446
bfd1e9d737c4 core (XMLUI): added value property for StringWidget and ListWidget
Goffi <goffi@goffi.org>
parents: 2429
diff changeset
1311 @property
bfd1e9d737c4 core (XMLUI): added value property for StringWidget and ListWidget
Goffi <goffi@goffi.org>
parents: 2429
diff changeset
1312 def value(self):
bfd1e9d737c4 core (XMLUI): added value property for StringWidget and ListWidget
Goffi <goffi@goffi.org>
parents: 2429
diff changeset
1313 """Return the value of first selected option"""
bfd1e9d737c4 core (XMLUI): added value property for StringWidget and ListWidget
Goffi <goffi@goffi.org>
parents: 2429
diff changeset
1314 for child in self.elem.childNodes:
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2958
diff changeset
1315 if child.tagName == "option" and child.getAttribute("selected") == "true":
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2958
diff changeset
1316 return child.getAttribute("value")
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2958
diff changeset
1317 return ""
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
1318
2397
7fff98d64ab5 core (XMLUI), template(XMLUI): added flags to ListWidget:
Goffi <goffi@goffi.org>
parents: 2386
diff changeset
1319
1476
48706f4ff19c core (xmlui): added JidsListWidget to manage editable list of jids:
Goffi <goffi@goffi.org>
parents: 1475
diff changeset
1320 class JidsListWidget(InputWidget):
48706f4ff19c core (xmlui): added JidsListWidget to manage editable list of jids:
Goffi <goffi@goffi.org>
parents: 1475
diff changeset
1321 """A list of text or jids where elements can be added/removed or modified"""
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
1322
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
1323 type = "jids_list"
1476
48706f4ff19c core (xmlui): added JidsListWidget to manage editable list of jids:
Goffi <goffi@goffi.org>
parents: 1475
diff changeset
1324
48706f4ff19c core (xmlui): added JidsListWidget to manage editable list of jids:
Goffi <goffi@goffi.org>
parents: 1475
diff changeset
1325 def __init__(self, xmlui, jids, styles=None, name=None, parent=None):
48706f4ff19c core (xmlui): added JidsListWidget to manage editable list of jids:
Goffi <goffi@goffi.org>
parents: 1475
diff changeset
1326 """
48706f4ff19c core (xmlui): added JidsListWidget to manage editable list of jids:
Goffi <goffi@goffi.org>
parents: 1475
diff changeset
1327
48706f4ff19c core (xmlui): added JidsListWidget to manage editable list of jids:
Goffi <goffi@goffi.org>
parents: 1475
diff changeset
1328 @param xmlui
48706f4ff19c core (xmlui): added JidsListWidget to manage editable list of jids:
Goffi <goffi@goffi.org>
parents: 1475
diff changeset
1329 @param jids (list[jid.JID]): base jids
48706f4ff19c core (xmlui): added JidsListWidget to manage editable list of jids:
Goffi <goffi@goffi.org>
parents: 1475
diff changeset
1330 @param styles (iterable[string]): flags to set the behaviour of the list
48706f4ff19c core (xmlui): added JidsListWidget to manage editable list of jids:
Goffi <goffi@goffi.org>
parents: 1475
diff changeset
1331 @param name (string)
48706f4ff19c core (xmlui): added JidsListWidget to manage editable list of jids:
Goffi <goffi@goffi.org>
parents: 1475
diff changeset
1332 @param parent
48706f4ff19c core (xmlui): added JidsListWidget to manage editable list of jids:
Goffi <goffi@goffi.org>
parents: 1475
diff changeset
1333 """
48706f4ff19c core (xmlui): added JidsListWidget to manage editable list of jids:
Goffi <goffi@goffi.org>
parents: 1475
diff changeset
1334 super(JidsListWidget, self).__init__(xmlui, name, parent)
48706f4ff19c core (xmlui): added JidsListWidget to manage editable list of jids:
Goffi <goffi@goffi.org>
parents: 1475
diff changeset
1335 styles = set() if styles is None else set(styles)
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
1336 if not styles.issubset([]): # TODO
1476
48706f4ff19c core (xmlui): added JidsListWidget to manage editable list of jids:
Goffi <goffi@goffi.org>
parents: 1475
diff changeset
1337 raise exceptions.DataError(_("invalid styles"))
48706f4ff19c core (xmlui): added JidsListWidget to manage editable list of jids:
Goffi <goffi@goffi.org>
parents: 1475
diff changeset
1338 for style in styles:
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
1339 self.elem.setAttribute(style, "yes")
1476
48706f4ff19c core (xmlui): added JidsListWidget to manage editable list of jids:
Goffi <goffi@goffi.org>
parents: 1475
diff changeset
1340 if not jids:
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
1341 log.debug("empty jids list")
1476
48706f4ff19c core (xmlui): added JidsListWidget to manage editable list of jids:
Goffi <goffi@goffi.org>
parents: 1475
diff changeset
1342 else:
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 4032
diff changeset
1343 self.add_jids(jids)
1476
48706f4ff19c core (xmlui): added JidsListWidget to manage editable list of jids:
Goffi <goffi@goffi.org>
parents: 1475
diff changeset
1344
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 4032
diff changeset
1345 def add_jids(self, jids):
1476
48706f4ff19c core (xmlui): added JidsListWidget to manage editable list of jids:
Goffi <goffi@goffi.org>
parents: 1475
diff changeset
1346 for jid_ in jids:
48706f4ff19c core (xmlui): added JidsListWidget to manage editable list of jids:
Goffi <goffi@goffi.org>
parents: 1475
diff changeset
1347 JidElement(self, jid_)
48706f4ff19c core (xmlui): added JidsListWidget to manage editable list of jids:
Goffi <goffi@goffi.org>
parents: 1475
diff changeset
1348
802
9007bb133009 core, frontends: XMLUI refactoring:
Goffi <goffi@goffi.org>
parents: 800
diff changeset
1349
1105
018bdd687747 core (XMLUI): Dialogs are now managemed in XMLUI:
Goffi <goffi@goffi.org>
parents: 1086
diff changeset
1350 ## Dialog Elements ##
018bdd687747 core (XMLUI): Dialogs are now managemed in XMLUI:
Goffi <goffi@goffi.org>
parents: 1086
diff changeset
1351
018bdd687747 core (XMLUI): Dialogs are now managemed in XMLUI:
Goffi <goffi@goffi.org>
parents: 1086
diff changeset
1352
018bdd687747 core (XMLUI): Dialogs are now managemed in XMLUI:
Goffi <goffi@goffi.org>
parents: 1086
diff changeset
1353 class DialogElement(Element):
4270
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4110
diff changeset
1354 """Main dialog element"""
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
1355
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
1356 type = "dialog"
1105
018bdd687747 core (XMLUI): Dialogs are now managemed in XMLUI:
Goffi <goffi@goffi.org>
parents: 1086
diff changeset
1357
018bdd687747 core (XMLUI): Dialogs are now managemed in XMLUI:
Goffi <goffi@goffi.org>
parents: 1086
diff changeset
1358 def __init__(self, parent, type_, level=None):
018bdd687747 core (XMLUI): Dialogs are now managemed in XMLUI:
Goffi <goffi@goffi.org>
parents: 1086
diff changeset
1359 if not isinstance(parent, TopElement):
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
1360 raise exceptions.DataError(
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
1361 _("DialogElement must be a direct child of TopElement")
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
1362 )
1105
018bdd687747 core (XMLUI): Dialogs are now managemed in XMLUI:
Goffi <goffi@goffi.org>
parents: 1086
diff changeset
1363 super(DialogElement, self).__init__(parent.xmlui, parent)
018bdd687747 core (XMLUI): Dialogs are now managemed in XMLUI:
Goffi <goffi@goffi.org>
parents: 1086
diff changeset
1364 self.elem.setAttribute(C.XMLUI_DATA_TYPE, type_)
018bdd687747 core (XMLUI): Dialogs are now managemed in XMLUI:
Goffi <goffi@goffi.org>
parents: 1086
diff changeset
1365 self.elem.setAttribute(C.XMLUI_DATA_LVL, level or C.XMLUI_DATA_LVL_DEFAULT)
018bdd687747 core (XMLUI): Dialogs are now managemed in XMLUI:
Goffi <goffi@goffi.org>
parents: 1086
diff changeset
1366
018bdd687747 core (XMLUI): Dialogs are now managemed in XMLUI:
Goffi <goffi@goffi.org>
parents: 1086
diff changeset
1367
018bdd687747 core (XMLUI): Dialogs are now managemed in XMLUI:
Goffi <goffi@goffi.org>
parents: 1086
diff changeset
1368 class MessageElement(Element):
018bdd687747 core (XMLUI): Dialogs are now managemed in XMLUI:
Goffi <goffi@goffi.org>
parents: 1086
diff changeset
1369 """Element with the instruction message"""
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
1370
1105
018bdd687747 core (XMLUI): Dialogs are now managemed in XMLUI:
Goffi <goffi@goffi.org>
parents: 1086
diff changeset
1371 type = C.XMLUI_DATA_MESS
018bdd687747 core (XMLUI): Dialogs are now managemed in XMLUI:
Goffi <goffi@goffi.org>
parents: 1086
diff changeset
1372
018bdd687747 core (XMLUI): Dialogs are now managemed in XMLUI:
Goffi <goffi@goffi.org>
parents: 1086
diff changeset
1373 def __init__(self, parent, message):
018bdd687747 core (XMLUI): Dialogs are now managemed in XMLUI:
Goffi <goffi@goffi.org>
parents: 1086
diff changeset
1374 if not isinstance(parent, DialogElement):
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
1375 raise exceptions.DataError(
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
1376 _("MessageElement must be a direct child of DialogElement")
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
1377 )
1105
018bdd687747 core (XMLUI): Dialogs are now managemed in XMLUI:
Goffi <goffi@goffi.org>
parents: 1086
diff changeset
1378 super(MessageElement, self).__init__(parent.xmlui, parent)
018bdd687747 core (XMLUI): Dialogs are now managemed in XMLUI:
Goffi <goffi@goffi.org>
parents: 1086
diff changeset
1379 message_txt = self.xmlui.doc.createTextNode(message)
018bdd687747 core (XMLUI): Dialogs are now managemed in XMLUI:
Goffi <goffi@goffi.org>
parents: 1086
diff changeset
1380 self.elem.appendChild(message_txt)
018bdd687747 core (XMLUI): Dialogs are now managemed in XMLUI:
Goffi <goffi@goffi.org>
parents: 1086
diff changeset
1381
018bdd687747 core (XMLUI): Dialogs are now managemed in XMLUI:
Goffi <goffi@goffi.org>
parents: 1086
diff changeset
1382
018bdd687747 core (XMLUI): Dialogs are now managemed in XMLUI:
Goffi <goffi@goffi.org>
parents: 1086
diff changeset
1383 class ButtonsElement(Element):
018bdd687747 core (XMLUI): Dialogs are now managemed in XMLUI:
Goffi <goffi@goffi.org>
parents: 1086
diff changeset
1384 """Buttons element which indicate which set to use"""
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
1385
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
1386 type = "buttons"
1105
018bdd687747 core (XMLUI): Dialogs are now managemed in XMLUI:
Goffi <goffi@goffi.org>
parents: 1086
diff changeset
1387
018bdd687747 core (XMLUI): Dialogs are now managemed in XMLUI:
Goffi <goffi@goffi.org>
parents: 1086
diff changeset
1388 def __init__(self, parent, set_):
018bdd687747 core (XMLUI): Dialogs are now managemed in XMLUI:
Goffi <goffi@goffi.org>
parents: 1086
diff changeset
1389 if not isinstance(parent, DialogElement):
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
1390 raise exceptions.DataError(
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
1391 _("ButtonsElement must be a direct child of DialogElement")
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
1392 )
1105
018bdd687747 core (XMLUI): Dialogs are now managemed in XMLUI:
Goffi <goffi@goffi.org>
parents: 1086
diff changeset
1393 super(ButtonsElement, self).__init__(parent.xmlui, parent)
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
1394 self.elem.setAttribute("set", set_)
1105
018bdd687747 core (XMLUI): Dialogs are now managemed in XMLUI:
Goffi <goffi@goffi.org>
parents: 1086
diff changeset
1395
018bdd687747 core (XMLUI): Dialogs are now managemed in XMLUI:
Goffi <goffi@goffi.org>
parents: 1086
diff changeset
1396
018bdd687747 core (XMLUI): Dialogs are now managemed in XMLUI:
Goffi <goffi@goffi.org>
parents: 1086
diff changeset
1397 class FileElement(Element):
018bdd687747 core (XMLUI): Dialogs are now managemed in XMLUI:
Goffi <goffi@goffi.org>
parents: 1086
diff changeset
1398 """File element used for FileDialog"""
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
1399
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
1400 type = "file"
1105
018bdd687747 core (XMLUI): Dialogs are now managemed in XMLUI:
Goffi <goffi@goffi.org>
parents: 1086
diff changeset
1401
018bdd687747 core (XMLUI): Dialogs are now managemed in XMLUI:
Goffi <goffi@goffi.org>
parents: 1086
diff changeset
1402 def __init__(self, parent, type_):
018bdd687747 core (XMLUI): Dialogs are now managemed in XMLUI:
Goffi <goffi@goffi.org>
parents: 1086
diff changeset
1403 if not isinstance(parent, DialogElement):
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
1404 raise exceptions.DataError(
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
1405 _("FileElement must be a direct child of DialogElement")
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
1406 )
1105
018bdd687747 core (XMLUI): Dialogs are now managemed in XMLUI:
Goffi <goffi@goffi.org>
parents: 1086
diff changeset
1407 super(FileElement, self).__init__(parent.xmlui, parent)
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
1408 self.elem.setAttribute("type", type_)
1105
018bdd687747 core (XMLUI): Dialogs are now managemed in XMLUI:
Goffi <goffi@goffi.org>
parents: 1086
diff changeset
1409
018bdd687747 core (XMLUI): Dialogs are now managemed in XMLUI:
Goffi <goffi@goffi.org>
parents: 1086
diff changeset
1410
802
9007bb133009 core, frontends: XMLUI refactoring:
Goffi <goffi@goffi.org>
parents: 800
diff changeset
1411 ## XMLUI main class
630
0b914394e74f core: added advanced list to XMLUI (need improvment, very basic so far)
Goffi <goffi@goffi.org>
parents: 615
diff changeset
1412
0b914394e74f core: added advanced list to XMLUI (need improvment, very basic so far)
Goffi <goffi@goffi.org>
parents: 615
diff changeset
1413
588
beaf6bec2fcd Remove every old-style class.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 587
diff changeset
1414 class XMLUI(object):
103
6be927a465ed XMLUI refactoring, step 1
Goffi <goffi@goffi.org>
parents: 102
diff changeset
1415 """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
1416
4270
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4110
diff changeset
1417 def __init__(
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4110
diff changeset
1418 self,
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4110
diff changeset
1419 panel_type="window",
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4110
diff changeset
1420 container="vertical",
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4110
diff changeset
1421 dialog_opt=None,
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4110
diff changeset
1422 title=None,
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4110
diff changeset
1423 submit_id=None,
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4110
diff changeset
1424 session_id=None,
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4110
diff changeset
1425 ):
103
6be927a465ed XMLUI refactoring, step 1
Goffi <goffi@goffi.org>
parents: 102
diff changeset
1426 """Init SàT XML Panel
1105
018bdd687747 core (XMLUI): Dialogs are now managemed in XMLUI:
Goffi <goffi@goffi.org>
parents: 1086
diff changeset
1427
103
6be927a465ed XMLUI refactoring, step 1
Goffi <goffi@goffi.org>
parents: 102
diff changeset
1428 @param panel_type: one of
1105
018bdd687747 core (XMLUI): Dialogs are now managemed in XMLUI:
Goffi <goffi@goffi.org>
parents: 1086
diff changeset
1429 - C.XMLUI_WINDOW (new window)
018bdd687747 core (XMLUI): Dialogs are now managemed in XMLUI:
Goffi <goffi@goffi.org>
parents: 1086
diff changeset
1430 - C.XMLUI_POPUP
2625
a55a14c3cbf4 plugin XEP-0070: use a confirm dialog instead of a form + simplified a bit the code
Goffi <goffi@goffi.org>
parents: 2624
diff changeset
1431 - C.XMLUI_FORM (form, depend of the frontend, usually a panel with
a55a14c3cbf4 plugin XEP-0070: use a confirm dialog instead of a form + simplified a bit the code
Goffi <goffi@goffi.org>
parents: 2624
diff changeset
1432 cancel/submit buttons)
1105
018bdd687747 core (XMLUI): Dialogs are now managemed in XMLUI:
Goffi <goffi@goffi.org>
parents: 1086
diff changeset
1433 - C.XMLUI_PARAM (parameters, presentation depend of the frontend)
018bdd687747 core (XMLUI): Dialogs are now managemed in XMLUI:
Goffi <goffi@goffi.org>
parents: 1086
diff changeset
1434 - C.XMLUI_DIALOG (one common dialog, presentation depend of frontend)
802
9007bb133009 core, frontends: XMLUI refactoring:
Goffi <goffi@goffi.org>
parents: 800
diff changeset
1435 @param container: disposition of elements, one of:
107
5ae370c71803 CS: message sending is now working
Goffi <goffi@goffi.org>
parents: 106
diff changeset
1436 - vertical: elements are disposed up to bottom
5ae370c71803 CS: message sending is now working
Goffi <goffi@goffi.org>
parents: 106
diff changeset
1437 - horizontal: elements are disposed left to right
5ae370c71803 CS: message sending is now working
Goffi <goffi@goffi.org>
parents: 106
diff changeset
1438 - pairs: elements come on two aligned columns
103
6be927a465ed XMLUI refactoring, step 1
Goffi <goffi@goffi.org>
parents: 102
diff changeset
1439 (usually one for a label, the next for the element)
2361
5defafc8ede6 core, frontends (xmlui): new LabelContainer:
Goffi <goffi@goffi.org>
parents: 2108
diff changeset
1440 - label: associations of one LabelWidget or EmptyWidget with an other widget
2625
a55a14c3cbf4 plugin XEP-0070: use a confirm dialog instead of a form + simplified a bit the code
Goffi <goffi@goffi.org>
parents: 2624
diff changeset
1441 similar to pairs but specialized in LabelWidget,
a55a14c3cbf4 plugin XEP-0070: use a confirm dialog instead of a form + simplified a bit the code
Goffi <goffi@goffi.org>
parents: 2624
diff changeset
1442 and not necessarily arranged in 2 columns
107
5ae370c71803 CS: message sending is now working
Goffi <goffi@goffi.org>
parents: 106
diff changeset
1443 - tabs: elemens are in categories with tabs (notebook)
2625
a55a14c3cbf4 plugin XEP-0070: use a confirm dialog instead of a form + simplified a bit the code
Goffi <goffi@goffi.org>
parents: 2624
diff changeset
1444 @param dialog_opt: only used if panel_type == C.XMLUI_DIALOG.
a55a14c3cbf4 plugin XEP-0070: use a confirm dialog instead of a form + simplified a bit the code
Goffi <goffi@goffi.org>
parents: 2624
diff changeset
1445 Dictionnary (string/string) where key can be:
1105
018bdd687747 core (XMLUI): Dialogs are now managemed in XMLUI:
Goffi <goffi@goffi.org>
parents: 1086
diff changeset
1446 - C.XMLUI_DATA_TYPE: type of dialog, value can be:
2625
a55a14c3cbf4 plugin XEP-0070: use a confirm dialog instead of a form + simplified a bit the code
Goffi <goffi@goffi.org>
parents: 2624
diff changeset
1447 - C.XMLUI_DIALOG_MESSAGE (default): an information/error message.
a55a14c3cbf4 plugin XEP-0070: use a confirm dialog instead of a form + simplified a bit the code
Goffi <goffi@goffi.org>
parents: 2624
diff changeset
1448 Action of user is necessary to close the dialog.
a55a14c3cbf4 plugin XEP-0070: use a confirm dialog instead of a form + simplified a bit the code
Goffi <goffi@goffi.org>
parents: 2624
diff changeset
1449 Usually the frontend display a classic popup.
a55a14c3cbf4 plugin XEP-0070: use a confirm dialog instead of a form + simplified a bit the code
Goffi <goffi@goffi.org>
parents: 2624
diff changeset
1450 - C.XMLUI_DIALOG_NOTE: like a C.XMLUI_DIALOG_MESSAGE, but action of user
a55a14c3cbf4 plugin XEP-0070: use a confirm dialog instead of a form + simplified a bit the code
Goffi <goffi@goffi.org>
parents: 2624
diff changeset
1451 is not necessary to close, at frontend choice (it can be closed after
a55a14c3cbf4 plugin XEP-0070: use a confirm dialog instead of a form + simplified a bit the code
Goffi <goffi@goffi.org>
parents: 2624
diff changeset
1452 a timeout). Usually the frontend display as a timed out notification
1105
018bdd687747 core (XMLUI): Dialogs are now managemed in XMLUI:
Goffi <goffi@goffi.org>
parents: 1086
diff changeset
1453 - C.XMLUI_DIALOG_CONFIRM: dialog with 2 choices (usualy "Ok"/"Cancel").
018bdd687747 core (XMLUI): Dialogs are now managemed in XMLUI:
Goffi <goffi@goffi.org>
parents: 1086
diff changeset
1454 returned data can contain:
2625
a55a14c3cbf4 plugin XEP-0070: use a confirm dialog instead of a form + simplified a bit the code
Goffi <goffi@goffi.org>
parents: 2624
diff changeset
1455 - "answer": "true" if answer is "ok", "yes" or equivalent,
a55a14c3cbf4 plugin XEP-0070: use a confirm dialog instead of a form + simplified a bit the code
Goffi <goffi@goffi.org>
parents: 2624
diff changeset
1456 "false" else
1105
018bdd687747 core (XMLUI): Dialogs are now managemed in XMLUI:
Goffi <goffi@goffi.org>
parents: 1086
diff changeset
1457 - C.XLMUI_DIALOG_FILE: a file selection dialog
018bdd687747 core (XMLUI): Dialogs are now managemed in XMLUI:
Goffi <goffi@goffi.org>
parents: 1086
diff changeset
1458 returned data can contain:
2625
a55a14c3cbf4 plugin XEP-0070: use a confirm dialog instead of a form + simplified a bit the code
Goffi <goffi@goffi.org>
parents: 2624
diff changeset
1459 - "cancelled": "true" if dialog has been cancelled, not present
a55a14c3cbf4 plugin XEP-0070: use a confirm dialog instead of a form + simplified a bit the code
Goffi <goffi@goffi.org>
parents: 2624
diff changeset
1460 or "false" else
1105
018bdd687747 core (XMLUI): Dialogs are now managemed in XMLUI:
Goffi <goffi@goffi.org>
parents: 1086
diff changeset
1461 - "path": path of the choosed file/dir
018bdd687747 core (XMLUI): Dialogs are now managemed in XMLUI:
Goffi <goffi@goffi.org>
parents: 1086
diff changeset
1462 - C.XMLUI_DATA_MESS: message shown in dialog
018bdd687747 core (XMLUI): Dialogs are now managemed in XMLUI:
Goffi <goffi@goffi.org>
parents: 1086
diff changeset
1463 - C.XMLUI_DATA_LVL: one of:
018bdd687747 core (XMLUI): Dialogs are now managemed in XMLUI:
Goffi <goffi@goffi.org>
parents: 1086
diff changeset
1464 - C.XMLUI_DATA_LVL_INFO (default): normal message
018bdd687747 core (XMLUI): Dialogs are now managemed in XMLUI:
Goffi <goffi@goffi.org>
parents: 1086
diff changeset
1465 - C.XMLUI_DATA_LVL_WARNING: attention of user is important
018bdd687747 core (XMLUI): Dialogs are now managemed in XMLUI:
Goffi <goffi@goffi.org>
parents: 1086
diff changeset
1466 - C.XMLUI_DATA_LVL_ERROR: something went wrong
018bdd687747 core (XMLUI): Dialogs are now managemed in XMLUI:
Goffi <goffi@goffi.org>
parents: 1086
diff changeset
1467 - C.XMLUI_DATA_BTNS_SET: one of:
2625
a55a14c3cbf4 plugin XEP-0070: use a confirm dialog instead of a form + simplified a bit the code
Goffi <goffi@goffi.org>
parents: 2624
diff changeset
1468 - C.XMLUI_DATA_BTNS_SET_OKCANCEL (default): classical "OK" and "Cancel"
a55a14c3cbf4 plugin XEP-0070: use a confirm dialog instead of a form + simplified a bit the code
Goffi <goffi@goffi.org>
parents: 2624
diff changeset
1469 set
a55a14c3cbf4 plugin XEP-0070: use a confirm dialog instead of a form + simplified a bit the code
Goffi <goffi@goffi.org>
parents: 2624
diff changeset
1470 - C.XMLUI_DATA_BTNS_SET_YESNO: a translated "yes" for OK, and "no" for
a55a14c3cbf4 plugin XEP-0070: use a confirm dialog instead of a form + simplified a bit the code
Goffi <goffi@goffi.org>
parents: 2624
diff changeset
1471 Cancel
1521
d2ab9c62ac3a core (xmlui): deferDialog can now manage additional dialog options through the "options" argument
Goffi <goffi@goffi.org>
parents: 1507
diff changeset
1472 - C.XMLUI_DATA_FILETYPE: only used for file dialogs, one of:
d2ab9c62ac3a core (xmlui): deferDialog can now manage additional dialog options through the "options" argument
Goffi <goffi@goffi.org>
parents: 1507
diff changeset
1473 - C.XMLUI_DATA_FILETYPE_FILE: a file path is requested
d2ab9c62ac3a core (xmlui): deferDialog can now manage additional dialog options through the "options" argument
Goffi <goffi@goffi.org>
parents: 1507
diff changeset
1474 - C.XMLUI_DATA_FILETYPE_DIR: a dir path is requested
d2ab9c62ac3a core (xmlui): deferDialog can now manage additional dialog options through the "options" argument
Goffi <goffi@goffi.org>
parents: 1507
diff changeset
1475 - C.XMLUI_DATA_FILETYPE_DEFAULT: same as C.XMLUI_DATA_FILETYPE_FILE
d2ab9c62ac3a core (xmlui): deferDialog can now manage additional dialog options through the "options" argument
Goffi <goffi@goffi.org>
parents: 1507
diff changeset
1476
107
5ae370c71803 CS: message sending is now working
Goffi <goffi@goffi.org>
parents: 106
diff changeset
1477 @param title: title or default if None
2625
a55a14c3cbf4 plugin XEP-0070: use a confirm dialog instead of a form + simplified a bit the code
Goffi <goffi@goffi.org>
parents: 2624
diff changeset
1478 @param submit_id: callback id to call for panel_type we can submit (form, param,
a55a14c3cbf4 plugin XEP-0070: use a confirm dialog instead of a form + simplified a bit the code
Goffi <goffi@goffi.org>
parents: 2624
diff changeset
1479 dialog)
a55a14c3cbf4 plugin XEP-0070: use a confirm dialog instead of a form + simplified a bit the code
Goffi <goffi@goffi.org>
parents: 2624
diff changeset
1480 @param session_id: use to keep a session attached to the dialog, must be
a55a14c3cbf4 plugin XEP-0070: use a confirm dialog instead of a form + simplified a bit the code
Goffi <goffi@goffi.org>
parents: 2624
diff changeset
1481 returned by frontends
2398
3ff9d7a7fe71 core (XMLUI): filters can now be used when converting data form to XMLUI:
Goffi <goffi@goffi.org>
parents: 2397
diff changeset
1482 @attribute named_widgets(dict): map from name to widget
103
6be927a465ed XMLUI refactoring, step 1
Goffi <goffi@goffi.org>
parents: 102
diff changeset
1483 """
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
1484 if panel_type not in [
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
1485 C.XMLUI_WINDOW,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
1486 C.XMLUI_FORM,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
1487 C.XMLUI_PARAM,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
1488 C.XMLUI_POPUP,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
1489 C.XMLUI_DIALOG,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
1490 ]:
760
73a0077f80cc backend, frontends: XMLUI refactoring:
Goffi <goffi@goffi.org>
parents: 759
diff changeset
1491 raise exceptions.DataError(_("Unknown panel type [%s]") % panel_type)
1105
018bdd687747 core (XMLUI): Dialogs are now managemed in XMLUI:
Goffi <goffi@goffi.org>
parents: 1086
diff changeset
1492 if panel_type == C.XMLUI_FORM and submit_id is None:
760
73a0077f80cc backend, frontends: XMLUI refactoring:
Goffi <goffi@goffi.org>
parents: 759
diff changeset
1493 raise exceptions.DataError(_("form XMLUI need a submit_id"))
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2958
diff changeset
1494 if not isinstance(container, str):
802
9007bb133009 core, frontends: XMLUI refactoring:
Goffi <goffi@goffi.org>
parents: 800
diff changeset
1495 raise exceptions.DataError(_("container argument must be a string"))
1105
018bdd687747 core (XMLUI): Dialogs are now managemed in XMLUI:
Goffi <goffi@goffi.org>
parents: 1086
diff changeset
1496 if dialog_opt is not None and panel_type != C.XMLUI_DIALOG:
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
1497 raise exceptions.DataError(
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
1498 _("dialog_opt can only be used with dialog panels")
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
1499 )
802
9007bb133009 core, frontends: XMLUI refactoring:
Goffi <goffi@goffi.org>
parents: 800
diff changeset
1500 self.type = panel_type
103
6be927a465ed XMLUI refactoring, step 1
Goffi <goffi@goffi.org>
parents: 102
diff changeset
1501 impl = minidom.getDOMImplementation()
6be927a465ed XMLUI refactoring, step 1
Goffi <goffi@goffi.org>
parents: 102
diff changeset
1502
6be927a465ed XMLUI refactoring, step 1
Goffi <goffi@goffi.org>
parents: 102
diff changeset
1503 self.doc = impl.createDocument(None, "sat_xmlui", None)
6be927a465ed XMLUI refactoring, step 1
Goffi <goffi@goffi.org>
parents: 102
diff changeset
1504 top_element = self.doc.documentElement
6be927a465ed XMLUI refactoring, step 1
Goffi <goffi@goffi.org>
parents: 102
diff changeset
1505 top_element.setAttribute("type", panel_type)
107
5ae370c71803 CS: message sending is now working
Goffi <goffi@goffi.org>
parents: 106
diff changeset
1506 if title:
5ae370c71803 CS: message sending is now working
Goffi <goffi@goffi.org>
parents: 106
diff changeset
1507 top_element.setAttribute("title", title)
1530
94cd4d242dc5 core (XMLUI): restorer submit_id:
Goffi <goffi@goffi.org>
parents: 1521
diff changeset
1508 self.submit_id = submit_id
798
8f5479f8709a core: XMLUI now use @property for session_id and submit
Goffi <goffi@goffi.org>
parents: 796
diff changeset
1509 self.session_id = session_id
1105
018bdd687747 core (XMLUI): Dialogs are now managemed in XMLUI:
Goffi <goffi@goffi.org>
parents: 1086
diff changeset
1510 if panel_type == C.XMLUI_DIALOG:
018bdd687747 core (XMLUI): Dialogs are now managemed in XMLUI:
Goffi <goffi@goffi.org>
parents: 1086
diff changeset
1511 if dialog_opt is None:
018bdd687747 core (XMLUI): Dialogs are now managemed in XMLUI:
Goffi <goffi@goffi.org>
parents: 1086
diff changeset
1512 dialog_opt = {}
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 4032
diff changeset
1513 self._create_dialog(dialog_opt)
1105
018bdd687747 core (XMLUI): Dialogs are now managemed in XMLUI:
Goffi <goffi@goffi.org>
parents: 1086
diff changeset
1514 return
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 4032
diff changeset
1515 self.main_container = self._create_container(container, TopElement(self))
802
9007bb133009 core, frontends: XMLUI refactoring:
Goffi <goffi@goffi.org>
parents: 800
diff changeset
1516 self.current_container = self.main_container
2398
3ff9d7a7fe71 core (XMLUI): filters can now be used when converting data form to XMLUI:
Goffi <goffi@goffi.org>
parents: 2397
diff changeset
1517 self.named_widgets = {}
802
9007bb133009 core, frontends: XMLUI refactoring:
Goffi <goffi@goffi.org>
parents: 800
diff changeset
1518
2762
5a51c7fc74a5 core (XMLUI): small optimisation: introspection is done once at module loading instead of on each XMLUI instantiation
Goffi <goffi@goffi.org>
parents: 2741
diff changeset
1519 @staticmethod
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 4032
diff changeset
1520 def creator_wrapper(widget_cls, is_input):
2762
5a51c7fc74a5 core (XMLUI): small optimisation: introspection is done once at module loading instead of on each XMLUI instantiation
Goffi <goffi@goffi.org>
parents: 2741
diff changeset
1521 # TODO: once moved to Python 3, use functools.partialmethod and
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 4032
diff changeset
1522 # remove the creator_wrapper
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 4032
diff changeset
1523 def create_widget(self, *args, **kwargs):
2762
5a51c7fc74a5 core (XMLUI): small optimisation: introspection is done once at module loading instead of on each XMLUI instantiation
Goffi <goffi@goffi.org>
parents: 2741
diff changeset
1524 if self.type == C.XMLUI_DIALOG:
4270
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4110
diff changeset
1525 raise exceptions.InternalError(
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4110
diff changeset
1526 _("create_widget can't be used with dialogs")
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4110
diff changeset
1527 )
2762
5a51c7fc74a5 core (XMLUI): small optimisation: introspection is done once at module loading instead of on each XMLUI instantiation
Goffi <goffi@goffi.org>
parents: 2741
diff changeset
1528 if "parent" not in kwargs:
5a51c7fc74a5 core (XMLUI): small optimisation: introspection is done once at module loading instead of on each XMLUI instantiation
Goffi <goffi@goffi.org>
parents: 2741
diff changeset
1529 kwargs["parent"] = self.current_container
5a51c7fc74a5 core (XMLUI): small optimisation: introspection is done once at module loading instead of on each XMLUI instantiation
Goffi <goffi@goffi.org>
parents: 2741
diff changeset
1530 if "name" not in kwargs and is_input:
5a51c7fc74a5 core (XMLUI): small optimisation: introspection is done once at module loading instead of on each XMLUI instantiation
Goffi <goffi@goffi.org>
parents: 2741
diff changeset
1531 # name can be given as first argument or in keyword
5a51c7fc74a5 core (XMLUI): small optimisation: introspection is done once at module loading instead of on each XMLUI instantiation
Goffi <goffi@goffi.org>
parents: 2741
diff changeset
1532 # arguments for InputWidgets
5a51c7fc74a5 core (XMLUI): small optimisation: introspection is done once at module loading instead of on each XMLUI instantiation
Goffi <goffi@goffi.org>
parents: 2741
diff changeset
1533 args = list(args)
5a51c7fc74a5 core (XMLUI): small optimisation: introspection is done once at module loading instead of on each XMLUI instantiation
Goffi <goffi@goffi.org>
parents: 2741
diff changeset
1534 kwargs["name"] = args.pop(0)
5a51c7fc74a5 core (XMLUI): small optimisation: introspection is done once at module loading instead of on each XMLUI instantiation
Goffi <goffi@goffi.org>
parents: 2741
diff changeset
1535 return widget_cls(self, *args, **kwargs)
4270
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4110
diff changeset
1536
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 4032
diff changeset
1537 return create_widget
2762
5a51c7fc74a5 core (XMLUI): small optimisation: introspection is done once at module loading instead of on each XMLUI instantiation
Goffi <goffi@goffi.org>
parents: 2741
diff changeset
1538
5a51c7fc74a5 core (XMLUI): small optimisation: introspection is done once at module loading instead of on each XMLUI instantiation
Goffi <goffi@goffi.org>
parents: 2741
diff changeset
1539 @classmethod
5a51c7fc74a5 core (XMLUI): small optimisation: introspection is done once at module loading instead of on each XMLUI instantiation
Goffi <goffi@goffi.org>
parents: 2741
diff changeset
1540 def _introspect(cls):
4270
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4110
diff changeset
1541 """Introspect module to find Widgets and Containers, and create addXXX methods"""
2762
5a51c7fc74a5 core (XMLUI): small optimisation: introspection is done once at module loading instead of on each XMLUI instantiation
Goffi <goffi@goffi.org>
parents: 2741
diff changeset
1542 # FIXME: we can't log anything because this file is used
5a51c7fc74a5 core (XMLUI): small optimisation: introspection is done once at module loading instead of on each XMLUI instantiation
Goffi <goffi@goffi.org>
parents: 2741
diff changeset
1543 # in bin/sat script then evaluated
5a51c7fc74a5 core (XMLUI): small optimisation: introspection is done once at module loading instead of on each XMLUI instantiation
Goffi <goffi@goffi.org>
parents: 2741
diff changeset
1544 # bin/sat should be refactored
5a51c7fc74a5 core (XMLUI): small optimisation: introspection is done once at module loading instead of on each XMLUI instantiation
Goffi <goffi@goffi.org>
parents: 2741
diff changeset
1545 # log.debug(u'introspecting XMLUI widgets and containers')
5a51c7fc74a5 core (XMLUI): small optimisation: introspection is done once at module loading instead of on each XMLUI instantiation
Goffi <goffi@goffi.org>
parents: 2741
diff changeset
1546 cls._containers = {}
5a51c7fc74a5 core (XMLUI): small optimisation: introspection is done once at module loading instead of on each XMLUI instantiation
Goffi <goffi@goffi.org>
parents: 2741
diff changeset
1547 cls._widgets = {}
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2958
diff changeset
1548 for obj in list(globals().values()):
802
9007bb133009 core, frontends: XMLUI refactoring:
Goffi <goffi@goffi.org>
parents: 800
diff changeset
1549 try:
9007bb133009 core, frontends: XMLUI refactoring:
Goffi <goffi@goffi.org>
parents: 800
diff changeset
1550 if issubclass(obj, Widget):
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
1551 if obj.__name__ == "Widget":
802
9007bb133009 core, frontends: XMLUI refactoring:
Goffi <goffi@goffi.org>
parents: 800
diff changeset
1552 continue
2762
5a51c7fc74a5 core (XMLUI): small optimisation: introspection is done once at module loading instead of on each XMLUI instantiation
Goffi <goffi@goffi.org>
parents: 2741
diff changeset
1553 cls._widgets[obj.type] = obj
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2958
diff changeset
1554 creator_name = "add" + obj.__name__
4270
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4110
diff changeset
1555 if creator_name.endswith("Widget"):
2762
5a51c7fc74a5 core (XMLUI): small optimisation: introspection is done once at module loading instead of on each XMLUI instantiation
Goffi <goffi@goffi.org>
parents: 2741
diff changeset
1556 creator_name = creator_name[:-6]
5a51c7fc74a5 core (XMLUI): small optimisation: introspection is done once at module loading instead of on each XMLUI instantiation
Goffi <goffi@goffi.org>
parents: 2741
diff changeset
1557 is_input = issubclass(obj, InputWidget)
5a51c7fc74a5 core (XMLUI): small optimisation: introspection is done once at module loading instead of on each XMLUI instantiation
Goffi <goffi@goffi.org>
parents: 2741
diff changeset
1558 # FIXME: cf. above comment
5a51c7fc74a5 core (XMLUI): small optimisation: introspection is done once at module loading instead of on each XMLUI instantiation
Goffi <goffi@goffi.org>
parents: 2741
diff changeset
1559 # log.debug(u"Adding {creator_name} creator (is_input={is_input}))"
5a51c7fc74a5 core (XMLUI): small optimisation: introspection is done once at module loading instead of on each XMLUI instantiation
Goffi <goffi@goffi.org>
parents: 2741
diff changeset
1560 # .format(creator_name=creator_name, is_input=is_input))
5a51c7fc74a5 core (XMLUI): small optimisation: introspection is done once at module loading instead of on each XMLUI instantiation
Goffi <goffi@goffi.org>
parents: 2741
diff changeset
1561
5a51c7fc74a5 core (XMLUI): small optimisation: introspection is done once at module loading instead of on each XMLUI instantiation
Goffi <goffi@goffi.org>
parents: 2741
diff changeset
1562 assert not hasattr(cls, creator_name)
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 4032
diff changeset
1563 # XXX: we need to use creator_wrapper because we are in a loop
2762
5a51c7fc74a5 core (XMLUI): small optimisation: introspection is done once at module loading instead of on each XMLUI instantiation
Goffi <goffi@goffi.org>
parents: 2741
diff changeset
1564 # and Python 2 doesn't support default values in kwargs
5a51c7fc74a5 core (XMLUI): small optimisation: introspection is done once at module loading instead of on each XMLUI instantiation
Goffi <goffi@goffi.org>
parents: 2741
diff changeset
1565 # when using *args, **kwargs
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 4032
diff changeset
1566 setattr(cls, creator_name, cls.creator_wrapper(obj, is_input))
2762
5a51c7fc74a5 core (XMLUI): small optimisation: introspection is done once at module loading instead of on each XMLUI instantiation
Goffi <goffi@goffi.org>
parents: 2741
diff changeset
1567
802
9007bb133009 core, frontends: XMLUI refactoring:
Goffi <goffi@goffi.org>
parents: 800
diff changeset
1568 elif issubclass(obj, Container):
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
1569 if obj.__name__ == "Container":
802
9007bb133009 core, frontends: XMLUI refactoring:
Goffi <goffi@goffi.org>
parents: 800
diff changeset
1570 continue
2762
5a51c7fc74a5 core (XMLUI): small optimisation: introspection is done once at module loading instead of on each XMLUI instantiation
Goffi <goffi@goffi.org>
parents: 2741
diff changeset
1571 cls._containers[obj.type] = obj
802
9007bb133009 core, frontends: XMLUI refactoring:
Goffi <goffi@goffi.org>
parents: 800
diff changeset
1572 except TypeError:
9007bb133009 core, frontends: XMLUI refactoring:
Goffi <goffi@goffi.org>
parents: 800
diff changeset
1573 pass
103
6be927a465ed XMLUI refactoring, step 1
Goffi <goffi@goffi.org>
parents: 102
diff changeset
1574
6be927a465ed XMLUI refactoring, step 1
Goffi <goffi@goffi.org>
parents: 102
diff changeset
1575 def __del__(self):
587
952322b1d490 Remove trailing whitespaces.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 572
diff changeset
1576 self.doc.unlink()
952322b1d490 Remove trailing whitespaces.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 572
diff changeset
1577
798
8f5479f8709a core: XMLUI now use @property for session_id and submit
Goffi <goffi@goffi.org>
parents: 796
diff changeset
1578 @property
8f5479f8709a core: XMLUI now use @property for session_id and submit
Goffi <goffi@goffi.org>
parents: 796
diff changeset
1579 def submit_id(self):
8f5479f8709a core: XMLUI now use @property for session_id and submit
Goffi <goffi@goffi.org>
parents: 796
diff changeset
1580 top_element = self.doc.documentElement
1530
94cd4d242dc5 core (XMLUI): restorer submit_id:
Goffi <goffi@goffi.org>
parents: 1521
diff changeset
1581 if not top_element.hasAttribute("submit"):
94cd4d242dc5 core (XMLUI): restorer submit_id:
Goffi <goffi@goffi.org>
parents: 1521
diff changeset
1582 # getAttribute never return None (it return empty string it attribute doesn't exists)
94cd4d242dc5 core (XMLUI): restorer submit_id:
Goffi <goffi@goffi.org>
parents: 1521
diff changeset
1583 # so we have to manage None here
94cd4d242dc5 core (XMLUI): restorer submit_id:
Goffi <goffi@goffi.org>
parents: 1521
diff changeset
1584 return None
798
8f5479f8709a core: XMLUI now use @property for session_id and submit
Goffi <goffi@goffi.org>
parents: 796
diff changeset
1585 value = top_element.getAttribute("submit")
1530
94cd4d242dc5 core (XMLUI): restorer submit_id:
Goffi <goffi@goffi.org>
parents: 1521
diff changeset
1586 return value
798
8f5479f8709a core: XMLUI now use @property for session_id and submit
Goffi <goffi@goffi.org>
parents: 796
diff changeset
1587
8f5479f8709a core: XMLUI now use @property for session_id and submit
Goffi <goffi@goffi.org>
parents: 796
diff changeset
1588 @submit_id.setter
8f5479f8709a core: XMLUI now use @property for session_id and submit
Goffi <goffi@goffi.org>
parents: 796
diff changeset
1589 def submit_id(self, value):
760
73a0077f80cc backend, frontends: XMLUI refactoring:
Goffi <goffi@goffi.org>
parents: 759
diff changeset
1590 top_element = self.doc.documentElement
798
8f5479f8709a core: XMLUI now use @property for session_id and submit
Goffi <goffi@goffi.org>
parents: 796
diff changeset
1591 if value is None:
8f5479f8709a core: XMLUI now use @property for session_id and submit
Goffi <goffi@goffi.org>
parents: 796
diff changeset
1592 try:
8f5479f8709a core: XMLUI now use @property for session_id and submit
Goffi <goffi@goffi.org>
parents: 796
diff changeset
1593 top_element.removeAttribute("submit")
8f5479f8709a core: XMLUI now use @property for session_id and submit
Goffi <goffi@goffi.org>
parents: 796
diff changeset
1594 except NotFoundErr:
8f5479f8709a core: XMLUI now use @property for session_id and submit
Goffi <goffi@goffi.org>
parents: 796
diff changeset
1595 pass
1531
51dec65ec62c core (xmlui): fixed submit_id returning None when its value is ''
Goffi <goffi@goffi.org>
parents: 1530
diff changeset
1596 else: # submit_id can be the empty string to bypass form restriction
798
8f5479f8709a core: XMLUI now use @property for session_id and submit
Goffi <goffi@goffi.org>
parents: 796
diff changeset
1597 top_element.setAttribute("submit", value)
760
73a0077f80cc backend, frontends: XMLUI refactoring:
Goffi <goffi@goffi.org>
parents: 759
diff changeset
1598
798
8f5479f8709a core: XMLUI now use @property for session_id and submit
Goffi <goffi@goffi.org>
parents: 796
diff changeset
1599 @property
8f5479f8709a core: XMLUI now use @property for session_id and submit
Goffi <goffi@goffi.org>
parents: 796
diff changeset
1600 def session_id(self):
8f5479f8709a core: XMLUI now use @property for session_id and submit
Goffi <goffi@goffi.org>
parents: 796
diff changeset
1601 top_element = self.doc.documentElement
8f5479f8709a core: XMLUI now use @property for session_id and submit
Goffi <goffi@goffi.org>
parents: 796
diff changeset
1602 value = top_element.getAttribute("session_id")
8f5479f8709a core: XMLUI now use @property for session_id and submit
Goffi <goffi@goffi.org>
parents: 796
diff changeset
1603 return value or None
8f5479f8709a core: XMLUI now use @property for session_id and submit
Goffi <goffi@goffi.org>
parents: 796
diff changeset
1604
8f5479f8709a core: XMLUI now use @property for session_id and submit
Goffi <goffi@goffi.org>
parents: 796
diff changeset
1605 @session_id.setter
8f5479f8709a core: XMLUI now use @property for session_id and submit
Goffi <goffi@goffi.org>
parents: 796
diff changeset
1606 def session_id(self, value):
8f5479f8709a core: XMLUI now use @property for session_id and submit
Goffi <goffi@goffi.org>
parents: 796
diff changeset
1607 top_element = self.doc.documentElement
8f5479f8709a core: XMLUI now use @property for session_id and submit
Goffi <goffi@goffi.org>
parents: 796
diff changeset
1608 if value is None:
8f5479f8709a core: XMLUI now use @property for session_id and submit
Goffi <goffi@goffi.org>
parents: 796
diff changeset
1609 try:
8f5479f8709a core: XMLUI now use @property for session_id and submit
Goffi <goffi@goffi.org>
parents: 796
diff changeset
1610 top_element.removeAttribute("session_id")
8f5479f8709a core: XMLUI now use @property for session_id and submit
Goffi <goffi@goffi.org>
parents: 796
diff changeset
1611 except NotFoundErr:
8f5479f8709a core: XMLUI now use @property for session_id and submit
Goffi <goffi@goffi.org>
parents: 796
diff changeset
1612 pass
8f5479f8709a core: XMLUI now use @property for session_id and submit
Goffi <goffi@goffi.org>
parents: 796
diff changeset
1613 elif value:
8f5479f8709a core: XMLUI now use @property for session_id and submit
Goffi <goffi@goffi.org>
parents: 796
diff changeset
1614 top_element.setAttribute("session_id", value)
8f5479f8709a core: XMLUI now use @property for session_id and submit
Goffi <goffi@goffi.org>
parents: 796
diff changeset
1615 else:
8f5479f8709a core: XMLUI now use @property for session_id and submit
Goffi <goffi@goffi.org>
parents: 796
diff changeset
1616 raise exceptions.DataError("session_id can't be empty")
760
73a0077f80cc backend, frontends: XMLUI refactoring:
Goffi <goffi@goffi.org>
parents: 759
diff changeset
1617
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 4032
diff changeset
1618 def _create_dialog(self, dialog_opt):
1105
018bdd687747 core (XMLUI): Dialogs are now managemed in XMLUI:
Goffi <goffi@goffi.org>
parents: 1086
diff changeset
1619 dialog_type = dialog_opt.setdefault(C.XMLUI_DATA_TYPE, C.XMLUI_DIALOG_MESSAGE)
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
1620 if (
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
1621 dialog_type in [C.XMLUI_DIALOG_CONFIRM, C.XMLUI_DIALOG_FILE]
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
1622 and self.submit_id is None
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
1623 ):
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
1624 raise exceptions.InternalError(
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
1625 _("Submit ID must be filled for this kind of dialog")
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
1626 )
1105
018bdd687747 core (XMLUI): Dialogs are now managemed in XMLUI:
Goffi <goffi@goffi.org>
parents: 1086
diff changeset
1627 top_element = TopElement(self)
018bdd687747 core (XMLUI): Dialogs are now managemed in XMLUI:
Goffi <goffi@goffi.org>
parents: 1086
diff changeset
1628 level = dialog_opt.get(C.XMLUI_DATA_LVL)
018bdd687747 core (XMLUI): Dialogs are now managemed in XMLUI:
Goffi <goffi@goffi.org>
parents: 1086
diff changeset
1629 dialog_elt = DialogElement(top_element, dialog_type, level)
018bdd687747 core (XMLUI): Dialogs are now managemed in XMLUI:
Goffi <goffi@goffi.org>
parents: 1086
diff changeset
1630
018bdd687747 core (XMLUI): Dialogs are now managemed in XMLUI:
Goffi <goffi@goffi.org>
parents: 1086
diff changeset
1631 try:
018bdd687747 core (XMLUI): Dialogs are now managemed in XMLUI:
Goffi <goffi@goffi.org>
parents: 1086
diff changeset
1632 MessageElement(dialog_elt, dialog_opt[C.XMLUI_DATA_MESS])
018bdd687747 core (XMLUI): Dialogs are now managemed in XMLUI:
Goffi <goffi@goffi.org>
parents: 1086
diff changeset
1633 except KeyError:
018bdd687747 core (XMLUI): Dialogs are now managemed in XMLUI:
Goffi <goffi@goffi.org>
parents: 1086
diff changeset
1634 pass
018bdd687747 core (XMLUI): Dialogs are now managemed in XMLUI:
Goffi <goffi@goffi.org>
parents: 1086
diff changeset
1635
018bdd687747 core (XMLUI): Dialogs are now managemed in XMLUI:
Goffi <goffi@goffi.org>
parents: 1086
diff changeset
1636 try:
018bdd687747 core (XMLUI): Dialogs are now managemed in XMLUI:
Goffi <goffi@goffi.org>
parents: 1086
diff changeset
1637 ButtonsElement(dialog_elt, dialog_opt[C.XMLUI_DATA_BTNS_SET])
018bdd687747 core (XMLUI): Dialogs are now managemed in XMLUI:
Goffi <goffi@goffi.org>
parents: 1086
diff changeset
1638 except KeyError:
018bdd687747 core (XMLUI): Dialogs are now managemed in XMLUI:
Goffi <goffi@goffi.org>
parents: 1086
diff changeset
1639 pass
018bdd687747 core (XMLUI): Dialogs are now managemed in XMLUI:
Goffi <goffi@goffi.org>
parents: 1086
diff changeset
1640
018bdd687747 core (XMLUI): Dialogs are now managemed in XMLUI:
Goffi <goffi@goffi.org>
parents: 1086
diff changeset
1641 try:
018bdd687747 core (XMLUI): Dialogs are now managemed in XMLUI:
Goffi <goffi@goffi.org>
parents: 1086
diff changeset
1642 FileElement(dialog_elt, dialog_opt[C.XMLUI_DATA_FILETYPE])
018bdd687747 core (XMLUI): Dialogs are now managemed in XMLUI:
Goffi <goffi@goffi.org>
parents: 1086
diff changeset
1643 except KeyError:
018bdd687747 core (XMLUI): Dialogs are now managemed in XMLUI:
Goffi <goffi@goffi.org>
parents: 1086
diff changeset
1644 pass
018bdd687747 core (XMLUI): Dialogs are now managemed in XMLUI:
Goffi <goffi@goffi.org>
parents: 1086
diff changeset
1645
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 4032
diff changeset
1646 def _create_container(self, container, parent=None, **kwargs):
802
9007bb133009 core, frontends: XMLUI refactoring:
Goffi <goffi@goffi.org>
parents: 800
diff changeset
1647 """Create a container element
1472
c005c212b538 xmlui: minor docstring fixes
Goffi <goffi@goffi.org>
parents: 1456
diff changeset
1648
802
9007bb133009 core, frontends: XMLUI refactoring:
Goffi <goffi@goffi.org>
parents: 800
diff changeset
1649 @param type: container type (cf init doc)
103
6be927a465ed XMLUI refactoring, step 1
Goffi <goffi@goffi.org>
parents: 102
diff changeset
1650 @parent: parent element or None
6be927a465ed XMLUI refactoring, step 1
Goffi <goffi@goffi.org>
parents: 102
diff changeset
1651 """
802
9007bb133009 core, frontends: XMLUI refactoring:
Goffi <goffi@goffi.org>
parents: 800
diff changeset
1652 if container not in self._containers:
9007bb133009 core, frontends: XMLUI refactoring:
Goffi <goffi@goffi.org>
parents: 800
diff changeset
1653 raise exceptions.DataError(_("Unknown container type [%s]") % container)
9007bb133009 core, frontends: XMLUI refactoring:
Goffi <goffi@goffi.org>
parents: 800
diff changeset
1654 cls = self._containers[container]
804
5174657b3378 XMLUI (core, frontends): added JidWidget and DividerWidget + popup type + some bugfixes:
Goffi <goffi@goffi.org>
parents: 803
diff changeset
1655 new_container = cls(self, parent=parent, **kwargs)
802
9007bb133009 core, frontends: XMLUI refactoring:
Goffi <goffi@goffi.org>
parents: 800
diff changeset
1656 return new_container
105
d2630fba8dfd params to XMLUI tools
Goffi <goffi@goffi.org>
parents: 104
diff changeset
1657
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 4032
diff changeset
1658 def change_container(self, container, **kwargs):
802
9007bb133009 core, frontends: XMLUI refactoring:
Goffi <goffi@goffi.org>
parents: 800
diff changeset
1659 """Change the current container
1472
c005c212b538 xmlui: minor docstring fixes
Goffi <goffi@goffi.org>
parents: 1456
diff changeset
1660
802
9007bb133009 core, frontends: XMLUI refactoring:
Goffi <goffi@goffi.org>
parents: 800
diff changeset
1661 @param container: either container type (container it then created),
9007bb133009 core, frontends: XMLUI refactoring:
Goffi <goffi@goffi.org>
parents: 800
diff changeset
1662 or an Container instance"""
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2958
diff changeset
1663 if isinstance(container, str):
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 4032
diff changeset
1664 self.current_container = self._create_container(
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
1665 container,
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 4032
diff changeset
1666 self.current_container.get_parent_container() or self.main_container,
4270
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4110
diff changeset
1667 **kwargs,
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
1668 )
802
9007bb133009 core, frontends: XMLUI refactoring:
Goffi <goffi@goffi.org>
parents: 800
diff changeset
1669 else:
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
1670 self.current_container = (
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
1671 self.main_container if container is None else container
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
1672 )
1475
7ac073d2e7e0 core(xmlui): removed useless parenthesis around assert
Goffi <goffi@goffi.org>
parents: 1473
diff changeset
1673 assert isinstance(self.current_container, Container)
802
9007bb133009 core, frontends: XMLUI refactoring:
Goffi <goffi@goffi.org>
parents: 800
diff changeset
1674 return self.current_container
103
6be927a465ed XMLUI refactoring, step 1
Goffi <goffi@goffi.org>
parents: 102
diff changeset
1675
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 4032
diff changeset
1676 def add_widget(self, type_, *args, **kwargs):
802
9007bb133009 core, frontends: XMLUI refactoring:
Goffi <goffi@goffi.org>
parents: 800
diff changeset
1677 """Convenience method to add an element"""
9007bb133009 core, frontends: XMLUI refactoring:
Goffi <goffi@goffi.org>
parents: 800
diff changeset
1678 if "parent" not in kwargs:
9007bb133009 core, frontends: XMLUI refactoring:
Goffi <goffi@goffi.org>
parents: 800
diff changeset
1679 kwargs["parent"] = self.current_container
2762
5a51c7fc74a5 core (XMLUI): small optimisation: introspection is done once at module loading instead of on each XMLUI instantiation
Goffi <goffi@goffi.org>
parents: 2741
diff changeset
1680 try:
5a51c7fc74a5 core (XMLUI): small optimisation: introspection is done once at module loading instead of on each XMLUI instantiation
Goffi <goffi@goffi.org>
parents: 2741
diff changeset
1681 cls = self._widgets[type_]
5a51c7fc74a5 core (XMLUI): small optimisation: introspection is done once at module loading instead of on each XMLUI instantiation
Goffi <goffi@goffi.org>
parents: 2741
diff changeset
1682 except KeyError:
5a51c7fc74a5 core (XMLUI): small optimisation: introspection is done once at module loading instead of on each XMLUI instantiation
Goffi <goffi@goffi.org>
parents: 2741
diff changeset
1683 raise exceptions.DataError(_("Invalid type [{type_}]").format(type_=type_))
802
9007bb133009 core, frontends: XMLUI refactoring:
Goffi <goffi@goffi.org>
parents: 800
diff changeset
1684 return cls(self, *args, **kwargs)
104
5458ac1380cc XMLUI: added tabs layout
Goffi <goffi@goffi.org>
parents: 103
diff changeset
1685
103
6be927a465ed XMLUI refactoring, step 1
Goffi <goffi@goffi.org>
parents: 102
diff changeset
1686 def toXml(self):
587
952322b1d490 Remove trailing whitespaces.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 572
diff changeset
1687 """return the XML representation of the panel"""
103
6be927a465ed XMLUI refactoring, step 1
Goffi <goffi@goffi.org>
parents: 102
diff changeset
1688 return self.doc.toxml()
289
0e54b1b0a8c8 xml_tools: added ElementParser for raw xml -> domish.Element conversion
Goffi <goffi@goffi.org>
parents: 228
diff changeset
1689
0e54b1b0a8c8 xml_tools: added ElementParser for raw xml -> domish.Element conversion
Goffi <goffi@goffi.org>
parents: 228
diff changeset
1690
2762
5a51c7fc74a5 core (XMLUI): small optimisation: introspection is done once at module loading instead of on each XMLUI instantiation
Goffi <goffi@goffi.org>
parents: 2741
diff changeset
1691 # we call this to have automatic discovery of containers and widgets
5a51c7fc74a5 core (XMLUI): small optimisation: introspection is done once at module loading instead of on each XMLUI instantiation
Goffi <goffi@goffi.org>
parents: 2741
diff changeset
1692 XMLUI._introspect()
5a51c7fc74a5 core (XMLUI): small optimisation: introspection is done once at module loading instead of on each XMLUI instantiation
Goffi <goffi@goffi.org>
parents: 2741
diff changeset
1693
5a51c7fc74a5 core (XMLUI): small optimisation: introspection is done once at module loading instead of on each XMLUI instantiation
Goffi <goffi@goffi.org>
parents: 2741
diff changeset
1694
1142
2f9f735334da core (XMLUI): sugar method "note" allow to easily create a XMLUI note dialog (more methods for other dialogs should follow)
Goffi <goffi@goffi.org>
parents: 1141
diff changeset
1695 # Some sugar for XMLUI dialogs
2f9f735334da core (XMLUI): sugar method "note" allow to easily create a XMLUI note dialog (more methods for other dialogs should follow)
Goffi <goffi@goffi.org>
parents: 1141
diff changeset
1696
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
1697
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
1698 def note(message, title="", level=C.XMLUI_DATA_LVL_INFO):
2044
4de202bdde05 core (xmlui): added a quickNote method to easily send notes
Goffi <goffi@goffi.org>
parents: 1934
diff changeset
1699 """sugar to easily create a Note Dialog
1142
2f9f735334da core (XMLUI): sugar method "note" allow to easily create a XMLUI note dialog (more methods for other dialogs should follow)
Goffi <goffi@goffi.org>
parents: 1141
diff changeset
1700
1504
a2e4b976e707 core (xmlui): added helper method to easily manage dialogs with a deferred
Goffi <goffi@goffi.org>
parents: 1500
diff changeset
1701 @param message(unicode): body of the note
a2e4b976e707 core (xmlui): added helper method to easily manage dialogs with a deferred
Goffi <goffi@goffi.org>
parents: 1500
diff changeset
1702 @param title(unicode): title of the note
a2e4b976e707 core (xmlui): added helper method to easily manage dialogs with a deferred
Goffi <goffi@goffi.org>
parents: 1500
diff changeset
1703 @param level(unicode): one of C.XMLUI_DATA_LVL_*
a2e4b976e707 core (xmlui): added helper method to easily manage dialogs with a deferred
Goffi <goffi@goffi.org>
parents: 1500
diff changeset
1704 @return(XMLUI): instance of XMLUI
1142
2f9f735334da core (XMLUI): sugar method "note" allow to easily create a XMLUI note dialog (more methods for other dialogs should follow)
Goffi <goffi@goffi.org>
parents: 1141
diff changeset
1705 """
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
1706 note_xmlui = XMLUI(
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
1707 C.XMLUI_DIALOG,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
1708 dialog_opt={
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
1709 C.XMLUI_DATA_TYPE: C.XMLUI_DIALOG_NOTE,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
1710 C.XMLUI_DATA_MESS: message,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
1711 C.XMLUI_DATA_LVL: level,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
1712 },
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
1713 title=title,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
1714 )
1142
2f9f735334da core (XMLUI): sugar method "note" allow to easily create a XMLUI note dialog (more methods for other dialogs should follow)
Goffi <goffi@goffi.org>
parents: 1141
diff changeset
1715 return note_xmlui
2f9f735334da core (XMLUI): sugar method "note" allow to easily create a XMLUI note dialog (more methods for other dialogs should follow)
Goffi <goffi@goffi.org>
parents: 1141
diff changeset
1716
1692
0fdd8fe34fbf core (xmlui): new deferredUI method allows to manage XMLUI with a Deferred insteand of manual submit_id handling:
Goffi <goffi@goffi.org>
parents: 1645
diff changeset
1717
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 4032
diff changeset
1718 def quick_note(host, client, message, title="", level=C.XMLUI_DATA_LVL_INFO):
2044
4de202bdde05 core (xmlui): added a quickNote method to easily send notes
Goffi <goffi@goffi.org>
parents: 1934
diff changeset
1719 """more sugar to do the whole note process"""
4de202bdde05 core (xmlui): added a quickNote method to easily send notes
Goffi <goffi@goffi.org>
parents: 1934
diff changeset
1720 note_ui = note(message, title, level)
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 4032
diff changeset
1721 host.action_new({"xmlui": note_ui.toXml()}, profile=client.profile)
2044
4de202bdde05 core (xmlui): added a quickNote method to easily send notes
Goffi <goffi@goffi.org>
parents: 1934
diff changeset
1722
4de202bdde05 core (xmlui): added a quickNote method to easily send notes
Goffi <goffi@goffi.org>
parents: 1934
diff changeset
1723
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 4032
diff changeset
1724 def deferred_ui(host, xmlui, chained=False):
4110
b274f0d5c138 tools (common/data_objects): handle messages without body
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
1725 """Create a deferred linked to XMLUI
1692
0fdd8fe34fbf core (xmlui): new deferredUI method allows to manage XMLUI with a Deferred insteand of manual submit_id handling:
Goffi <goffi@goffi.org>
parents: 1645
diff changeset
1726
0fdd8fe34fbf core (xmlui): new deferredUI method allows to manage XMLUI with a Deferred insteand of manual submit_id handling:
Goffi <goffi@goffi.org>
parents: 1645
diff changeset
1727 @param xmlui(XMLUI): instance of the XMLUI
0fdd8fe34fbf core (xmlui): new deferredUI method allows to manage XMLUI with a Deferred insteand of manual submit_id handling:
Goffi <goffi@goffi.org>
parents: 1645
diff changeset
1728 Must be an XMLUI that you can submit, with submit_id set to ''
0fdd8fe34fbf core (xmlui): new deferredUI method allows to manage XMLUI with a Deferred insteand of manual submit_id handling:
Goffi <goffi@goffi.org>
parents: 1645
diff changeset
1729 @param chained(bool): True if the Deferred result must be returned to the frontend
0fdd8fe34fbf core (xmlui): new deferredUI method allows to manage XMLUI with a Deferred insteand of manual submit_id handling:
Goffi <goffi@goffi.org>
parents: 1645
diff changeset
1730 useful when backend is in a series of dialogs with an ui
0fdd8fe34fbf core (xmlui): new deferredUI method allows to manage XMLUI with a Deferred insteand of manual submit_id handling:
Goffi <goffi@goffi.org>
parents: 1645
diff changeset
1731 @return (D(data)): a deferred which fire the data
0fdd8fe34fbf core (xmlui): new deferredUI method allows to manage XMLUI with a Deferred insteand of manual submit_id handling:
Goffi <goffi@goffi.org>
parents: 1645
diff changeset
1732 """
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
1733 assert xmlui.submit_id == ""
1692
0fdd8fe34fbf core (xmlui): new deferredUI method allows to manage XMLUI with a Deferred insteand of manual submit_id handling:
Goffi <goffi@goffi.org>
parents: 1645
diff changeset
1734 xmlui_d = defer.Deferred()
0fdd8fe34fbf core (xmlui): new deferredUI method allows to manage XMLUI with a Deferred insteand of manual submit_id handling:
Goffi <goffi@goffi.org>
parents: 1645
diff changeset
1735
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 4032
diff changeset
1736 def on_submit(data, profile):
1692
0fdd8fe34fbf core (xmlui): new deferredUI method allows to manage XMLUI with a Deferred insteand of manual submit_id handling:
Goffi <goffi@goffi.org>
parents: 1645
diff changeset
1737 xmlui_d.callback(data)
0fdd8fe34fbf core (xmlui): new deferredUI method allows to manage XMLUI with a Deferred insteand of manual submit_id handling:
Goffi <goffi@goffi.org>
parents: 1645
diff changeset
1738 return xmlui_d if chained else {}
0fdd8fe34fbf core (xmlui): new deferredUI method allows to manage XMLUI with a Deferred insteand of manual submit_id handling:
Goffi <goffi@goffi.org>
parents: 1645
diff changeset
1739
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 4032
diff changeset
1740 xmlui.submit_id = host.register_callback(on_submit, with_data=True, one_shot=True)
1692
0fdd8fe34fbf core (xmlui): new deferredUI method allows to manage XMLUI with a Deferred insteand of manual submit_id handling:
Goffi <goffi@goffi.org>
parents: 1645
diff changeset
1741 return xmlui_d
0fdd8fe34fbf core (xmlui): new deferredUI method allows to manage XMLUI with a Deferred insteand of manual submit_id handling:
Goffi <goffi@goffi.org>
parents: 1645
diff changeset
1742
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
1743
4270
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4110
diff changeset
1744 def defer_xmlui(
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4110
diff changeset
1745 host,
4299
d2deddd6df44 tools (xml_tools): add `namespace` argument to `xmlui_result_to_elt`:
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
1746 xmlui: XMLUI,
d2deddd6df44 tools (xml_tools): add `namespace` argument to `xmlui_result_to_elt`:
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
1747 action_extra: dict | None = None,
d2deddd6df44 tools (xml_tools): add `namespace` argument to `xmlui_result_to_elt`:
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
1748 security_limit: int = C.NO_SECURITY_LIMIT,
d2deddd6df44 tools (xml_tools): add `namespace` argument to `xmlui_result_to_elt`:
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
1749 chained: bool = False,
d2deddd6df44 tools (xml_tools): add `namespace` argument to `xmlui_result_to_elt`:
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
1750 profile: str = C.PROF_KEY_NONE,
d2deddd6df44 tools (xml_tools): add `namespace` argument to `xmlui_result_to_elt`:
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
1751 ) -> defer.Deferred[dict]:
1504
a2e4b976e707 core (xmlui): added helper method to easily manage dialogs with a deferred
Goffi <goffi@goffi.org>
parents: 1500
diff changeset
1752 """Create a deferred linked to XMLUI
a2e4b976e707 core (xmlui): added helper method to easily manage dialogs with a deferred
Goffi <goffi@goffi.org>
parents: 1500
diff changeset
1753
4299
d2deddd6df44 tools (xml_tools): add `namespace` argument to `xmlui_result_to_elt`:
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
1754 @param xmlui: instance of the XMLUI
1504
a2e4b976e707 core (xmlui): added helper method to easily manage dialogs with a deferred
Goffi <goffi@goffi.org>
parents: 1500
diff changeset
1755 Must be an XMLUI that you can submit, with submit_id set to ''
a2e4b976e707 core (xmlui): added helper method to easily manage dialogs with a deferred
Goffi <goffi@goffi.org>
parents: 1500
diff changeset
1756 @param profile: %(doc_profile)s
4299
d2deddd6df44 tools (xml_tools): add `namespace` argument to `xmlui_result_to_elt`:
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
1757 @param action_extra: extra action to merge with xmlui
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 4032
diff changeset
1758 mainly used to add meta informations (see action_new doc)
1599
e2ed8009e66e backend, bridge, frontends: actionNew has now a security_limit argument + added some docstring to explain data argument
Goffi <goffi@goffi.org>
parents: 1531
diff changeset
1759 @param security_limit: %(doc_security_limit)s
4299
d2deddd6df44 tools (xml_tools): add `namespace` argument to `xmlui_result_to_elt`:
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
1760 @param chained: True if the Deferred result must be returned to the frontend
1692
0fdd8fe34fbf core (xmlui): new deferredUI method allows to manage XMLUI with a Deferred insteand of manual submit_id handling:
Goffi <goffi@goffi.org>
parents: 1645
diff changeset
1761 useful when backend is in a series of dialogs with an ui
4299
d2deddd6df44 tools (xml_tools): add `namespace` argument to `xmlui_result_to_elt`:
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
1762 @return: a deferred which fire the data
1504
a2e4b976e707 core (xmlui): added helper method to easily manage dialogs with a deferred
Goffi <goffi@goffi.org>
parents: 1500
diff changeset
1763 """
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 4032
diff changeset
1764 xmlui_d = deferred_ui(host, xmlui, chained)
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
1765 action_data = {"xmlui": xmlui.toXml()}
1601
e0a152f2cf6d core (xmlui), plugin file: added action_extra param to deferXMLUI/deferDialog which is merged to the action data dict when actionNew is called
Goffi <goffi@goffi.org>
parents: 1599
diff changeset
1766 if action_extra is not None:
e0a152f2cf6d core (xmlui), plugin file: added action_extra param to deferXMLUI/deferDialog which is merged to the action data dict when actionNew is called
Goffi <goffi@goffi.org>
parents: 1599
diff changeset
1767 action_data.update(action_extra)
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 4032
diff changeset
1768 host.action_new(
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
1769 action_data,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
1770 security_limit=security_limit,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
1771 keep_id=xmlui.submit_id,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
1772 profile=profile,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
1773 )
1504
a2e4b976e707 core (xmlui): added helper method to easily manage dialogs with a deferred
Goffi <goffi@goffi.org>
parents: 1500
diff changeset
1774 return xmlui_d
a2e4b976e707 core (xmlui): added helper method to easily manage dialogs with a deferred
Goffi <goffi@goffi.org>
parents: 1500
diff changeset
1775
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
1776
4041
2594e1951cf7 core (bridge): `action_new` now use serialised dict for extra data.
Goffi <goffi@goffi.org>
parents: 4037
diff changeset
1777 def defer_dialog(
2594e1951cf7 core (bridge): `action_new` now use serialised dict for extra data.
Goffi <goffi@goffi.org>
parents: 4037
diff changeset
1778 host,
2594e1951cf7 core (bridge): `action_new` now use serialised dict for extra data.
Goffi <goffi@goffi.org>
parents: 4037
diff changeset
1779 message: str,
2594e1951cf7 core (bridge): `action_new` now use serialised dict for extra data.
Goffi <goffi@goffi.org>
parents: 4037
diff changeset
1780 title: str = "Please confirm",
2594e1951cf7 core (bridge): `action_new` now use serialised dict for extra data.
Goffi <goffi@goffi.org>
parents: 4037
diff changeset
1781 type_: str = C.XMLUI_DIALOG_CONFIRM,
2594e1951cf7 core (bridge): `action_new` now use serialised dict for extra data.
Goffi <goffi@goffi.org>
parents: 4037
diff changeset
1782 options: Optional[dict] = None,
2594e1951cf7 core (bridge): `action_new` now use serialised dict for extra data.
Goffi <goffi@goffi.org>
parents: 4037
diff changeset
1783 action_extra: Optional[dict] = None,
2594e1951cf7 core (bridge): `action_new` now use serialised dict for extra data.
Goffi <goffi@goffi.org>
parents: 4037
diff changeset
1784 security_limit: int = C.NO_SECURITY_LIMIT,
2594e1951cf7 core (bridge): `action_new` now use serialised dict for extra data.
Goffi <goffi@goffi.org>
parents: 4037
diff changeset
1785 chained: bool = False,
4270
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4110
diff changeset
1786 profile: str = C.PROF_KEY_NONE,
4041
2594e1951cf7 core (bridge): `action_new` now use serialised dict for extra data.
Goffi <goffi@goffi.org>
parents: 4037
diff changeset
1787 ) -> defer.Deferred:
1504
a2e4b976e707 core (xmlui): added helper method to easily manage dialogs with a deferred
Goffi <goffi@goffi.org>
parents: 1500
diff changeset
1788 """Create a submitable dialog and manage it with a deferred
a2e4b976e707 core (xmlui): added helper method to easily manage dialogs with a deferred
Goffi <goffi@goffi.org>
parents: 1500
diff changeset
1789
4041
2594e1951cf7 core (bridge): `action_new` now use serialised dict for extra data.
Goffi <goffi@goffi.org>
parents: 4037
diff changeset
1790 @param message: message to display
2594e1951cf7 core (bridge): `action_new` now use serialised dict for extra data.
Goffi <goffi@goffi.org>
parents: 4037
diff changeset
1791 @param title: title of the dialog
2594e1951cf7 core (bridge): `action_new` now use serialised dict for extra data.
Goffi <goffi@goffi.org>
parents: 4037
diff changeset
1792 @param type: dialog type (C.XMLUI_DIALOG_* or plugin specific string)
2594e1951cf7 core (bridge): `action_new` now use serialised dict for extra data.
Goffi <goffi@goffi.org>
parents: 4037
diff changeset
1793 @param options: if not None, will be used to update (extend) dialog_opt arguments of
2594e1951cf7 core (bridge): `action_new` now use serialised dict for extra data.
Goffi <goffi@goffi.org>
parents: 4037
diff changeset
1794 XMLUI
2594e1951cf7 core (bridge): `action_new` now use serialised dict for extra data.
Goffi <goffi@goffi.org>
parents: 4037
diff changeset
1795 @param action_extra: extra action to merge with xmlui
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 4032
diff changeset
1796 mainly used to add meta informations (see action_new doc)
1601
e0a152f2cf6d core (xmlui), plugin file: added action_extra param to deferXMLUI/deferDialog which is merged to the action data dict when actionNew is called
Goffi <goffi@goffi.org>
parents: 1599
diff changeset
1797 @param security_limit: %(doc_security_limit)s
4041
2594e1951cf7 core (bridge): `action_new` now use serialised dict for extra data.
Goffi <goffi@goffi.org>
parents: 4037
diff changeset
1798 @param chained: True if the Deferred result must be returned to the frontend
1737
ad5da569ecf4 core (xmlui): fixed deferConfirm
Goffi <goffi@goffi.org>
parents: 1736
diff changeset
1799 useful when backend is in a series of dialogs with an ui
1504
a2e4b976e707 core (xmlui): added helper method to easily manage dialogs with a deferred
Goffi <goffi@goffi.org>
parents: 1500
diff changeset
1800 @param profile: %(doc_profile)s
4041
2594e1951cf7 core (bridge): `action_new` now use serialised dict for extra data.
Goffi <goffi@goffi.org>
parents: 4037
diff changeset
1801 @return: answer dict
1504
a2e4b976e707 core (xmlui): added helper method to easily manage dialogs with a deferred
Goffi <goffi@goffi.org>
parents: 1500
diff changeset
1802 """
a2e4b976e707 core (xmlui): added helper method to easily manage dialogs with a deferred
Goffi <goffi@goffi.org>
parents: 1500
diff changeset
1803 assert profile is not None
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
1804 dialog_opt = {"type": type_, "message": message}
1521
d2ab9c62ac3a core (xmlui): deferDialog can now manage additional dialog options through the "options" argument
Goffi <goffi@goffi.org>
parents: 1507
diff changeset
1805 if options is not None:
d2ab9c62ac3a core (xmlui): deferDialog can now manage additional dialog options through the "options" argument
Goffi <goffi@goffi.org>
parents: 1507
diff changeset
1806 dialog_opt.update(options)
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
1807 dialog = XMLUI(C.XMLUI_DIALOG, title=title, dialog_opt=dialog_opt, submit_id="")
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 4032
diff changeset
1808 return defer_xmlui(host, dialog, action_extra, security_limit, chained, profile)
1504
a2e4b976e707 core (xmlui): added helper method to easily manage dialogs with a deferred
Goffi <goffi@goffi.org>
parents: 1500
diff changeset
1809
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
1810
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 4032
diff changeset
1811 def defer_confirm(*args, **kwargs):
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 4032
diff changeset
1812 """call defer_dialog and return a boolean instead of the whole data dict"""
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 4032
diff changeset
1813 d = defer_dialog(*args, **kwargs)
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
1814 d.addCallback(lambda data: C.bool(data["answer"]))
1504
a2e4b976e707 core (xmlui): added helper method to easily manage dialogs with a deferred
Goffi <goffi@goffi.org>
parents: 1500
diff changeset
1815 return d
a2e4b976e707 core (xmlui): added helper method to easily manage dialogs with a deferred
Goffi <goffi@goffi.org>
parents: 1500
diff changeset
1816
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
1817
802
9007bb133009 core, frontends: XMLUI refactoring:
Goffi <goffi@goffi.org>
parents: 800
diff changeset
1818 # Misc other funtions
9007bb133009 core, frontends: XMLUI refactoring:
Goffi <goffi@goffi.org>
parents: 800
diff changeset
1819
4270
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4110
diff changeset
1820
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 4032
diff changeset
1821 def element_copy(
4270
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4110
diff changeset
1822 element: domish.Element, with_parent: bool = True, with_children: bool = True
3395
d6a482a78bda tools (xml_tools): added `with_parent` and `with_children` argument to `elementCopy`
Goffi <goffi@goffi.org>
parents: 3393
diff changeset
1823 ) -> domish.Element:
2814
777a582d9641 core (xml_tools): new elementCopy method to shallow copy a domish.Element
Goffi <goffi@goffi.org>
parents: 2782
diff changeset
1824 """Make a copy of a domish.Element
777a582d9641 core (xml_tools): new elementCopy method to shallow copy a domish.Element
Goffi <goffi@goffi.org>
parents: 2782
diff changeset
1825
777a582d9641 core (xml_tools): new elementCopy method to shallow copy a domish.Element
Goffi <goffi@goffi.org>
parents: 2782
diff changeset
1826 The copy will have its own children list, so other elements
777a582d9641 core (xml_tools): new elementCopy method to shallow copy a domish.Element
Goffi <goffi@goffi.org>
parents: 2782
diff changeset
1827 can be added as direct children without modifying orignal one.
777a582d9641 core (xml_tools): new elementCopy method to shallow copy a domish.Element
Goffi <goffi@goffi.org>
parents: 2782
diff changeset
1828 Children are not deeply copied, so if an element is added to a child or grandchild,
777a582d9641 core (xml_tools): new elementCopy method to shallow copy a domish.Element
Goffi <goffi@goffi.org>
parents: 2782
diff changeset
1829 it will also affect original element.
3395
d6a482a78bda tools (xml_tools): added `with_parent` and `with_children` argument to `elementCopy`
Goffi <goffi@goffi.org>
parents: 3393
diff changeset
1830 @param element: Element to clone
2814
777a582d9641 core (xml_tools): new elementCopy method to shallow copy a domish.Element
Goffi <goffi@goffi.org>
parents: 2782
diff changeset
1831 """
777a582d9641 core (xml_tools): new elementCopy method to shallow copy a domish.Element
Goffi <goffi@goffi.org>
parents: 2782
diff changeset
1832 new_elt = domish.Element(
777a582d9641 core (xml_tools): new elementCopy method to shallow copy a domish.Element
Goffi <goffi@goffi.org>
parents: 2782
diff changeset
1833 (element.uri, element.name),
4270
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4110
diff changeset
1834 defaultUri=element.defaultUri,
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4110
diff changeset
1835 attribs=element.attributes,
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4110
diff changeset
1836 localPrefixes=element.localPrefixes,
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4110
diff changeset
1837 )
3395
d6a482a78bda tools (xml_tools): added `with_parent` and `with_children` argument to `elementCopy`
Goffi <goffi@goffi.org>
parents: 3393
diff changeset
1838 if with_parent:
d6a482a78bda tools (xml_tools): added `with_parent` and `with_children` argument to `elementCopy`
Goffi <goffi@goffi.org>
parents: 3393
diff changeset
1839 new_elt.parent = element.parent
d6a482a78bda tools (xml_tools): added `with_parent` and `with_children` argument to `elementCopy`
Goffi <goffi@goffi.org>
parents: 3393
diff changeset
1840 if with_children:
d6a482a78bda tools (xml_tools): added `with_parent` and `with_children` argument to `elementCopy`
Goffi <goffi@goffi.org>
parents: 3393
diff changeset
1841 new_elt.children = element.children[:]
2814
777a582d9641 core (xml_tools): new elementCopy method to shallow copy a domish.Element
Goffi <goffi@goffi.org>
parents: 2782
diff changeset
1842 return new_elt
777a582d9641 core (xml_tools): new elementCopy method to shallow copy a domish.Element
Goffi <goffi@goffi.org>
parents: 2782
diff changeset
1843
777a582d9641 core (xml_tools): new elementCopy method to shallow copy a domish.Element
Goffi <goffi@goffi.org>
parents: 2782
diff changeset
1844
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 4032
diff changeset
1845 def is_xhtml_field(field):
2782
b17e6fa1e607 core (XMLUI): new XHTMLBox widget:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
1846 """Check if a data_form.Field is an XHTML one"""
4270
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4110
diff changeset
1847 return (
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4110
diff changeset
1848 field.fieldType is None
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4110
diff changeset
1849 and field.ext_type == "xml"
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4110
diff changeset
1850 and field.value.uri == C.NS_XHTML
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4110
diff changeset
1851 )
2782
b17e6fa1e607 core (XMLUI): new XHTMLBox widget:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
1852
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
1853
3393
2b6f69f6df8c tools(xml_tools): fixed `<div>` unwrapping + added `parse` instance:
Goffi <goffi@goffi.org>
parents: 3233
diff changeset
1854 class ElementParser:
2b6f69f6df8c tools(xml_tools): fixed `<div>` unwrapping + added `parse` instance:
Goffi <goffi@goffi.org>
parents: 3233
diff changeset
1855 """Callable class to parse XML string into Element"""
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
1856
1818
7e6342de71fb core (xml_tools): unescape HTML sequences to avoid parsing error in ElementParser
Goffi <goffi@goffi.org>
parents: 1817
diff changeset
1857 # XXX: Found at http://stackoverflow.com/questions/2093400/how-to-create-twisted-words-xish-domish-element-entirely-from-raw-xml/2095942#2095942
1472
c005c212b538 xmlui: minor docstring fixes
Goffi <goffi@goffi.org>
parents: 1456
diff changeset
1858
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 4032
diff changeset
1859 def _escape_html(self, matchobj):
1818
7e6342de71fb core (xml_tools): unescape HTML sequences to avoid parsing error in ElementParser
Goffi <goffi@goffi.org>
parents: 1817
diff changeset
1860 entity = matchobj.group(1)
7e6342de71fb core (xml_tools): unescape HTML sequences to avoid parsing error in ElementParser
Goffi <goffi@goffi.org>
parents: 1817
diff changeset
1861 if entity in XML_ENTITIES:
7e6342de71fb core (xml_tools): unescape HTML sequences to avoid parsing error in ElementParser
Goffi <goffi@goffi.org>
parents: 1817
diff changeset
1862 # we don't escape XML entities
7e6342de71fb core (xml_tools): unescape HTML sequences to avoid parsing error in ElementParser
Goffi <goffi@goffi.org>
parents: 1817
diff changeset
1863 return matchobj.group(0)
7e6342de71fb core (xml_tools): unescape HTML sequences to avoid parsing error in ElementParser
Goffi <goffi@goffi.org>
parents: 1817
diff changeset
1864 else:
7e6342de71fb core (xml_tools): unescape HTML sequences to avoid parsing error in ElementParser
Goffi <goffi@goffi.org>
parents: 1817
diff changeset
1865 try:
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2958
diff changeset
1866 return chr(html.entities.name2codepoint[entity])
1818
7e6342de71fb core (xml_tools): unescape HTML sequences to avoid parsing error in ElementParser
Goffi <goffi@goffi.org>
parents: 1817
diff changeset
1867 except KeyError:
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2958
diff changeset
1868 log.warning("removing unknown entity {}".format(entity))
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2958
diff changeset
1869 return ""
289
0e54b1b0a8c8 xml_tools: added ElementParser for raw xml -> domish.Element conversion
Goffi <goffi@goffi.org>
parents: 228
diff changeset
1870
1817
7ef0f5f90862 core (xml_tools), plugin XEP-0277: ElementParser element now manage automatically the wrapping with <div/> element when needed + fixed content_xhtml/title_xhtml in XEP-0277
Goffi <goffi@goffi.org>
parents: 1792
diff changeset
1871 def __call__(self, raw_xml, force_spaces=False, namespace=None):
905
cad8e52bb2e6 tools: xml_tools.ElementParser do not replace '\n' and '\t' with ' ' by default
souliane <souliane@mailoo.org>
parents: 876
diff changeset
1872 """
1456
a13be5c22334 xml_tools: minor attribute renaming
Goffi <goffi@goffi.org>
parents: 1444
diff changeset
1873 @param raw_xml(unicode): the raw XML
2625
a55a14c3cbf4 plugin XEP-0070: use a confirm dialog instead of a form + simplified a bit the code
Goffi <goffi@goffi.org>
parents: 2624
diff changeset
1874 @param force_spaces (bool): if True, replace occurrences of '\n' and '\t'
a55a14c3cbf4 plugin XEP-0070: use a confirm dialog instead of a form + simplified a bit the code
Goffi <goffi@goffi.org>
parents: 2624
diff changeset
1875 with ' '.
a55a14c3cbf4 plugin XEP-0070: use a confirm dialog instead of a form + simplified a bit the code
Goffi <goffi@goffi.org>
parents: 2624
diff changeset
1876 @param namespace(unicode, None): if set, use this namespace for the wrapping
a55a14c3cbf4 plugin XEP-0070: use a confirm dialog instead of a form + simplified a bit the code
Goffi <goffi@goffi.org>
parents: 2624
diff changeset
1877 element
905
cad8e52bb2e6 tools: xml_tools.ElementParser do not replace '\n' and '\t' with ' ' by default
souliane <souliane@mailoo.org>
parents: 876
diff changeset
1878 """
1817
7ef0f5f90862 core (xml_tools), plugin XEP-0277: ElementParser element now manage automatically the wrapping with <div/> element when needed + fixed content_xhtml/title_xhtml in XEP-0277
Goffi <goffi@goffi.org>
parents: 1792
diff changeset
1879 # we need to wrap element in case
7ef0f5f90862 core (xml_tools), plugin XEP-0277: ElementParser element now manage automatically the wrapping with <div/> element when needed + fixed content_xhtml/title_xhtml in XEP-0277
Goffi <goffi@goffi.org>
parents: 1792
diff changeset
1880 # there is not a unique one on the top
7ef0f5f90862 core (xml_tools), plugin XEP-0277: ElementParser element now manage automatically the wrapping with <div/> element when needed + fixed content_xhtml/title_xhtml in XEP-0277
Goffi <goffi@goffi.org>
parents: 1792
diff changeset
1881 if namespace is not None:
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2958
diff changeset
1882 raw_xml = "<div xmlns='{}'>{}</div>".format(namespace, raw_xml)
1817
7ef0f5f90862 core (xml_tools), plugin XEP-0277: ElementParser element now manage automatically the wrapping with <div/> element when needed + fixed content_xhtml/title_xhtml in XEP-0277
Goffi <goffi@goffi.org>
parents: 1792
diff changeset
1883 else:
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2958
diff changeset
1884 raw_xml = "<div>{}</div>".format(raw_xml)
1817
7ef0f5f90862 core (xml_tools), plugin XEP-0277: ElementParser element now manage automatically the wrapping with <div/> element when needed + fixed content_xhtml/title_xhtml in XEP-0277
Goffi <goffi@goffi.org>
parents: 1792
diff changeset
1885
1818
7e6342de71fb core (xml_tools): unescape HTML sequences to avoid parsing error in ElementParser
Goffi <goffi@goffi.org>
parents: 1817
diff changeset
1886 # avoid ParserError on HTML escaped chars
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 4032
diff changeset
1887 raw_xml = html_entity_re.sub(self._escape_html, raw_xml)
1818
7e6342de71fb core (xml_tools): unescape HTML sequences to avoid parsing error in ElementParser
Goffi <goffi@goffi.org>
parents: 1817
diff changeset
1888
289
0e54b1b0a8c8 xml_tools: added ElementParser for raw xml -> domish.Element conversion
Goffi <goffi@goffi.org>
parents: 228
diff changeset
1889 self.result = None
590
56531f9e9ac7 Fix pep8 support in src/tools.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 588
diff changeset
1890
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 4032
diff changeset
1891 def on_start(elem):
590
56531f9e9ac7 Fix pep8 support in src/tools.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 588
diff changeset
1892 self.result = elem
56531f9e9ac7 Fix pep8 support in src/tools.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 588
diff changeset
1893
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 4032
diff changeset
1894 def on_end():
289
0e54b1b0a8c8 xml_tools: added ElementParser for raw xml -> domish.Element conversion
Goffi <goffi@goffi.org>
parents: 228
diff changeset
1895 pass
590
56531f9e9ac7 Fix pep8 support in src/tools.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 588
diff changeset
1896
56531f9e9ac7 Fix pep8 support in src/tools.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 588
diff changeset
1897 def onElement(elem):
56531f9e9ac7 Fix pep8 support in src/tools.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 588
diff changeset
1898 self.result.addChild(elem)
289
0e54b1b0a8c8 xml_tools: added ElementParser for raw xml -> domish.Element conversion
Goffi <goffi@goffi.org>
parents: 228
diff changeset
1899
0e54b1b0a8c8 xml_tools: added ElementParser for raw xml -> domish.Element conversion
Goffi <goffi@goffi.org>
parents: 228
diff changeset
1900 parser = domish.elementStream()
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 4032
diff changeset
1901 parser.DocumentStartEvent = on_start
289
0e54b1b0a8c8 xml_tools: added ElementParser for raw xml -> domish.Element conversion
Goffi <goffi@goffi.org>
parents: 228
diff changeset
1902 parser.ElementEvent = onElement
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 4032
diff changeset
1903 parser.DocumentEndEvent = on_end
536
a31abb97310d core, plugins: fixed bad namespace in stanza creation
Goffi <goffi@goffi.org>
parents: 480
diff changeset
1904 tmp = domish.Element((None, "s"))
905
cad8e52bb2e6 tools: xml_tools.ElementParser do not replace '\n' and '\t' with ' ' by default
souliane <souliane@mailoo.org>
parents: 876
diff changeset
1905 if force_spaces:
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
1906 raw_xml = raw_xml.replace("\n", " ").replace("\t", " ")
1456
a13be5c22334 xml_tools: minor attribute renaming
Goffi <goffi@goffi.org>
parents: 1444
diff changeset
1907 tmp.addRawXml(raw_xml)
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
1908 parser.parse(tmp.toXml().encode("utf-8"))
1817
7ef0f5f90862 core (xml_tools), plugin XEP-0277: ElementParser element now manage automatically the wrapping with <div/> element when needed + fixed content_xhtml/title_xhtml in XEP-0277
Goffi <goffi@goffi.org>
parents: 1792
diff changeset
1909 top_elt = self.result.firstChildElement()
7ef0f5f90862 core (xml_tools), plugin XEP-0277: ElementParser element now manage automatically the wrapping with <div/> element when needed + fixed content_xhtml/title_xhtml in XEP-0277
Goffi <goffi@goffi.org>
parents: 1792
diff changeset
1910 # we now can check if there was a unique element on the top
3393
2b6f69f6df8c tools(xml_tools): fixed `<div>` unwrapping + added `parse` instance:
Goffi <goffi@goffi.org>
parents: 3233
diff changeset
1911 # and remove our wrapping <div/> is this is the case
2b6f69f6df8c tools(xml_tools): fixed `<div>` unwrapping + added `parse` instance:
Goffi <goffi@goffi.org>
parents: 3233
diff changeset
1912 top_elt_children = list(top_elt.elements())
2b6f69f6df8c tools(xml_tools): fixed `<div>` unwrapping + added `parse` instance:
Goffi <goffi@goffi.org>
parents: 3233
diff changeset
1913 if len(top_elt_children) == 1:
2b6f69f6df8c tools(xml_tools): fixed `<div>` unwrapping + added `parse` instance:
Goffi <goffi@goffi.org>
parents: 3233
diff changeset
1914 top_elt = top_elt_children[0]
1817
7ef0f5f90862 core (xml_tools), plugin XEP-0277: ElementParser element now manage automatically the wrapping with <div/> element when needed + fixed content_xhtml/title_xhtml in XEP-0277
Goffi <goffi@goffi.org>
parents: 1792
diff changeset
1915 return top_elt
1732
cf11cfc87ef9 xml_tools, plugin XEP-0071, XEP-0277: add method expandNewLinesToXHTML:
souliane <souliane@mailoo.org>
parents: 1692
diff changeset
1916
cf11cfc87ef9 xml_tools, plugin XEP-0071, XEP-0277: add method expandNewLinesToXHTML:
souliane <souliane@mailoo.org>
parents: 1692
diff changeset
1917
3393
2b6f69f6df8c tools(xml_tools): fixed `<div>` unwrapping + added `parse` instance:
Goffi <goffi@goffi.org>
parents: 3233
diff changeset
1918 parse = ElementParser()
2b6f69f6df8c tools(xml_tools): fixed `<div>` unwrapping + added `parse` instance:
Goffi <goffi@goffi.org>
parents: 3233
diff changeset
1919
2b6f69f6df8c tools(xml_tools): fixed `<div>` unwrapping + added `parse` instance:
Goffi <goffi@goffi.org>
parents: 3233
diff changeset
1920
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 4032
diff changeset
1921 # FIXME: this method is duplicated from frontends.tools.xmlui.get_text
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 4032
diff changeset
1922 def get_text(node):
1740
681fe91abcc0 memory (params): parameter jids_list values are specified with <jid>...</jid>
souliane <souliane@mailoo.org>
parents: 1737
diff changeset
1923 """Get child text nodes of a domish.Element.
681fe91abcc0 memory (params): parameter jids_list values are specified with <jid>...</jid>
souliane <souliane@mailoo.org>
parents: 1737
diff changeset
1924
681fe91abcc0 memory (params): parameter jids_list values are specified with <jid>...</jid>
souliane <souliane@mailoo.org>
parents: 1737
diff changeset
1925 @param node (domish.Element)
681fe91abcc0 memory (params): parameter jids_list values are specified with <jid>...</jid>
souliane <souliane@mailoo.org>
parents: 1737
diff changeset
1926 @return: joined unicode text of all nodes
681fe91abcc0 memory (params): parameter jids_list values are specified with <jid>...</jid>
souliane <souliane@mailoo.org>
parents: 1737
diff changeset
1927 """
681fe91abcc0 memory (params): parameter jids_list values are specified with <jid>...</jid>
souliane <souliane@mailoo.org>
parents: 1737
diff changeset
1928 data = []
681fe91abcc0 memory (params): parameter jids_list values are specified with <jid>...</jid>
souliane <souliane@mailoo.org>
parents: 1737
diff changeset
1929 for child in node.childNodes:
681fe91abcc0 memory (params): parameter jids_list values are specified with <jid>...</jid>
souliane <souliane@mailoo.org>
parents: 1737
diff changeset
1930 if child.nodeType == child.TEXT_NODE:
681fe91abcc0 memory (params): parameter jids_list values are specified with <jid>...</jid>
souliane <souliane@mailoo.org>
parents: 1737
diff changeset
1931 data.append(child.wholeText)
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2958
diff changeset
1932 return "".join(data)
1819
c271087d2020 core (xml_tools): added a method to find all elements corresponding to criteria
Goffi <goffi@goffi.org>
parents: 1818
diff changeset
1933
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
1934
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 4032
diff changeset
1935 def find_all(elt, namespaces=None, names=None):
1819
c271087d2020 core (xml_tools): added a method to find all elements corresponding to criteria
Goffi <goffi@goffi.org>
parents: 1818
diff changeset
1936 """Find child element at any depth matching criteria
c271087d2020 core (xml_tools): added a method to find all elements corresponding to criteria
Goffi <goffi@goffi.org>
parents: 1818
diff changeset
1937
c271087d2020 core (xml_tools): added a method to find all elements corresponding to criteria
Goffi <goffi@goffi.org>
parents: 1818
diff changeset
1938 @param elt(domish.Element): top parent of the elements to find
2108
70f23bc7859b core (xml_tools): fixed findAll
Goffi <goffi@goffi.org>
parents: 2071
diff changeset
1939 @param names(iterable[unicode], basestring, None): names to match
1819
c271087d2020 core (xml_tools): added a method to find all elements corresponding to criteria
Goffi <goffi@goffi.org>
parents: 1818
diff changeset
1940 None to accept every names
2108
70f23bc7859b core (xml_tools): fixed findAll
Goffi <goffi@goffi.org>
parents: 2071
diff changeset
1941 @param namespace(iterable[unicode], basestring, None): URIs to match
1819
c271087d2020 core (xml_tools): added a method to find all elements corresponding to criteria
Goffi <goffi@goffi.org>
parents: 1818
diff changeset
1942 None to accept every namespaces
2108
70f23bc7859b core (xml_tools): fixed findAll
Goffi <goffi@goffi.org>
parents: 2071
diff changeset
1943 @return ((G)domish.Element): found elements
1819
c271087d2020 core (xml_tools): added a method to find all elements corresponding to criteria
Goffi <goffi@goffi.org>
parents: 1818
diff changeset
1944 """
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2958
diff changeset
1945 if isinstance(namespaces, str):
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
1946 namespaces = tuple((namespaces,))
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2958
diff changeset
1947 if isinstance(names, str):
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
1948 names = tuple((names,))
1819
c271087d2020 core (xml_tools): added a method to find all elements corresponding to criteria
Goffi <goffi@goffi.org>
parents: 1818
diff changeset
1949
c271087d2020 core (xml_tools): added a method to find all elements corresponding to criteria
Goffi <goffi@goffi.org>
parents: 1818
diff changeset
1950 for child in elt.elements():
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
1951 if (
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
1952 domish.IElement.providedBy(child)
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
1953 and (not names or child.name in names)
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
1954 and (not namespaces or child.uri in namespaces)
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
1955 ):
1819
c271087d2020 core (xml_tools): added a method to find all elements corresponding to criteria
Goffi <goffi@goffi.org>
parents: 1818
diff changeset
1956 yield child
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 4032
diff changeset
1957 for found in find_all(child, namespaces, names):
1819
c271087d2020 core (xml_tools): added a method to find all elements corresponding to criteria
Goffi <goffi@goffi.org>
parents: 1818
diff changeset
1958 yield found
3396
a4774f5b6b17 tools (xml_tools): `domish.Element` pretty formatting functions:
Goffi <goffi@goffi.org>
parents: 3395
diff changeset
1959
a4774f5b6b17 tools (xml_tools): `domish.Element` pretty formatting functions:
Goffi <goffi@goffi.org>
parents: 3395
diff changeset
1960
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 4032
diff changeset
1961 def find_ancestor(
4270
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4110
diff changeset
1962 elt, name: str, namespace: Optional[Union[str, Iterable[str]]] = None
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4110
diff changeset
1963 ) -> domish.Element:
4010
818db4ca3717 tools (xml_tools): accept several namespaces in `findAncestor`
Goffi <goffi@goffi.org>
parents: 3967
diff changeset
1964 """Retrieve ancestor of an element
818db4ca3717 tools (xml_tools): accept several namespaces in `findAncestor`
Goffi <goffi@goffi.org>
parents: 3967
diff changeset
1965
818db4ca3717 tools (xml_tools): accept several namespaces in `findAncestor`
Goffi <goffi@goffi.org>
parents: 3967
diff changeset
1966 @param elt: starting element, its parent will be checked recursively until the
818db4ca3717 tools (xml_tools): accept several namespaces in `findAncestor`
Goffi <goffi@goffi.org>
parents: 3967
diff changeset
1967 required one if found
818db4ca3717 tools (xml_tools): accept several namespaces in `findAncestor`
Goffi <goffi@goffi.org>
parents: 3967
diff changeset
1968 @param name: name of the element to find
818db4ca3717 tools (xml_tools): accept several namespaces in `findAncestor`
Goffi <goffi@goffi.org>
parents: 3967
diff changeset
1969 @param namespace: namespace of the element to find
818db4ca3717 tools (xml_tools): accept several namespaces in `findAncestor`
Goffi <goffi@goffi.org>
parents: 3967
diff changeset
1970 - None to find any element with that name
818db4ca3717 tools (xml_tools): accept several namespaces in `findAncestor`
Goffi <goffi@goffi.org>
parents: 3967
diff changeset
1971 - a simple string to find the namespace
818db4ca3717 tools (xml_tools): accept several namespaces in `findAncestor`
Goffi <goffi@goffi.org>
parents: 3967
diff changeset
1972 - several namespaces can be specified in an iterable, if an element with any of
818db4ca3717 tools (xml_tools): accept several namespaces in `findAncestor`
Goffi <goffi@goffi.org>
parents: 3967
diff changeset
1973 this namespace and given name is found, it will match
818db4ca3717 tools (xml_tools): accept several namespaces in `findAncestor`
Goffi <goffi@goffi.org>
parents: 3967
diff changeset
1974
818db4ca3717 tools (xml_tools): accept several namespaces in `findAncestor`
Goffi <goffi@goffi.org>
parents: 3967
diff changeset
1975 """
818db4ca3717 tools (xml_tools): accept several namespaces in `findAncestor`
Goffi <goffi@goffi.org>
parents: 3967
diff changeset
1976 if isinstance(namespace, str):
818db4ca3717 tools (xml_tools): accept several namespaces in `findAncestor`
Goffi <goffi@goffi.org>
parents: 3967
diff changeset
1977 namespace = [namespace]
3491
2bd75fc2555d tools (xml_tools): new findAncestor method:
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
1978 current = elt.parent
2bd75fc2555d tools (xml_tools): new findAncestor method:
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
1979 while True:
2bd75fc2555d tools (xml_tools): new findAncestor method:
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
1980 if current is None:
2bd75fc2555d tools (xml_tools): new findAncestor method:
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
1981 raise exceptions.NotFound(
2bd75fc2555d tools (xml_tools): new findAncestor method:
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
1982 f"Can't find any ancestor {name!r} (xmlns: {namespace!r})"
2bd75fc2555d tools (xml_tools): new findAncestor method:
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
1983 )
4010
818db4ca3717 tools (xml_tools): accept several namespaces in `findAncestor`
Goffi <goffi@goffi.org>
parents: 3967
diff changeset
1984 if current.name == name and (namespace is None or current.uri in namespace):
3491
2bd75fc2555d tools (xml_tools): new findAncestor method:
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
1985 return current
2bd75fc2555d tools (xml_tools): new findAncestor method:
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
1986 current = current.parent
2bd75fc2555d tools (xml_tools): new findAncestor method:
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
1987
2bd75fc2555d tools (xml_tools): new findAncestor method:
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
1988
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 4032
diff changeset
1989 def p_fmt_elt(elt, indent=0, defaultUri=""):
3396
a4774f5b6b17 tools (xml_tools): `domish.Element` pretty formatting functions:
Goffi <goffi@goffi.org>
parents: 3395
diff changeset
1990 """Pretty format a domish.Element"""
a4774f5b6b17 tools (xml_tools): `domish.Element` pretty formatting functions:
Goffi <goffi@goffi.org>
parents: 3395
diff changeset
1991 strings = []
a4774f5b6b17 tools (xml_tools): `domish.Element` pretty formatting functions:
Goffi <goffi@goffi.org>
parents: 3395
diff changeset
1992 for child in elt.children:
a4774f5b6b17 tools (xml_tools): `domish.Element` pretty formatting functions:
Goffi <goffi@goffi.org>
parents: 3395
diff changeset
1993 if domish.IElement.providedBy(child):
4270
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4110
diff changeset
1994 strings.append(p_fmt_elt(child, indent + 2, defaultUri=elt.defaultUri))
3396
a4774f5b6b17 tools (xml_tools): `domish.Element` pretty formatting functions:
Goffi <goffi@goffi.org>
parents: 3395
diff changeset
1995 else:
a4774f5b6b17 tools (xml_tools): `domish.Element` pretty formatting functions:
Goffi <goffi@goffi.org>
parents: 3395
diff changeset
1996 strings.append(f"{(indent+2)*' '}{child!s}")
a4774f5b6b17 tools (xml_tools): `domish.Element` pretty formatting functions:
Goffi <goffi@goffi.org>
parents: 3395
diff changeset
1997 if elt.children:
a4774f5b6b17 tools (xml_tools): `domish.Element` pretty formatting functions:
Goffi <goffi@goffi.org>
parents: 3395
diff changeset
1998 nochild_elt = domish.Element(
3466
80f8635dc66e tools (xml_tools): fix pFmtElt:
Goffi <goffi@goffi.org>
parents: 3448
diff changeset
1999 (elt.uri, elt.name), elt.defaultUri, elt.attributes, elt.localPrefixes
3396
a4774f5b6b17 tools (xml_tools): `domish.Element` pretty formatting functions:
Goffi <goffi@goffi.org>
parents: 3395
diff changeset
2000 )
3466
80f8635dc66e tools (xml_tools): fix pFmtElt:
Goffi <goffi@goffi.org>
parents: 3448
diff changeset
2001 strings.insert(0, f"{indent*' '}{nochild_elt.toXml(defaultUri=defaultUri)[:-2]}>")
3396
a4774f5b6b17 tools (xml_tools): `domish.Element` pretty formatting functions:
Goffi <goffi@goffi.org>
parents: 3395
diff changeset
2002 strings.append(f"{indent*' '}</{nochild_elt.name}>")
a4774f5b6b17 tools (xml_tools): `domish.Element` pretty formatting functions:
Goffi <goffi@goffi.org>
parents: 3395
diff changeset
2003 else:
3466
80f8635dc66e tools (xml_tools): fix pFmtElt:
Goffi <goffi@goffi.org>
parents: 3448
diff changeset
2004 strings.append(f"{indent*' '}{elt.toXml(defaultUri=defaultUri)}")
4270
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4110
diff changeset
2005 return "\n".join(strings)
3396
a4774f5b6b17 tools (xml_tools): `domish.Element` pretty formatting functions:
Goffi <goffi@goffi.org>
parents: 3395
diff changeset
2006
a4774f5b6b17 tools (xml_tools): `domish.Element` pretty formatting functions:
Goffi <goffi@goffi.org>
parents: 3395
diff changeset
2007
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 4032
diff changeset
2008 def pp_elt(elt):
3396
a4774f5b6b17 tools (xml_tools): `domish.Element` pretty formatting functions:
Goffi <goffi@goffi.org>
parents: 3395
diff changeset
2009 """Pretty print a domish.Element"""
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 4032
diff changeset
2010 print(p_fmt_elt(elt))
3914
4cb38c8312a1 plugin XEP-0384, xml_tools: avoid `getItems` timeout + fix empty node crash + parsing:
Goffi <goffi@goffi.org>
parents: 3903
diff changeset
2011
4cb38c8312a1 plugin XEP-0384, xml_tools: avoid `getItems` timeout + fix empty node crash + parsing:
Goffi <goffi@goffi.org>
parents: 3903
diff changeset
2012
4cb38c8312a1 plugin XEP-0384, xml_tools: avoid `getItems` timeout + fix empty node crash + parsing:
Goffi <goffi@goffi.org>
parents: 3903
diff changeset
2013 # ElementTree
4cb38c8312a1 plugin XEP-0384, xml_tools: avoid `getItems` timeout + fix empty node crash + parsing:
Goffi <goffi@goffi.org>
parents: 3903
diff changeset
2014
4270
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4110
diff changeset
2015
3914
4cb38c8312a1 plugin XEP-0384, xml_tools: avoid `getItems` timeout + fix empty node crash + parsing:
Goffi <goffi@goffi.org>
parents: 3903
diff changeset
2016 def et_get_namespace_and_name(et_elt: ET.Element) -> Tuple[Optional[str], str]:
4cb38c8312a1 plugin XEP-0384, xml_tools: avoid `getItems` timeout + fix empty node crash + parsing:
Goffi <goffi@goffi.org>
parents: 3903
diff changeset
2017 """Retrieve element namespace and name from ElementTree element
4cb38c8312a1 plugin XEP-0384, xml_tools: avoid `getItems` timeout + fix empty node crash + parsing:
Goffi <goffi@goffi.org>
parents: 3903
diff changeset
2018
4cb38c8312a1 plugin XEP-0384, xml_tools: avoid `getItems` timeout + fix empty node crash + parsing:
Goffi <goffi@goffi.org>
parents: 3903
diff changeset
2019 @param et_elt: ElementTree element
4cb38c8312a1 plugin XEP-0384, xml_tools: avoid `getItems` timeout + fix empty node crash + parsing:
Goffi <goffi@goffi.org>
parents: 3903
diff changeset
2020 @return: namespace and name of the element
4cb38c8312a1 plugin XEP-0384, xml_tools: avoid `getItems` timeout + fix empty node crash + parsing:
Goffi <goffi@goffi.org>
parents: 3903
diff changeset
2021 if not namespace if specified, None is returned
4cb38c8312a1 plugin XEP-0384, xml_tools: avoid `getItems` timeout + fix empty node crash + parsing:
Goffi <goffi@goffi.org>
parents: 3903
diff changeset
2022 """
4cb38c8312a1 plugin XEP-0384, xml_tools: avoid `getItems` timeout + fix empty node crash + parsing:
Goffi <goffi@goffi.org>
parents: 3903
diff changeset
2023 name = et_elt.tag
4cb38c8312a1 plugin XEP-0384, xml_tools: avoid `getItems` timeout + fix empty node crash + parsing:
Goffi <goffi@goffi.org>
parents: 3903
diff changeset
2024 if not name:
4cb38c8312a1 plugin XEP-0384, xml_tools: avoid `getItems` timeout + fix empty node crash + parsing:
Goffi <goffi@goffi.org>
parents: 3903
diff changeset
2025 raise ValueError("no name set in ET element")
4cb38c8312a1 plugin XEP-0384, xml_tools: avoid `getItems` timeout + fix empty node crash + parsing:
Goffi <goffi@goffi.org>
parents: 3903
diff changeset
2026 elif name[0] != "{":
4cb38c8312a1 plugin XEP-0384, xml_tools: avoid `getItems` timeout + fix empty node crash + parsing:
Goffi <goffi@goffi.org>
parents: 3903
diff changeset
2027 return None, name
4cb38c8312a1 plugin XEP-0384, xml_tools: avoid `getItems` timeout + fix empty node crash + parsing:
Goffi <goffi@goffi.org>
parents: 3903
diff changeset
2028 end_idx = name.find("}")
4cb38c8312a1 plugin XEP-0384, xml_tools: avoid `getItems` timeout + fix empty node crash + parsing:
Goffi <goffi@goffi.org>
parents: 3903
diff changeset
2029 if end_idx == -1:
4cb38c8312a1 plugin XEP-0384, xml_tools: avoid `getItems` timeout + fix empty node crash + parsing:
Goffi <goffi@goffi.org>
parents: 3903
diff changeset
2030 raise ValueError("Invalid ET name")
4270
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4110
diff changeset
2031 return name[1:end_idx], name[end_idx + 1 :]
3914
4cb38c8312a1 plugin XEP-0384, xml_tools: avoid `getItems` timeout + fix empty node crash + parsing:
Goffi <goffi@goffi.org>
parents: 3903
diff changeset
2032
4cb38c8312a1 plugin XEP-0384, xml_tools: avoid `getItems` timeout + fix empty node crash + parsing:
Goffi <goffi@goffi.org>
parents: 3903
diff changeset
2033
3955
323017a4e4d2 tools (xml_tools): `domish_elt_2_et_elt` and `et_elt_2_domish_elt` now handle lxml.etree
Goffi <goffi@goffi.org>
parents: 3914
diff changeset
2034 def et_elt_2_domish_elt(et_elt: Union[ET.Element, etree.Element]) -> domish.Element:
3914
4cb38c8312a1 plugin XEP-0384, xml_tools: avoid `getItems` timeout + fix empty node crash + parsing:
Goffi <goffi@goffi.org>
parents: 3903
diff changeset
2035 """Convert ElementTree element to Twisted's domish.Element
4cb38c8312a1 plugin XEP-0384, xml_tools: avoid `getItems` timeout + fix empty node crash + parsing:
Goffi <goffi@goffi.org>
parents: 3903
diff changeset
2036
4cb38c8312a1 plugin XEP-0384, xml_tools: avoid `getItems` timeout + fix empty node crash + parsing:
Goffi <goffi@goffi.org>
parents: 3903
diff changeset
2037 Note: this is a naive implementation, adapted to XMPP, and some content are ignored
4cb38c8312a1 plugin XEP-0384, xml_tools: avoid `getItems` timeout + fix empty node crash + parsing:
Goffi <goffi@goffi.org>
parents: 3903
diff changeset
2038 (attributes namespaces, tail)
4cb38c8312a1 plugin XEP-0384, xml_tools: avoid `getItems` timeout + fix empty node crash + parsing:
Goffi <goffi@goffi.org>
parents: 3903
diff changeset
2039 """
4cb38c8312a1 plugin XEP-0384, xml_tools: avoid `getItems` timeout + fix empty node crash + parsing:
Goffi <goffi@goffi.org>
parents: 3903
diff changeset
2040 namespace, name = et_get_namespace_and_name(et_elt)
4cb38c8312a1 plugin XEP-0384, xml_tools: avoid `getItems` timeout + fix empty node crash + parsing:
Goffi <goffi@goffi.org>
parents: 3903
diff changeset
2041 elt = domish.Element((namespace, name), attribs=et_elt.attrib)
4cb38c8312a1 plugin XEP-0384, xml_tools: avoid `getItems` timeout + fix empty node crash + parsing:
Goffi <goffi@goffi.org>
parents: 3903
diff changeset
2042 if et_elt.text:
4cb38c8312a1 plugin XEP-0384, xml_tools: avoid `getItems` timeout + fix empty node crash + parsing:
Goffi <goffi@goffi.org>
parents: 3903
diff changeset
2043 elt.addContent(et_elt.text)
4cb38c8312a1 plugin XEP-0384, xml_tools: avoid `getItems` timeout + fix empty node crash + parsing:
Goffi <goffi@goffi.org>
parents: 3903
diff changeset
2044 for child in et_elt:
4cb38c8312a1 plugin XEP-0384, xml_tools: avoid `getItems` timeout + fix empty node crash + parsing:
Goffi <goffi@goffi.org>
parents: 3903
diff changeset
2045 elt.addChild(et_elt_2_domish_elt(child))
4cb38c8312a1 plugin XEP-0384, xml_tools: avoid `getItems` timeout + fix empty node crash + parsing:
Goffi <goffi@goffi.org>
parents: 3903
diff changeset
2046 return elt
4cb38c8312a1 plugin XEP-0384, xml_tools: avoid `getItems` timeout + fix empty node crash + parsing:
Goffi <goffi@goffi.org>
parents: 3903
diff changeset
2047
4cb38c8312a1 plugin XEP-0384, xml_tools: avoid `getItems` timeout + fix empty node crash + parsing:
Goffi <goffi@goffi.org>
parents: 3903
diff changeset
2048
3955
323017a4e4d2 tools (xml_tools): `domish_elt_2_et_elt` and `et_elt_2_domish_elt` now handle lxml.etree
Goffi <goffi@goffi.org>
parents: 3914
diff changeset
2049 @overload
4270
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4110
diff changeset
2050 def domish_elt_2_et_elt(elt: domish.Element, lxml: Literal[False]) -> ET.Element: ...
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4110
diff changeset
2051
3955
323017a4e4d2 tools (xml_tools): `domish_elt_2_et_elt` and `et_elt_2_domish_elt` now handle lxml.etree
Goffi <goffi@goffi.org>
parents: 3914
diff changeset
2052
323017a4e4d2 tools (xml_tools): `domish_elt_2_et_elt` and `et_elt_2_domish_elt` now handle lxml.etree
Goffi <goffi@goffi.org>
parents: 3914
diff changeset
2053 @overload
4270
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4110
diff changeset
2054 def domish_elt_2_et_elt(elt: domish.Element, lxml: Literal[True]) -> etree.Element: ...
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4110
diff changeset
2055
3955
323017a4e4d2 tools (xml_tools): `domish_elt_2_et_elt` and `et_elt_2_domish_elt` now handle lxml.etree
Goffi <goffi@goffi.org>
parents: 3914
diff changeset
2056
323017a4e4d2 tools (xml_tools): `domish_elt_2_et_elt` and `et_elt_2_domish_elt` now handle lxml.etree
Goffi <goffi@goffi.org>
parents: 3914
diff changeset
2057 @overload
323017a4e4d2 tools (xml_tools): `domish_elt_2_et_elt` and `et_elt_2_domish_elt` now handle lxml.etree
Goffi <goffi@goffi.org>
parents: 3914
diff changeset
2058 def domish_elt_2_et_elt(
323017a4e4d2 tools (xml_tools): `domish_elt_2_et_elt` and `et_elt_2_domish_elt` now handle lxml.etree
Goffi <goffi@goffi.org>
parents: 3914
diff changeset
2059 elt: domish.Element, lxml: bool
4270
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4110
diff changeset
2060 ) -> Union[ET.Element, etree.Element]: ...
3955
323017a4e4d2 tools (xml_tools): `domish_elt_2_et_elt` and `et_elt_2_domish_elt` now handle lxml.etree
Goffi <goffi@goffi.org>
parents: 3914
diff changeset
2061
4270
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4110
diff changeset
2062
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4110
diff changeset
2063 def domish_elt_2_et_elt(elt, lxml=False):
3914
4cb38c8312a1 plugin XEP-0384, xml_tools: avoid `getItems` timeout + fix empty node crash + parsing:
Goffi <goffi@goffi.org>
parents: 3903
diff changeset
2064 """Convert Twisted's domish.Element to ElementTree equivalent
4cb38c8312a1 plugin XEP-0384, xml_tools: avoid `getItems` timeout + fix empty node crash + parsing:
Goffi <goffi@goffi.org>
parents: 3903
diff changeset
2065
3955
323017a4e4d2 tools (xml_tools): `domish_elt_2_et_elt` and `et_elt_2_domish_elt` now handle lxml.etree
Goffi <goffi@goffi.org>
parents: 3914
diff changeset
2066 Note: this is a naive implementation, adapted to XMPP, and some text content may be
3914
4cb38c8312a1 plugin XEP-0384, xml_tools: avoid `getItems` timeout + fix empty node crash + parsing:
Goffi <goffi@goffi.org>
parents: 3903
diff changeset
2067 missing (content put after a tag, i.e. what would go to the "tail" attribute of ET
4cb38c8312a1 plugin XEP-0384, xml_tools: avoid `getItems` timeout + fix empty node crash + parsing:
Goffi <goffi@goffi.org>
parents: 3903
diff changeset
2068 Element)
4cb38c8312a1 plugin XEP-0384, xml_tools: avoid `getItems` timeout + fix empty node crash + parsing:
Goffi <goffi@goffi.org>
parents: 3903
diff changeset
2069 """
4cb38c8312a1 plugin XEP-0384, xml_tools: avoid `getItems` timeout + fix empty node crash + parsing:
Goffi <goffi@goffi.org>
parents: 3903
diff changeset
2070 tag = f"{{{elt.uri}}}{elt.name}" if elt.uri else elt.name
3955
323017a4e4d2 tools (xml_tools): `domish_elt_2_et_elt` and `et_elt_2_domish_elt` now handle lxml.etree
Goffi <goffi@goffi.org>
parents: 3914
diff changeset
2071 if lxml:
323017a4e4d2 tools (xml_tools): `domish_elt_2_et_elt` and `et_elt_2_domish_elt` now handle lxml.etree
Goffi <goffi@goffi.org>
parents: 3914
diff changeset
2072 et_elt = etree.Element(tag, attr=elt.attributes)
323017a4e4d2 tools (xml_tools): `domish_elt_2_et_elt` and `et_elt_2_domish_elt` now handle lxml.etree
Goffi <goffi@goffi.org>
parents: 3914
diff changeset
2073 else:
323017a4e4d2 tools (xml_tools): `domish_elt_2_et_elt` and `et_elt_2_domish_elt` now handle lxml.etree
Goffi <goffi@goffi.org>
parents: 3914
diff changeset
2074 et_elt = ET.Element(tag, attrib=elt.attributes)
3914
4cb38c8312a1 plugin XEP-0384, xml_tools: avoid `getItems` timeout + fix empty node crash + parsing:
Goffi <goffi@goffi.org>
parents: 3903
diff changeset
2075 content = str(elt)
4cb38c8312a1 plugin XEP-0384, xml_tools: avoid `getItems` timeout + fix empty node crash + parsing:
Goffi <goffi@goffi.org>
parents: 3903
diff changeset
2076 if content:
4cb38c8312a1 plugin XEP-0384, xml_tools: avoid `getItems` timeout + fix empty node crash + parsing:
Goffi <goffi@goffi.org>
parents: 3903
diff changeset
2077 et_elt.text = str(elt)
4cb38c8312a1 plugin XEP-0384, xml_tools: avoid `getItems` timeout + fix empty node crash + parsing:
Goffi <goffi@goffi.org>
parents: 3903
diff changeset
2078 for child in elt.elements():
3955
323017a4e4d2 tools (xml_tools): `domish_elt_2_et_elt` and `et_elt_2_domish_elt` now handle lxml.etree
Goffi <goffi@goffi.org>
parents: 3914
diff changeset
2079 et_elt.append(domish_elt_2_et_elt(child, lxml=lxml))
3914
4cb38c8312a1 plugin XEP-0384, xml_tools: avoid `getItems` timeout + fix empty node crash + parsing:
Goffi <goffi@goffi.org>
parents: 3903
diff changeset
2080 return et_elt
3967
f461f11ea176 plugin XEP-0384: Implementation of Automatic Trust Management:
Syndace <me@syndace.dev>
parents: 3955
diff changeset
2081
f461f11ea176 plugin XEP-0384: Implementation of Automatic Trust Management:
Syndace <me@syndace.dev>
parents: 3955
diff changeset
2082
f461f11ea176 plugin XEP-0384: Implementation of Automatic Trust Management:
Syndace <me@syndace.dev>
parents: 3955
diff changeset
2083 def domish_elt_2_et_elt2(element: domish.Element) -> ET.Element:
f461f11ea176 plugin XEP-0384: Implementation of Automatic Trust Management:
Syndace <me@syndace.dev>
parents: 3955
diff changeset
2084 """
f461f11ea176 plugin XEP-0384: Implementation of Automatic Trust Management:
Syndace <me@syndace.dev>
parents: 3955
diff changeset
2085 WIP, originally from the OMEMO plugin
f461f11ea176 plugin XEP-0384: Implementation of Automatic Trust Management:
Syndace <me@syndace.dev>
parents: 3955
diff changeset
2086 """
f461f11ea176 plugin XEP-0384: Implementation of Automatic Trust Management:
Syndace <me@syndace.dev>
parents: 3955
diff changeset
2087
f461f11ea176 plugin XEP-0384: Implementation of Automatic Trust Management:
Syndace <me@syndace.dev>
parents: 3955
diff changeset
2088 element_name = element.name
f461f11ea176 plugin XEP-0384: Implementation of Automatic Trust Management:
Syndace <me@syndace.dev>
parents: 3955
diff changeset
2089 if element.uri is not None:
f461f11ea176 plugin XEP-0384: Implementation of Automatic Trust Management:
Syndace <me@syndace.dev>
parents: 3955
diff changeset
2090 element_name = "{" + element.uri + "}" + element_name
f461f11ea176 plugin XEP-0384: Implementation of Automatic Trust Management:
Syndace <me@syndace.dev>
parents: 3955
diff changeset
2091
f461f11ea176 plugin XEP-0384: Implementation of Automatic Trust Management:
Syndace <me@syndace.dev>
parents: 3955
diff changeset
2092 attrib: Dict[str, str] = {}
f461f11ea176 plugin XEP-0384: Implementation of Automatic Trust Management:
Syndace <me@syndace.dev>
parents: 3955
diff changeset
2093 for qname, value in element.attributes.items():
f461f11ea176 plugin XEP-0384: Implementation of Automatic Trust Management:
Syndace <me@syndace.dev>
parents: 3955
diff changeset
2094 attribute_name = qname[1] if isinstance(qname, tuple) else qname
f461f11ea176 plugin XEP-0384: Implementation of Automatic Trust Management:
Syndace <me@syndace.dev>
parents: 3955
diff changeset
2095 attribute_namespace = qname[0] if isinstance(qname, tuple) else None
f461f11ea176 plugin XEP-0384: Implementation of Automatic Trust Management:
Syndace <me@syndace.dev>
parents: 3955
diff changeset
2096 if attribute_namespace is not None:
f461f11ea176 plugin XEP-0384: Implementation of Automatic Trust Management:
Syndace <me@syndace.dev>
parents: 3955
diff changeset
2097 attribute_name = "{" + attribute_namespace + "}" + attribute_name
f461f11ea176 plugin XEP-0384: Implementation of Automatic Trust Management:
Syndace <me@syndace.dev>
parents: 3955
diff changeset
2098
f461f11ea176 plugin XEP-0384: Implementation of Automatic Trust Management:
Syndace <me@syndace.dev>
parents: 3955
diff changeset
2099 attrib[attribute_name] = value
f461f11ea176 plugin XEP-0384: Implementation of Automatic Trust Management:
Syndace <me@syndace.dev>
parents: 3955
diff changeset
2100
f461f11ea176 plugin XEP-0384: Implementation of Automatic Trust Management:
Syndace <me@syndace.dev>
parents: 3955
diff changeset
2101 result = ET.Element(element_name, attrib)
f461f11ea176 plugin XEP-0384: Implementation of Automatic Trust Management:
Syndace <me@syndace.dev>
parents: 3955
diff changeset
2102
f461f11ea176 plugin XEP-0384: Implementation of Automatic Trust Management:
Syndace <me@syndace.dev>
parents: 3955
diff changeset
2103 last_child: Optional[ET.Element] = None
f461f11ea176 plugin XEP-0384: Implementation of Automatic Trust Management:
Syndace <me@syndace.dev>
parents: 3955
diff changeset
2104 for child in element.children:
f461f11ea176 plugin XEP-0384: Implementation of Automatic Trust Management:
Syndace <me@syndace.dev>
parents: 3955
diff changeset
2105 if isinstance(child, str):
f461f11ea176 plugin XEP-0384: Implementation of Automatic Trust Management:
Syndace <me@syndace.dev>
parents: 3955
diff changeset
2106 if last_child is None:
f461f11ea176 plugin XEP-0384: Implementation of Automatic Trust Management:
Syndace <me@syndace.dev>
parents: 3955
diff changeset
2107 result.text = child
f461f11ea176 plugin XEP-0384: Implementation of Automatic Trust Management:
Syndace <me@syndace.dev>
parents: 3955
diff changeset
2108 else:
f461f11ea176 plugin XEP-0384: Implementation of Automatic Trust Management:
Syndace <me@syndace.dev>
parents: 3955
diff changeset
2109 last_child.tail = child
f461f11ea176 plugin XEP-0384: Implementation of Automatic Trust Management:
Syndace <me@syndace.dev>
parents: 3955
diff changeset
2110 else:
f461f11ea176 plugin XEP-0384: Implementation of Automatic Trust Management:
Syndace <me@syndace.dev>
parents: 3955
diff changeset
2111 last_child = domish_elt_2_et_elt2(child)
f461f11ea176 plugin XEP-0384: Implementation of Automatic Trust Management:
Syndace <me@syndace.dev>
parents: 3955
diff changeset
2112 result.append(last_child)
f461f11ea176 plugin XEP-0384: Implementation of Automatic Trust Management:
Syndace <me@syndace.dev>
parents: 3955
diff changeset
2113
f461f11ea176 plugin XEP-0384: Implementation of Automatic Trust Management:
Syndace <me@syndace.dev>
parents: 3955
diff changeset
2114 return result