Commits

Rodrigo Bistolfi committed c3b5bb2 Merge

Merge branch 'urwid'

  • Participants
  • Parent commits 482f561, 3aab410

Comments (0)

Files changed (1)

vinstall/ui/urwid.py

 from itertools import cycle
 from urwid_combobox import ComboBox
 from vinstall.core import core, model, command
+from vinstall.backend import mountpoints
 
 
 class UrwidView:
 
         """
         return urwid.Pile([self.caption, urwid.Divider(), self.progress_bar])
+
+
+@core.renders(mountpoints.MountPoints)
+class MountPointSelection(UrwidView):
+    """Interface for declaring mountpoints.
+
+    """
+    DNF = "Don't format"
+    DNU = "Not use"
+    columns = "Device", "Mnt", "Fs"
+
+    def __init__(self, mountpoints):
+        self.mountpoints = mountpoints
+
+    def configure(self):
+        """Setup the elements composing the model view."""
+
+        headers = urwid.Columns(
+                [("fixed", 26, urwid.Text(self.columns[0])),
+                    ("fixed", 15, urwid.Text(self.columns[1])),
+                    ("fixed", 18, urwid.Text(self.columns[2]))])
+        self.rows = [headers, urwid.Divider()]
+        for d in self.mountpoints.partitions:
+            row = self.make_row(d)
+            self.rows.append(urwid.Columns(row, dividechars=1, min_width=1))
+
+    def make_row(self, device):
+        """Create a row for the columns widget """
+
+        dev = urwid.Text(str(device))
+        available_mountpoints = [self.DNU]
+        available_mountpoints += [str(i) for i in
+                self.mountpoints.available_mountpoints()]
+        mountpoints = ComboBox(available_mountpoints)
+
+        available_filesystems = [self.DNF]
+        available_filesystems += [str(i) for i in
+                self.mountpoints.filesystems]
+        filesystems = ComboBox(available_filesystems)
+
+        urwid.connect_signal(mountpoints, "change",
+                self.make_set_default_fs_function(filesystems, "/", "ext3"),
+                user_arg=device)
+        urwid.connect_signal(mountpoints, "change",
+                self.make_set_default_fs_function(filesystems, "swap", "swap"),
+                user_arg=device)
+        urwid.connect_signal(mountpoints, "change", self.mountpoint_selected,
+                user_arg=device)
+        urwid.connect_signal(filesystems, "change", self.filesystem_selected,
+                user_arg=device)
+
+        row = [("fixed", 25, dev), ("fixed", 14, mountpoints), ("fixed", 18,
+            filesystems)]
+        return row
+
+    def make_set_default_fs_function(self, fs_combo, part, value):
+        """ Create a function enclosing the combo widget for fs.
+        The returned function will set the fs combo widget to a default value
+        if the partition <part> is selected
+
+        """
+        def f(combo, item, selected, partition):
+            if item.get_label() == part:
+                for item in fs_combo.menu.items:
+                    label = item.get_label()
+                    if label == value:
+                        break
+                item.set_state(True)
+                fs_combo.item_changed(item, True)
+        return f
+
+    def mountpoint_selected(self, combo, item, selected, partition):
+        """A mountpoint has been selected in the mountpoints menu
+
+        """
+        mountpoint = item.get_label()
+        if mountpoint == self.DNU:
+            self.mountpoints.clear_mount(partition)
+        else:
+            self.mountpoints.set_mount(partition, mountpoint)
+
+    def filesystem_selected(self, combo, item, selected, partition):
+        """A mountpoint has been selected in the mountpoints menu
+
+        """
+        filesystem = item.get_label()
+        if filesystem == self.DNF:
+            self.mountpoints.clear_filesystem(partition)
+        else:
+            self.mountpoints.set_filesystem(partition, filesystem)
+
+    def get_value(self):
+        """Returns the text entered by the user."""
+        return self.mountpoints.mapping
+
+    def accept(self):
+        """Returns the configured top level container widget."""
+        return urwid.BoxAdapter(
+                urwid.ListBox(urwid.SimpleFocusListWalker(self.rows)),
+                len(self.mountpoints.partitions) + 2)