1717
1818# isort: STDLIB
1919from argparse import Namespace
20+ from typing import Callable
2021
2122# isort: THIRDPARTY
2223from justbytes import Range
2324
25+ # isort: FIRSTPARTY
26+ from dbus_client_gen import DbusClientMissingPropertyError
27+
2428from .._stratisd_constants import BlockDevTiers
2529from ._connection import get_object
2630from ._constants import TOP_OBJECT
@@ -79,7 +83,23 @@ def list_devices(namespace: Namespace): # pylint: disable=too-many-locals
7983 ).search (managed_objects )
8084 )
8185
82- def paths (modev ):
86+ def catch_missing_property (modev , prop_to_name : Callable ) -> str :
87+ """
88+ Get a property from an modev. If the property is missing,
89+ return the TABLE_UNKNOWN_STRING value.
90+ """
91+ try :
92+ return prop_to_name (modev )
93+ except DbusClientMissingPropertyError : # pragma: no cover
94+ return TABLE_UNKNOWN_STRING
95+
96+ def pool_name_lookup (modev ) -> str :
97+ """
98+ Look up a name for a pool.
99+ """
100+ return path_to_name .get (modev .Pool (), TABLE_UNKNOWN_STRING )
101+
102+ def paths (modev ) -> str :
83103 """
84104 Return <physical_path> (<metadata_path>) if they are different,
85105 otherwise, just <metadata_path>.
@@ -100,37 +120,44 @@ def paths(modev):
100120 else f"{ physical_path } ({ metadata_path } )"
101121 )
102122
103- def size (modev ):
123+ def size (modev ) -> str :
104124 """
105125 Return in-use size (observed size) if they are different, otherwise
106126 just in-use size.
107127 """
108128 in_use_size = Range (modev .TotalPhysicalSize ())
109129 observed_size = get_property (modev .NewPhysicalSize (), Range , in_use_size )
130+
110131 return (
111132 f"{ in_use_size } "
112133 if in_use_size == observed_size
113134 else f"{ in_use_size } ({ observed_size } )"
114135 )
115136
116- def tier_str (value ) :
137+ def tier_str (modev ) -> str :
117138 """
118139 String representation of a tier.
119140 """
120141 try :
121- return str (BlockDevTiers (value ))
142+ return str (BlockDevTiers (modev . Tier () ))
122143 except ValueError : # pragma: no cover
123144 return TABLE_UNKNOWN_STRING
124145
125146 format_uuid = get_uuid_formatter (namespace .unhyphenated_uuids )
126147
148+ def uuid_str (modev ) -> str :
149+ """
150+ String representation of UUID.
151+ """
152+ return format_uuid (modev .Uuid ())
153+
127154 tables = [
128155 [
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 () ),
156+ catch_missing_property (modev , pool_name_lookup ),
157+ catch_missing_property (modev , paths ),
158+ catch_missing_property (modev , size ),
159+ catch_missing_property (modev , tier_str ),
160+ catch_missing_property (modev , uuid_str ),
134161 ]
135162 for modev in modevs
136163 ]
0 commit comments