- Notifications
You must be signed in to change notification settings - Fork396
script to search for and complete probable 'index entries'#2706
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Merged
Uh oh!
There was an error while loading.Please reload this page.
Merged
Changes fromall commits
Commits
Show all changes
7 commits Select commitHold shift + click to select a range
d8906b8
script to search for and complete probable 'index entries'
cacrespo1e3d6a0
Merge branch '3.12' into script_complete_index
cacrespoa89bcfd
out_of_order() as a new function
cacrespof3f4dc6
Update scripts/complete_index.py
cacrespo74e3404
Update scripts/complete_index.py
cacrespo26bb8ba
wrapwidth=79
cacrespo39be902
Merge branch '3.12' into script_complete_index
cacrespoFile filter
Filter by extension
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
There are no files selected for viewing
78 changes: 78 additions & 0 deletionsscripts/complete_index.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
""" | ||
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. | ||
""" | ||
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_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 | ||
val_max = max(val_max, source_index) | ||
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. | ||
""" | ||
# Read .po files | ||
if not po_files: | ||
po_files = Path(".").glob("**/*.po") | ||
for po_file_path in po_files: | ||
try: | ||
po_file = polib.pofile(po_file_path, wrapwidth=79) | ||
# 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 | ||
needs_save = True | ||
if needs_save: | ||
po_file.save() | ||
except KeyboardInterrupt: | ||
break | ||
except Exception as e: | ||
cacrespo marked this conversation as resolved. Show resolvedHide resolvedUh oh!There was an error while loading.Please reload this page. | ||
print(f"Error! file {po_file_path}: {e}\n") | ||
else: | ||
print(f"\n---\n{po_file_path} processed!\n---") | ||
if __name__ == "__main__": | ||
po_files = sys.argv[1:] | ||
complete_index(po_files) |
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.