Skip to content

Commit b64972a

Browse files
Kept edited lines only
Some submods copy entire files whereas it is better to replace only new lines for multilingualism
1 parent eb40a1a commit b64972a

File tree

6 files changed

+145
-3
lines changed

6 files changed

+145
-3
lines changed

badges/coverage.svg

Lines changed: 1 addition & 1 deletion
Loading

badges/tests.svg

Lines changed: 1 addition & 1 deletion
Loading

coverage.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
poetry shell
22
coverage run --omit "tests/*" -m pytest ./tests/ --junitxml=./report.xml -s
3+
python -m coverage xml
34
genbadge tests -i ./report.xml -s -o ./badges/tests.svg
45
genbadge coverage -i ./coverage.xml -s -o ./badges/coverage.svg
56
coverage report
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
import argparse
2+
import os
3+
4+
import sys
5+
6+
sys.path.append(os.path.abspath(os.path.dirname(os.path.dirname(__file__))))
7+
from paradox_localization_utils.read_localization_file import (
8+
file_to_keys_and_lines,
9+
get_key,
10+
BadLocalizationException,
11+
)
12+
13+
14+
def get_args():
15+
parser = argparse.ArgumentParser(description="Keep edited lines only")
16+
parser.add_argument("source_dir", type=str, help="Directory with Paradox files to filter")
17+
parser.add_argument("original_dir", type=str, help="Directory with original Paradox files")
18+
parser.add_argument("target_dir", type=str, help="Directory where to write filtered lines")
19+
parser.add_argument("-language", type=str, help="Language to filter", default="english")
20+
parser.add_argument("-target_prefix", type=str, help="Prefix added to files in target_dir", default="replace ")
21+
return parser.parse_args()
22+
23+
24+
def write_kept_lines(source_file_path: str, output_file_path: str, original_lines_by_key: dict[str, str]):
25+
"""Write in output_file_path the lines of source_file_path that are not in or are edited from original_lines_by_key
26+
27+
:param source_file_path: File to filter
28+
:param output_file_path: File where we write the filtered lines
29+
:param original_lines_by_key: Dictionary with the original lines by key
30+
"""
31+
with open(source_file_path, "r", encoding="utf8") as f:
32+
lines = f.readlines()
33+
34+
with open(output_file_path, "w", encoding="utf8") as f:
35+
language_found = False
36+
for line in lines:
37+
# Keep the language definition line
38+
if not language_found and line.startswith("l_"):
39+
f.write(line)
40+
language_found = True
41+
continue
42+
try:
43+
key = get_key(line)
44+
if key not in original_lines_by_key or line != original_lines_by_key[key]:
45+
# It is a new or edited line, we keep it
46+
f.write(line)
47+
except BadLocalizationException:
48+
pass
49+
50+
51+
def keep_only_edited_lines(
52+
source_dir: str,
53+
original_dir: str,
54+
target_dir: str,
55+
language: str,
56+
target_prefix: str
57+
):
58+
# Store original file paths
59+
original_file_paths_by_file_name: dict[str, str] = dict()
60+
for root, _, files in os.walk(original_dir):
61+
for file in files:
62+
if file.endswith(language + ".yml"):
63+
original_file_paths_by_file_name[file] = os.path.join(root, file)
64+
65+
# Analyze each file in source_dir to kepts only edited lines
66+
for root, _, files in os.walk(source_dir):
67+
for file in files:
68+
if file.endswith(language + ".yml"):
69+
print(f"Processing {file}...")
70+
# TODO Copy file when file is not in original_file_paths_by_file_name
71+
original_lines_by_key, _ = file_to_keys_and_lines(original_file_paths_by_file_name[file])
72+
write_kept_lines(
73+
os.path.join(root, file),
74+
os.path.join(target_dir, target_prefix + file),
75+
original_lines_by_key,
76+
)
77+
78+
79+
if __name__ == "__main__":
80+
args = get_args()
81+
keep_only_edited_lines(
82+
args.source_dir,
83+
args.original_dir,
84+
args.target_dir,
85+
args.language,
86+
args.target_prefix,
87+
)

paradox_localization_utils/read_localization_file.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ def get_key(line):
8686
:return: (localization key, value corresponding to the localization key, version of the value)
8787
"""
8888
i = 0
89-
while i < len(line) and (line[i] == " " or line[i] == "\t"):
89+
while i < len(line) and (line[i] == " " or line[i] == "\t" or line[i] == "\ufeff"):
9090
i += 1
9191
if i < len(line) and line[i] == "#":
9292
raise BadLocalizationException("Comment line")
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
from pathlib import Path
2+
from paradox_localization_utils.keep_edited_lines_only import write_kept_lines
3+
4+
class TestWriteKeptLines:
5+
def test_almost_empty_original(self, tmp_path: Path):
6+
input_source_file_path = tmp_path / "toto_l_english.yml"
7+
input_source_file_path.write_text("l_english:")
8+
input_original_lines_by_key = {
9+
"l_english": "l_english:"
10+
}
11+
12+
output_file_path = tmp_path / "replace toto_l_english.yml"
13+
write_kept_lines(input_source_file_path, output_file_path, input_original_lines_by_key)
14+
15+
assert output_file_path.read_text() == "l_english:"
16+
17+
def test_new_line(self, tmp_path: Path):
18+
input_source_file_path = tmp_path / "toto_l_english.yml"
19+
input_source_file_path.write_text("l_english:\nkey: value")
20+
input_original_lines_by_key = {
21+
"l_english": "l_english:"
22+
}
23+
24+
output_file_path = tmp_path / "replace toto_l_english.yml"
25+
write_kept_lines(input_source_file_path, output_file_path, input_original_lines_by_key)
26+
27+
assert output_file_path.read_text() == "l_english:\nkey: value"
28+
29+
def test_edited_line(self, tmp_path: Path):
30+
input_source_file_path = tmp_path / "toto_l_english.yml"
31+
input_source_file_path.write_text("l_english:\nkey: value2")
32+
input_original_lines_by_key = {
33+
"l_english": "l_english:",
34+
"key": "key: value1"
35+
}
36+
37+
output_file_path = tmp_path / "replace toto_l_english.yml"
38+
write_kept_lines(input_source_file_path, output_file_path, input_original_lines_by_key)
39+
40+
assert output_file_path.read_text() == "l_english:\nkey: value2"
41+
42+
def test_unedited_line(self, tmp_path: Path):
43+
input_source_file_path = tmp_path / "toto_l_english.yml"
44+
input_source_file_path.write_text("l_english:\nkey: value")
45+
input_original_lines_by_key = {
46+
"l_english": "l_english:",
47+
"key": "key: value"
48+
}
49+
50+
output_file_path = tmp_path / "replace toto_l_english.yml"
51+
write_kept_lines(input_source_file_path, output_file_path, input_original_lines_by_key)
52+
53+
assert output_file_path.read_text() == "l_english:\n"
54+

0 commit comments

Comments
 (0)