Skip to content

Commit 0b6517f

Browse files
committed
devmate/cli: Refactor list_devices output to tabular format.
Enhance the readability of the list_devices function output by presenting it in a structured tabular format. - Replaced the previous list output with a table-like structure using PrettyTable. - Updated dependencies to include PrettyTable. - Modified the unit tests to match the new table output. Signed-off-by: Nikolay Martyanov <nikolay@zededa.com>
1 parent 7e8b37b commit 0b6517f

3 files changed

Lines changed: 30 additions & 10 deletions

File tree

client/devmatecli/client.py

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import sys
55
from datetime import datetime, timezone
66
import humanize
7+
from prettytable import PrettyTable
78

89
DEVMATE_ADDRESS = os.environ.get('DEVMATE_ADDRESS', 'devmate.zededa.net')
910
DEVMATE_PORT = os.environ.get('DEVMATE_PORT', '8000')
@@ -39,10 +40,16 @@ def list_devices():
3940
response = requests.get(f"{BASE_URL}/devices/list")
4041
if response.status_code == 200:
4142
devices = response.json().get('devices', [])
42-
print(f"Found {len(devices)} device{'s' if len(devices) != 1 else ''}:")
43+
44+
# Create a table object
45+
table = PrettyTable()
46+
table.field_names = ["Name", "Model", "Status", "Reserved By", "Reserved At", "Reserved For"]
47+
4348
for device in devices:
4449
status = device.get('status')
4550
reserved_by = device.get('user')
51+
reserved_at = ""
52+
reserved_for = ""
4653
if status == 'reserved' and reserved_by:
4754
# Assuming that the reservation time is in UTC
4855
utc_time = datetime.fromisoformat(device.get('reservation_time')).replace(tzinfo=timezone.utc)
@@ -52,10 +59,12 @@ def list_devices():
5259
reserved_at = local_time.strftime("%d.%m.%Y %H:%M:%S")
5360
# For how long the device is reserved
5461
reserved_for = datetime_now() - utc_time
55-
reserved_for = humanize.naturaldelta(reserved_for)
56-
print(f" {device.get('name')} ({device.get('model')}) is {status} by {reserved_by} {reserved_at} ({reserved_for} by now)")
57-
else:
58-
print(f" {device.get('name')} ({device.get('model')}) is {status}")
62+
reserved_for = humanize.naturaldelta(reserved_for) + " by now"
63+
64+
table.add_row(
65+
[device.get('name'), device.get('model'), status, reserved_by or "", reserved_at, reserved_for])
66+
67+
print(table)
5968
elif response.status_code == 204:
6069
print("No devices found.")
6170
else:

client/requirements.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
pyinstaller
22
requests
3-
humanize
3+
humanize
4+
prettytable

client/tests/test_client.py

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,13 @@ def test_list_devices_found(self, mock_stdout, mock_get):
4141
mock_get.return_value.status_code = HTTPStatus.OK
4242
mock_get.return_value.json.return_value = {'devices': [{'name': 'Device1', 'model': 'Model1', 'status': 'free'}]}
4343
device_management.list_devices()
44-
expected_output = "Found 1 device:\n Device1 (Model1) is free\n"
44+
expected_output = (
45+
"+---------+--------+--------+-------------+-------------+--------------+\n"
46+
"| Name | Model | Status | Reserved By | Reserved At | Reserved For |\n"
47+
"+---------+--------+--------+-------------+-------------+--------------+\n"
48+
"| Device1 | Model1 | free | | | |\n"
49+
"+---------+--------+--------+-------------+-------------+--------------+\n"
50+
)
4551
self.assertEqual(mock_stdout.getvalue(), expected_output)
4652

4753
@patch('requests.get')
@@ -65,9 +71,13 @@ def test_list_devices_reserved_by(self, mock_stdout, mock_get, mock_datetime_now
6571
]
6672
}
6773
device_management.list_devices()
68-
69-
# Match the output including the date and duration strings
70-
expected_output = "Found 1 device:\n Device1 (Model1) is reserved by User1 01.01.2021 01:00:00 (an hour by now)\n"
74+
expected_output = (
75+
"+---------+--------+----------+-------------+---------------------+----------------+\n"
76+
"| Name | Model | Status | Reserved By | Reserved At | Reserved For |\n"
77+
"+---------+--------+----------+-------------+---------------------+----------------+\n"
78+
"| Device1 | Model1 | reserved | User1 | 01.01.2021 01:00:00 | an hour by now |\n"
79+
"+---------+--------+----------+-------------+---------------------+----------------+\n"
80+
)
7181
self.assertEqual(mock_stdout.getvalue(), expected_output)
7282

7383
@patch('requests.get')

0 commit comments

Comments
 (0)