Skip to content

Commit 552e4f4

Browse files
authored
Merge pull request #1228 from mulkieran/size-quartet
Refactor filesystem_size_quartet method
2 parents 5ff4e89 + 77f5e2c commit 552e4f4

File tree

2 files changed

+31
-17
lines changed

2 files changed

+31
-17
lines changed

src/stratis_cli/_actions/_formatting.py

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

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

2223
# isort: THIRDPARTY
24+
from dbus import Struct
25+
from justbytes import Range
2326
from wcwidth import wcswidth
2427

2528
# placeholder for tables where a desired value was not obtained from stratisd
@@ -33,7 +36,7 @@
3336
TOTAL_USED_FREE = "Total / Used / Free"
3437

3538

36-
def size_triple(size, used):
39+
def size_triple(size: Range, used: Optional[Range]) -> str:
3740
"""
3841
Given size and used, return a properly formatted string Total/ Used / Free
3942
@@ -44,16 +47,16 @@ def size_triple(size, used):
4447
:rtype: str
4548
:returns: formatted string for display
4649
"""
47-
free = None if size is None or used is None else size - used
50+
free = None if used is None else size - used
4851

4952
return (
50-
f"{TABLE_FAILURE_STRING if size is None else size} / "
53+
f"{size} / "
5154
f"{TABLE_FAILURE_STRING if used is None else used} / "
5255
f"{TABLE_FAILURE_STRING if free is None else free}"
5356
)
5457

5558

56-
def get_property(prop, to_repr, default):
59+
def get_property(prop: Struct, to_repr: Callable, default: Optional[Any]):
5760
"""
5861
Get a representation of an optional D-Bus property. An optional
5962
D-Bus property is one that may be unknown to stratisd.
@@ -70,7 +73,7 @@ def get_property(prop, to_repr, default):
7073
return to_repr(value) if valid else default
7174

7275

73-
def _get_column_len(column_width, entry_len, entry_width):
76+
def _get_column_len(column_width: int, entry_len: int, entry_width: int) -> int:
7477
"""
7578
From the desired column width in cells and the item to be printed,
7679
calculate the required number of characters to pass to the format method.
@@ -94,7 +97,13 @@ def _get_column_len(column_width, entry_len, entry_width):
9497
return column_width - (entry_width - entry_len)
9598

9699

97-
def _print_row(file, row, row_widths, column_widths, column_alignments):
100+
def _print_row(
101+
file: Any,
102+
row: Any,
103+
row_widths: List[int],
104+
column_widths: List[int],
105+
column_alignments: List[str],
106+
):
98107
"""
99108
Print a single row in a table. The row might be the header row, or
100109
a row of data items.
@@ -117,7 +126,12 @@ def _print_row(file, row, row_widths, column_widths, column_alignments):
117126
print(" ".join(entries), end="", file=file)
118127

119128

120-
def print_table(column_headings, row_entries, alignment, file=sys.stdout):
129+
def print_table(
130+
column_headings: List[str],
131+
row_entries: List[Any],
132+
alignment: List[str],
133+
file=sys.stdout,
134+
):
121135
"""
122136
Given the column headings and the row_entries, print a table.
123137
Align according to alignment specification and always pad with 2 spaces.
@@ -156,7 +170,7 @@ def print_table(column_headings, row_entries, alignment, file=sys.stdout):
156170
print(file=file)
157171

158172

159-
def get_uuid_formatter(unhyphenated):
173+
def get_uuid_formatter(unhyphenated: bool) -> Callable:
160174
"""
161175
Get a function to format UUIDs.
162176

src/stratis_cli/_actions/_list_filesystem.py

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

1818
# isort: STDLIB
1919
from abc import ABC, abstractmethod
20-
from typing import Any, Callable, Dict, List
20+
from typing import Any, Callable, Dict, List, Optional
2121

2222
# isort: THIRDPARTY
2323
from dateutil import parser as date_parser
@@ -124,26 +124,26 @@ def display(self):
124124
List the filesystems.
125125
"""
126126

127-
def filesystem_size_quartet(dbus_props):
127+
def filesystem_size_quartet(
128+
total: Range, used: Optional[Range], limit: Optional[Range]
129+
) -> str:
128130
"""
129131
Calculate the triple to display for filesystem size.
130132
131-
:param dbus_props: filesystem D-Bus properties
132-
:type dbus_props: MOFilesystem
133-
134133
:returns: a string a formatted string showing all three values
135134
:rtype: str
136135
"""
137-
total = Range(dbus_props.Size())
138-
used = get_property(dbus_props.Used(), Range, None)
139-
limit = get_property(dbus_props.SizeLimit(), Range, None)
140136
return f'{size_triple(total, used)} / {"None" if limit is None else limit}'
141137

142138
tables = [
143139
(
144140
self.pool_object_path_to_pool_name[mofilesystem.Pool()],
145141
mofilesystem.Name(),
146-
filesystem_size_quartet(mofilesystem),
142+
filesystem_size_quartet(
143+
Range(mofilesystem.Size()),
144+
get_property(mofilesystem.Used(), Range, None),
145+
get_property(mofilesystem.SizeLimit(), Range, None),
146+
),
147147
mofilesystem.Devnode(),
148148
self.uuid_formatter(mofilesystem.Uuid()),
149149
)

0 commit comments

Comments
 (0)