Skip to content

Commit b5afc8c

Browse files
christophehenryzamentur
authored andcommitted
Apply suggestions from peer review
1 parent 90ea517 commit b5afc8c

File tree

4 files changed

+25
-40
lines changed

4 files changed

+25
-40
lines changed

doc/generate_bash_completion.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@
4242
def get_dict_actions(OPTION_SUBTREE, category):
4343
ACTIONS = [
4444
action
45-
for action in OPTION_SUBTREE[category]["actions"].keys()
45+
for action in OPTION_SUBTREE[category].get("actions", {}).keys()
4646
if not action.startswith("_")
4747
]
4848
ACTIONS_STR = "{}".format(" ".join(ACTIONS))

share/actionsmap.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -2117,7 +2117,7 @@ storage:
21172117
action: store_true
21182118
info:
21192119
action_help: Get hard-drive information
2120-
api: GET /users/<name>
2120+
api: GET /storage/disk/info/<name>
21212121
arguments:
21222122
name:
21232123
help: Name of the hard drive as returned by 'list' command

src/disks.py

+16-32
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,5 @@
1-
from logging import getLogger
2-
31
import dbus
4-
from _dbus_bindings import PROPERTIES_IFACE
5-
6-
logger = getLogger("yunohost.storage")
2+
from yunohost.utils.system import binary_to_human
73

84

95
__all__ = ["info", "list"]
@@ -13,30 +9,21 @@
139
UDISK_DRIVE_IFC = "org.freedesktop.UDisks2.Drive"
1410

1511

16-
def _humaize(byte_size):
17-
suffixes = "kMGTPEZYRQ"
18-
19-
byte_size = float(byte_size)
20-
21-
if byte_size < 1024:
22-
return f"{byte_size}B"
23-
24-
for i, s in enumerate(suffixes, start=2):
25-
unit = 1024**i
26-
if byte_size <= unit:
27-
return f"{(1024 * (byte_size / unit)):.1f} {s}B"
28-
29-
30-
def _query_udisks():
12+
def _query_udisks() -> list[tuple[str, dict]]:
3113
bus = dbus.SystemBus()
3214
manager = dbus.Interface(
3315
bus.get_object("org.freedesktop.UDisks2", "/org/freedesktop/UDisks2"),
3416
"org.freedesktop.DBus.ObjectManager",
3517
)
3618

37-
for name, dev in manager.GetManagedObjects().items():
38-
if name.startswith(UDISK_DRIVE_PATH):
39-
yield name.removeprefix(UDISK_DRIVE_PATH), dev[UDISK_DRIVE_IFC]
19+
return sorted(
20+
(
21+
(name.removeprefix(UDISK_DRIVE_PATH), dev[UDISK_DRIVE_IFC])
22+
for name, dev in manager.GetManagedObjects().items()
23+
if name.startswith(UDISK_DRIVE_PATH)
24+
),
25+
key=lambda item: item[1]["SortKey"],
26+
)
4027

4128

4229
def _disk_infos(name: str, drive: dict, human_readable=False):
@@ -45,7 +32,7 @@ def _disk_infos(name: str, drive: dict, human_readable=False):
4532
"model": drive["Model"],
4633
"serial": drive["Serial"],
4734
"removable": bool(drive["MediaRemovable"]),
48-
"size": _humaize(drive["Size"]) if human_readable else drive["Size"],
35+
"size": binary_to_human(drive["Size"]) if human_readable else drive["Size"],
4936
}
5037

5138
if connection_bus := drive["ConnectionBus"]:
@@ -55,7 +42,7 @@ def _disk_infos(name: str, drive: dict, human_readable=False):
5542
result.update(
5643
{
5744
"type": "HDD",
58-
"rpm": "Unknown",
45+
"rpm": "Unknown" if human_readable else None,
5946
}
6047
)
6148
elif rotation_rate == 0:
@@ -75,15 +62,12 @@ def list(with_info=False, human_readable=False):
7562
if not with_info:
7663
return [name for name, _ in _query_udisks()]
7764

78-
result = {}
65+
result = []
7966

8067
for name, drive in _query_udisks():
81-
result[name] = _disk_infos(name, drive, human_readable)
68+
result.append(_disk_infos(name, drive, human_readable))
8269

83-
if human_readable and not result:
84-
return "No external media found"
85-
86-
return result
70+
return {"disks": result}
8771

8872

8973
def info(name, human_readable=False):
@@ -92,7 +76,7 @@ def info(name, human_readable=False):
9276
bus.get_object(
9377
"org.freedesktop.UDisks2", f"/org/freedesktop/UDisks2/drives/{name}"
9478
),
95-
PROPERTIES_IFACE,
79+
dbus.PROPERTIES_IFACE,
9680
).GetAll("org.freedesktop.UDisks2.Drive")
9781

9882
return _disk_infos(name, drive, human_readable)

src/storage.py

+7-6
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
1-
def storage_disk_list(name, human_readable=False):
2-
from yunohost.disks import info
3-
4-
return info(name, human_readable=human_readable)
5-
6-
def storage_disk_info(with_info=False, human_readable=False):
1+
def storage_disk_list(with_info=False, human_readable=False):
72
from yunohost.disks import list
83

94
return list(with_info=with_info, human_readable=human_readable)
5+
6+
7+
def storage_disk_info(name, human_readable=False):
8+
from yunohost.disks import info
9+
10+
return info(name, human_readable=human_readable)

0 commit comments

Comments
 (0)