示例#1
0
文件: tools.py 项目: willkg/dennis
def parse_pofile(fn_or_string):
    """Parses a po file and attaches original poentry blocks

    When polib parses a pofile, it captures the line number of the
    start of the block, but doesn't capture the original string for
    the block. When you call str() on the poentry, it "reassembles"
    the block with textwrapped lines, so it returns something
    substantially different than the original block. This is
    problematic if we want to print out the block with the line
    numbers--one for each line.

    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.
        while lines and not lines[-1].strip():
            lines.pop()

        # Join them and voila!
        poentry.original = ''.join(lines)

    return parsed_pofile
示例#2
0
文件: tools.py 项目: novaeeken/dennis
def parse_pofile(fn_or_string):
    """Parses a po file and attaches original poentry blocks

    When polib parses a pofile, it captures the line number of the
    start of the block, but doesn't capture the original string for
    the block. When you call str() on the poentry, it "reassembles"
    the block with textwrapped lines, so it returns something
    substantially different than the original block. This is
    problematic if we want to print out the block with the line
    numbers--one for each line.

    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.
        while lines and not lines[-1].strip():
            lines.pop()

        # Join them and voila!
        poentry.original = "".join(lines)

    return parsed_pofile
示例#3
0
 def test_is_file(self):
     self.assertTrue(polib._is_file(os.path.abspath(__file__)))
     self.assertFalse(polib._is_file('This is not a file !!!!'))
     self.assertFalse(polib._is_file(True))