[elbe-devel] [PATCH v2 06/13] Debianize - Form widget

dion at linutronix.de dion at linutronix.de
Thu Aug 1 17:49:27 CEST 2019


From: Olivier Dion <dion at linutronix.de>

A form is a smart grid.  It attaches a name to its children and
generates 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 called.

If the abort button is pressed, a QUIT signal is emitted by default
and the TUI is closed.

Signed-off-by: Olivier Dion <dion at linutronix.de>
---
 elbepack/debianize/widgets/form.py | 70 ++++++++++++++++++++++++++++++++++++++
 1 file changed, 70 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..74cee445
--- /dev/null
+++ b/elbepack/debianize/widgets/form.py
@@ -0,0 +1,70 @@
+# ELBE - Debian Based Embedded Rootfilesystem Builder
+# Copyright (c) 2019 Olivier Dion <dion at linutronix.de>
+#
+# SPDX-License-Identifier: GPL-3.0-or-later
+
+
+from urwid import (
+    Edit,
+    Filler,
+    IntEdit,
+    WidgetDecoration,
+    emit_signal
+)
+
+from elbepack.debianize.base.tui import TUISignal
+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 = [TUISignal.QUIT]
+
+    def __init__(self, named_grid_elements):
+        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(Form, self).__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, TUISignal.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