|
| 1 | +""" |
| 2 | +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. |
| 12 | +""" |
| 13 | + |
| 14 | +from pathlib import Path |
| 15 | +import sys |
| 16 | + |
| 17 | +import polib |
| 18 | + |
| 19 | + |
| 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_entries = [entry for entry in po_file if not entry.obsolete] |
| 26 | + val_max = 0 |
| 27 | + |
| 28 | + for entry in po_entries: |
| 29 | + source_index = int(entry.occurrences[0][1]) |
| 30 | + |
| 31 | + if source_index <= val_max: |
| 32 | + yield entry |
| 33 | + |
| 34 | + val_max = max(val_max, source_index) |
| 35 | + |
| 36 | + |
| 37 | +def complete_index(po_files=None): |
| 38 | + """ |
| 39 | + Identifies general index entries based on source order and completes them |
| 40 | + with original content. |
| 41 | +
|
| 42 | + args: |
| 43 | + po_files: List of .po files to process. If not provided, it processes |
| 44 | + all .po files in the current directory and immediate subdirectories. |
| 45 | + """ |
| 46 | + |
| 47 | + # Read .po files |
| 48 | + if not po_files: |
| 49 | + po_files = Path(".").glob("**/*.po") |
| 50 | + |
| 51 | + for po_file_path in po_files: |
| 52 | + |
| 53 | + try: |
| 54 | + po_file = polib.pofile(po_file_path, wrapwidth=79) |
| 55 | + |
| 56 | + # Ask to complete entries out of order with original text |
| 57 | + needs_save = False |
| 58 | + for entry in out_of_order_entries(po_file): |
| 59 | + user_input = input(f"\n{entry}\nIs this a index entry? (y/N):") |
| 60 | + if user_input.lower() == "y": |
| 61 | + entry.msgstr = entry.msgid |
| 62 | + needs_save = True |
| 63 | + if needs_save: |
| 64 | + po_file.save() |
| 65 | + |
| 66 | + except KeyboardInterrupt: |
| 67 | + break |
| 68 | + |
| 69 | + except Exception as e: |
| 70 | + print(f"Error! file {po_file_path}: {e}\n") |
| 71 | + |
| 72 | + else: |
| 73 | + print(f"\n---\n{po_file_path} processed!\n---") |
| 74 | + |
| 75 | + |
| 76 | +if __name__ == "__main__": |
| 77 | + po_files = sys.argv[1:] |
| 78 | + complete_index(po_files) |
0 commit comments