From d8906b889d2a964c704c3ad024e186e172385aeb Mon Sep 17 00:00:00 2001 From: "Carlos A. Crespo" Date: Mon, 23 Oct 2023 08:47:29 -0300 Subject: [PATCH 1/5] script to search for and complete probable 'index entries' --- scripts/complete_index.py | 68 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 68 insertions(+) create mode 100644 scripts/complete_index.py diff --git a/scripts/complete_index.py b/scripts/complete_index.py new file mode 100644 index 0000000000..0859edfeb1 --- /dev/null +++ b/scripts/complete_index.py @@ -0,0 +1,68 @@ +""" +Script to identify and complete general index entries with original content. +""" + +import glob +import sys + +import polib + + +def complete_index(po_files=None): + """ + Identifies general index entries based on source order and completes them + with original content. + + args: + po_files: List of .po files to process. If not provided, it processes + all .po files in the current directory and immediate subdirectories. + + returns: + str: Files and entries modified. + """ + + # Read .po files + if not po_files: + po_files = [f for f in glob.glob("**/*.po", recursive=True)] + + modified_texts = [] + po_entries = [] + for file in po_files: + try: + po_file = polib.pofile(file) + po_entries = [entry for entry in po_file if not entry.obsolete] + val_max = 0 + marks = [] + + # Compare source index with file order and create marks + for i, entry in enumerate(po_entries): + + source_index = int(entry.occurrences[0][1]) + if source_index <= val_max: + marks.append(i) + val_max = val_max if source_index <= val_max else source_index + + # We only keep the entries that are marked + po_entries = [j for i, j in enumerate(po_entries) if i in marks] + + # Complete translation with original text + for entry in po_entries: + user_input = input(f"\n{entry}\nIs this a index entry? (y/N):") + if user_input.lower() == "y": + entry.msgstr = entry.msgid + modified_texts.append(f"Adjusted: {file}\n{entry}") + po_file.save() + + except Exception as e: + print(f"{len(modified_texts)} text(s) adjusted.", + f"Error! file {file}: {e}\n") + return 1 + + print(f"\n{len(modified_texts)} text(s) adjusted", + f"{len(po_files)} file(s) processed.") + + +if __name__ == "__main__": + po_files = sys.argv[1:] + results = complete_index(po_files) + sys.exit(0 if results != 1 else -1) From a89bcfdaae29140f39545912547a70e13ab4cee5 Mon Sep 17 00:00:00 2001 From: "Carlos A. Crespo" Date: Tue, 14 Nov 2023 22:00:13 -0300 Subject: [PATCH 2/5] out_of_order() as a new function --- scripts/complete_index.py | 79 +++++++++++++++++++++------------------ 1 file changed, 42 insertions(+), 37 deletions(-) diff --git a/scripts/complete_index.py b/scripts/complete_index.py index 0859edfeb1..7f4a4a7d53 100644 --- a/scripts/complete_index.py +++ b/scripts/complete_index.py @@ -1,13 +1,41 @@ """ Script to identify and complete general index entries with original content. + +This script processes .po files, identifies out-of-order entries based on the +source order, and completes them with original content. + +Usage: + python script_name.py [list of .po files] + +If no list of .po files is provided, the script processes all .po files in the +current directory and its immediate subdirectories. """ -import glob +from pathlib import Path import sys import polib +def out_of_order_entries(po_file): + """ + Compare the order of source lines against the order in which they appear in + the file, and return a generator with entries that are out of order. + """ + po_file = polib.pofile(po_file) + po_entries = [entry for entry in po_file if not entry.obsolete] + val_max = 0 + + for entry in po_entries: + source_index = int(entry.occurrences[0][1]) + + if source_index <= val_max: + yield(entry) + po_file.save() + + val_max = max(val_max, source_index) + + def complete_index(po_files=None): """ Identifies general index entries based on source order and completes them @@ -16,53 +44,30 @@ def complete_index(po_files=None): args: po_files: List of .po files to process. If not provided, it processes all .po files in the current directory and immediate subdirectories. - - returns: - str: Files and entries modified. - """ + """ # Read .po files if not po_files: - po_files = [f for f in glob.glob("**/*.po", recursive=True)] + po_files = Path(".").glob("**/*.po") - modified_texts = [] - po_entries = [] - for file in po_files: + for po_file in po_files: try: - po_file = polib.pofile(file) - po_entries = [entry for entry in po_file if not entry.obsolete] - val_max = 0 - marks = [] - - # Compare source index with file order and create marks - for i, entry in enumerate(po_entries): - - source_index = int(entry.occurrences[0][1]) - if source_index <= val_max: - marks.append(i) - val_max = val_max if source_index <= val_max else source_index - - # We only keep the entries that are marked - po_entries = [j for i, j in enumerate(po_entries) if i in marks] - - # Complete translation with original text - for entry in po_entries: + # Ask to complete entries out of order with original text + for entry in out_of_order_entries(po_file): user_input = input(f"\n{entry}\nIs this a index entry? (y/N):") if user_input.lower() == "y": - entry.msgstr = entry.msgid - modified_texts.append(f"Adjusted: {file}\n{entry}") - po_file.save() + entry.msgstr = entry.msgid + + except KeyboardInterrupt: + break except Exception as e: - print(f"{len(modified_texts)} text(s) adjusted.", - f"Error! file {file}: {e}\n") - return 1 + print(f"Error! file {po_file}: {e}\n") - print(f"\n{len(modified_texts)} text(s) adjusted", - f"{len(po_files)} file(s) processed.") + else: + print(f"{po_file} processed!\n") if __name__ == "__main__": po_files = sys.argv[1:] - results = complete_index(po_files) - sys.exit(0 if results != 1 else -1) + complete_index(po_files) From f3f4dc64b635ba0d3b6fcafea13bcd7e7e986f34 Mon Sep 17 00:00:00 2001 From: "Carlos A. Crespo" Date: Mon, 27 Nov 2023 18:17:53 -0300 Subject: [PATCH 3/5] Update scripts/complete_index.py Apply suggestions from @rtobar --- scripts/complete_index.py | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/scripts/complete_index.py b/scripts/complete_index.py index 7f4a4a7d53..fc02153b4b 100644 --- a/scripts/complete_index.py +++ b/scripts/complete_index.py @@ -22,7 +22,6 @@ def out_of_order_entries(po_file): Compare the order of source lines against the order in which they appear in the file, and return a generator with entries that are out of order. """ - po_file = polib.pofile(po_file) po_entries = [entry for entry in po_file if not entry.obsolete] val_max = 0 @@ -30,8 +29,7 @@ def out_of_order_entries(po_file): source_index = int(entry.occurrences[0][1]) if source_index <= val_max: - yield(entry) - po_file.save() + yield entry val_max = max(val_max, source_index) @@ -50,22 +48,26 @@ def complete_index(po_files=None): if not po_files: po_files = Path(".").glob("**/*.po") - for po_file in po_files: + for po_file_path in po_files: + try: + po_file = polib.pofile(po_file_path) + # Ask to complete entries out of order with original text for entry in out_of_order_entries(po_file): user_input = input(f"\n{entry}\nIs this a index entry? (y/N):") if user_input.lower() == "y": entry.msgstr = entry.msgid + po_file.save() # Save if an entry has been modified except KeyboardInterrupt: break except Exception as e: - print(f"Error! file {po_file}: {e}\n") + print(f"Error! file {po_file_path}: {e}\n") else: - print(f"{po_file} processed!\n") + print(f"\n---\n{po_file_path} processed!\n---") if __name__ == "__main__": From 74e34049b83eee850071d20fd7ed818b5df012d6 Mon Sep 17 00:00:00 2001 From: "Carlos A. Crespo" Date: Mon, 11 Dec 2023 11:08:59 -0300 Subject: [PATCH 4/5] Update scripts/complete_index.py Co-authored-by: rtobar --- scripts/complete_index.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/scripts/complete_index.py b/scripts/complete_index.py index fc02153b4b..d0668c2442 100644 --- a/scripts/complete_index.py +++ b/scripts/complete_index.py @@ -54,11 +54,14 @@ def complete_index(po_files=None): po_file = polib.pofile(po_file_path) # Ask to complete entries out of order with original text + needs_save = False for entry in out_of_order_entries(po_file): user_input = input(f"\n{entry}\nIs this a index entry? (y/N):") if user_input.lower() == "y": entry.msgstr = entry.msgid - po_file.save() # Save if an entry has been modified + needs_save = True + if needs_save: + po_file.save() except KeyboardInterrupt: break From 26bb8ba83b114dd77c5818b281a7bed9e2fbfaee Mon Sep 17 00:00:00 2001 From: "Carlos A. Crespo" Date: Mon, 11 Dec 2023 11:43:59 -0300 Subject: [PATCH 5/5] wrapwidth=79 --- scripts/complete_index.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/scripts/complete_index.py b/scripts/complete_index.py index d0668c2442..2f63644537 100644 --- a/scripts/complete_index.py +++ b/scripts/complete_index.py @@ -51,7 +51,7 @@ def complete_index(po_files=None): for po_file_path in po_files: try: - po_file = polib.pofile(po_file_path) + po_file = polib.pofile(po_file_path, wrapwidth=79) # Ask to complete entries out of order with original text needs_save = False