Mercurial > libervia-backend
annotate sat_frontends/jp/xmlui_manager.py @ 3382:3b08caa805e7
plugin app manager docker: typo
author | Goffi <goffi@goffi.org> |
---|---|
date | Tue, 20 Oct 2020 09:12:38 +0200 |
parents | 89d97776fd34 |
children | 2f0be2b7de68 |
rev | line source |
---|---|
3137 | 1 #!/usr/bin/env python3 |
2 | |
2408 | 3 |
4 # JP: a SàT frontend | |
3136 | 5 # Copyright (C) 2009-2020 Jérôme Poisson (goffi@goffi.org) |
2408 | 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 | |
3040 | 20 from functools import partial |
2408 | 21 from sat.core.log import getLogger |
2669
bdb8276fd2da
frontends (xmlui): class_map is now an arg of create function:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
22 from sat_frontends.tools import xmlui as xmlui_base |
2408 | 23 from sat_frontends.jp.constants import Const as C |
24 from sat.tools.common.ansi import ANSI as A | |
25 from sat.core.i18n import _ | |
3040 | 26 |
27 log = getLogger(__name__) | |
2408 | 28 |
29 # workflow constants | |
30 | |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
31 SUBMIT = "SUBMIT" # submit form |
2408 | 32 |
33 | |
34 ## Widgets ## | |
35 | |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
36 |
2408 | 37 class Base(object): |
38 """Base for Widget and Container""" | |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
39 |
2408 | 40 type = None |
41 _root = None | |
42 | |
43 def __init__(self, xmlui_parent): | |
44 self.xmlui_parent = xmlui_parent | |
45 self.host = self.xmlui_parent.host | |
46 | |
47 @property | |
48 def root(self): | |
49 """retrieve main XMLUI parent class""" | |
50 if self._root is not None: | |
51 return self._root | |
52 root = self | |
2669
bdb8276fd2da
frontends (xmlui): class_map is now an arg of create function:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
53 while not isinstance(root, xmlui_base.XMLUIBase): |
2408 | 54 root = root.xmlui_parent |
55 self._root = root | |
56 return root | |
57 | |
58 def disp(self, *args, **kwargs): | |
59 self.host.disp(*args, **kwargs) | |
60 | |
61 | |
62 class Widget(Base): | |
3028 | 63 category = "widget" |
2408 | 64 enabled = True |
65 | |
66 @property | |
67 def name(self): | |
68 return self._xmlui_name | |
69 | |
3040 | 70 async def show(self): |
2408 | 71 """display current widget |
72 | |
73 must be overriden by subclasses | |
74 """ | |
75 raise NotImplementedError(self.__class__) | |
76 | |
77 def verboseName(self, elems=None, value=None): | |
78 """add name in color to the elements | |
79 | |
80 helper method to display name which can then be used to automate commands | |
81 elems is only modified if verbosity is > 0 | |
82 @param elems(list[unicode], None): elements to display | |
83 None to display name directly | |
84 @param value(unicode, None): value to show | |
85 use self.name if None | |
86 """ | |
87 if value is None: | |
88 value = self.name | |
89 if self.host.verbosity: | |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
90 to_disp = [ |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
91 A.FG_MAGENTA, |
3028 | 92 " " if elems else "", |
93 "({})".format(value), | |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
94 A.RESET, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
95 ] |
2408 | 96 if elems is None: |
97 self.host.disp(A.color(*to_disp)) | |
98 else: | |
99 elems.extend(to_disp) | |
100 | |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
101 |
2408 | 102 class ValueWidget(Widget): |
103 def __init__(self, xmlui_parent, value): | |
104 super(ValueWidget, self).__init__(xmlui_parent) | |
105 self.value = value | |
106 | |
107 @property | |
108 def values(self): | |
109 return [self.value] | |
110 | |
111 | |
112 class InputWidget(ValueWidget): | |
113 def __init__(self, xmlui_parent, value, read_only=False): | |
114 super(InputWidget, self).__init__(xmlui_parent, value) | |
115 self.read_only = read_only | |
116 | |
117 def _xmluiGetValue(self): | |
118 return self.value | |
119 | |
120 | |
121 class OptionsWidget(Widget): | |
122 def __init__(self, xmlui_parent, options, selected, style): | |
123 super(OptionsWidget, self).__init__(xmlui_parent) | |
124 self.options = options | |
125 self.selected = selected | |
126 self.style = style | |
127 | |
128 @property | |
129 def values(self): | |
130 return self.selected | |
131 | |
132 @values.setter | |
133 def values(self, values): | |
134 self.selected = values | |
135 | |
136 @property | |
137 def value(self): | |
138 return self.selected[0] | |
139 | |
140 @value.setter | |
141 def value(self, value): | |
142 self.selected = [value] | |
143 | |
144 def _xmluiSelectValue(self, value): | |
145 self.value = value | |
146 | |
147 def _xmluiSelectValues(self, values): | |
148 self.values = values | |
149 | |
150 def _xmluiGetSelectedValues(self): | |
151 return self.values | |
152 | |
153 @property | |
154 def labels(self): | |
155 """return only labels from self.items""" | |
156 for value, label in self.items: | |
157 yield label | |
158 | |
159 @property | |
160 def items(self): | |
161 """return suitable items, according to style""" | |
162 no_select = self.no_select | |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
163 for value, label in self.options: |
2408 | 164 if no_select or value in self.selected: |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
165 yield value, label |
2408 | 166 |
167 @property | |
168 def inline(self): | |
3028 | 169 return "inline" in self.style |
2408 | 170 |
171 @property | |
172 def no_select(self): | |
3028 | 173 return "noselect" in self.style |
2408 | 174 |
175 | |
2669
bdb8276fd2da
frontends (xmlui): class_map is now an arg of create function:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
176 class EmptyWidget(xmlui_base.EmptyWidget, Widget): |
2739
e8dc00f612fb
jp (xmlui): JidWidget + small improvments:
Goffi <goffi@goffi.org>
parents:
2669
diff
changeset
|
177 |
e8dc00f612fb
jp (xmlui): JidWidget + small improvments:
Goffi <goffi@goffi.org>
parents:
2669
diff
changeset
|
178 def __init__(self, xmlui_parent): |
e8dc00f612fb
jp (xmlui): JidWidget + small improvments:
Goffi <goffi@goffi.org>
parents:
2669
diff
changeset
|
179 Widget.__init__(self, xmlui_parent) |
e8dc00f612fb
jp (xmlui): JidWidget + small improvments:
Goffi <goffi@goffi.org>
parents:
2669
diff
changeset
|
180 |
3040 | 181 async def show(self): |
3028 | 182 self.host.disp('') |
2408 | 183 |
184 | |
2669
bdb8276fd2da
frontends (xmlui): class_map is now an arg of create function:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
185 class TextWidget(xmlui_base.TextWidget, ValueWidget): |
3028 | 186 type = "text" |
2408 | 187 |
3040 | 188 async def show(self): |
2408 | 189 self.host.disp(self.value) |
190 | |
191 | |
2669
bdb8276fd2da
frontends (xmlui): class_map is now an arg of create function:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
192 class LabelWidget(xmlui_base.LabelWidget, ValueWidget): |
3028 | 193 type = "label" |
2408 | 194 |
195 @property | |
196 def for_name(self): | |
197 try: | |
198 return self._xmlui_for_name | |
199 except AttributeError: | |
200 return None | |
201 | |
3040 | 202 async def show(self, no_lf=False, ansi=""): |
2408 | 203 """show label |
204 | |
205 @param no_lf(bool): same as for [JP.disp] | |
206 @param ansi(unicode): ansi escape code to print before label | |
207 """ | |
208 self.disp(A.color(ansi, self.value), no_lf=no_lf) | |
209 | |
210 | |
2739
e8dc00f612fb
jp (xmlui): JidWidget + small improvments:
Goffi <goffi@goffi.org>
parents:
2669
diff
changeset
|
211 class JidWidget(xmlui_base.JidWidget, TextWidget): |
3028 | 212 type = "jid" |
2739
e8dc00f612fb
jp (xmlui): JidWidget + small improvments:
Goffi <goffi@goffi.org>
parents:
2669
diff
changeset
|
213 |
2669
bdb8276fd2da
frontends (xmlui): class_map is now an arg of create function:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
214 class StringWidget(xmlui_base.StringWidget, InputWidget): |
3028 | 215 type = "string" |
2408 | 216 |
3040 | 217 async def show(self): |
2961
620bbcec884c
jp (xmlui): check root read_only status in addition to widget one
Goffi <goffi@goffi.org>
parents:
2960
diff
changeset
|
218 if self.read_only or self.root.read_only: |
2408 | 219 self.disp(self.value) |
220 else: | |
221 elems = [] | |
222 self.verboseName(elems) | |
223 if self.value: | |
3040 | 224 elems.append(_(f"(enter: {self.value})")) |
3028 | 225 elems.extend([C.A_HEADER, "> "]) |
3040 | 226 value = await self.host.ainput(A.color(*elems)) |
2408 | 227 if value: |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
228 # TODO: empty value should be possible |
2408 | 229 # an escape key should be used for default instead of enter with empty value |
230 self.value = value | |
231 | |
232 | |
2669
bdb8276fd2da
frontends (xmlui): class_map is now an arg of create function:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
233 class JidInputWidget(xmlui_base.JidInputWidget, StringWidget): |
3028 | 234 type = "jid_input" |
2408 | 235 |
236 | |
2669
bdb8276fd2da
frontends (xmlui): class_map is now an arg of create function:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
237 class TextBoxWidget(xmlui_base.TextWidget, StringWidget): |
3028 | 238 type = "textbox" |
2939
18a98a541f7a
jp (xmlui manager): basic handling of multi-lines text in TextBoxWidget
Goffi <goffi@goffi.org>
parents:
2783
diff
changeset
|
239 # TODO: use a more advanced input method |
18a98a541f7a
jp (xmlui manager): basic handling of multi-lines text in TextBoxWidget
Goffi <goffi@goffi.org>
parents:
2783
diff
changeset
|
240 |
3040 | 241 async def show(self): |
2939
18a98a541f7a
jp (xmlui manager): basic handling of multi-lines text in TextBoxWidget
Goffi <goffi@goffi.org>
parents:
2783
diff
changeset
|
242 self.verboseName() |
2961
620bbcec884c
jp (xmlui): check root read_only status in addition to widget one
Goffi <goffi@goffi.org>
parents:
2960
diff
changeset
|
243 if self.read_only or self.root.read_only: |
2939
18a98a541f7a
jp (xmlui manager): basic handling of multi-lines text in TextBoxWidget
Goffi <goffi@goffi.org>
parents:
2783
diff
changeset
|
244 self.disp(self.value) |
18a98a541f7a
jp (xmlui manager): basic handling of multi-lines text in TextBoxWidget
Goffi <goffi@goffi.org>
parents:
2783
diff
changeset
|
245 else: |
18a98a541f7a
jp (xmlui manager): basic handling of multi-lines text in TextBoxWidget
Goffi <goffi@goffi.org>
parents:
2783
diff
changeset
|
246 if self.value: |
3028 | 247 self.disp(A.color(C.A_HEADER, "↓ current value ↓\n", A.FG_CYAN, self.value, |
2939
18a98a541f7a
jp (xmlui manager): basic handling of multi-lines text in TextBoxWidget
Goffi <goffi@goffi.org>
parents:
2783
diff
changeset
|
248 "")) |
18a98a541f7a
jp (xmlui manager): basic handling of multi-lines text in TextBoxWidget
Goffi <goffi@goffi.org>
parents:
2783
diff
changeset
|
249 |
18a98a541f7a
jp (xmlui manager): basic handling of multi-lines text in TextBoxWidget
Goffi <goffi@goffi.org>
parents:
2783
diff
changeset
|
250 values = [] |
18a98a541f7a
jp (xmlui manager): basic handling of multi-lines text in TextBoxWidget
Goffi <goffi@goffi.org>
parents:
2783
diff
changeset
|
251 while True: |
18a98a541f7a
jp (xmlui manager): basic handling of multi-lines text in TextBoxWidget
Goffi <goffi@goffi.org>
parents:
2783
diff
changeset
|
252 try: |
18a98a541f7a
jp (xmlui manager): basic handling of multi-lines text in TextBoxWidget
Goffi <goffi@goffi.org>
parents:
2783
diff
changeset
|
253 if not values: |
3040 | 254 line = await self.host.ainput(A.color(C.A_HEADER, "[Ctrl-D to finish]> ")) |
2939
18a98a541f7a
jp (xmlui manager): basic handling of multi-lines text in TextBoxWidget
Goffi <goffi@goffi.org>
parents:
2783
diff
changeset
|
255 else: |
3040 | 256 line = await self.host.ainput() |
2939
18a98a541f7a
jp (xmlui manager): basic handling of multi-lines text in TextBoxWidget
Goffi <goffi@goffi.org>
parents:
2783
diff
changeset
|
257 values.append(line) |
18a98a541f7a
jp (xmlui manager): basic handling of multi-lines text in TextBoxWidget
Goffi <goffi@goffi.org>
parents:
2783
diff
changeset
|
258 except EOFError: |
18a98a541f7a
jp (xmlui manager): basic handling of multi-lines text in TextBoxWidget
Goffi <goffi@goffi.org>
parents:
2783
diff
changeset
|
259 break |
18a98a541f7a
jp (xmlui manager): basic handling of multi-lines text in TextBoxWidget
Goffi <goffi@goffi.org>
parents:
2783
diff
changeset
|
260 |
3028 | 261 self.value = '\n'.join(values).rstrip() |
2408 | 262 |
263 | |
2783
442ab697f831
frontends, jp, templates: added XHTMLBox widget:
Goffi <goffi@goffi.org>
parents:
2771
diff
changeset
|
264 class XHTMLBoxWidget(xmlui_base.XHTMLBoxWidget, StringWidget): |
3028 | 265 type = "xhtmlbox" |
2783
442ab697f831
frontends, jp, templates: added XHTMLBox widget:
Goffi <goffi@goffi.org>
parents:
2771
diff
changeset
|
266 |
3040 | 267 async def show(self): |
2783
442ab697f831
frontends, jp, templates: added XHTMLBox widget:
Goffi <goffi@goffi.org>
parents:
2771
diff
changeset
|
268 # FIXME: we use bridge in a blocking way as permitted by python-dbus |
442ab697f831
frontends, jp, templates: added XHTMLBox widget:
Goffi <goffi@goffi.org>
parents:
2771
diff
changeset
|
269 # this only for now to make it simpler, it must be refactored |
442ab697f831
frontends, jp, templates: added XHTMLBox widget:
Goffi <goffi@goffi.org>
parents:
2771
diff
changeset
|
270 # to use async when jp will be fully async (expected for 0.8) |
3040 | 271 self.value = await self.host.bridge.syntaxConvert( |
2783
442ab697f831
frontends, jp, templates: added XHTMLBox widget:
Goffi <goffi@goffi.org>
parents:
2771
diff
changeset
|
272 self.value, C.SYNTAX_XHTML, "markdown", False, self.host.profile) |
3040 | 273 await super(XHTMLBoxWidget, self).show() |
2783
442ab697f831
frontends, jp, templates: added XHTMLBox widget:
Goffi <goffi@goffi.org>
parents:
2771
diff
changeset
|
274 |
442ab697f831
frontends, jp, templates: added XHTMLBox widget:
Goffi <goffi@goffi.org>
parents:
2771
diff
changeset
|
275 |
2669
bdb8276fd2da
frontends (xmlui): class_map is now an arg of create function:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
276 class ListWidget(xmlui_base.ListWidget, OptionsWidget): |
3028 | 277 type = "list" |
2408 | 278 # TODO: handle flags, notably multi |
279 | |
3040 | 280 async def show(self): |
2541
65695b9343d3
jp (xmlui): added whitelist, read_only and values_only options:
Goffi <goffi@goffi.org>
parents:
2483
diff
changeset
|
281 if self.root.values_only: |
65695b9343d3
jp (xmlui): added whitelist, read_only and values_only options:
Goffi <goffi@goffi.org>
parents:
2483
diff
changeset
|
282 for value in self.values: |
65695b9343d3
jp (xmlui): added whitelist, read_only and values_only options:
Goffi <goffi@goffi.org>
parents:
2483
diff
changeset
|
283 self.disp(self.value) |
65695b9343d3
jp (xmlui): added whitelist, read_only and values_only options:
Goffi <goffi@goffi.org>
parents:
2483
diff
changeset
|
284 return |
2408 | 285 if not self.options: |
286 return | |
287 | |
288 # list display | |
289 self.verboseName() | |
290 | |
291 for idx, (value, label) in enumerate(self.options): | |
292 elems = [] | |
2541
65695b9343d3
jp (xmlui): added whitelist, read_only and values_only options:
Goffi <goffi@goffi.org>
parents:
2483
diff
changeset
|
293 if not self.root.read_only: |
3028 | 294 elems.extend([C.A_SUBHEADER, str(idx), A.RESET, ": "]) |
2408 | 295 elems.append(label) |
296 self.verboseName(elems, value) | |
297 self.disp(A.color(*elems)) | |
298 | |
2541
65695b9343d3
jp (xmlui): added whitelist, read_only and values_only options:
Goffi <goffi@goffi.org>
parents:
2483
diff
changeset
|
299 if self.root.read_only: |
2408 | 300 return |
301 | |
302 if len(self.options) == 1: | |
303 # we have only one option, no need to ask | |
304 self.value = self.options[0][0] | |
305 return | |
306 | |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
307 # we ask use to choose an option |
2408 | 308 choice = None |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
309 limit_max = len(self.options) - 1 |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
310 while choice is None or choice < 0 or choice > limit_max: |
3040 | 311 choice = await self.host.ainput( |
312 A.color(C.A_HEADER, _(f"your choice (0-{limit_max}): ")) | |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
313 ) |
2408 | 314 try: |
315 choice = int(choice) | |
316 except ValueError: | |
317 choice = None | |
318 self.value = self.options[choice][0] | |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
319 self.disp("") |
2408 | 320 |
321 | |
2669
bdb8276fd2da
frontends (xmlui): class_map is now an arg of create function:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
322 class BoolWidget(xmlui_base.BoolWidget, InputWidget): |
3028 | 323 type = "bool" |
2408 | 324 |
3040 | 325 async def show(self): |
3028 | 326 disp_true = A.color(A.FG_GREEN, "TRUE") |
327 disp_false = A.color(A.FG_RED, "FALSE") | |
2961
620bbcec884c
jp (xmlui): check root read_only status in addition to widget one
Goffi <goffi@goffi.org>
parents:
2960
diff
changeset
|
328 if self.read_only or self.root.read_only: |
2408 | 329 self.disp(disp_true if self.value else disp_false) |
330 else: | |
3028 | 331 self.disp(A.color(C.A_HEADER, "0: ", |
2739
e8dc00f612fb
jp (xmlui): JidWidget + small improvments:
Goffi <goffi@goffi.org>
parents:
2669
diff
changeset
|
332 disp_false, A.RESET, |
3028 | 333 " *" if not self.value else "")) |
334 self.disp(A.color(C.A_HEADER, "1: ", | |
2739
e8dc00f612fb
jp (xmlui): JidWidget + small improvments:
Goffi <goffi@goffi.org>
parents:
2669
diff
changeset
|
335 disp_true, A.RESET, |
3028 | 336 " *" if self.value else "")) |
2408 | 337 choice = None |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
338 while choice not in ("0", "1"): |
3028 | 339 elems = [C.A_HEADER, _("your choice (0,1): ")] |
2408 | 340 self.verboseName(elems) |
3040 | 341 choice = await self.host.ainput(A.color(*elems)) |
2408 | 342 self.value = bool(int(choice)) |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
343 self.disp("") |
2408 | 344 |
345 def _xmluiGetValue(self): | |
346 return C.boolConst(self.value) | |
347 | |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
348 |
2408 | 349 ## Containers ## |
350 | |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
351 |
2408 | 352 class Container(Base): |
3028 | 353 category = "container" |
2408 | 354 |
355 def __init__(self, xmlui_parent): | |
356 super(Container, self).__init__(xmlui_parent) | |
357 self.children = [] | |
358 | |
359 def __iter__(self): | |
360 return iter(self.children) | |
361 | |
362 def _xmluiAppend(self, widget): | |
363 self.children.append(widget) | |
364 | |
2541
65695b9343d3
jp (xmlui): added whitelist, read_only and values_only options:
Goffi <goffi@goffi.org>
parents:
2483
diff
changeset
|
365 def _xmluiRemove(self, widget): |
65695b9343d3
jp (xmlui): added whitelist, read_only and values_only options:
Goffi <goffi@goffi.org>
parents:
2483
diff
changeset
|
366 self.children.remove(widget) |
65695b9343d3
jp (xmlui): added whitelist, read_only and values_only options:
Goffi <goffi@goffi.org>
parents:
2483
diff
changeset
|
367 |
3040 | 368 async def show(self): |
2408 | 369 for child in self.children: |
3040 | 370 await child.show() |
2408 | 371 |
372 | |
2669
bdb8276fd2da
frontends (xmlui): class_map is now an arg of create function:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
373 class VerticalContainer(xmlui_base.VerticalContainer, Container): |
3028 | 374 type = "vertical" |
2408 | 375 |
376 | |
2669
bdb8276fd2da
frontends (xmlui): class_map is now an arg of create function:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
377 class PairsContainer(xmlui_base.PairsContainer, Container): |
3028 | 378 type = "pairs" |
2408 | 379 |
380 | |
2669
bdb8276fd2da
frontends (xmlui): class_map is now an arg of create function:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
381 class LabelContainer(xmlui_base.PairsContainer, Container): |
3028 | 382 type = "label" |
2408 | 383 |
3040 | 384 async def show(self): |
2408 | 385 for child in self.children: |
386 no_lf = False | |
387 # we check linked widget type | |
388 # to see if we want the label on the same line or not | |
3028 | 389 if child.type == "label": |
2408 | 390 for_name = child.for_name |
2739
e8dc00f612fb
jp (xmlui): JidWidget + small improvments:
Goffi <goffi@goffi.org>
parents:
2669
diff
changeset
|
391 if for_name: |
2408 | 392 for_widget = self.root.widgets[for_name] |
393 wid_type = for_widget.type | |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
394 if self.root.values_only or wid_type in ( |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
395 "text", |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
396 "string", |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
397 "jid_input", |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
398 ): |
2408 | 399 no_lf = True |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
400 elif wid_type == "bool" and for_widget.read_only: |
2408 | 401 no_lf = True |
3040 | 402 await child.show(no_lf=no_lf, ansi=A.FG_CYAN) |
2408 | 403 else: |
3040 | 404 await child.show() |
2408 | 405 |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
406 |
2408 | 407 ## Dialogs ## |
408 | |
409 | |
410 class Dialog(object): | |
411 def __init__(self, xmlui_parent): | |
412 self.xmlui_parent = xmlui_parent | |
413 self.host = self.xmlui_parent.host | |
414 | |
415 def disp(self, *args, **kwargs): | |
416 self.host.disp(*args, **kwargs) | |
417 | |
3040 | 418 async def show(self): |
2408 | 419 """display current dialog |
420 | |
421 must be overriden by subclasses | |
422 """ | |
423 raise NotImplementedError(self.__class__) | |
424 | |
3040 | 425 class MessageDialog(xmlui_base.MessageDialog, Dialog): |
426 | |
427 def __init__(self, xmlui_parent, title, message, level): | |
428 Dialog.__init__(self, xmlui_parent) | |
429 xmlui_base.MessageDialog.__init__(self, xmlui_parent) | |
430 self.title, self.message, self.level = title, message, level | |
431 | |
432 async def show(self): | |
433 # TODO: handle level | |
434 if self.title: | |
435 self.disp(A.color(C.A_HEADER, self.title)) | |
436 self.disp(self.message) | |
437 | |
2408 | 438 |
2669
bdb8276fd2da
frontends (xmlui): class_map is now an arg of create function:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
439 class NoteDialog(xmlui_base.NoteDialog, Dialog): |
2408 | 440 |
2739
e8dc00f612fb
jp (xmlui): JidWidget + small improvments:
Goffi <goffi@goffi.org>
parents:
2669
diff
changeset
|
441 def __init__(self, xmlui_parent, title, message, level): |
e8dc00f612fb
jp (xmlui): JidWidget + small improvments:
Goffi <goffi@goffi.org>
parents:
2669
diff
changeset
|
442 Dialog.__init__(self, xmlui_parent) |
e8dc00f612fb
jp (xmlui): JidWidget + small improvments:
Goffi <goffi@goffi.org>
parents:
2669
diff
changeset
|
443 xmlui_base.NoteDialog.__init__(self, xmlui_parent) |
2408 | 444 self.title, self.message, self.level = title, message, level |
445 | |
3040 | 446 async def show(self): |
3093
d909473a76cc
jp (xmlui_manager): use level for notes:
Goffi <goffi@goffi.org>
parents:
3040
diff
changeset
|
447 # TODO: handle title |
d909473a76cc
jp (xmlui_manager): use level for notes:
Goffi <goffi@goffi.org>
parents:
3040
diff
changeset
|
448 error = self.level in (C.XMLUI_DATA_LVL_WARNING, C.XMLUI_DATA_LVL_ERROR) |
d909473a76cc
jp (xmlui_manager): use level for notes:
Goffi <goffi@goffi.org>
parents:
3040
diff
changeset
|
449 if self.level == C.XMLUI_DATA_LVL_WARNING: |
d909473a76cc
jp (xmlui_manager): use level for notes:
Goffi <goffi@goffi.org>
parents:
3040
diff
changeset
|
450 msg = A.color(C.A_WARNING, self.message) |
d909473a76cc
jp (xmlui_manager): use level for notes:
Goffi <goffi@goffi.org>
parents:
3040
diff
changeset
|
451 elif self.level == C.XMLUI_DATA_LVL_ERROR: |
d909473a76cc
jp (xmlui_manager): use level for notes:
Goffi <goffi@goffi.org>
parents:
3040
diff
changeset
|
452 msg = A.color(C.A_FAILURE, self.message) |
d909473a76cc
jp (xmlui_manager): use level for notes:
Goffi <goffi@goffi.org>
parents:
3040
diff
changeset
|
453 else: |
d909473a76cc
jp (xmlui_manager): use level for notes:
Goffi <goffi@goffi.org>
parents:
3040
diff
changeset
|
454 msg = self.message |
d909473a76cc
jp (xmlui_manager): use level for notes:
Goffi <goffi@goffi.org>
parents:
3040
diff
changeset
|
455 self.disp(msg, error=error) |
3040 | 456 |
457 | |
458 class ConfirmDialog(xmlui_base.ConfirmDialog, Dialog): | |
459 | |
460 def __init__(self, xmlui_parent, title, message, level, buttons_set): | |
461 Dialog.__init__(self, xmlui_parent) | |
462 xmlui_base.ConfirmDialog.__init__(self, xmlui_parent) | |
463 self.title, self.message, self.level, self.buttons_set = ( | |
464 title, message, level, buttons_set) | |
465 | |
466 async def show(self): | |
467 # TODO: handle buttons_set and level | |
468 self.disp(self.message) | |
469 if self.title: | |
470 self.disp(A.color(C.A_HEADER, self.title)) | |
471 input_ = None | |
472 while input_ not in ('y', 'n'): | |
473 input_ = await self.host.ainput(f"{self.message} (y/n)? ") | |
474 input_ = input_.lower() | |
475 if input_ == 'y': | |
476 self._xmluiValidated() | |
477 else: | |
478 self._xmluiCancelled() | |
479 | |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
480 |
2408 | 481 ## Factory ## |
482 | |
483 | |
484 class WidgetFactory(object): | |
485 def __getattr__(self, attr): | |
486 if attr.startswith("create"): | |
487 cls = globals()[attr[6:]] | |
488 return cls | |
489 | |
490 | |
3040 | 491 class XMLUIPanel(xmlui_base.AIOXMLUIPanel): |
2408 | 492 widget_factory = WidgetFactory() |
493 _actions = 0 # use to keep track of bridge's launchAction calls | |
2541
65695b9343d3
jp (xmlui): added whitelist, read_only and values_only options:
Goffi <goffi@goffi.org>
parents:
2483
diff
changeset
|
494 read_only = False |
65695b9343d3
jp (xmlui): added whitelist, read_only and values_only options:
Goffi <goffi@goffi.org>
parents:
2483
diff
changeset
|
495 values_only = False |
2408 | 496 workflow = None |
497 _submit_cb = None | |
498 | |
2669
bdb8276fd2da
frontends (xmlui): class_map is now an arg of create function:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
499 def __init__(self, host, parsed_dom, title=None, flags=None, callback=None, |
bdb8276fd2da
frontends (xmlui): class_map is now an arg of create function:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
500 ignore=None, whitelist=None, profile=None): |
bdb8276fd2da
frontends (xmlui): class_map is now an arg of create function:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
501 xmlui_base.XMLUIPanel.__init__( |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
502 self, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
503 host, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
504 parsed_dom, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
505 title=title, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
506 flags=flags, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
507 ignore=ignore, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
508 whitelist=whitelist, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
509 profile=host.profile, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
510 ) |
2408 | 511 self.submitted = False |
512 | |
513 @property | |
514 def command(self): | |
515 return self.host.command | |
516 | |
3212
89d97776fd34
jp (xmlui): added missing `disp` method in XMLUIPanel
Goffi <goffi@goffi.org>
parents:
3137
diff
changeset
|
517 def disp(self, *args, **kwargs): |
89d97776fd34
jp (xmlui): added missing `disp` method in XMLUIPanel
Goffi <goffi@goffi.org>
parents:
3137
diff
changeset
|
518 self.host.disp(*args, **kwargs) |
89d97776fd34
jp (xmlui): added missing `disp` method in XMLUIPanel
Goffi <goffi@goffi.org>
parents:
3137
diff
changeset
|
519 |
3040 | 520 async def show(self, workflow=None, read_only=False, values_only=False): |
2408 | 521 """display the panel |
522 | |
523 @param workflow(list, None): command to execute if not None | |
524 put here for convenience, the main workflow is the class attribute | |
525 (because workflow can continue in subclasses) | |
526 command are a list of consts or lists: | |
527 - SUBMIT is the only constant so far, it submits the XMLUI | |
528 - list must contain widget name/widget value to fill | |
2541
65695b9343d3
jp (xmlui): added whitelist, read_only and values_only options:
Goffi <goffi@goffi.org>
parents:
2483
diff
changeset
|
529 @param read_only(bool): if True, don't request values |
65695b9343d3
jp (xmlui): added whitelist, read_only and values_only options:
Goffi <goffi@goffi.org>
parents:
2483
diff
changeset
|
530 @param values_only(bool): if True, only show select values (imply read_only) |
2408 | 531 """ |
2541
65695b9343d3
jp (xmlui): added whitelist, read_only and values_only options:
Goffi <goffi@goffi.org>
parents:
2483
diff
changeset
|
532 self.read_only = read_only |
65695b9343d3
jp (xmlui): added whitelist, read_only and values_only options:
Goffi <goffi@goffi.org>
parents:
2483
diff
changeset
|
533 self.values_only = values_only |
65695b9343d3
jp (xmlui): added whitelist, read_only and values_only options:
Goffi <goffi@goffi.org>
parents:
2483
diff
changeset
|
534 if self.values_only: |
65695b9343d3
jp (xmlui): added whitelist, read_only and values_only options:
Goffi <goffi@goffi.org>
parents:
2483
diff
changeset
|
535 self.read_only = True |
2408 | 536 if workflow: |
537 XMLUIPanel.workflow = workflow | |
538 if XMLUIPanel.workflow: | |
3040 | 539 await self.runWorkflow() |
2408 | 540 else: |
3040 | 541 await self.main_cont.show() |
2408 | 542 |
3040 | 543 async def runWorkflow(self): |
2408 | 544 """loop into workflow commands and execute commands |
545 | |
546 SUBMIT will interrupt workflow (which will be continue on callback) | |
547 @param workflow(list): same as [show] | |
548 """ | |
549 workflow = XMLUIPanel.workflow | |
550 while True: | |
551 try: | |
552 cmd = workflow.pop(0) | |
553 except IndexError: | |
554 break | |
555 if cmd == SUBMIT: | |
3040 | 556 await self.onFormSubmitted() |
2408 | 557 self.submit_id = None # avoid double submit |
558 return | |
559 elif isinstance(cmd, list): | |
560 name, value = cmd | |
2412
7641bef56dcd
jp (xmlui): fixed workflow when value is for a BoolWidget
Goffi <goffi@goffi.org>
parents:
2410
diff
changeset
|
561 widget = self.widgets[name] |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
562 if widget.type == "bool": |
2412
7641bef56dcd
jp (xmlui): fixed workflow when value is for a BoolWidget
Goffi <goffi@goffi.org>
parents:
2410
diff
changeset
|
563 value = C.bool(value) |
7641bef56dcd
jp (xmlui): fixed workflow when value is for a BoolWidget
Goffi <goffi@goffi.org>
parents:
2410
diff
changeset
|
564 widget.value = value |
3040 | 565 await self.show() |
2408 | 566 |
3040 | 567 async def submitForm(self, callback=None): |
2408 | 568 XMLUIPanel._submit_cb = callback |
3040 | 569 await self.onFormSubmitted() |
2408 | 570 |
3040 | 571 async def onFormSubmitted(self, ignore=None): |
572 # self.submitted is a Q&D workaround to avoid | |
2408 | 573 # double submit when a workflow is set |
574 if self.submitted: | |
575 return | |
576 self.submitted = True | |
3040 | 577 await super(XMLUIPanel, self).onFormSubmitted(ignore) |
2408 | 578 |
579 def _xmluiClose(self): | |
580 pass | |
581 | |
3040 | 582 async def _launchActionCb(self, data): |
2408 | 583 XMLUIPanel._actions -= 1 |
584 assert XMLUIPanel._actions >= 0 | |
3028 | 585 if "xmlui" in data: |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
586 xmlui_raw = data["xmlui"] |
2669
bdb8276fd2da
frontends (xmlui): class_map is now an arg of create function:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
587 xmlui = create(self.host, xmlui_raw) |
3040 | 588 await xmlui.show() |
2408 | 589 if xmlui.submit_id: |
3040 | 590 await xmlui.onFormSubmitted() |
2408 | 591 # TODO: handle data other than XMLUI |
592 if not XMLUIPanel._actions: | |
593 if self._submit_cb is None: | |
594 self.host.quit() | |
595 else: | |
596 self._submit_cb() | |
597 | |
3040 | 598 async def _xmluiLaunchAction(self, action_id, data): |
2408 | 599 XMLUIPanel._actions += 1 |
3040 | 600 try: |
601 data = await self.host.bridge.launchAction( | |
602 action_id, | |
603 data, | |
604 self.profile, | |
605 ) | |
606 except Exception as e: | |
607 self.disp(f"can't launch XMLUI action: {e}", error=True) | |
608 self.host.quit(C.EXIT_BRIDGE_ERRBACK) | |
609 else: | |
610 await self._launchActionCb(data) | |
2408 | 611 |
612 | |
2669
bdb8276fd2da
frontends (xmlui): class_map is now an arg of create function:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
613 class XMLUIDialog(xmlui_base.XMLUIDialog): |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
614 type = "dialog" |
2408 | 615 dialog_factory = WidgetFactory() |
2541
65695b9343d3
jp (xmlui): added whitelist, read_only and values_only options:
Goffi <goffi@goffi.org>
parents:
2483
diff
changeset
|
616 read_only = False |
2408 | 617 |
3040 | 618 async def show(self, __=None): |
619 await self.dlg.show() | |
2408 | 620 |
621 def _xmluiClose(self): | |
622 pass | |
623 | |
624 | |
2669
bdb8276fd2da
frontends (xmlui): class_map is now an arg of create function:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
625 create = partial(xmlui_base.create, class_map={ |
bdb8276fd2da
frontends (xmlui): class_map is now an arg of create function:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
626 xmlui_base.CLASS_PANEL: XMLUIPanel, |
bdb8276fd2da
frontends (xmlui): class_map is now an arg of create function:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
627 xmlui_base.CLASS_DIALOG: XMLUIDialog}) |