(fn)
return 0, data, untranslated
Open po file and return a dict containing msgid as keys and msgstr as value.
    Return None on syntax errors, raise an exception on other errors.
Open po file and return a dict containing msgid as keys and msgstr as value.
    Return None on syntax errors, raise an exception on other errors.
Open po file and return a dict containing msgid as keys and msgstr as value . Return None on syntax errors raise an exception on other errors .
def parsepo(fn):
    """
    Open po file and return a dict containing msgid as keys and msgstr as value.
    Return None on syntax errors, raise an exception on other errors.
    """
    data = OrderedDict()
    if isinstance(fn, basestring):
        with file(fn, 'rb') as fp:
            lines = fp.readlines()
    else:
        lines = fn.readlines()

    msgid = None
    msgstr = None
    default = None
    first = True
    lastline = None
    untranslated = []
    errors = 0

    # find end of header
    for line_number, line in enumerate(lines):
        try:
            line = unicode(line, 'utf-8')
        except UnicodeError:
            try:
                line = unicode(line, 'latin-1')
            except UnicodeError:
                errors += 1
                print >> sys.stderr, 'ERROR: Could not decode data in line %d: %r' % (line_number+1, line)
                continue

        if line[-2:] == '\r\n':
            errors += 1
            print >> sys.stderr, 'ERROR: line %d has Windoze line endings' % (line_number+1)
            line = line[:-2]

        if line[-1:] == '\n':
            line = line[:-1]

        if line != line.strip():
            errors += 1
            print >> sys.stderr, 'ERROR: line %d contains leading and/or trailing whitespaces' % (line_number+1)

        if line.startswith("msgid"):
            msgid = line.strip()[7:-1]
            msgstr = None
        elif line.startswith("msgstr"):
            msgstr = line.strip()[8:-1]
        elif line.startswith('"'):
            if msgstr is None:
                msgid += line.strip()[1:-1]
            elif msgstr is not None:
                msgstr += line.strip()[1:-1]
        elif line.startswith("#. Default: "):
            default = line.strip()[13:-1]
        elif line.startswith("#"):
            lastline = line
            continue
        elif not line:
            # blank line -> must be finished
            if msgid is None and msgstr is None:
                if lastline.startswith('#~'):
                    lastline = line
                    continue
                else:
                    errors += 1
                    print >> sys.stderr, 'ERROR: Got blank line in %d without msgstr or msgid.' % (line_number+1)
                    continue

            if first:
                # skip first occurance as this is the header
                first = False
                lastline = line
                continue

            if not msgstr:
                untranslated.append(msgid)

            # set data
            data[msgid] = (msgstr, default)
            msgid = msgstr = default = None

            lastline = line

    if msgid is not None and msgstr is not None:
        # add last line if not followed by empty line
        if not msgstr:
            untranslated.append(msgid)

        data[msgid] = (msgstr, default)
        msgid = msgstr = None

    if errors:
        return errors, data, untranslated

    return 0, data, untranslated
def parsepo ( fn ) : data = OrderedDict ( ) if isinstance ( fn , basestring ) : with file ( fn , 'rb' ) as fp : lines = fp . readlines ( ) else : lines = fn . readlines ( ) msgid = None msgstr = None default = None first = True lastline = None untranslated = [ ] errors = 0 # find end of header for line_number , line in enumerate ( lines ) : try : line = unicode ( line , 'utf-8' ) except UnicodeError : try : line = unicode ( line , 'latin-1' ) except UnicodeError : errors += 1 print >> sys . stderr , 'ERROR: Could not decode data in line %d: %r' % ( line_number + 1 , line ) continue if line [ - 2 : ] == '\r\n' : errors += 1 print >> sys . stderr , 'ERROR: line %d has Windoze line endings' % ( line_number + 1 ) line = line [ : - 2 ] if line [ - 1 : ] == '\n' : line = line [ : - 1 ] if line != line . strip ( ) : errors += 1 print >> sys . stderr , 'ERROR: line %d contains leading and/or trailing whitespaces' % ( line_number + 1 ) if line . startswith ( "msgid" ) : msgid = line . strip ( ) [ 7 : - 1 ] msgstr = None elif line . startswith ( "msgstr" ) : msgstr = line . strip ( ) [ 8 : - 1 ] elif line . startswith ( '"' ) : if msgstr is None : msgid += line . strip ( ) [ 1 : - 1 ] elif msgstr is not None : msgstr += line . strip ( ) [ 1 : - 1 ] elif line . startswith ( "#. Default: " ) : default = line . strip ( ) [ 13 : - 1 ] elif line . startswith ( "#" ) : lastline = line continue elif not line : # blank line -> must be finished if msgid is None and msgstr is None : if lastline . startswith ( '#~' ) : lastline = line continue else : errors += 1 print >> sys . stderr , 'ERROR: Got blank line in %d without msgstr or msgid.' % ( line_number + 1 ) continue if first : # skip first occurance as this is the header first = False lastline = line continue if not msgstr : untranslated . append ( msgid ) # set data data [ msgid ] = ( msgstr , default ) msgid = msgstr = default = None lastline = line if msgid is not None and msgstr is not None : # add last line if not followed by empty line if not msgstr : untranslated . append ( msgid ) data [ msgid ] = ( msgstr , default ) msgid = msgstr = None if errors : return errors , data , untranslated return 0 , data , untranslated