1717
1818# isort: STDLIB
1919from abc import ABC , abstractmethod
20- from typing import Any , Callable , Dict , List , Optional
20+ from typing import Any , Callable , Dict , List
2121
2222# isort: THIRDPARTY
2323from dateutil import parser as date_parser
2424from dbus import ObjectPath , String
2525from justbytes import Range
2626
27+ # isort: FIRSTPARTY
28+ from dbus_client_gen import DbusClientMissingPropertyError
29+
2730from ._connection import get_object
2831from ._constants import TOP_OBJECT
2932from ._formatting import (
30- TABLE_FAILURE_STRING ,
33+ TABLE_UNKNOWN_STRING ,
3134 TOTAL_USED_FREE ,
3235 get_property ,
3336 print_table ,
@@ -124,7 +127,67 @@ def size_triple(mofs: Any) -> SizeTriple:
124127 """
125128 Calculate size triple
126129 """
127- return SizeTriple (Range (mofs .Size ()), get_property (mofs .Used (), Range , None ))
130+ try :
131+ size = Range (mofs .Size ())
132+ except DbusClientMissingPropertyError :
133+ size = None
134+
135+ try :
136+ used = get_property (mofs .Used (), Range , None )
137+ except DbusClientMissingPropertyError :
138+ used = None
139+
140+ return SizeTriple (size , used )
141+
142+ @staticmethod
143+ def limit_str (mofs : Any ) -> str :
144+ """
145+ Return limit representation for printing.
146+ """
147+ try :
148+ return str (get_property (mofs .SizeLimit (), Range , None ))
149+ except DbusClientMissingPropertyError :
150+ return TABLE_UNKNOWN_STRING
151+
152+ @staticmethod
153+ def devnode_str (mofs : Any ) -> str :
154+ """
155+ Return devnode representation for printing.
156+ """
157+ try :
158+ return mofs .Devnode ()
159+ except DbusClientMissingPropertyError :
160+ return TABLE_UNKNOWN_STRING
161+
162+ @staticmethod
163+ def name_str (mofs : Any ) -> str :
164+ """
165+ Return name representation for printing.
166+ """
167+ try :
168+ return mofs .Name ()
169+ except DbusClientMissingPropertyError : # pragma: no cover
170+ return TABLE_UNKNOWN_STRING
171+
172+ def uuid_str (self , mofs : Any ) -> str :
173+ """
174+ Return representation of UUID, correctly formatted according to options.
175+ """
176+ try :
177+ return self .uuid_formatter (mofs .Uuid ())
178+ except DbusClientMissingPropertyError : # pragma: no cover
179+ return TABLE_UNKNOWN_STRING
180+
181+ def pool_name_str (self , mofs : Any ) -> str :
182+ """
183+ Return the name of this filesystem's pool.
184+ """
185+ try :
186+ return self .pool_object_path_to_pool_name .get (
187+ mofs .Pool (), TABLE_UNKNOWN_STRING
188+ )
189+ except DbusClientMissingPropertyError : # pragma: no cover
190+ return TABLE_UNKNOWN_STRING
128191
129192
130193class Table (ListFilesystem ): # pylint: disable=too-few-public-methods
@@ -137,18 +200,19 @@ def display(self):
137200 List the filesystems.
138201 """
139202
140- def filesystem_size_quartet (
141- size_triple : SizeTriple , limit : Optional [Range ]
142- ) -> str :
203+ def filesystem_size_quartet (mofs : Any ) -> str :
143204 """
144- Calculate the triple to display for filesystem size .
205+ Calculate the string to display for filesystem sizes .
145206
146- :returns: a string a formatted string showing all three values
207+ :returns: a properly formatted string
147208 :rtype: str
148209 """
210+ size_triple = ListFilesystem .size_triple (mofs )
211+ limit = ListFilesystem .limit_str (mofs )
212+
149213 triple_str = " / " .join (
150214 (
151- TABLE_FAILURE_STRING if x is None else str (x )
215+ TABLE_UNKNOWN_STRING if x is None else str (x )
152216 for x in (
153217 size_triple .total (),
154218 size_triple .used (),
@@ -160,14 +224,11 @@ def filesystem_size_quartet(
160224
161225 tables = [
162226 (
163- self .pool_object_path_to_pool_name [mofilesystem .Pool ()],
164- mofilesystem .Name (),
165- filesystem_size_quartet (
166- ListFilesystem .size_triple (mofilesystem ),
167- get_property (mofilesystem .SizeLimit (), Range , None ),
168- ),
169- mofilesystem .Devnode (),
170- self .uuid_formatter (mofilesystem .Uuid ()),
227+ self .pool_name_str (mofilesystem ),
228+ ListFilesystem .name_str (mofilesystem ),
229+ filesystem_size_quartet (mofilesystem ),
230+ ListFilesystem .devnode_str (mofilesystem ),
231+ self .uuid_str (mofilesystem ),
171232 )
172233 for mofilesystem in self .filesystems_with_props
173234 ]
@@ -198,36 +259,47 @@ def display(self):
198259
199260 fs = self .filesystems_with_props [0 ]
200261
201- size_triple = ListFilesystem .size_triple (fs )
202- limit = get_property (fs .SizeLimit (), Range , None )
203- created = (
204- date_parser .isoparse (fs .Created ()).astimezone ().strftime ("%b %d %Y %H:%M" )
205- )
262+ print (f"UUID: { self .uuid_str (fs )} " )
263+ print (f"Name: { ListFilesystem .name_str (fs )} " )
264+ print (f"Pool: { self .pool_name_str (fs )} " )
206265
207- origin = get_property (fs .Origin (), self .uuid_formatter , None )
208-
209- print (f"UUID: { self .uuid_formatter (fs .Uuid ())} " )
210- print (f"Name: { fs .Name ()} " )
211- print (f"Pool: { self .pool_object_path_to_pool_name [fs .Pool ()]} " )
212266 print ()
213- print (f"Device: { fs .Devnode ()} " )
267+ print (f"Device: { ListFilesystem .devnode_str (fs )} " )
268+
269+ try :
270+ created = (
271+ date_parser .isoparse (fs .Created ())
272+ .astimezone ()
273+ .strftime ("%b %d %Y %H:%M" )
274+ )
275+ except DbusClientMissingPropertyError :
276+ created = TABLE_UNKNOWN_STRING
214277 print ()
215278 print (f"Created: { created } " )
279+
216280 print ()
217- print (f"Snapshot origin: { origin } " )
218- if origin is not None :
219- scheduled = "Yes" if fs .MergeScheduled () else "No"
220- print (f" Revert scheduled: { scheduled } " )
281+ try :
282+ origin = get_property (fs .Origin (), self .uuid_formatter , None )
283+ print (f"Snapshot origin: { origin } " )
284+ if origin is not None :
285+ try :
286+ scheduled = "Yes" if fs .MergeScheduled () else "No"
287+ except DbusClientMissingPropertyError :
288+ scheduled = TABLE_UNKNOWN_STRING
289+ print (f" Revert scheduled: { scheduled } " )
290+ except DbusClientMissingPropertyError :
291+ print (f"Snapshot origin: { TABLE_UNKNOWN_STRING } " )
292+
293+ def size_str (value : Range | None ) -> str :
294+ return TABLE_UNKNOWN_STRING if value is None else str (value )
295+
296+ size_triple = ListFilesystem .size_triple (fs )
221297 print ()
222298 print ("Sizes:" )
223- print (f" Logical size of thin device: { size_triple .total ()} " )
224- print (
225- " Total used (including XFS metadata): "
226- f"{ TABLE_FAILURE_STRING if size_triple .used () is None else size_triple .used ()} "
227- )
228- print (
229- " Free: "
230- f"{ TABLE_FAILURE_STRING if size_triple .free () is None else size_triple .free ()} "
231- )
299+ print (f" Logical size of thin device: { size_str (size_triple .total ())} " )
300+ print (f" Total used (including XFS metadata): { size_str (size_triple .used ())} " )
301+ print (f" Free: { size_str (size_triple .free ())} " )
302+
303+ limit = ListFilesystem .limit_str (fs )
232304 print ()
233305 print (f" Size Limit: { limit } " )
0 commit comments