-
Notifications
You must be signed in to change notification settings - Fork 336
/
minifier.py
67 lines (52 loc) · 2.07 KB
/
minifier.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
import json
from pathlib import Path
from typing import Iterable
from colorama import Fore, Style
def human_repr(num: float) -> str:
for unit in ["B", "KB", "MB"]:
if abs(num) < 1024.0:
return f"{num:3.1f}{unit}"
num /= 1024.0
return f"{num:.1f}"
class Minimizer:
"""Minify content of all json files for all locales."""
def __init__(self, *, files: Iterable[Path]) -> None:
"""Find all files of all locales."""
self.files = files
self.before_total = 0
self.after_total = 0
def run(self) -> None:
"""Start json minimizer and exit when all json files were minimized."""
for file in self.files:
self.minify(file)
after = human_repr(self.after_total)
before = human_repr(self.before_total)
saved = human_repr(self.before_total - self.after_total)
info = (
"\nTotal: "
f"{Fore.LIGHTGREEN_EX}{before}{Style.RESET_ALL} -> {Fore.LIGHTGREEN_EX}{after}{Style.RESET_ALL}. "
f"Compressed: {Fore.LIGHTGREEN_EX}{saved}{Style.RESET_ALL}\n"
)
print(info)
def minify(self, file: Path) -> None:
size_before = file.stat().st_size
self.before_total += size_before
before = human_repr(size_before)
minimized = json.dumps(
json.loads(file.read_text()), separators=(",", ":"), ensure_ascii=False
)
file.write_text(minimized)
size_after = file.stat().st_size
self.after_total += size_after
after = human_repr(size_after)
rel_file = file.relative_to(file.parent.parent)
info = (
f"{Fore.BLUE}{str(rel_file):<30}{Style.RESET_ALL} : "
f"{Fore.LIGHTGREEN_EX}minimized{Style.RESET_ALL} : "
f"{Fore.YELLOW}{before:<7}{Style.RESET_ALL} -> {Fore.LIGHTGREEN_EX}{after:<7}{Style.RESET_ALL}"
)
print(info)
if __name__ == "__main__": # pragma: no cover
data_dir = Path(__file__).parent / "mimesis" / "data"
files = sorted(data_dir.rglob("*.json"))
Minimizer(files=files).run()