Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
So this wrapper captures the line numbers and original text for
each block and attaches that to the parsed poentries in an
attribute named "original" thus allowing us to print the original
text with line numbers.
"""
from polib import _is_file, detect_encoding, io, pofile
# This parses the pofile
parsed_pofile = pofile(fn_or_string)
# Now we need to build a linenumber -> block hash so that we can
# accurately print out what was in the pofile because polib will
# reassembled what it parsed, but it's not the same.
if _is_file(fn_or_string):
enc = detect_encoding(fn_or_string, "pofile")
fp = io.open(fn_or_string, "rt", encoding=enc)
else:
fp = fn_or_string.splitlines(True)
fp = list(fp)
entries = list(parsed_pofile)
for i, poentry in enumerate(entries):
# Grab the lines that make up the poentry.
# Note: linenum is 1-based, so we convert it to 0-based.
try:
lines = fp[poentry.linenum - 1 : entries[i + 1].linenum - 1]
except IndexError:
lines = fp[poentry.linenum - 1 :]
# Nix blank lines at the end.