How to use the polib.io.open function in polib

To help you get started, we’ve selected a few polib examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github willkg / dennis / dennis / tools.py View on Github external
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.
        while lines and not lines[-1].strip():
            lines.pop()