-
Notifications
You must be signed in to change notification settings - Fork 397
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 our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 1 commit
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
d8906b8
script to search for and complete probable 'index entries'
cacrespo 1e3d6a0
Merge branch '3.12' into script_complete_index
cacrespo a89bcfd
out_of_order() as a new function
cacrespo f3f4dc6
Update scripts/complete_index.py
cacrespo 74e3404
Update scripts/complete_index.py
cacrespo 26bb8ba
wrapwidth=79
cacrespo 39be902
Merge branch '3.12' into script_complete_index
cacrespo File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next
Next commit
script to search for and complete probable 'index entries'
- Loading branch information
commit d8906b889d2a964c704c3ad024e186e172385aeb
There are no files selected for viewing
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,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. | ||
cacrespo marked this conversation as resolved.
Show resolved
Hide resolved
|
||
""" | ||
|
||
# Read .po files | ||
if not po_files: | ||
po_files = [f for f in glob.glob("**/*.po", recursive=True)] | ||
cacrespo marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
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 = [] | ||
cacrespo marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
# Compare source index with file order and create marks | ||
for i, entry in enumerate(po_entries): | ||
cacrespo marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
source_index = int(entry.occurrences[0][1]) | ||
cacrespo marked this conversation as resolved.
Show resolved
Hide resolved
|
||
if source_index <= val_max: | ||
marks.append(i) | ||
cacrespo marked this conversation as resolved.
Show resolved
Hide resolved
|
||
val_max = val_max if source_index <= val_max else source_index | ||
cacrespo marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
# We only keep the entries that are marked | ||
po_entries = [j for i, j in enumerate(po_entries) if i in marks] | ||
cacrespo marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
# Complete translation with original text | ||
for entry in po_entries: | ||
cacrespo marked this conversation as resolved.
Show resolved
Hide resolved
|
||
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: | ||
cacrespo marked this conversation as resolved.
Show resolved
Hide resolved
|
||
print(f"{len(modified_texts)} text(s) adjusted.", | ||
cacrespo marked this conversation as resolved.
Show resolved
Hide resolved
|
||
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) |
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.
Uh oh!
There was an error while loading. Please reload this page.