Fix blockdevice key error (#1079)

Co-authored-by: Daniel Girtler <girtler.daniel@gmail.com>
This commit is contained in:
Daniel 2022-04-22 21:23:38 +10:00 committed by GitHub
parent 0604ed45c3
commit 2529d6a5f5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 8 additions and 7 deletions

View File

@ -7,12 +7,13 @@ from typing import Optional, Dict, Any, Iterator, Tuple, List, TYPE_CHECKING
# https://stackoverflow.com/a/39757388/929999 # https://stackoverflow.com/a/39757388/929999
if TYPE_CHECKING: if TYPE_CHECKING:
from .partition import Partition from .partition import Partition
from ..exceptions import DiskError, SysCallError from ..exceptions import DiskError, SysCallError
from ..output import log from ..output import log
from ..general import SysCommand from ..general import SysCommand
from ..storage import storage from ..storage import storage
class BlockDevice: class BlockDevice:
def __init__(self, path :str, info :Optional[Dict[str, Any]] = None): def __init__(self, path :str, info :Optional[Dict[str, Any]] = None):
if not info: if not info:
@ -38,7 +39,9 @@ class BlockDevice:
yield self.partitions[partition] yield self.partitions[partition]
def __getitem__(self, key :str, *args :str, **kwargs :str) -> Any: def __getitem__(self, key :str, *args :str, **kwargs :str) -> Any:
if key not in self.info: if hasattr(self, key):
return getattr(self, key)
elif key not in self.info:
raise KeyError(f'{self} does not contain information: "{key}"') raise KeyError(f'{self} does not contain information: "{key}"')
return self.info[key] return self.info[key]

View File

@ -221,10 +221,8 @@ def all_blockdevices(mappers=False, partitions=False, error=False) -> Dict[str,
device_path = f"/dev/{pathlib.Path(block_device).readlink().name}" device_path = f"/dev/{pathlib.Path(block_device).readlink().name}"
try: try:
information = blkid(f'blkid -p -o export {device_path}') information = blkid(f'blkid -p -o export {device_path}')
except SysCallError as ex:
# TODO: No idea why F841 is raised here: if ex.exit_code in (512, 2):
except SysCallError as error: # noqa: F841
if error.exit_code in (512, 2):
# Assume that it's a loop device, and try to get info on it # Assume that it's a loop device, and try to get info on it
try: try:
information = get_loop_info(device_path) information = get_loop_info(device_path)
@ -234,7 +232,7 @@ def all_blockdevices(mappers=False, partitions=False, error=False) -> Dict[str,
except SysCallError: except SysCallError:
information = get_blockdevice_uevent(pathlib.Path(block_device).readlink().name) information = get_blockdevice_uevent(pathlib.Path(block_device).readlink().name)
else: else:
raise error raise ex
information = enrich_blockdevice_information(information) information = enrich_blockdevice_information(information)