-
-
Notifications
You must be signed in to change notification settings - Fork 71
/
compile_translations.py
82 lines (69 loc) · 2.49 KB
/
compile_translations.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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
"""Compile language files from ./locale"""
import logging
import os
import subprocess
from pathlib import Path
# TODO(Martin): import this class from tauon.py instead
class CustomLoggingFormatter(logging.Formatter):
"""Nicely format logging.loglevel logs"""
grey = "\x1b[38;20m"
grey_bold = "\x1b[38;1m"
yellow = "\x1b[33;20m"
yellow_bold = "\x1b[33;1m"
red = "\x1b[31;20m"
bold_red = "\x1b[31;1m"
reset = "\x1b[0m"
format = "%(asctime)s [%(levelname)s] [%(module)s] %(message)s"
format_verbose = "%(asctime)s [%(levelname)s] [%(module)s] %(message)s (%(filename)s:%(lineno)d)"
FORMATS = {
logging.DEBUG: grey_bold + format_verbose + reset,
logging.INFO: yellow + format + reset,
logging.WARNING: yellow_bold + format + reset,
logging.ERROR: red + format + reset,
logging.CRITICAL: bold_red + format_verbose + reset,
}
def format(self, record: dict) -> str:
log_fmt = self.FORMATS.get(record.levelno)
# Remove the miliseconds(%f) from the default string
date_fmt = "%Y-%m-%d %H:%M:%S"
formatter = logging.Formatter(log_fmt, date_fmt)
# Center align + min length things to prevent logs jumping around when switching between different values
record.levelname = f"{record.levelname:^7}"
record.module = f"{record.module:^10}"
return formatter.format(record)
# DEBUG+ to file and std_err
logging.basicConfig(
level=logging.DEBUG,
handlers=[
logging.StreamHandler(),
# logging.FileHandler('/tmp/tauon.log'),
],
)
# INFO+ to std_err
logging.getLogger().handlers[0].setLevel(logging.INFO)
logging.getLogger().handlers[0].setFormatter(CustomLoggingFormatter())
def main() -> None:
compile_failure = False
locale_folder = Path("locale").absolute()
languages = locale_folder.iterdir()
for lang_file in languages:
if lang_file.name == "messages.pot":
continue
po_path = locale_folder / lang_file / "LC_MESSAGES" / "tauon.po"
mo_path = locale_folder / lang_file / "LC_MESSAGES" / "tauon.mo"
if po_path.exists():
try:
subprocess.run(["msgfmt", "-o", mo_path, po_path], check=True)
except Exception:
# Don't log the exception to make the build log clear
logging.error(f"Failed to compile translations for {lang_file}")
compile_failure = True
else:
logging.info(f"Compiled: {lang_file}")
else:
logging.critical(f"Missing po file: {po_path}")
if compile_failure:
raise Exception("Compiling had errors!")
logging.info("Done")
if __name__ == "__main__":
main()