8000 out_of_order() as a new function · python/python-docs-es@a89bcfd · GitHub
[go: up one dir, main page]

Skip to content

Commit a89bcfd

Browse files
committed
out_of_order() as a new function
1 parent 1e3d6a0 commit a89bcfd

File tree

1 file changed

+42
-37
lines changed

1 file changed

+42
-37
lines changed

scripts/complete_index.py

Lines changed: 42 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,41 @@
11
"""
22
Script to identify and complete general index entries with original content.
3+
4+
This script processes .po files, identifies out-of-order entries based on the
5+
source order, and completes them with original content.
6+
7+
Usage:
8+
python script_name.py [list of .po files]
9+
10+
If no list of .po files is provided, the script processes all .po files in the
11+
current directory and its immediate subdirectories.
312
"""
413

5-
import glob
14+
from pathlib import Path
615
import sys
716

817
import polib
918

1019

20+
def out_of_order_entries(po_file):
21+
"""
22+
Compare the order of source lines against the order in which they appear in
23+
the file, and return a generator with entries that are out of order.
24+
"""
25+
po_file = polib.pofile(po_file)
26+
po_entries = [entry for entry in po_file if not entry.obsolete]
27+
val_max = 0
28+
29+
for entry in po_entries:
30+
source_index = int(entry.occurrences[0][1])
31+
32+
if source_index <= val_max:
33+
yield(entry)
34+
po_file.save()
35+
36+
val_max = max(val_max, source_index)
37+
38+
1139
def complete_index(po_files=None):
1240
"""
1341
Identifies general index entries based on source order and completes them
@@ -16,53 +44,30 @@ def complete_index(po_files=None):
1644
args:
1745
po_files: List of .po files to process. If not provided, it processes
1846
all .po files in the current directory and immediate subdirectories.
19-
20-
returns:
21-
str: Files and entries modified.
22-
"""
47+
"""
2348

2449
# Read .po files
2550
if not po_files:
26-
po_files = [f for f in glob.glob("**/*.po", recursive=True)]
51+
po_files = Path(".").glob("**/*.po")
2752

28-
modified_texts = []
29-
po_entries = []
30-
for file in po_files:
53+
for po_file in po_files:
3154
try:
32-
po_file = polib.pofile(file)
33-
po_entries = [entry for entry in po_file if not entry.obsolete]
34-
val_max = 0
35-
marks = []
36-
37-
# Compare source index with file order and create marks
38-
for i, entry in enumerate(po_entries):
39-
40-
source_index = int(entry.occurrences[0][1])
41-
if source_index <= val_max:
42-
marks.append(i)
43-
val_max = val_max if source_index <= val_max else source_index
44-
45-
# We only keep the entries that are marked
46-
po_entries = [j for i, j in enumerate(po_entries) if i in marks]
47-
48-
# Complete translation with original text
49-
for entry in po_entries:
55+
# Ask to complete entries out of order with original text
56+
for entry in out_of_order_entries(po_file):
5057
user_input = input(f"\n{entry}\nIs this a index entry? (y/N):")
5158
if user_input.lower() == "y":
52-
entry.msgstr = entry.msgid
53-
modified_texts.append(f"Adjusted: {file}\n{entry}")
54-
po_file.save()
59+
entry.msgstr = entry.msgid
60+
61+
except KeyboardInterrupt:
62+
break
5563

5664
except Exception as e:
57-
print(f"{len(modified_texts)} text(s) adjusted.",
58-
f"Error! file {file}: {e}\n")
59-
return 1
65+
print(f"Error! file {po_file}: {e}\n")
6066

61-
print(f"\n{len(modified_texts)} text(s) adjusted",
62-
f"{len(po_files)} file(s) processed.")
67+
else:
68+
print(f"{po_file} processed!\n")
6369

6470

6571
if __name__ == "__main__":
6672
po_files = sys.argv[1:]
67-
results = complete_index(po_files)
68-
sys.exit(0 if results != 1 else -1)
73+
complete_index(po_files)

0 commit comments

Comments
 (0)
0