Skip to content

Commit 4f1aed7

Browse files
Compare value instead of line to keep them
1 parent 26e9243 commit 4f1aed7

File tree

2 files changed

+47
-27
lines changed

2 files changed

+47
-27
lines changed

paradox_localization_utils/keep_edited_lines_only.py

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@
55

66
sys.path.append(os.path.abspath(os.path.dirname(os.path.dirname(__file__))))
77
from paradox_localization_utils.read_localization_file import (
8-
file_to_keys_and_lines,
9-
get_key,
8+
file_to_keys_and_values,
9+
get_key_value_and_version,
1010
BadLocalizationException,
1111
)
1212

@@ -21,12 +21,12 @@ def get_args():
2121
return parser.parse_args()
2222

2323

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
24+
def write_kept_lines(source_file_path: str, output_file_path: str, original_values_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_values_by_key
2626
2727
:param source_file_path: File to filter
2828
:param output_file_path: File where we write the filtered lines
29-
:param original_lines_by_key: Dictionary with the original lines by key
29+
:param original_values_by_key: Dictionary with the original values by key
3030
"""
3131
with open(source_file_path, "r", encoding="utf8") as f:
3232
lines = f.readlines()
@@ -35,13 +35,13 @@ def write_kept_lines(source_file_path: str, output_file_path: str, original_line
3535
language_found = False
3636
for line in lines:
3737
# Keep the language definition line
38-
if not language_found and line.startswith("l_"):
38+
if not language_found and line.replace("\ufeff", "").startswith("l_"):
3939
f.write(line)
4040
language_found = True
4141
continue
4242
try:
43-
key = get_key(line)
44-
if key not in original_lines_by_key or line != original_lines_by_key[key]:
43+
key, value, _ = get_key_value_and_version(line)
44+
if key not in original_values_by_key or value != original_values_by_key[key]:
4545
# It is a new or edited line, we keep it
4646
f.write(line)
4747
except BadLocalizationException:
@@ -62,11 +62,11 @@ def keep_only_edited_lines(source_dir: str, original_dir: str, target_dir: str,
6262
if file.endswith(language + ".yml"):
6363
print(f"Processing {file}...")
6464
# TODO Copy file when file is not in original_file_paths_by_file_name
65-
original_lines_by_key, _ = file_to_keys_and_lines(original_file_paths_by_file_name[file])
65+
original_values_versions_by_key, _ = file_to_keys_and_values(original_file_paths_by_file_name[file])
6666
write_kept_lines(
6767
os.path.join(root, file),
6868
os.path.join(target_dir, target_prefix + file),
69-
original_lines_by_key,
69+
{key: original_values_versions_by_key[key]["value"] for key in original_values_versions_by_key},
7070
)
7171

7272

Lines changed: 37 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,64 @@
11
from pathlib import Path
2+
3+
import pytest
24
from paradox_localization_utils.keep_edited_lines_only import write_kept_lines
35

46

57
class TestWriteKeptLines:
6-
def test_almost_empty_original(self, tmp_path: Path):
8+
# TODO Test empty file
9+
10+
@pytest.mark.parametrize(
11+
"input_original_values_by_key, input_text_content",
12+
[
13+
({"l_english": ""}, "l_english:"),
14+
({"\ufeffl_english": ""}, "l_english:"),
15+
({"l_english": ""}, "\ufeffl_english:"),
16+
({"\ufeffl_english": ""}, "\ufeffl_english:"),
17+
],
18+
ids=[
19+
"Original no BOM, new text no BOM",
20+
"Original with BOM, new text no BOM",
21+
"Original no BOM, new text with BOM",
22+
"Original with BOM, new text with BOM",
23+
],
24+
)
25+
def test_almost_empty_original(
26+
self, tmp_path: Path, input_original_values_by_key: dict[str, str], input_text_content: str
27+
):
728
input_source_file_path = tmp_path / "toto_l_english.yml"
8-
input_source_file_path.write_text("l_english:")
9-
input_original_lines_by_key = {"l_english": "l_english:"}
29+
input_source_file_path.write_text(input_text_content, encoding="utf-8")
1030

1131
output_file_path = tmp_path / "replace toto_l_english.yml"
12-
write_kept_lines(input_source_file_path, output_file_path, input_original_lines_by_key)
32+
write_kept_lines(input_source_file_path, output_file_path, input_original_values_by_key)
1333

14-
assert output_file_path.read_text() == "l_english:"
34+
assert output_file_path.read_text(encoding="utf-8") == input_text_content
1535

1636
def test_new_line(self, tmp_path: Path):
1737
input_source_file_path = tmp_path / "toto_l_english.yml"
18-
input_source_file_path.write_text("l_english:\nkey: value")
19-
input_original_lines_by_key = {"l_english": "l_english:"}
38+
input_source_file_path.write_text('l_english:\nkey: "value"', encoding="utf-8")
39+
input_original_values_by_key = {"l_english": ""}
2040

2141
output_file_path = tmp_path / "replace toto_l_english.yml"
22-
write_kept_lines(input_source_file_path, output_file_path, input_original_lines_by_key)
42+
write_kept_lines(input_source_file_path, output_file_path, input_original_values_by_key)
2343

24-
assert output_file_path.read_text() == "l_english:\nkey: value"
44+
assert output_file_path.read_text(encoding="utf-8") == 'l_english:\nkey: "value"'
2545

2646
def test_edited_line(self, tmp_path: Path):
2747
input_source_file_path = tmp_path / "toto_l_english.yml"
28-
input_source_file_path.write_text("l_english:\nkey: value2")
29-
input_original_lines_by_key = {"l_english": "l_english:", "key": "key: value1"}
48+
input_source_file_path.write_text('l_english:\nkey: "value2"', encoding="utf-8")
49+
input_original_values_by_key = {"l_english": "l_english:", "key": "value1"}
3050

3151
output_file_path = tmp_path / "replace toto_l_english.yml"
32-
write_kept_lines(input_source_file_path, output_file_path, input_original_lines_by_key)
52+
write_kept_lines(input_source_file_path, output_file_path, input_original_values_by_key)
3353

34-
assert output_file_path.read_text() == "l_english:\nkey: value2"
54+
assert output_file_path.read_text(encoding="utf-8") == 'l_english:\nkey: "value2"'
3555

3656
def test_unedited_line(self, tmp_path: Path):
3757
input_source_file_path = tmp_path / "toto_l_english.yml"
38-
input_source_file_path.write_text("l_english:\nkey: value")
39-
input_original_lines_by_key = {"l_english": "l_english:", "key": "key: value"}
58+
input_source_file_path.write_text('l_english:\nkey: "value"', encoding="utf-8")
59+
input_original_values_by_key = {"l_english": "l_english:", "key": "value"}
4060

4161
output_file_path = tmp_path / "replace toto_l_english.yml"
42-
write_kept_lines(input_source_file_path, output_file_path, input_original_lines_by_key)
62+
write_kept_lines(input_source_file_path, output_file_path, input_original_values_by_key)
4363

44-
assert output_file_path.read_text() == "l_english:\n"
64+
assert output_file_path.read_text(encoding="utf-8") == "l_english:\n"

0 commit comments

Comments
 (0)