diff --git a/src/stratis_cli/_actions/_formatting.py b/src/stratis_cli/_actions/_formatting.py index af11b62af..a40b17cb9 100644 --- a/src/stratis_cli/_actions/_formatting.py +++ b/src/stratis_cli/_actions/_formatting.py @@ -22,7 +22,6 @@ # isort: THIRDPARTY from dbus import Struct -from justbytes import Range from wcwidth import wcswidth # placeholder for tables where a desired value was not obtained from stratisd @@ -36,26 +35,6 @@ TOTAL_USED_FREE = "Total / Used / Free" -def size_triple(size: Range, used: Optional[Range]) -> str: - """ - Given size and used, return a properly formatted string Total/ Used / Free - - :param size: total size - :type size: Range or NoneType - :param used: total amount used - :type used: Range or NoneType - :rtype: str - :returns: formatted string for display - """ - free = None if used is None else size - used - - return ( - f"{size} / " - f"{TABLE_FAILURE_STRING if used is None else used} / " - f"{TABLE_FAILURE_STRING if free is None else free}" - ) - - def get_property(prop: Struct, to_repr: Callable, default: Optional[Any]): """ Get a representation of an optional D-Bus property. An optional diff --git a/src/stratis_cli/_actions/_list_filesystem.py b/src/stratis_cli/_actions/_list_filesystem.py index 1a82e3e71..d63250144 100644 --- a/src/stratis_cli/_actions/_list_filesystem.py +++ b/src/stratis_cli/_actions/_list_filesystem.py @@ -26,7 +26,13 @@ from ._connection import get_object from ._constants import TOP_OBJECT -from ._formatting import TOTAL_USED_FREE, get_property, print_table, size_triple +from ._formatting import ( + TABLE_FAILURE_STRING, + TOTAL_USED_FREE, + get_property, + print_table, +) +from ._utils import SizeTriple def list_filesystems( @@ -133,7 +139,18 @@ def filesystem_size_quartet( :returns: a string a formatted string showing all three values :rtype: str """ - return f'{size_triple(total, used)} / {"None" if limit is None else limit}' + size_triple = SizeTriple(total, used) + triple_str = " / ".join( + ( + TABLE_FAILURE_STRING if x is None else str(x) + for x in ( + size_triple.total(), + size_triple.used(), + size_triple.free(), + ) + ) + ) + return f'{triple_str} / {"None" if limit is None else limit}' tables = [ ( @@ -176,8 +193,7 @@ def display(self): fs = self.filesystems_with_props[0] - total = Range(fs.Size()) - used = get_property(fs.Used(), Range, None) + size_triple = SizeTriple(Range(fs.Size()), get_property(fs.Used(), Range, None)) limit = get_property(fs.SizeLimit(), Range, None) created = ( date_parser.isoparse(fs.Created()).astimezone().strftime("%b %d %Y %H:%M") @@ -199,8 +215,14 @@ def display(self): print(f" Revert scheduled: {scheduled}") print() print("Sizes:") - print(f" Logical size of thin device: {total}") - print(f" Total used (including XFS metadata): {used}") - print(f" Free: {total - used}") + print(f" Logical size of thin device: {size_triple.total()}") + print( + " Total used (including XFS metadata): " + f"{TABLE_FAILURE_STRING if size_triple.used() is None else size_triple.used()}" + ) + print( + " Free: " + f"{TABLE_FAILURE_STRING if size_triple.free() is None else size_triple.free()}" + ) print() print(f" Size Limit: {'None' if limit is None else limit}") diff --git a/src/stratis_cli/_actions/_list_pool.py b/src/stratis_cli/_actions/_list_pool.py index e12e0ffa4..a60f26066 100644 --- a/src/stratis_cli/_actions/_list_pool.py +++ b/src/stratis_cli/_actions/_list_pool.py @@ -39,12 +39,12 @@ TOTAL_USED_FREE, get_property, print_table, - size_triple, ) from ._utils import ( EncryptionInfoClevis, EncryptionInfoKeyDescription, PoolFeature, + SizeTriple, StoppedPool, fetch_stopped_pools_property, ) @@ -399,19 +399,19 @@ def _print_detail_view( else: print("Encryption Enabled: No") - total_physical_used = get_property(mopool.TotalPhysicalUsed(), Range, None) + size_triple = SizeTriple( + Range(mopool.TotalPhysicalSize()), + get_property(mopool.TotalPhysicalUsed(), Range, None), + ) print(f"Fully Allocated: {'Yes' if mopool.NoAllocSpace() else 'No'}") - print(f" Size: {Range(mopool.TotalPhysicalSize())}") + print(f" Size: {size_triple.total()}") print(f" Allocated: {Range(mopool.AllocatedSize())}") - - total_physical_used = get_property(mopool.TotalPhysicalUsed(), Range, None) - total_physical_used_str = ( - TABLE_FAILURE_STRING if total_physical_used is None else total_physical_used + print( + " Used: " + f"{TABLE_FAILURE_STRING if size_triple.used() is None else size_triple.used()}" ) - print(f" Used: {total_physical_used_str}") - def display(self): """ List a single pool in detail. @@ -471,9 +471,21 @@ def physical_size_triple(mopool): :returns: a string to display in the resulting list output :rtype: str """ - total_physical_size = Range(mopool.TotalPhysicalSize()) - total_physical_used = get_property(mopool.TotalPhysicalUsed(), Range, None) - return size_triple(total_physical_size, total_physical_used) + size_triple = SizeTriple( + Range(mopool.TotalPhysicalSize()), + get_property(mopool.TotalPhysicalUsed(), Range, None), + ) + + return " / ".join( + ( + TABLE_FAILURE_STRING if x is None else str(x) + for x in ( + size_triple.total(), + size_triple.used(), + size_triple.free(), + ) + ) + ) def properties_string(mopool): """ diff --git a/src/stratis_cli/_actions/_utils.py b/src/stratis_cli/_actions/_utils.py index 61d02c513..d2c01e0e5 100644 --- a/src/stratis_cli/_actions/_utils.py +++ b/src/stratis_cli/_actions/_utils.py @@ -23,12 +23,13 @@ import sys import termios from enum import Enum -from typing import Any, Tuple +from typing import Any, Optional, Tuple from uuid import UUID # isort: THIRDPARTY from dbus import Dictionary, Struct from dbus.proxies import ProxyObject +from justbytes import Range from .._errors import ( StratisCliKeyfileNotFoundError, @@ -266,3 +267,31 @@ def fetch_stopped_pools_property(proxy: ProxyObject) -> Dictionary: from ._data import Manager return Manager.Properties.StoppedPools.Get(proxy) + + +class SizeTriple: + """ + Manage values in a size triple. + """ + + def __init__(self, total: Range, used: Optional[Range]): + self._total = total + self._used = used + + def total(self) -> Range: + """ + Total. + """ + return self._total + + def used(self) -> Optional[Range]: + """ + Used. + """ + return self._used + + def free(self) -> Optional[Range]: + """ + Total - used. + """ + return None if self._used is None else self._total - self._used