-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathproperties_converter.py
More file actions
179 lines (154 loc) · 5.61 KB
/
Copy pathproperties_converter.py
File metadata and controls
179 lines (154 loc) · 5.61 KB
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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
"""Convert Transifex source/target .properties pairs into ParaTranz CSV."""
from __future__ import annotations
import argparse
import csv
from pathlib import Path
from sync_audit import emit_event
from sync_config import (
PARATRANZ_OUTPUT_DIR,
TRANSIFEX_SOURCE_DIR,
TRANSIFEX_TRANSLATION_DIR,
)
def _has_continuation(line: str) -> bool:
backslashes = 0
for character in reversed(line):
if character != "\\":
break
backslashes += 1
return backslashes % 2 == 1
def _logical_lines(path: Path):
pending = ""
for physical in path.read_text(encoding="utf-8-sig").splitlines():
line = physical
if pending:
line = pending + line.lstrip(" \t\f")
if _has_continuation(line):
pending = line[:-1]
continue
pending = ""
yield line
if pending:
yield pending
def _split_property(line: str) -> tuple[str, str] | None:
stripped = line.lstrip(" \t\f")
if not stripped or stripped.startswith(("#", "!")):
return None
escaped = False
separator_index: int | None = None
whitespace_separator = False
for index, character in enumerate(stripped):
if escaped:
escaped = False
continue
if character == "\\":
escaped = True
continue
if character in "=:":
separator_index = index
break
if character in " \t\f":
separator_index = index
whitespace_separator = True
break
if separator_index is None:
return stripped, ""
key = stripped[:separator_index]
cursor = separator_index
if whitespace_separator:
while cursor < len(stripped) and stripped[cursor] in " \t\f":
cursor += 1
if cursor < len(stripped) and stripped[cursor] in "=:":
cursor += 1
else:
cursor += 1
while cursor < len(stripped) and stripped[cursor] in " \t\f":
cursor += 1
return key, stripped[cursor:]
def parse_properties(path: Path) -> dict[str, str]:
properties: dict[str, str] = {}
for line_number, line in enumerate(_logical_lines(path), start=1):
item = _split_property(line)
if item is None:
continue
key, value = item
if not key:
raise ValueError(f"Empty property key in {path}:{line_number}")
if key in properties:
raise ValueError(f"Duplicate property key {key!r} in {path}:{line_number}")
properties[key] = value
return properties
def convert_file(
source_path: Path,
translation_path: Path | None,
output_path: Path,
) -> tuple[int, int, int]:
source_data = parse_properties(source_path)
translation_data = (
parse_properties(translation_path) if translation_path is not None else {}
)
extra_keys = set(translation_data) - set(source_data)
if extra_keys and translation_path is not None:
print(
f"Warning: {translation_path.name} has {len(extra_keys)} keys "
"that are absent from the current source"
)
output_path.parent.mkdir(parents=True, exist_ok=True)
temporary = output_path.with_name(f".{output_path.name}.tmp")
with temporary.open("w", newline="", encoding="utf-8-sig") as csv_file:
writer = csv.writer(csv_file)
writer.writerow(["key", "source", "target"])
for key, source_text in source_data.items():
writer.writerow([key, source_text, translation_data.get(key, "")])
temporary.replace(output_path)
return len(source_data), len(translation_data), len(extra_keys)
def parse_args() -> argparse.Namespace:
parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument(
"--source-only",
action="store_true",
help="Create CSV source columns without reading Transifex translations.",
)
return parser.parse_args()
def main() -> int:
args = parse_args()
if not TRANSIFEX_SOURCE_DIR.is_dir():
print(f"Error: missing folder {TRANSIFEX_SOURCE_DIR}")
return 1
failures = 0
source_files = sorted(TRANSIFEX_SOURCE_DIR.glob("*.properties"))
if not source_files:
print("Error: no source .properties files found")
return 1
for source_path in source_files:
translation_path = TRANSIFEX_TRANSLATION_DIR / source_path.name
if not args.source_only and not translation_path.is_file():
print(f"Warning: translation file not found for {source_path.name}; skipping")
failures += 1
continue
output_path = PARATRANZ_OUTPUT_DIR / f"{source_path.stem}.csv"
try:
source_count, translation_count, extra_count = convert_file(
source_path,
None if args.source_only else translation_path,
output_path,
)
print(f"Converted {source_path.name} -> {output_path.name}")
emit_event(
"conversion.properties_to_csv",
file=source_path.name,
source_entries=source_count,
translation_entries=translation_count,
translation_keys_absent_from_source=extra_count,
source_only=args.source_only,
)
except (OSError, ValueError, csv.Error) as exc:
failures += 1
print(f"Failed {source_path.name}: {exc}")
emit_event(
"conversion.properties_to_csv_failed",
file=source_path.name,
error=str(exc),
)
return 1 if failures else 0
if __name__ == "__main__":
raise SystemExit(main())