lsdisk 0.1.1__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,101 @@
1
+ """Serialize storage inventory for machine-readable output."""
2
+
3
+ from typing import Any
4
+
5
+ from lsdisk.analyzers.capacity import format_bytes
6
+ from lsdisk.analyzers.filesystems import classify_filesystem
7
+ from lsdisk.inventory import StorageInventory
8
+ from lsdisk.models import BlockDevice, FilesystemUsage
9
+
10
+
11
+ SUMMARY_CATEGORIES = {"persistent", "network"}
12
+
13
+
14
+ def _device_to_dict(device: BlockDevice) -> dict[str, Any]:
15
+ """Convert a device and its children to JSON-compatible data."""
16
+ return {
17
+ "name": device.name,
18
+ "path": device.path,
19
+ "type": device.device_type,
20
+ "size_bytes": device.size_bytes,
21
+ "size_human": format_bytes(device.size_bytes),
22
+ "physical": device.physical,
23
+ "parent_name": device.parent_name,
24
+ "model": device.model,
25
+ "transport": device.transport,
26
+ "rotational": device.rotational,
27
+ "filesystem_type": device.filesystem_type,
28
+ "filesystem_label": device.filesystem_label,
29
+ "filesystem_uuid": device.filesystem_uuid,
30
+ "mountpoints": device.mountpoints,
31
+ "children": [_device_to_dict(child) for child in device.children],
32
+ }
33
+
34
+
35
+ def _mount_to_dict(filesystem: FilesystemUsage) -> dict[str, Any]:
36
+ """Convert one mountpoint and its capacity statistics."""
37
+ return {
38
+ "mountpoint": filesystem.mountpoint,
39
+ "source": filesystem.source,
40
+ "filesystem_type": filesystem.filesystem_type,
41
+ "category": classify_filesystem(filesystem),
42
+ "device_id": filesystem.device_id,
43
+ "filesystem_root": filesystem.filesystem_root,
44
+ "total_bytes": filesystem.total_bytes,
45
+ "free_bytes": filesystem.free_bytes,
46
+ "available_bytes": filesystem.available_bytes,
47
+ "used_bytes": filesystem.used_bytes,
48
+ "used_percent": round(filesystem.used_percent, 2),
49
+ }
50
+
51
+
52
+ def inventory_to_dict(
53
+ inventory: StorageInventory,
54
+ *,
55
+ include_all: bool = False,
56
+ ) -> dict[str, Any]:
57
+ """Convert inventory to a stable, JSON-compatible structure."""
58
+ if include_all:
59
+ devices = inventory.devices
60
+ else:
61
+ devices = [
62
+ device
63
+ for device in inventory.devices
64
+ if device.is_physical_disk
65
+ ]
66
+
67
+ groups = []
68
+ for group in inventory.filesystem_groups:
69
+ filesystem = group.representative
70
+ category = classify_filesystem(filesystem)
71
+
72
+ if not include_all and category not in SUMMARY_CATEGORIES:
73
+ continue
74
+
75
+ groups.append(
76
+ {
77
+ "source": filesystem.source,
78
+ "filesystem_type": filesystem.filesystem_type,
79
+ "category": category,
80
+ "device_id": filesystem.device_id,
81
+ "total_bytes": group.total_bytes,
82
+ "used_bytes": group.used_bytes,
83
+ "available_bytes": group.available_bytes,
84
+ "used_percent": round(filesystem.used_percent, 2),
85
+ "mountpoints": group.mountpoints,
86
+ }
87
+ )
88
+
89
+ mounts = (
90
+ [_mount_to_dict(filesystem) for filesystem in inventory.filesystems]
91
+ if include_all
92
+ else []
93
+ )
94
+
95
+ return {
96
+ "schema_version": 1,
97
+ "mode": "all" if include_all else "simple",
98
+ "devices": [_device_to_dict(device) for device in devices],
99
+ "filesystems": groups,
100
+ "mounts": mounts,
101
+ }
lsdisk/ui/__init__.py ADDED
File without changes