Skip to content

Commit ed6e849

Browse files
committed
Remove stale sfall female files
1 parent ff8f619 commit ed6e849

2 files changed

Lines changed: 50 additions & 15 deletions

File tree

msg2po/conversion.py

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

66
from __future__ import annotations
77

8-
import os
9-
from collections import OrderedDict
10-
from dataclasses import dataclass
11-
from typing import TYPE_CHECKING
8+
import os
9+
from collections import OrderedDict
10+
from dataclasses import dataclass
11+
from pathlib import Path
12+
from typing import TYPE_CHECKING
1213

1314
if TYPE_CHECKING:
1415
from msg2po.indexed_po import IndexedPO
@@ -184,14 +185,18 @@ def po2file(
184185
# what's out path?
185186
female_file = get_female_filepath(output_file, dst_dir, same)
186187

187-
# If need to create the file
188-
if same: # if female translation is the same?
189-
if female_file is None: # don't need to copy, automatic fallback
190-
logger.debug(f"Female strings are same, not copying - sfall will fallback to male {occurrence_path}")
191-
return
192-
else:
193-
logger.debug(f"Female strings are same, copying to {female_file}")
194-
copycreate(output_file, female_file)
188+
# If need to create the file
189+
if same: # if female translation is the same?
190+
if female_file is None: # don't need to copy, automatic fallback
191+
stale_female = get_stale_female_filepath(output_file)
192+
if stale_female is not None and Path(stale_female).exists():
193+
logger.debug(f"Female strings are same, deleting stale female file {stale_female}")
194+
Path(stale_female).unlink()
195+
logger.debug(f"Female strings are same, not copying - sfall will fallback to male {occurrence_path}")
196+
return
197+
else:
198+
logger.debug(f"Female strings are same, copying to {female_file}")
199+
copycreate(output_file, female_file)
195200
else: # if it's different, extract separately
196201
if female_file is None:
197202
logger.warning(
@@ -205,7 +210,7 @@ def po2file(
205210
file2.writelines(lines_female)
206211

207212

208-
def get_female_filepath(path: str, dst_dir: str, same: bool = True) -> str | None:
213+
def get_female_filepath(path: str, dst_dir: str, same: bool = True) -> str | None:
209214
"""Determine female file path based on format and sfall conventions."""
210215
# default: just add _female suffix
211216
female_path: str | None = path.replace(dst_dir + os.sep, dst_dir + CONFIG.female_dir_suffix + os.sep)
@@ -214,8 +219,17 @@ def get_female_filepath(path: str, dst_dir: str, same: bool = True) -> str | Non
214219
if "cuts" in path.split(os.sep): # cuts dont' fallback
215220
female_path = path.replace(os.sep + "cuts" + os.sep, os.sep + "cuts_female" + os.sep)
216221
if "dialog" in path.split(os.sep) and not same: # dialog, female translation differs
217-
female_path = path.replace(os.sep + "dialog" + os.sep, os.sep + "dialog_female" + os.sep)
218-
return female_path
222+
female_path = path.replace(os.sep + "dialog" + os.sep, os.sep + "dialog_female" + os.sep)
223+
return female_path
224+
225+
226+
def get_stale_female_filepath(path: str) -> str | None:
227+
"""Return the stale dialog_female path to remove when sfall falls back to male."""
228+
if CONFIG.extract_format != "sfall":
229+
return None
230+
if "dialog" not in path.split(os.sep):
231+
return None
232+
return path.replace(os.sep + "dialog" + os.sep, os.sep + "dialog_female" + os.sep)
219233

220234

221235
def get_line_format(e, ext: str):

tests/test_conversion.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
# Tests for file2po, po2file, and roundtrip conversions.
22

3+
from dataclasses import replace
4+
35
import polib
46
import pytest
57

68
from msg2po.conversion import file2msgstr, file2po, po2file
9+
from msg2po.config import CONFIG
710
from msg2po.po_utils import (
811
CONTEXT_FEMALE,
912
EMPTY_COMMENT,
@@ -147,6 +150,24 @@ def test_translated_uses_msgstr(self, msg_file, tmp_path):
147150
assert "Bonjour le monde" in content
148151
assert "{100}{}{Bonjour le monde}" in content
149152

153+
def test_removes_stale_dialog_female_file_when_falling_back_to_male(self, tmp_path, monkeypatch):
154+
po = polib.POFile()
155+
po.append(polib.POEntry(msgid="Hello", msgstr="Bonjour", occurrences=[("dialog/test.msg", "100")]))
156+
157+
config = replace(CONFIG, extract_format="sfall")
158+
monkeypatch.setattr("msg2po.conversion.CONFIG", config)
159+
160+
dst_dir = tmp_path / "french"
161+
output = dst_dir / "dialog" / "test.msg"
162+
stale_female = dst_dir / "dialog_female" / "test.msg"
163+
stale_female.parent.mkdir(parents=True)
164+
stale_female.write_text("{100}{}{STALE}\n", encoding="utf-8")
165+
166+
po2file(po, str(output), "utf-8", "dialog/test.msg", dst_dir=str(dst_dir))
167+
168+
assert output.exists()
169+
assert not stale_female.exists()
170+
150171

151172
class TestFile2Msgstr:
152173
def test_loads_translations(self, msg_file, msg_translated_file):

0 commit comments

Comments
 (0)