disk: simplify device info for disk or partition
This commit is contained in:
@@ -62,7 +62,7 @@ class DiskPage(Gtk.Box, Page):
|
||||
disks = disk_provider.get_disks()
|
||||
for disk_info in disks:
|
||||
too_small = disk_info.size < self.minimum_disk_size
|
||||
row = DeviceRow('disk', disk_info, too_small)
|
||||
row = DeviceRow(disk_info, too_small)
|
||||
self.disk_list.add(row)
|
||||
|
||||
# show
|
||||
@@ -80,10 +80,10 @@ class DiskPage(Gtk.Box, Page):
|
||||
|
||||
# fill partition list
|
||||
disk_uefi_okay = not is_booted_with_uefi() or disk_info.efi_partition
|
||||
if disk_uefi_okay:
|
||||
if disk_uefi_okay and len(disk_info.partitions) > 0:
|
||||
for partition_info in disk_info.partitions:
|
||||
too_small = partition_info.size < self.minimum_disk_size
|
||||
row = DeviceRow('partition', partition_info, too_small)
|
||||
row = DeviceRow(partition_info, too_small)
|
||||
self.partition_list.add(row)
|
||||
else:
|
||||
self.partition_list.add(NoPartitionsRow())
|
||||
@@ -94,8 +94,8 @@ class DiskPage(Gtk.Box, Page):
|
||||
def _store_device_info(self, info):
|
||||
global_state.set_config('disk_name', info.name)
|
||||
global_state.set_config('disk_device_path', info.device_path)
|
||||
global_state.set_config('disk_is_partition', info.is_partition)
|
||||
global_state.set_config('disk_efi_partition', info.efi_partition)
|
||||
global_state.set_config('disk_is_partition', not type(info) == type(self.current_disk))
|
||||
global_state.set_config('disk_efi_partition', self.current_disk.efi_partition)
|
||||
|
||||
### callbacks ###
|
||||
|
||||
|
||||
@@ -23,18 +23,19 @@ class DeviceRow(Gtk.ListBoxRow):
|
||||
arrow_stack = Gtk.Template.Child()
|
||||
device_path = Gtk.Template.Child()
|
||||
|
||||
def __init__(self, row_type, info, too_small, **kwargs):
|
||||
def __init__(self, info, too_small, **kwargs):
|
||||
super().__init__(**kwargs)
|
||||
|
||||
self.info = info
|
||||
self.size.set_label(info.size_text)
|
||||
self.device_path.set_label(info.device_path)
|
||||
|
||||
self.name_stack.set_visible_child_name(row_type)
|
||||
if 'disk' == row_type:
|
||||
if hasattr(info, 'partitions'):
|
||||
self.name_stack.set_visible_child_name('disk')
|
||||
if info.name:
|
||||
self.disk_name.set_label(info.name)
|
||||
elif 'partition' == row_type:
|
||||
else:
|
||||
self.name_stack.set_visible_child_name('partition')
|
||||
if not info.prefixed:
|
||||
info.name = self.partition_name.get_label() + ' ' + info.name
|
||||
info.prefixed = True
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
# SPDX-License-Identifier: GPL-3.0-or-later
|
||||
|
||||
from gi.repository import GLib, UDisks
|
||||
from gi.repository import GLib, GObject, UDisks
|
||||
|
||||
EFI_PARTITION_GUID = 'C12A7328-F81F-11D2-BA4B-00A0C93EC93B'
|
||||
EFI_PARTITON_FLAGS = UDisks.PartitionTypeInfoFlags.SYSTEM.numerator
|
||||
@@ -8,20 +8,19 @@ EFI_PARTITON_FLAGS = UDisks.PartitionTypeInfoFlags.SYSTEM.numerator
|
||||
|
||||
class DeviceInfo:
|
||||
device_path: str
|
||||
efi_partition: str = ''
|
||||
is_partition: bool = True
|
||||
name: str
|
||||
prefixed: bool = False
|
||||
size: int
|
||||
size_text: str
|
||||
|
||||
|
||||
class DeviceWithTableInfo(DeviceInfo):
|
||||
is_gpt: bool
|
||||
class Disk(DeviceInfo):
|
||||
has_table: bool = False
|
||||
is_msdos: bool = False
|
||||
is_gpt: bool = False
|
||||
partitions: list = []
|
||||
efi_partition: str = ''
|
||||
|
||||
def __init__(self):
|
||||
self.is_partition = False
|
||||
|
||||
class DiskProvider:
|
||||
udisks_client = UDisks.Client.new_sync()
|
||||
@@ -60,27 +59,31 @@ class DiskProvider:
|
||||
else:
|
||||
print('Unhandled partiton in partition table, ignoring.')
|
||||
|
||||
# set efi partition
|
||||
for partition in partitions:
|
||||
partition.efi_partition = efi_partition
|
||||
|
||||
return (partitions, efi_partition)
|
||||
|
||||
def _get_disk_info(self, block, drive, partition_table):
|
||||
# disk info
|
||||
disk_info = DeviceWithTableInfo()
|
||||
disk_info = Disk()
|
||||
disk_info.name = (drive.props.vendor + ' ' + drive.props.model).strip()
|
||||
disk_info.size = block.props.size
|
||||
disk_info.size_text = self._size_to_str(disk_info.size)
|
||||
disk_info.device_path = block.props.device
|
||||
disk_info.is_gpt = 'gpt' == partition_table.props.type
|
||||
|
||||
# partitions
|
||||
disk_info.partitions, disk_info.efi_partition = self._get_partitions(partition_table, disk_info)
|
||||
if partition_table:
|
||||
disk_info.has_table = True
|
||||
disk_info.is_gpt = 'gpt' == partition_table.props.type
|
||||
disk_info.is_msdos = 'msdos' == partition_table.props.type
|
||||
disk_info.partitions, disk_info.efi_partition = self._get_partitions(partition_table, disk_info)
|
||||
|
||||
return disk_info
|
||||
|
||||
def _get_available_disks(self):
|
||||
def _size_to_str(self, size):
|
||||
return self.udisks_client.get_size_for_display(size, False, False)
|
||||
|
||||
### public methods ###
|
||||
|
||||
def get_disks(self):
|
||||
disks = []
|
||||
|
||||
# get available devices
|
||||
@@ -91,9 +94,13 @@ class DiskProvider:
|
||||
for device in devices:
|
||||
udisks_object = self.udisks_client.get_object(device)
|
||||
if udisks_object:
|
||||
partition = udisks_object.get_partition()
|
||||
if partition:
|
||||
continue # skip partitions
|
||||
|
||||
block = udisks_object.get_block()
|
||||
partition_table = udisks_object.get_partition_table()
|
||||
if block and partition_table:
|
||||
if block:
|
||||
drive = self.udisks_client.get_drive_for_block(block)
|
||||
if drive:
|
||||
disk_info = self._get_disk_info(block, drive, partition_table)
|
||||
@@ -101,14 +108,5 @@ class DiskProvider:
|
||||
|
||||
return disks
|
||||
|
||||
def _size_to_str(self, size):
|
||||
return self.udisks_client.get_size_for_display(size, False, False)
|
||||
|
||||
### public methods ###
|
||||
|
||||
def get_disks(self):
|
||||
# get current disks information via udisks
|
||||
return self._get_available_disks()
|
||||
|
||||
|
||||
disk_provider = DiskProvider()
|
||||
|
||||
Reference in New Issue
Block a user