migrate usage_labels to singledispatch

This commit is contained in:
Michael Hudson-Doyle 2021-05-31 20:08:56 +12:00
parent 55191bafb5
commit 7f67f65875
5 changed files with 69 additions and 58 deletions

View File

@ -87,3 +87,55 @@ def _annotations_vg(vg):
# Flag for a LVM volume group
r.append(_("encrypted"))
return r
def _usage_labels_generic(device):
cd = device.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 = device.fs()
if fs is not None:
if fs.preserve:
format_desc = _("already formatted as {fstype}")
elif device.original_fstype() is not None:
format_desc = _("to be reformatted as {fstype}")
else:
format_desc = _("to be formatted as {fstype}")
r = [format_desc.format(fstype=fs.fstype)]
if device._m.is_mounted_filesystem(fs.fstype):
m = fs.mount()
if m:
# A filesytem
r.append(_("mounted at {path}").format(path=m.path))
elif not getattr(device, 'is_esp', False):
# A filesytem
r.append(_("not mounted"))
elif fs.preserve:
if fs.mount() is None:
# A filesytem that cannot be mounted (i.e. swap)
# is used or unused
r.append(_("unused"))
else:
# A filesytem that cannot be mounted (i.e. swap)
# is used or unused
r.append(_("used"))
return r
else:
return [_("unused")]
@functools.singledispatch
def usage_labels(device):
return _usage_labels_generic(device)
@usage_labels.register(Partition)
def _usage_labels_partition(partition):
if partition.flag == "prep" or partition.flag == "bios_grub":
return []
return _usage_labels_generic(partition)

View File

@ -434,45 +434,6 @@ class _Formattable(ABC):
# Raid or LVM_VolGroup for now, but one day ZPool, BCache...
_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:
if fs.preserve:
format_desc = _("already formatted as {fstype}")
elif self.original_fstype() is not None:
format_desc = _("to be reformatted as {fstype}")
else:
format_desc = _("to be formatted as {fstype}")
r = [format_desc.format(fstype=fs.fstype)]
if self._m.is_mounted_filesystem(fs.fstype):
m = fs.mount()
if m:
# A filesytem
r.append(_("mounted at {path}").format(path=m.path))
elif not getattr(self, 'is_esp', False):
# A filesytem
r.append(_("not mounted"))
elif fs.preserve:
if fs.mount() is None:
# A filesytem that cannot be mounted (i.e. swap)
# is used or unused
r.append(_("unused"))
else:
# A filesytem that cannot be mounted (i.e. swap)
# is used or unused
r.append(_("used"))
return r
else:
return [_("unused")]
def _is_entirely_used(self):
return self._fs is not None or self._constructed_device is not None
@ -709,13 +670,14 @@ class Disk(_Device):
ok_for_lvm_vg = ok_for_raid
def for_client(self, min_size):
from subiquity.common.filesystem.labels import usage_labels
from subiquity.common.types import Disk
return Disk(
id=self.id,
label=self.label,
type=self.desc(),
size=self.size,
usage_labels=self.usage_labels(),
usage_labels=usage_labels(self),
partitions=[p.for_client() for p in self._partitions],
ok_for_guided=self.size >= min_size)
@ -733,11 +695,6 @@ class Partition(_Formattable):
name = attr.ib(default=None)
multipath = attr.ib(default=None)
def usage_labels(self):
if self.flag == "prep" or self.flag == "bios_grub":
return []
return super().usage_labels()
def desc(self):
return _("partition of {device}").format(device=self.device.desc())
@ -817,12 +774,15 @@ class Partition(_Formattable):
ok_for_lvm_vg = ok_for_raid
def for_client(self):
from subiquity.common.filesystem.labels import annotations
from subiquity.common.filesystem.labels import (
annotations,
usage_labels,
)
from subiquity.common.types import Partition
return Partition(
size=self.size,
number=self._number,
annotations=annotations(self) + self.usage_labels())
annotations=annotations(self) + usage_labels(self))
@fsobj("raid")

View File

@ -19,6 +19,7 @@ import attr
from subiquity.common.filesystem.labels import (
annotations,
usage_labels,
)
from subiquity.models.filesystem import (
Bootloader,
@ -356,25 +357,25 @@ class TestFilesystemModel(unittest.TestCase):
def test_partition_usage_labels(self):
model, partition = make_model_and_partition()
self.assertEqual(partition.usage_labels(), ["unused"])
self.assertEqual(usage_labels(partition), ["unused"])
fs = model.add_filesystem(partition, 'ext4')
self.assertEqual(
partition.usage_labels(),
usage_labels(partition),
["to be formatted as ext4", "not mounted"])
model._orig_config = model._render_actions()
fs.preserve = True
partition.preserve = True
self.assertEqual(
partition.usage_labels(),
usage_labels(partition),
["already formatted as ext4", "not mounted"])
model.remove_filesystem(fs)
fs2 = model.add_filesystem(partition, 'ext4')
self.assertEqual(
partition.usage_labels(),
usage_labels(partition),
["to be reformatted as ext4", "not mounted"])
model.add_mount(fs2, '/')
self.assertEqual(
partition.usage_labels(),
usage_labels(partition),
["to be reformatted as ext4", "mounted at /"])
def test_is_esp(self):

View File

@ -44,9 +44,6 @@ from subiquitycore.ui.utils import (
)
from subiquitycore.view import BaseView
from subiquity.common.filesystem.labels import (
annotations,
)
from subiquity.common.types import GuidedChoice
from subiquity.models.filesystem import humanize_size
@ -93,7 +90,7 @@ def summarize_device(disk):
])]
if disk.partitions:
for part in disk.partitions:
details = ", ".join(annotations(part))
details = ", ".join(part.annotations)
rows.append((part, [
Text(_("partition {number}").format(number=part.number)),
(2, Text(details)),

View File

@ -21,6 +21,7 @@ from subiquitycore.ui.utils import (
from subiquity.common.filesystem.labels import (
annotations,
usage_labels,
)
from subiquity.models.filesystem import (
humanize_size,
@ -49,7 +50,7 @@ def summarize_device(device, part_filter=lambda p: True):
for part in device.partitions():
if not part_filter(part):
continue
details = ", ".join(annotations(part) + part.usage_labels())
details = ", ".join(annotations(part) + usage_labels(part))
rows.append((part, [
Text(part.short_label),
(2, Text(details)),
@ -57,6 +58,6 @@ def summarize_device(device, part_filter=lambda p: True):
]))
else:
rows.append((None, [
(4, Color.info_minor(Text(", ".join(device.usage_labels()))))
(4, Color.info_minor(Text(", ".join(usage_labels(device)))))
]))
return rows