-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
134 lines (110 loc) · 4.81 KB
/
main.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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
"""
#
# trans-tool
# The translation files checker and syncing tool.
#
# Copyright ©2021-2024 Marcin Orlowski <MarcinOrlowski.com>
# https://github.com/MarcinOrlowski/trans-tool/
#
"""
import copy
import sys
from pathlib import Path
from transtool.config.builder import ConfigBuilder
from transtool.config.config import Config
from transtool.prop.file import PropFile
from .const import Const
from simplelog.log import Log
from .utils import Utils
class TransTool(object):
@staticmethod
def start() -> int:
# Cannot rely on argparse here as we have required arguments there.
if '--version' in sys.argv:
Log.banner(Const.APP_DESCRIPTION, top=False, bottom=False)
return 0
# Config with built-in defaults
config = Config()
# Configure Log with defaults (i.e. colors etc)
Log.configure(config)
# Parse args and update the config if needed
ConfigBuilder.build(config)
# Reconfigure once we got user settings handled.
Log.configure(config)
if '--config-dump' in sys.argv:
config.dump()
return 0
if not config.files:
Log.e('No base file(s) specified.')
return 200 # noqa: WPS432
errors = 0
for file_str in config.files:
reference_path = Path(file_str)
tmp = Path(reference_path).name.split('.')
if len(tmp) != 2:
Log.e('Base filename format invalid. Must be "prefix.suffix".')
Utils.abort()
name_prefix = tmp[0]
name_suffix = tmp[1]
# Main push
Log.push(f'Base: {reference_path}')
reference = PropFile(config)
try:
reference.load(reference_path)
except (FileNotFoundError, SyntaxError) as load_base_ex:
Log.e(str(load_base_ex))
Utils.abort()
# Validate base file.
checks_executed = 0
for _, checker_info in config.checks.items():
# Almost any check validates translation against reference file, so we cannot use all checks here,
# but there are some that process single file independently so they in fact do not need any reference
# file. For them we pass our base file as translation which will do the trick.
checker = checker_info.callable(checker_info.config)
if checker.is_single_file_check:
# Each validator gets copy of the files, to prevent any potential destructive operation.
reference_copy = copy.copy(reference)
reference.report.add(checker.check(reference_copy))
checks_executed += 1
if reference.report.not_empty():
# There's something to fix, but not necessary critical.
reference.report.dump()
else:
Log.v(f'Checks passed: {checks_executed}.')
# No reference files errors. Warnings are just fine, though.
for lang in config.languages:
translation_path = Path(reference_path.parent / f'{name_prefix}_{lang}.{name_suffix}')
translation = PropFile(config)
trans_level_label = f'{lang.upper()}: {translation_path}'
if lang in config.languages_skip:
Log.v(f'{trans_level_label}: SKIPPED')
continue
Log.push(trans_level_label, deferred=True)
try:
translation.load(translation_path, lang)
# Validate loaded translation file and report any issue detected.
if not translation.validate(reference):
translation.report.dump()
errors += translation.report.errors
if config.write:
translation.update(reference)
translation.save()
except SyntaxError as load_trans_ex:
# We need to stop when loading failed due to syntax error
Log.e(str(load_trans_ex))
return Const.RC.TRANSLATION_SYNTAX_ERROR
except FileNotFoundError:
# Missing translation file is not a big deal.
if config.write:
translation.update(reference)
Log.push('Creating new translation file')
translation.save(translation_path)
Log.pop()
else:
Log.e(f'File not found: {translation_path}')
if Log.pop():
Log.i(f'%ok%{trans_level_label}: OK')
# Close main push.
Log.pop()
# Done.
return 100 if errors else 0