move "usage labels" from filesystem view to model objects

So the logic can be reused.
This commit is contained in:
Michael Hudson-Doyle 2019-05-21 11:47:12 +12:00
parent f45880944e
commit 3bad9e408a
3 changed files with 48 additions and 35 deletions

View File

@ -386,6 +386,28 @@ class _Formattable(ABC):
# Raid or LVM_VolGroup for now, but one day ZPool, BCache... # Raid or LVM_VolGroup for now, but one day ZPool, BCache...
_constructed_device = attributes.backlink() _constructed_device = attributes.backlink()
def usage_labels(self):
cd = self.constructed_device()
if cd is not None:
return [
_("{component_name} of {desc} {name}").format(
component_name=cd.component_name,
desc=cd.desc(),
name=cd.name),
]
fs = self.fs()
if fs is not None:
r = [_("formatted as {fstype}").format(fstype=fs.fstype)]
if self._m.is_mounted_filesystem(fs.fstype):
m = fs.mount()
if m:
r.append(_("mounted at {path}").format(path=m.path))
else:
r.append(_("not mounted"))
return r
else:
return [_("unused")]
def _is_entirely_used(self): def _is_entirely_used(self):
return self._fs is not None or self._constructed_device is not None return self._fs is not None or self._constructed_device is not None
@ -657,6 +679,11 @@ class Partition(_Formattable):
r.append("bios_grub") r.append("bios_grub")
return r return r
def usage_labels(self):
if self.flag == "prep" or self.flag == "bios_grub":
return []
return super().usage_labels()
def desc(self): def desc(self):
return _("partition of {}").format(self.device.desc()) return _("partition of {}").format(self.device.desc())

View File

@ -246,6 +246,16 @@ class TestFilesystemModel(unittest.TestCase):
self.assertFalse(lv.ok_for_raid) self.assertFalse(lv.ok_for_raid)
self.assertFalse(lv.ok_for_lvm_vg) self.assertFalse(lv.ok_for_lvm_vg)
def test_partition_usage_labels(self):
model, partition = make_model_and_partition()
self.assertEqual(partition.usage_labels(), ["unused"])
fs = model.add_filesystem(partition, 'ext4')
self.assertEqual(
partition.usage_labels(), ["formatted as ext4", "not mounted"])
model.add_mount(fs, '/')
self.assertEqual(
partition.usage_labels(), ["formatted as ext4", "mounted at /"])
def assertActionNotSupported(self, obj, action): def assertActionNotSupported(self, obj, action):
self.assertNotIn(action, obj.supported_actions) self.assertNotIn(action, obj.supported_actions)

View File

@ -378,27 +378,15 @@ class DeviceList(WidgetWrap):
log.debug('FileSystemView: building device list') log.debug('FileSystemView: building device list')
rows = [] rows = []
def _usage_label(obj): def _append_usage_labels(obj, indent):
cd = obj.constructed_device() label = ", ".join(obj.usage_labels())
if cd is not None: if label:
return _("{component_name} of {name}").format( rows.append(TableRow([
component_name=cd.component_name, name=cd.name) Text(""),
fs = obj.fs() (3, Text(indent + label)),
if fs is not None: Text(""),
if not self.parent.model.is_mounted_filesystem(fs.fstype): Text(""),
return _("formatted as {fstype}").format( ]))
fstype=fs.fstype)
else:
m = fs.mount()
if m:
return _(
"formatted as {fstype}, mounted at {path}").format(
fstype=fs.fstype, path=m.path)
else:
return _("formatted as {fstype}, not mounted").format(
fstype=fs.fstype)
else:
return _("unused")
rows.append(TableRow([Color.info_minor(heading) for heading in [ rows.append(TableRow([Color.info_minor(heading) for heading in [
Text(" "), Text(" "),
@ -425,12 +413,7 @@ class DeviceList(WidgetWrap):
rows.append(row) rows.append(row)
if not device.partitions(): if not device.partitions():
rows.append(TableRow([ _append_usage_labels(device, " ")
Text(""),
(3, Text(" " + _usage_label(device))),
Text(""),
Text(""),
]))
else: else:
for part in device.partitions(): for part in device.partitions():
if part.available() != self.show_available: if part.available() != self.show_available:
@ -452,14 +435,7 @@ class DeviceList(WidgetWrap):
] ]
row = make_action_menu_row(cells, menu, cursor_x=4) row = make_action_menu_row(cells, menu, cursor_x=4)
rows.append(row) rows.append(row)
if part.flag in ["bios_grub", "prep"]: _append_usage_labels(part, " ")
continue
rows.append(TableRow([
Text(""),
(3, Text(" " + _usage_label(part))),
Text(""),
Text(""),
]))
if (self.show_available if (self.show_available
and device.used > 0 and device.used > 0
and device.free_for_partitions > 0): and device.free_for_partitions > 0):