Skip to content

Commit 3350c4a

Browse files
committed
Do not allow not found exceptions to propagate in list context
zbus crate omits property values from the GetManagedObjects result if it can not get them. Instead of a stack trace, return a TABLE_UNKNOWN_STRING for printing. Signed-off-by: mulhern <amulhern@redhat.com>
1 parent 5032781 commit 3350c4a

4 files changed

Lines changed: 103 additions & 30 deletions

File tree

src/stratis_cli/_actions/_formatting.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,17 @@
1717

1818
# isort: STDLIB
1919
import sys
20+
from functools import wraps
2021
from typing import Any, Callable, List, Optional
2122
from uuid import UUID
2223

2324
# isort: THIRDPARTY
2425
from dbus import Struct
2526
from wcwidth import wcswidth
2627

28+
# isort: FIRSTPARTY
29+
from dbus_client_gen import DbusClientMissingPropertyError
30+
2731
# placeholder for tables where a desired value was not obtained from stratisd
2832
# when the value should be supported.
2933
TABLE_FAILURE_STRING = "FAILURE"
@@ -160,3 +164,23 @@ def get_uuid_formatter(unhyphenated: bool) -> Callable:
160164
return (
161165
(lambda u: UUID(str(u)).hex) if unhyphenated else (lambda u: str(UUID(str(u))))
162166
)
167+
168+
169+
def catch_missing_property(
170+
prop_to_str: Callable[[Any], Any], default: Any
171+
) -> Callable[[Any], Any]:
172+
"""
173+
Return a function to just return a default if a property is missing.
174+
"""
175+
176+
@wraps(prop_to_str)
177+
def inner(mo: Any) -> str:
178+
"""
179+
Catch the exception and return a default
180+
"""
181+
try:
182+
return prop_to_str(mo)
183+
except DbusClientMissingPropertyError: # pragma: no cover
184+
return default
185+
186+
return inner

src/stratis_cli/_actions/_list_filesystem.py

Lines changed: 31 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,9 @@
2828
from ._constants import TOP_OBJECT
2929
from ._formatting import (
3030
TABLE_FAILURE_STRING,
31+
TABLE_UNKNOWN_STRING,
3132
TOTAL_USED_FREE,
33+
catch_missing_property,
3234
get_property,
3335
print_table,
3436
)
@@ -152,17 +154,37 @@ def filesystem_size_quartet(
152154
)
153155
return f"{triple_str} / {limit}"
154156

157+
pool_name_func = catch_missing_property(
158+
lambda mo: self.pool_object_path_to_pool_name.get(
159+
mo.Pool(), TABLE_UNKNOWN_STRING
160+
),
161+
TABLE_UNKNOWN_STRING,
162+
)
163+
name_func = catch_missing_property(
164+
lambda mofs: mofs.Name(), TABLE_UNKNOWN_STRING
165+
)
166+
size_func = catch_missing_property(
167+
lambda mofs: filesystem_size_quartet(
168+
Range(mofs.Size()),
169+
get_property(mofs.Used(), Range, None),
170+
get_property(mofs.SizeLimit(), Range, None),
171+
),
172+
TABLE_UNKNOWN_STRING,
173+
)
174+
devnode_func = catch_missing_property(
175+
lambda mofs: mofs.Devnode(), TABLE_UNKNOWN_STRING
176+
)
177+
uuid_func = catch_missing_property(
178+
lambda mofs: self.uuid_formatter(mofs.Uuid()), TABLE_UNKNOWN_STRING
179+
)
180+
155181
tables = [
156182
(
157-
self.pool_object_path_to_pool_name[mofilesystem.Pool()],
158-
mofilesystem.Name(),
159-
filesystem_size_quartet(
160-
Range(mofilesystem.Size()),
161-
get_property(mofilesystem.Used(), Range, None),
162-
get_property(mofilesystem.SizeLimit(), Range, None),
163-
),
164-
mofilesystem.Devnode(),
165-
self.uuid_formatter(mofilesystem.Uuid()),
183+
pool_name_func(mofilesystem),
184+
name_func(mofilesystem),
185+
size_func(mofilesystem),
186+
devnode_func(mofilesystem),
187+
uuid_func(mofilesystem),
166188
)
167189
for mofilesystem in self.filesystems_with_props
168190
]

src/stratis_cli/_actions/_list_pool.py

Lines changed: 26 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
from justbytes import Range
3333

