Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions src/stratis_cli/_actions/_formatting.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,17 @@

# isort: STDLIB
import sys
from functools import wraps
from typing import Any, Callable, List, Optional
from uuid import UUID

# isort: THIRDPARTY
from dbus import Struct
from wcwidth import wcswidth

# isort: FIRSTPARTY
from dbus_client_gen import DbusClientMissingPropertyError

# placeholder for tables where a desired value was not obtained from stratisd
# when the value should be supported.
TABLE_FAILURE_STRING = "FAILURE"
Expand Down Expand Up @@ -160,3 +164,23 @@ def get_uuid_formatter(unhyphenated: bool) -> Callable:
return (
(lambda u: UUID(str(u)).hex) if unhyphenated else (lambda u: str(UUID(str(u))))
)


def catch_missing_property(
prop_to_str: Callable[[Any], Any], default: Any
) -> Callable[[Any], Any]:
"""
Return a function to just return a default if a property is missing.
"""

@wraps(prop_to_str)
def inner(mo: Any) -> str:
"""
Catch the exception and return a default
"""
try:
return prop_to_str(mo)
except DbusClientMissingPropertyError: # pragma: no cover
return default

return inner
40 changes: 31 additions & 9 deletions src/stratis_cli/_actions/_list_filesystem.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,9 @@
from ._constants import TOP_OBJECT
from ._formatting import (
TABLE_FAILURE_STRING,
TABLE_UNKNOWN_STRING,
TOTAL_USED_FREE,
catch_missing_property,
get_property,
print_table,
)
Expand Down Expand Up @@ -152,17 +154,37 @@ def filesystem_size_quartet(
)
return f"{triple_str} / {limit}"

pool_name_func = catch_missing_property(
lambda mo: self.pool_object_path_to_pool_name.get(
mo.Pool(), TABLE_UNKNOWN_STRING
),
TABLE_UNKNOWN_STRING,
)
name_func = catch_missing_property(
lambda mofs: mofs.Name(), TABLE_UNKNOWN_STRING
)
size_func = catch_missing_property(
lambda mofs: filesystem_size_quartet(
Range(mofs.Size()),
get_property(mofs.Used(), Range, None),
get_property(mofs.SizeLimit(), Range, None),
),
TABLE_UNKNOWN_STRING,
)
devnode_func = catch_missing_property(
lambda mofs: mofs.Devnode(), TABLE_UNKNOWN_STRING
)
uuid_func = catch_missing_property(
lambda mofs: self.uuid_formatter(mofs.Uuid()), TABLE_UNKNOWN_STRING
)

tables = [
(
self.pool_object_path_to_pool_name[mofilesystem.Pool()],
mofilesystem.Name(),
filesystem_size_quartet(
Range(mofilesystem.Size()),
get_property(mofilesystem.Used(), Range, None),
get_property(mofilesystem.SizeLimit(), Range, None),
),
mofilesystem.Devnode(),
self.uuid_formatter(mofilesystem.Uuid()),
pool_name_func(mofilesystem),
name_func(mofilesystem),
size_func(mofilesystem),
devnode_func(mofilesystem),
uuid_func(mofilesystem),
)
for mofilesystem in self.filesystems_with_props
]
Expand Down
Loading
Loading