Mercurial > libervia-backend
annotate sat/tools/common/template_xmlui.py @ 3136:9d0df638c8b4
dates update
author | Goffi <goffi@goffi.org> |
---|---|
date | Wed, 29 Jan 2020 09:50:54 +0100 |
parents | ab2696e34d29 |
children | 559a625a236b |
rev | line source |
---|---|
3028 | 1 #!/usr/bin/env python3 |
2362 | 2 # -*- coding: utf-8 -*- |
3 | |
4 # SAT: a jabber client | |
3136 | 5 # Copyright (C) 2009-2020 Jérôme Poisson (goffi@goffi.org) |
2362 | 6 |
7 # This program is free software: you can redistribute it and/or modify | |
8 # it under the terms of the GNU Affero General Public License as published by | |
9 # the Free Software Foundation, either version 3 of the License, or | |
10 # (at your option) any later version. | |
11 | |
12 # This program is distributed in the hope that it will be useful, | |
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
15 # GNU Affero General Public License for more details. | |
16 | |
17 # You should have received a copy of the GNU Affero General Public License | |
18 # along with this program. If not, see <http://www.gnu.org/licenses/>. | |
19 | |
20 """ template XMLUI parsing | |
21 | |
22 XMLUI classes from this modules can then be iterated to create the template | |
23 """ | |
24 | |
25 from sat.core.log import getLogger | |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
26 |
2362 | 27 log = getLogger(__name__) |
28 from sat_frontends.tools import xmlui | |
2669
bdb8276fd2da
frontends (xmlui): class_map is now an arg of create function:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
29 from functools import partial |
2783
442ab697f831
frontends, jp, templates: added XHTMLBox widget:
Goffi <goffi@goffi.org>
parents:
2771
diff
changeset
|
30 try: |
442ab697f831
frontends, jp, templates: added XHTMLBox widget:
Goffi <goffi@goffi.org>
parents:
2771
diff
changeset
|
31 from jinja2 import Markup as safe |
442ab697f831
frontends, jp, templates: added XHTMLBox widget:
Goffi <goffi@goffi.org>
parents:
2771
diff
changeset
|
32 except ImportError: |
442ab697f831
frontends, jp, templates: added XHTMLBox widget:
Goffi <goffi@goffi.org>
parents:
2771
diff
changeset
|
33 # Safe marks XHTML values as usable as it. |
442ab697f831
frontends, jp, templates: added XHTMLBox widget:
Goffi <goffi@goffi.org>
parents:
2771
diff
changeset
|
34 # If jinja2 is not there, we can use a simple lamba |
442ab697f831
frontends, jp, templates: added XHTMLBox widget:
Goffi <goffi@goffi.org>
parents:
2771
diff
changeset
|
35 safe = lambda x: x |
2362 | 36 |
37 | |
38 ## Widgets ## | |
39 | |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
40 |
2362 | 41 class Widget(object): |
3028 | 42 category = "widget" |
2362 | 43 type = None |
44 enabled = True | |
45 read_only = True | |
46 | |
47 def __init__(self, xmlui_parent): | |
48 self.xmlui_parent = xmlui_parent | |
49 | |
50 @property | |
51 def name(self): | |
52 return self._xmlui_name | |
53 | |
54 | |
55 class ValueWidget(Widget): | |
56 def __init__(self, xmlui_parent, value): | |
57 super(ValueWidget, self).__init__(xmlui_parent) | |
58 self.value = value | |
59 | |
2367
9878635586f3
template (xmlui): added values property to be able to use always values even when there is only one value
Goffi <goffi@goffi.org>
parents:
2362
diff
changeset
|
60 @property |
9878635586f3
template (xmlui): added values property to be able to use always values even when there is only one value
Goffi <goffi@goffi.org>
parents:
2362
diff
changeset
|
61 def values(self): |
9878635586f3
template (xmlui): added values property to be able to use always values even when there is only one value
Goffi <goffi@goffi.org>
parents:
2362
diff
changeset
|
62 return [self.value] |
9878635586f3
template (xmlui): added values property to be able to use always values even when there is only one value
Goffi <goffi@goffi.org>
parents:
2362
diff
changeset
|
63 |
2455
7b02372f8734
template (xmlui): added labels property in ValueWidget (actually return values) and values property in OptionsWidget
Goffi <goffi@goffi.org>
parents:
2435
diff
changeset
|
64 @property |
7b02372f8734
template (xmlui): added labels property in ValueWidget (actually return values) and values property in OptionsWidget
Goffi <goffi@goffi.org>
parents:
2435
diff
changeset
|
65 def labels(self): |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
66 # helper property, there is not label for ValueWidget |
2455
7b02372f8734
template (xmlui): added labels property in ValueWidget (actually return values) and values property in OptionsWidget
Goffi <goffi@goffi.org>
parents:
2435
diff
changeset
|
67 # but using labels make rendering more easy (one single method to call) |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
68 # values are actually returned |
2455
7b02372f8734
template (xmlui): added labels property in ValueWidget (actually return values) and values property in OptionsWidget
Goffi <goffi@goffi.org>
parents:
2435
diff
changeset
|
69 return [self.value] |
7b02372f8734
template (xmlui): added labels property in ValueWidget (actually return values) and values property in OptionsWidget
Goffi <goffi@goffi.org>
parents:
2435
diff
changeset
|
70 |
2362 | 71 |
72 class InputWidget(ValueWidget): | |
73 def __init__(self, xmlui_parent, value, read_only=False): | |
74 super(InputWidget, self).__init__(xmlui_parent, value) | |
75 self.read_only = read_only | |
76 | |
77 | |
78 class OptionsWidget(Widget): | |
79 def __init__(self, xmlui_parent, options, selected, style): | |
80 super(OptionsWidget, self).__init__(xmlui_parent) | |
81 self.options = options | |
82 self.selected = selected | |
83 self.style = style | |
84 | |
2379
42a54cbc1872
template (xmlui): new properties + inheritance fix:
Goffi <goffi@goffi.org>
parents:
2367
diff
changeset
|
85 @property |
42a54cbc1872
template (xmlui): new properties + inheritance fix:
Goffi <goffi@goffi.org>
parents:
2367
diff
changeset
|
86 def values(self): |
2455
7b02372f8734
template (xmlui): added labels property in ValueWidget (actually return values) and values property in OptionsWidget
Goffi <goffi@goffi.org>
parents:
2435
diff
changeset
|
87 for value, label in self.items: |
7b02372f8734
template (xmlui): added labels property in ValueWidget (actually return values) and values property in OptionsWidget
Goffi <goffi@goffi.org>
parents:
2435
diff
changeset
|
88 yield value |
2379
42a54cbc1872
template (xmlui): new properties + inheritance fix:
Goffi <goffi@goffi.org>
parents:
2367
diff
changeset
|
89 |
42a54cbc1872
template (xmlui): new properties + inheritance fix:
Goffi <goffi@goffi.org>
parents:
2367
diff
changeset
|
90 @property |
42a54cbc1872
template (xmlui): new properties + inheritance fix:
Goffi <goffi@goffi.org>
parents:
2367
diff
changeset
|
91 def labels(self): |
2400
8253ea069781
template(XMLUI): added items property to ListWidget:
Goffi <goffi@goffi.org>
parents:
2397
diff
changeset
|
92 """return only labels from self.items""" |
8253ea069781
template(XMLUI): added items property to ListWidget:
Goffi <goffi@goffi.org>
parents:
2397
diff
changeset
|
93 for value, label in self.items: |
8253ea069781
template(XMLUI): added items property to ListWidget:
Goffi <goffi@goffi.org>
parents:
2397
diff
changeset
|
94 yield label |
8253ea069781
template(XMLUI): added items property to ListWidget:
Goffi <goffi@goffi.org>
parents:
2397
diff
changeset
|
95 |
8253ea069781
template(XMLUI): added items property to ListWidget:
Goffi <goffi@goffi.org>
parents:
2397
diff
changeset
|
96 @property |
8253ea069781
template(XMLUI): added items property to ListWidget:
Goffi <goffi@goffi.org>
parents:
2397
diff
changeset
|
97 def items(self): |
8253ea069781
template(XMLUI): added items property to ListWidget:
Goffi <goffi@goffi.org>
parents:
2397
diff
changeset
|
98 """return suitable items, according to style""" |
8253ea069781
template(XMLUI): added items property to ListWidget:
Goffi <goffi@goffi.org>
parents:
2397
diff
changeset
|
99 no_select = self.no_select |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
100 for value, label in self.options: |
2400
8253ea069781
template(XMLUI): added items property to ListWidget:
Goffi <goffi@goffi.org>
parents:
2397
diff
changeset
|
101 if no_select or value in self.selected: |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
102 yield value, label |
2379
42a54cbc1872
template (xmlui): new properties + inheritance fix:
Goffi <goffi@goffi.org>
parents:
2367
diff
changeset
|
103 |
2397
7fff98d64ab5
core (XMLUI), template(XMLUI): added flags to ListWidget:
Goffi <goffi@goffi.org>
parents:
2379
diff
changeset
|
104 @property |
7fff98d64ab5
core (XMLUI), template(XMLUI): added flags to ListWidget:
Goffi <goffi@goffi.org>
parents:
2379
diff
changeset
|
105 def inline(self): |
3028 | 106 return "inline" in self.style |
2397
7fff98d64ab5
core (XMLUI), template(XMLUI): added flags to ListWidget:
Goffi <goffi@goffi.org>
parents:
2379
diff
changeset
|
107 |
7fff98d64ab5
core (XMLUI), template(XMLUI): added flags to ListWidget:
Goffi <goffi@goffi.org>
parents:
2379
diff
changeset
|
108 @property |
7fff98d64ab5
core (XMLUI), template(XMLUI): added flags to ListWidget:
Goffi <goffi@goffi.org>
parents:
2379
diff
changeset
|
109 def no_select(self): |
3028 | 110 return "noselect" in self.style |
2397
7fff98d64ab5
core (XMLUI), template(XMLUI): added flags to ListWidget:
Goffi <goffi@goffi.org>
parents:
2379
diff
changeset
|
111 |
2362 | 112 |
113 class EmptyWidget(xmlui.EmptyWidget, Widget): | |
114 def __init__(self, _xmlui_parent): | |
115 Widget.__init__(self) | |
116 | |
117 | |
118 class TextWidget(xmlui.TextWidget, ValueWidget): | |
3028 | 119 type = "text" |
2362 | 120 |
121 | |
122 class LabelWidget(xmlui.LabelWidget, ValueWidget): | |
3028 | 123 type = "label" |
2362 | 124 |
2379
42a54cbc1872
template (xmlui): new properties + inheritance fix:
Goffi <goffi@goffi.org>
parents:
2367
diff
changeset
|
125 @property |
42a54cbc1872
template (xmlui): new properties + inheritance fix:
Goffi <goffi@goffi.org>
parents:
2367
diff
changeset
|
126 def for_name(self): |
42a54cbc1872
template (xmlui): new properties + inheritance fix:
Goffi <goffi@goffi.org>
parents:
2367
diff
changeset
|
127 try: |
42a54cbc1872
template (xmlui): new properties + inheritance fix:
Goffi <goffi@goffi.org>
parents:
2367
diff
changeset
|
128 return self._xmlui_for_name |
42a54cbc1872
template (xmlui): new properties + inheritance fix:
Goffi <goffi@goffi.org>
parents:
2367
diff
changeset
|
129 except AttributeError: |
42a54cbc1872
template (xmlui): new properties + inheritance fix:
Goffi <goffi@goffi.org>
parents:
2367
diff
changeset
|
130 return None |
2362 | 131 |
2379
42a54cbc1872
template (xmlui): new properties + inheritance fix:
Goffi <goffi@goffi.org>
parents:
2367
diff
changeset
|
132 |
42a54cbc1872
template (xmlui): new properties + inheritance fix:
Goffi <goffi@goffi.org>
parents:
2367
diff
changeset
|
133 class StringWidget(xmlui.StringWidget, InputWidget): |
3028 | 134 type = "string" |
2362 | 135 |
136 | |
2435
49884c579266
template (XMLUI): added JidInputWidget
Goffi <goffi@goffi.org>
parents:
2419
diff
changeset
|
137 class JidInputWidget(xmlui.JidInputWidget, StringWidget): |
3028 | 138 type = "jid" |
2435
49884c579266
template (XMLUI): added JidInputWidget
Goffi <goffi@goffi.org>
parents:
2419
diff
changeset
|
139 |
49884c579266
template (XMLUI): added JidInputWidget
Goffi <goffi@goffi.org>
parents:
2419
diff
changeset
|
140 |
2379
42a54cbc1872
template (xmlui): new properties + inheritance fix:
Goffi <goffi@goffi.org>
parents:
2367
diff
changeset
|
141 class TextBoxWidget(xmlui.TextWidget, InputWidget): |
3028 | 142 type = "textbox" |
2362 | 143 |
144 | |
2783
442ab697f831
frontends, jp, templates: added XHTMLBox widget:
Goffi <goffi@goffi.org>
parents:
2771
diff
changeset
|
145 class XHTMLBoxWidget(xmlui.XHTMLBoxWidget, InputWidget): |
3028 | 146 type = "xhtmlbox" |
2783
442ab697f831
frontends, jp, templates: added XHTMLBox widget:
Goffi <goffi@goffi.org>
parents:
2771
diff
changeset
|
147 |
442ab697f831
frontends, jp, templates: added XHTMLBox widget:
Goffi <goffi@goffi.org>
parents:
2771
diff
changeset
|
148 def __init__(self, xmlui_parent, value, read_only=False): |
442ab697f831
frontends, jp, templates: added XHTMLBox widget:
Goffi <goffi@goffi.org>
parents:
2771
diff
changeset
|
149 # XXX: XHTMLBoxWidget value must be cleaned (harmful XHTML must be removed) |
442ab697f831
frontends, jp, templates: added XHTMLBox widget:
Goffi <goffi@goffi.org>
parents:
2771
diff
changeset
|
150 # This is normally done in the backend, the frontends should not need to |
442ab697f831
frontends, jp, templates: added XHTMLBox widget:
Goffi <goffi@goffi.org>
parents:
2771
diff
changeset
|
151 # worry about it. |
442ab697f831
frontends, jp, templates: added XHTMLBox widget:
Goffi <goffi@goffi.org>
parents:
2771
diff
changeset
|
152 super(XHTMLBoxWidget, self).__init__( |
442ab697f831
frontends, jp, templates: added XHTMLBox widget:
Goffi <goffi@goffi.org>
parents:
2771
diff
changeset
|
153 xmlui_parent=xmlui_parent, value=safe(value), read_only=read_only) |
442ab697f831
frontends, jp, templates: added XHTMLBox widget:
Goffi <goffi@goffi.org>
parents:
2771
diff
changeset
|
154 |
442ab697f831
frontends, jp, templates: added XHTMLBox widget:
Goffi <goffi@goffi.org>
parents:
2771
diff
changeset
|
155 |
2362 | 156 class ListWidget(xmlui.ListWidget, OptionsWidget): |
3028 | 157 type = "list" |
2362 | 158 |
159 | |
160 ## Containers ## | |
161 | |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
162 |
2362 | 163 class Container(object): |
3028 | 164 category = "container" |
2362 | 165 type = None |
166 | |
167 def __init__(self, xmlui_parent): | |
168 self.xmlui_parent = xmlui_parent | |
169 self.children = [] | |
170 | |
171 def __iter__(self): | |
172 return iter(self.children) | |
173 | |
174 def _xmluiAppend(self, widget): | |
175 self.children.append(widget) | |
176 | |
2419
c38c54c47e16
frontends (xmlui): added an attribute to ignore some widgets (and their label) in create
Goffi <goffi@goffi.org>
parents:
2414
diff
changeset
|
177 def _xmluiRemove(self, widget): |
c38c54c47e16
frontends (xmlui): added an attribute to ignore some widgets (and their label) in create
Goffi <goffi@goffi.org>
parents:
2414
diff
changeset
|
178 self.children.remove(widget) |
c38c54c47e16
frontends (xmlui): added an attribute to ignore some widgets (and their label) in create
Goffi <goffi@goffi.org>
parents:
2414
diff
changeset
|
179 |
2362 | 180 |
181 class VerticalContainer(xmlui.VerticalContainer, Container): | |
3028 | 182 type = "vertical" |
2362 | 183 |
184 | |
185 class PairsContainer(xmlui.PairsContainer, Container): | |
3028 | 186 type = "pairs" |
2362 | 187 |
188 | |
189 class LabelContainer(xmlui.PairsContainer, Container): | |
3028 | 190 type = "label" |
2362 | 191 |
192 | |
193 ## Factory ## | |
194 | |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
195 |
2362 | 196 class WidgetFactory(object): |
2669
bdb8276fd2da
frontends (xmlui): class_map is now an arg of create function:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
197 |
2362 | 198 def __getattr__(self, attr): |
199 if attr.startswith("create"): | |
200 cls = globals()[attr[6:]] | |
201 return cls | |
202 | |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
203 |
2362 | 204 ## Core ## |
205 | |
206 | |
207 class XMLUIPanel(xmlui.XMLUIPanel): | |
208 widget_factory = WidgetFactory() | |
209 | |
210 def show(self, *args, **kwargs): | |
211 raise NotImplementedError | |
212 | |
213 | |
214 class XMLUIDialog(xmlui.XMLUIDialog): | |
215 dialog_factory = WidgetFactory() | |
216 | |
217 def __init__(*args, **kwargs): | |
218 raise NotImplementedError | |
219 | |
220 | |
2669
bdb8276fd2da
frontends (xmlui): class_map is now an arg of create function:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
221 create = partial(xmlui.create, class_map={ |
bdb8276fd2da
frontends (xmlui): class_map is now an arg of create function:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
222 xmlui.CLASS_PANEL: XMLUIPanel, |
bdb8276fd2da
frontends (xmlui): class_map is now an arg of create function:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
223 xmlui.CLASS_DIALOG: XMLUIDialog}) |