-
-
Notifications
You must be signed in to change notification settings - Fork 39
/
Copy pathstats.py
executable file
·55 lines (43 loc) · 1.61 KB
/
stats.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
#!/usr/bin/env python
"""
Obtain translation stats from the PO files directory and
store it with JSON format into 'stats.json'.
"""
import json
import os
import logging
from datetime import datetime, timezone
from pathlib import Path
from potodo.potodo import scan_path
logging.basicConfig(level=logging.INFO)
def main() -> None:
"""Main function to generate translation stats."""
language = os.environ.get("PYDOC_LANGUAGE")
if not language:
raise ValueError("Environment variable PYDOC_LANGUAGE is not set.")
pofiles_path = Path(f"cpython/Doc/locales/{language}/LC_MESSAGES")
if not pofiles_path.exists():
raise FileNotFoundError(f"Path does not exist: {pofiles_path}")
# Check for PO files inside the pofiles_path
if not list(pofiles_path.rglob("*.po")):
raise FileNotFoundError(f"No PO files found in {pofiles_path}")
stats = scan_path(pofiles_path, no_cache=True, hide_reserved=False, api_url="")
stats_data = {
"completion": str(round(stats.completion, 2)) + "%",
"translated": stats.translated,
"entries": stats.entries,
"updated_at": datetime.now(timezone.utc).isoformat(timespec="seconds") + "Z",
}
stats_json = pofiles_path / "stats.json"
try:
with stats_json.open("w") as output_file:
json.dump(stats_data, output_file)
logging.info(f"Content written to {stats_json}")
except IOError as e:
logging.error(f"Failed to write to {stats_json}: {e}")
raise
if __name__ == "__main__":
try:
main()
except Exception as e:
logging.error(f"An error occurred: {e}")