Coverage for opt/mealie/lib/python3.12/site-packages/mealie/pkgs/stats/fs_stats.py: 38%
26 statements
« prev ^ index » next coverage.py v7.10.6, created at 2025-11-25 15:48 +0000
« prev ^ index » next coverage.py v7.10.6, created at 2025-11-25 15:48 +0000
1import os 1a
2from pathlib import Path 1a
4megabyte = 1_048_576 1a
5gigabyte = 1_073_741_824 1a
8def pretty_size(size: int) -> str: 1a
9 """
10 Pretty size takes in a integer value of a file size and returns the most applicable
11 file unit and the size.
12 """
13 if size < 1024:
14 return f"{size} bytes"
15 elif size < 1024**2: 15 ↛ 16line 15 didn't jump to line 16 because the condition on line 15 was never true
16 return f"{round(size / 1024, 2)} KB"
17 elif size < 1024**2 * 1024: 17 ↛ 19line 17 didn't jump to line 19 because the condition on line 17 was always true
18 return f"{round(size / 1024 / 1024, 2)} MB"
19 elif size < 1024**2 * 1024 * 1024:
20 return f"{round(size / 1024 / 1024 / 1024, 2)} GB"
21 else:
22 return f"{round(size / 1024 / 1024 / 1024 / 1024, 2)} TB"
25def get_dir_size(path: Path | str) -> int: 1a
26 """
27 Get the size of a directory
28 """
29 try:
30 total_size = os.path.getsize(path)
31 except FileNotFoundError:
32 return 0
34 for item in os.listdir(path):
35 itempath = os.path.join(path, item)
36 if os.path.isfile(itempath):
37 total_size += os.path.getsize(itempath)
38 elif os.path.isdir(itempath):
39 total_size += get_dir_size(itempath)
40 return total_size