[elbe-devel] [PATCH 07/14] Debianize - Form widget

dion at linutronix.de dion at linutronix.de
Thu Aug 1 14:14:06 CEST 2019


From: Olivier Dion <dion at linutronix.de>

A form is a smart grid.  It attachs name to its children and generate
2 buttons.

The confirm button when pressed will first call the sanitize_inputs
callback.  It should return a dictionary where the keys are the name
of the bad field and the value a string that describe why the field is
not good.  If all fields passed the sanitization test, then the
on_submit callback is call.

If the abort button is pressent, by default a QUIT signal is emit and
the TUI is closed.

Signed-off-by: Olivier Dion <dion at linutronix.de>
---
 elbepack/debianize/widgets/form.py | 72 ++++++++++++++++++++++++++++++++++++++
 1 file changed, 72 insertions(+)
 create mode 100644 elbepack/debianize/widgets/form.py

diff --git a/elbepack/debianize/widgets/form.py b/elbepack/debianize/widgets/form.py
new file mode 100644
index 00000000..6b66b312
--- /dev/null
+++ b/elbepack/debianize/widgets/form.py
@@ -0,0 +1,72 @@
+# ELBE - Debian Based Embedded Rootfilesystem Builder
+# Copyright (c) 2019 Olivier Dion <dion at linutronix.de>
+#
+# SPDX-License-Identifier: GPL-3.0-or-later
+
+
+from collections import Callable
+
+from urwid import (
+    Edit,
+    Filler,
+    IntEdit,
+    WidgetDecoration,
+    emit_signal
+)
+
+from elbepack.debianize.base.signal import SignalType
+from elbepack.debianize.base.tui import TUI
+from elbepack.debianize.widgets.button import Button
+from elbepack.debianize.widgets.grid import Grid
+
+
+WidgetDecoration.get_data = lambda wrapped_widget: wrapped_widget.base_widget.get_data()
+Edit.get_data = Edit.get_edit_text
+IntEdit.get_data = IntEdit.value
+
+
+class Form(Grid):
+
+    signals = [SignalType.QUIT]
+
+    def __init__(self, named_grid_elements: list):
+        self.named_widgets = {}
+        unnamed_grid_elements = []
+        for row in named_grid_elements:
+            self.named_widgets.update(row)
+            unnamed_grid_elements.append(list(row.values()))
+
+        confirm = Button("Confirm", "confirm_button", self.confirm)
+        abort = Button("Abort", "abort_button", self.abort)
+        unnamed_grid_elements.append([confirm, abort])
+
+        super().__init__(unnamed_grid_elements)
+        self.root = Filler(self, valign="top")
+
+        self.keybind["f5"] = self.confirm
+        self.keybind["f10"] = self.abort
+
+    def confirm(self):
+        data = self.get_form_data()
+        sanitize = self.sanitize_inputs(data)
+        if sanitize:
+            # TODO - Better sanitization handling
+            buf = []
+            for key, value in sanitize.items():
+                buf.append("{}: {}".format(key, value))
+            TUI.printf('\n'.join(buf))
+        else:
+            self.on_submit(data)
+
+    def abort(self):
+        emit_signal(self, SignalType.QUIT)
+        TUI.quit()
+
+    def get_form_data(self):
+        return {name: widget.get_data() for name, widget in self.named_widgets.items()}
+
+    def on_submit(self, data):
+        raise Exception("Form can not submit")
+
+    def sanitize_inputs(self, data):
+        return False
-- 
2.11.0




More information about the elbe-devel mailing list