3434
from .._alerts import (
35+
PoolAlertType,
3536
PoolAllocSpaceAlert,
3637
PoolDeviceSizeChangeAlert,
3738
PoolEncryptionAlert,
@@ -44,7 +45,9 @@
4445
from ._constants import TOP_OBJECT
4546
from ._formatting import (
4647
TABLE_FAILURE_STRING,
48+
TABLE_UNKNOWN_STRING,
4749
TOTAL_USED_FREE,
50+
catch_missing_property,
4851
get_property,
4952
print_table,
5053
)
@@ -445,7 +448,7 @@ def __init__(self, uuid_formatter: Callable[[str | UUID], str]):
445448
"""
446449
self.uuid_formatter = uuid_formatter
447450

448-
def display(self):
451+
def display(self): # pylint: disable=too-many-locals
449452
"""
450453
List pools in table view.
451454
"""
@@ -522,20 +525,31 @@ def gen_string(has_property: bool, code: str) -> str:
522525
(objpath, MOPool(info)) for objpath, info in pools().search(managed_objects)
523526
]
524527

528+
name_func = catch_missing_property(lambda mo: mo.Name(), TABLE_UNKNOWN_STRING)
529+
size_func = catch_missing_property(physical_size_triple, TABLE_UNKNOWN_STRING)
530+
properties_func = catch_missing_property(
531+
properties_string, TABLE_UNKNOWN_STRING
532+
)
533+
uuid_func = catch_missing_property(
534+
lambda mo: self.uuid_formatter(mo.Uuid()), TABLE_UNKNOWN_STRING
535+
)
536+
537+
def alert_func(pool_object_path: str, mopool: Any) -> List[PoolAlertType]:
538+
"""
539+
Combined alert codes.
540+
"""
541+
return catch_missing_property(
542+
Default.alert_codes, default=[TABLE_UNKNOWN_STRING]
543+
)(mopool) + alerts.alert_codes(pool_object_path)
544+
525545
tables = [
526546
(
527-
mopool.Name(),
528-
physical_size_triple(mopool),
529-
properties_string(mopool),
530-
self.uuid_formatter(mopool.Uuid()),
547+
name_func(mopool),
548+
size_func(mopool),
549+
properties_func(mopool),
550+
uuid_func(mopool),
531551
", ".join(
532-
sorted(
533-
str(code)
534-
for code in (
535-
Default.alert_codes(mopool)
536-
+ alerts.alert_codes(pool_object_path)
537-
)
538-
)
552+
sorted(str(code) for code in alert_func(pool_object_path, mopool))
539553
),
540554
)
541555
for (pool_object_path, mopool) in pools_with_props

src/stratis_cli/_actions/_physical.py

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
# isort: STDLIB
1919
from argparse import Namespace
20+
from typing import Any
2021

2122
# isort: THIRDPARTY
2223
from justbytes import Range
@@ -26,6 +27,7 @@
2627
from ._constants import TOP_OBJECT
2728
from ._formatting import (
2829
TABLE_UNKNOWN_STRING,
30+
catch_missing_property,
2931
get_property,
3032
get_uuid_formatter,
3133
print_table,
@@ -79,7 +81,7 @@ def list_devices(namespace: Namespace): # pylint: disable=too-many-locals
7981
).search(managed_objects)
8082
)
8183

82-
def paths(modev):
84+
def paths(modev: Any) -> str:
8385
"""
8486
Return <physical_path> (<metadata_path>) if they are different,
8587
otherwise, just <metadata_path>.
@@ -100,7 +102,7 @@ def paths(modev):
100102
else f"{physical_path} ({metadata_path})"
101103
)
102104

103-
def size(modev):
105+
def size_str(modev: Any) -> str:
104106
"""
105107
Return in-use size (observed size) if they are different, otherwise
106108
just in-use size.
@@ -113,24 +115,35 @@ def size(modev):
113115
else f"{in_use_size} ({observed_size})"
114116
)
115117

116-
def tier_str(value):
118+
def tier_str(modev: Any) -> str:
117119
"""
118120
String representation of a tier.
119121
"""
120122
try:
121-
return str(BlockDevTiers(value))
123+
return str(BlockDevTiers(modev.Tier()))
122124
except ValueError: # pragma: no cover
123125
return TABLE_UNKNOWN_STRING
124126

125127
format_uuid = get_uuid_formatter(namespace.unhyphenated_uuids)
126128

129+
pool_name_func = catch_missing_property(
130+
lambda mo: path_to_name.get(mo.Pool(), TABLE_UNKNOWN_STRING),
131+
TABLE_UNKNOWN_STRING,
132+
)
133+
paths_func = catch_missing_property(paths, TABLE_UNKNOWN_STRING)
134+
size_func = catch_missing_property(size_str, TABLE_UNKNOWN_STRING)
135+
tier_func = catch_missing_property(tier_str, TABLE_UNKNOWN_STRING)
136+
uuid_func = catch_missing_property(
137+
lambda modev: format_uuid(modev.Uuid()), TABLE_UNKNOWN_STRING
138+
)
139+
127140
tables = [
128141
[
129-
path_to_name.get(modev.Pool(), TABLE_UNKNOWN_STRING),
130-
paths(modev),
131-
size(modev),
132-
tier_str(modev.Tier()),
133-
format_uuid(modev.Uuid()),
142+
pool_name_func(modev),
143+
paths_func(modev),
144+
size_func(modev),
145+
tier_func(modev),
146+
uuid_func(modev),
134147
]
135148
for modev in modevs
136149
]

0 commit comments

Comments
 (0)