Mercurial > libervia-backend
annotate sat/tools/common/template_xmlui.py @ 2774:95321f233387
plugin pubsub schema: code formatting minor update
author | Goffi <goffi@goffi.org> |
---|---|
date | Sat, 12 Jan 2019 16:50:13 +0100 |
parents | 003b8b4b56a7 |
children | 442ab697f831 |
rev | line source |
---|---|
2362 | 1 #!/usr/bin/env python2 |
2 # -*- coding: utf-8 -*- | |
3 | |
4 # SAT: a jabber client | |
2771 | 5 # Copyright (C) 2009-2019 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 |
2362 | 30 |
31 | |
32 ## Widgets ## | |
33 | |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
34 |
2362 | 35 class Widget(object): |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
36 category = u"widget" |
2362 | 37 type = None |
38 enabled = True | |
39 read_only = True | |
40 | |
41 def __init__(self, xmlui_parent): | |
42 self.xmlui_parent = xmlui_parent | |
43 | |
44 @property | |
45 def name(self): | |
46 return self._xmlui_name | |
47 | |
48 | |
49 class ValueWidget(Widget): | |
50 def __init__(self, xmlui_parent, value): | |
51 super(ValueWidget, self).__init__(xmlui_parent) | |
52 self.value = value | |
53 | |
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
|
54 @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
|
55 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
|
56 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
|
57 |
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
|
58 @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
|
59 def labels(self): |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
60 # 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
|
61 # 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
|
62 # 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
|
63 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
|
64 |
2362 | 65 |
66 class InputWidget(ValueWidget): | |
67 def __init__(self, xmlui_parent, value, read_only=False): | |
68 super(InputWidget, self).__init__(xmlui_parent, value) | |
69 self.read_only = read_only | |
70 | |
71 | |
72 class OptionsWidget(Widget): | |
73 def __init__(self, xmlui_parent, options, selected, style): | |
74 super(OptionsWidget, self).__init__(xmlui_parent) | |
75 self.options = options | |
76 self.selected = selected | |
77 self.style = style | |
78 | |
2379
42a54cbc1872
template (xmlui): new properties + inheritance fix:
Goffi <goffi@goffi.org>
parents:
2367
diff
changeset
|
79 @property |
42a54cbc1872
template (xmlui): new properties + inheritance fix:
Goffi <goffi@goffi.org>
parents:
2367
diff
changeset
|
80 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
|
81 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
|
82 yield value |
2379
42a54cbc1872
template (xmlui): new properties + inheritance fix:
Goffi <goffi@goffi.org>
parents:
2367
diff
changeset
|
83 |
42a54cbc1872
template (xmlui): new properties + inheritance fix:
Goffi <goffi@goffi.org>
parents:
2367
diff
changeset
|
84 @property |
42a54cbc1872
template (xmlui): new properties + inheritance fix:
Goffi <goffi@goffi.org>
parents:
2367
diff
changeset
|
85 def labels(self): |
2400
8253ea069781
template(XMLUI): added items property to ListWidget:
Goffi <goffi@goffi.org>
parents:
2397
diff
changeset
|
86 """return only labels from self.items""" |
8253ea069781
template(XMLUI): added items property to ListWidget:
Goffi <goffi@goffi.org>
parents:
2397
diff
changeset
|
87 for value, label in self.items: |
8253ea069781
template(XMLUI): added items property to ListWidget:
Goffi <goffi@goffi.org>
parents:
2397
diff
changeset
|
88 yield label |
8253ea069781
template(XMLUI): added items property to ListWidget:
Goffi <goffi@goffi.org>
parents:
2397
diff
changeset
|
89 |
8253ea069781
template(XMLUI): added items property to ListWidget:
Goffi <goffi@goffi.org>
parents:
2397
diff
changeset
|
90 @property |
8253ea069781
template(XMLUI): added items property to ListWidget:
Goffi <goffi@goffi.org>
parents:
2397
diff
changeset
|
91 def items(self): |
8253ea069781
template(XMLUI): added items property to ListWidget:
Goffi <goffi@goffi.org>
parents:
2397
diff
changeset
|
92 """return suitable items, according to style""" |
8253ea069781
template(XMLUI): added items property to ListWidget:
Goffi <goffi@goffi.org>
parents:
2397
diff
changeset
|
93 no_select = self.no_select |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
94 for value, label in self.options: |
2400
8253ea069781
template(XMLUI): added items property to ListWidget:
Goffi <goffi@goffi.org>
parents:
2397
diff
changeset
|
95 if no_select or value in self.selected: |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
96 yield value, label |
2379
42a54cbc1872
template (xmlui): new properties + inheritance fix:
Goffi <goffi@goffi.org>
parents:
2367
diff
changeset
|
97 |
2397
7fff98d64ab5
core (XMLUI), template(XMLUI): added flags to ListWidget:
Goffi <goffi@goffi.org>
parents:
2379
diff
changeset
|
98 @property |
7fff98d64ab5
core (XMLUI), template(XMLUI): added flags to ListWidget:
Goffi <goffi@goffi.org>
parents:
2379
diff
changeset
|
99 def inline(self): |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
100 return u"inline" in self.style |
2397
7fff98d64ab5
core (XMLUI), template(XMLUI): added flags to ListWidget:
Goffi <goffi@goffi.org>
parents:
2379
diff
changeset
|
101 |
7fff98d64ab5
core (XMLUI), template(XMLUI): added flags to ListWidget:
Goffi <goffi@goffi.org>
parents:
2379
diff
changeset
|
102 @property |
7fff98d64ab5
core (XMLUI), template(XMLUI): added flags to ListWidget:
Goffi <goffi@goffi.org>
parents:
2379
diff
changeset
|
103 def no_select(self): |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
104 return u"noselect" in self.style |
2397
7fff98d64ab5
core (XMLUI), template(XMLUI): added flags to ListWidget:
Goffi <goffi@goffi.org>
parents:
2379
diff
changeset
|
105 |
2362 | 106 |
107 class EmptyWidget(xmlui.EmptyWidget, Widget): | |
108 def __init__(self, _xmlui_parent): | |
109 Widget.__init__(self) | |
110 | |
111 | |
112 class TextWidget(xmlui.TextWidget, ValueWidget): | |
113 type = u"text" | |
114 | |
115 | |
116 class LabelWidget(xmlui.LabelWidget, ValueWidget): | |
117 type = u"label" | |
118 | |
2379
42a54cbc1872
template (xmlui): new properties + inheritance fix:
Goffi <goffi@goffi.org>
parents:
2367
diff
changeset
|
119 @property |
42a54cbc1872
template (xmlui): new properties + inheritance fix:
Goffi <goffi@goffi.org>
parents:
2367
diff
changeset
|
120 def for_name(self): |
42a54cbc1872
template (xmlui): new properties + inheritance fix:
Goffi <goffi@goffi.org>
parents:
2367
diff
changeset
|
121 try: |
42a54cbc1872
template (xmlui): new properties + inheritance fix:
Goffi <goffi@goffi.org>
parents:
2367
diff
changeset
|
122 return self._xmlui_for_name |
42a54cbc1872
template (xmlui): new properties + inheritance fix:
Goffi <goffi@goffi.org>
parents:
2367
diff
changeset
|
123 except AttributeError: |
42a54cbc1872
template (xmlui): new properties + inheritance fix:
Goffi <goffi@goffi.org>
parents:
2367
diff
changeset
|
124 return None |
2362 | 125 |
2379
42a54cbc1872
template (xmlui): new properties + inheritance fix:
Goffi <goffi@goffi.org>
parents:
2367
diff
changeset
|
126 |
42a54cbc1872
template (xmlui): new properties + inheritance fix:
Goffi <goffi@goffi.org>
parents:
2367
diff
changeset
|
127 class StringWidget(xmlui.StringWidget, InputWidget): |
2362 | 128 type = u"string" |
129 | |
130 | |
2435
49884c579266
template (XMLUI): added JidInputWidget
Goffi <goffi@goffi.org>
parents:
2419
diff
changeset
|
131 class JidInputWidget(xmlui.JidInputWidget, StringWidget): |
49884c579266
template (XMLUI): added JidInputWidget
Goffi <goffi@goffi.org>
parents:
2419
diff
changeset
|
132 type = u"jid" |
49884c579266
template (XMLUI): added JidInputWidget
Goffi <goffi@goffi.org>
parents:
2419
diff
changeset
|
133 |
49884c579266
template (XMLUI): added JidInputWidget
Goffi <goffi@goffi.org>
parents:
2419
diff
changeset
|
134 |
2379
42a54cbc1872
template (xmlui): new properties + inheritance fix:
Goffi <goffi@goffi.org>
parents:
2367
diff
changeset
|
135 class TextBoxWidget(xmlui.TextWidget, InputWidget): |
2362 | 136 type = u"textbox" |
137 | |
138 | |
139 class ListWidget(xmlui.ListWidget, OptionsWidget): | |
140 type = u"list" | |
141 | |
142 | |
143 ## Containers ## | |
144 | |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
145 |
2362 | 146 class Container(object): |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
147 category = u"container" |
2362 | 148 type = None |
149 | |
150 def __init__(self, xmlui_parent): | |
151 self.xmlui_parent = xmlui_parent | |
152 self.children = [] | |
153 | |
154 def __iter__(self): | |
155 return iter(self.children) | |
156 | |
157 def _xmluiAppend(self, widget): | |
158 self.children.append(widget) | |
159 | |
2419
c38c54c47e16
frontends (xmlui): added an attribute to ignore some widgets (and their label) in create
Goffi <goffi@goffi.org>
parents:
2414
diff
changeset
|
160 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
|
161 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
|
162 |
2362 | 163 |
164 class VerticalContainer(xmlui.VerticalContainer, Container): | |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
165 type = u"vertical" |
2362 | 166 |
167 | |
168 class PairsContainer(xmlui.PairsContainer, Container): | |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
169 type = u"pairs" |
2362 | 170 |
171 | |
172 class LabelContainer(xmlui.PairsContainer, Container): | |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
173 type = u"label" |
2362 | 174 |
175 | |
176 ## Factory ## | |
177 | |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
178 |
2362 | 179 class WidgetFactory(object): |
2669
bdb8276fd2da
frontends (xmlui): class_map is now an arg of create function:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
180 |
2362 | 181 def __getattr__(self, attr): |
182 if attr.startswith("create"): | |
183 cls = globals()[attr[6:]] | |
184 return cls | |
185 | |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
186 |
2362 | 187 ## Core ## |
188 | |
189 | |
190 class XMLUIPanel(xmlui.XMLUIPanel): | |
191 widget_factory = WidgetFactory() | |
192 | |
193 def show(self, *args, **kwargs): | |
194 raise NotImplementedError | |
195 | |
196 | |
197 class XMLUIDialog(xmlui.XMLUIDialog): | |
198 dialog_factory = WidgetFactory() | |
199 | |
200 def __init__(*args, **kwargs): | |
201 raise NotImplementedError | |
202 | |
203 | |
2669
bdb8276fd2da
frontends (xmlui): class_map is now an arg of create function:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
204 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
|
205 xmlui.CLASS_PANEL: XMLUIPanel, |
bdb8276fd2da
frontends (xmlui): class_map is now an arg of create function:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
206 xmlui.CLASS_DIALOG: XMLUIDialog}) |