|
| 1 | +from .udisks2_dbus_async import ( |
| 2 | + UDisks2BlockAsyncInterface, |
| 3 | + UDisks2DriveAsyncInterface, |
| 4 | + UDisks2PartitionAsyncInterface, |
| 5 | + UDisks2FileSystemAsyncInterface, |
| 6 | + UDisks2PartitionTableAsyncInterface, |
| 7 | +) |
| 8 | + |
| 9 | + |
| 10 | +class Device: |
| 11 | + def __init__( |
| 12 | + self, |
| 13 | + path: str, |
| 14 | + DriveInterface: UDisks2DriveAsyncInterface, |
| 15 | + symlink_path: str, |
| 16 | + ) -> None: |
| 17 | + self.path: str = path |
| 18 | + self.symlink_path: str = symlink_path |
| 19 | + self.driver_interface: UDisks2DriveAsyncInterface = DriveInterface |
| 20 | + self.partitions: dict[str, UDisks2PartitionAsyncInterface] = {} |
| 21 | + self.raw_block: dict[str, UDisks2BlockAsyncInterface] = {} |
| 22 | + self.logical_blocks: dict[str, UDisks2BlockAsyncInterface] = {} |
| 23 | + self.file_systems: dict[str, UDisks2FileSystemAsyncInterface] = {} |
| 24 | + self.partition_tables: dict[str, UDisks2PartitionTableAsyncInterface] = {} |
| 25 | + self.symlinks: list[str] = [] |
| 26 | + |
| 27 | + def get_logical_blocks(self) -> dict[str, UDisks2BlockAsyncInterface]: |
| 28 | + """The available logical blocks for the device""" |
| 29 | + return self.logical_blocks |
| 30 | + |
| 31 | + def get_driver(self) -> UDisks2DriveAsyncInterface | None: |
| 32 | + """Get current device driver""" |
| 33 | + if not self.driver_interface: |
| 34 | + return None |
| 35 | + return self.driver_interface |
| 36 | + |
| 37 | + def update_file_system( |
| 38 | + self, path: str, data: UDisks2FileSystemAsyncInterface |
| 39 | + ) -> None: |
| 40 | + """Add or update a filesystem for this device |
| 41 | +
|
| 42 | + Args: |
| 43 | + path (str): filesystem path |
| 44 | + data (UDisks2FileSystemAsyncInterface): The interface |
| 45 | + """ |
| 46 | + self.file_systems.update({path: data}) |
| 47 | + |
| 48 | + def update_raw_block(self, path: str, block: UDisks2BlockAsyncInterface) -> None: |
| 49 | + """Add or update a raw block for this device |
| 50 | +
|
| 51 | + Args: |
| 52 | + path (str): block path |
| 53 | + data (UDisks2BlockAsyncInterface): The blocks interface |
| 54 | + """ |
| 55 | + self.raw_block.update({path: block}) |
| 56 | + |
| 57 | + def update_logical_blocks( |
| 58 | + self, path: str, block: UDisks2BlockAsyncInterface |
| 59 | + ) -> None: |
| 60 | + """Add or update a logical block for this device |
| 61 | +
|
| 62 | + Args: |
| 63 | + path (str): block path |
| 64 | + data (UDisks2BlockAsyncInterface): The block interface |
| 65 | + """ |
| 66 | + self.logical_blocks.update({path: block}) |
| 67 | + |
| 68 | + def update_part_table( |
| 69 | + self, path: str, part: UDisks2PartitionTableAsyncInterface |
| 70 | + ) -> None: |
| 71 | + """Add or update partition table for this device |
| 72 | +
|
| 73 | + Args: |
| 74 | + path (str): Partition table path |
| 75 | + part (UDisks2PartitionTableAsyncInterface): The interface |
| 76 | + """ |
| 77 | + self.partition_tables.update({path: part}) |
| 78 | + |
| 79 | + def update_partitions( |
| 80 | + self, path: str, block: UDisks2PartitionAsyncInterface |
| 81 | + ) -> None: |
| 82 | + """Add or update partitions for the current device |
| 83 | +
|
| 84 | + Args: |
| 85 | + path (str): the partition path |
| 86 | + data (UDisks2PartitionAsyncInterface): The partition interface |
| 87 | + """ |
| 88 | + self.partitions.update({path: block}) |
| 89 | + |
| 90 | + def kill(self) -> None: |
| 91 | + """Delete the device and removes any track of it |
| 92 | +
|
| 93 | + Especially used when devices were removed unsafely |
| 94 | + """ |
| 95 | + self.delete() |
| 96 | + |
| 97 | + def delete(self) -> None: |
| 98 | + """Cleanup and delete this device""" |
| 99 | + del self.driver_interface |
| 100 | + self.partitions.clear() |
| 101 | + self.raw_block.clear() |
| 102 | + self.file_systems.clear() |
| 103 | + self.partition_tables.clear() |
| 104 | + self.symlinks.clear() |
0 commit comments