Skip to content

Commit 9fcb4ca

Browse files
committed
Speed up dir2msgstr
1 parent 6dcf7c1 commit 9fcb4ca

File tree

1 file changed

+18
-12
lines changed

1 file changed

+18
-12
lines changed

msg2po/dir2msgstr.py

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,7 @@ def dir2msgstr(
3131
same: bool = False,
3232
indexed_po: Optional[IndexedPO] = None,
3333
):
34-
"""Loads translated strings from files in src_dir into po (mutating it),
35-
then returns a deduplicated copy via po_make_unique."""
34+
"""Loads translated strings from files in src_dir into po, mutating it in place."""
3635
logger.debug(f"overwrite is {overwrite}")
3736

3837
skip_files = CONFIG.skip_files
@@ -41,24 +40,29 @@ def dir2msgstr(
4140
indexed_po = IndexedPO.from_po(po)
4241

4342
abs_src = os.path.abspath(src_dir)
43+
female_suffix = CONFIG.female_dir_suffix
4444
for dir_name, _subdir_list, file_list in os.walk(abs_src, topdown=False, followlinks=True):
45+
if dir_name.endswith(female_suffix):
46+
continue
47+
# Compute relative dir prefix once per directory instead of per file
48+
rel_dir = os.path.relpath(dir_name, abs_src)
4549
for file_name in file_list:
46-
abs_path = os.path.join(dir_name, file_name)
47-
rel_name = os.path.relpath(abs_path, abs_src)
48-
if os.sep != "/":
49-
rel_name = rel_name.replace(os.sep, "/")
5050
fext = get_ext(file_name)
5151
if fext != extension:
5252
continue
53-
if dir_name.endswith(CONFIG.female_dir_suffix):
54-
logger.debug(f"{rel_name} is a file with female strings, skipping")
55-
continue
53+
if rel_dir == ".":
54+
rel_name = file_name
55+
else:
56+
rel_name = os.path.join(rel_dir, file_name)
57+
if os.sep != "/":
58+
rel_name = rel_name.replace(os.sep, "/")
5659

5760
# Skip files as configured
5861
if rel_name in skip_files:
5962
logger.debug(f"{rel_name} is in skip_files. Skipping!")
6063
continue
6164

65+
abs_path = os.path.join(dir_name, file_name)
6266
enc = get_enc(po_path, file_name)
6367
logger.info(f"processing {rel_name} with encoding {enc}")
6468
file2msgstr(
@@ -70,7 +74,7 @@ def dir2msgstr(
7074
same=same,
7175
indexed_po=indexed_po,
7276
)
73-
return po_make_unique(po)
77+
return po
7478

7579

7680
@cli_entry
@@ -114,14 +118,15 @@ def main():
114118
output_file = args.output_file
115119
po = pofile(output_file)
116120
snapshot = po_content_snapshot(po)
117-
po = dir2msgstr(
121+
dir2msgstr(
118122
src_dir=args.src_dir,
119123
po=po,
120124
po_path=output_file,
121125
overwrite=args.overwrite,
122126
extension=args.file_ext,
123127
same=args.same,
124128
)
129+
po = po_make_unique(po)
125130
if po_content_snapshot(po) != snapshot:
126131
po.save(output_file, newline=CONFIG.newline_po)
127132
logger.info(f"Processed directory {args.src_dir}, the result is in {output_file}")
@@ -140,7 +145,7 @@ def main():
140145
snapshot = po_content_snapshot(po)
141146
ipo = IndexedPO.from_po(po)
142147
for ve in VALID_EXTENSIONS:
143-
po = dir2msgstr(
148+
dir2msgstr(
144149
src_dir=lang_dir,
145150
po=po,
146151
po_path=pf,
@@ -150,6 +155,7 @@ def main():
150155
indexed_po=ipo,
151156
)
152157
logger.info(f"Processed {ve} files in directory {slug}, the result is in {rel_pf}")
158+
po = po_make_unique(po)
153159
if po_content_snapshot(po) != snapshot:
154160
po.save(pf, newline=CONFIG.newline_po)
155161

0 commit comments

Comments
 (0)