<s> import os , sys <EOL> def parse_translation ( f , lineno ) : <EOL> """<STR_LIT>""" <EOL> line = f . readline ( ) <EOL> def get_line ( f , line , need_keys , lineno , default = '<STR_LIT>' ) : <EOL> line = line . rstrip ( ) <EOL> if not line : <EOL> return lineno , need_keys [ <NUM_LIT:0> ] , default , line <EOL> key , value = line . split ( '<STR_LIT:U+0020>' , <NUM_LIT:1> ) <EOL> if key not in need_keys : <EOL> print '<STR_LIT>' % ( need_keys , lineno , line ) <EOL> raise RuntimeError ( "<STR_LIT>" ) <EOL> v = value <EOL> while <NUM_LIT:1> : <EOL> line = f . readline ( ) <EOL> line = line . rstrip ( ) <EOL> lineno += <NUM_LIT:1> <EOL> if not line or line [ <NUM_LIT:0> ] != '<STR_LIT:">' : <EOL> break <EOL> v += '<STR_LIT:\n>' + line [ : ] <EOL> return lineno , key , v , line <EOL> comments = [ ] <EOL> while <NUM_LIT:1> : <EOL> if not line : <EOL> return lineno , None , None , None <EOL> if line . strip ( ) == '<STR_LIT>' : <EOL> return lineno , comments , None , None <EOL> elif line [ <NUM_LIT:0> ] == '<STR_LIT:#>' : <EOL> comments . append ( line [ : - <NUM_LIT:1> ] ) <EOL> else : <EOL> break <EOL> line = f . readline ( ) <EOL> lineno += <NUM_LIT:1> <EOL> lineno , key , msgid , line = get_line ( f , line , [ '<STR_LIT>' ] , lineno ) <EOL> lineno , key , value , line = get_line ( f , line , [ '<STR_LIT>' , '<STR_LIT>' ] , lineno ) <EOL> if key == '<STR_LIT>' : <EOL> msgid = ( msgid , value ) <EOL> lineno , key , v1 , line = get_line ( f , line , [ '<STR_LIT>' ] , lineno ) <EOL> lineno , key , v2 , line = get_line ( f , line , [ '<STR_LIT>' ] , lineno ) <EOL> msgstr = ( v1 , v2 ) <EOL> else : <EOL> msgstr = value <EOL> if line != '<STR_LIT>' : <EOL> print '<STR_LIT>' % ( f . name , line ) <EOL> raise RuntimeError ( "<STR_LIT>" ) <EOL> return lineno , comments , msgid , msgstr <EOL> def split_comments ( comments ) : <EOL> """<STR_LIT>""" <EOL> flags = [ ] <EOL> other = [ ] <EOL> for c in comments : <EOL> if len ( c ) > <NUM_LIT:1> and c [ <NUM_LIT:1> ] == '<STR_LIT:U+002C>' : <EOL> flags . append ( c ) <EOL> else : <EOL> other . append ( c ) <EOL> return flags , other <EOL> def write_msg ( comments , msgid , msgstr ) : <EOL> s = [ ] <EOL> s . append ( '<STR_LIT:\n>' . join ( comments ) + '<STR_LIT:\n>' ) <EOL> if isinstance ( msgid , ( tuple , list ) ) : <EOL> s . append ( '<STR_LIT>' + msgid [ <NUM_LIT:0> ] + '<STR_LIT:\n>' ) <EOL> s . append ( '<STR_LIT>' + msgid [ <NUM_LIT:1> ] + '<STR_LIT:\n>' ) <EOL> else : <EOL> s . append ( '<STR_LIT>' + msgid + '<STR_LIT:\n>' ) <EOL> if isinstance ( msgstr , ( tuple , list ) ) : <EOL> s . append ( '<STR_LIT>' + msgstr [ <NUM_LIT:0> ] + '<STR_LIT:\n>' ) <EOL> if msgstr [ <NUM_LIT:0> ] != '<STR_LIT>' : <EOL> s . append ( '<STR_LIT:\n>' ) <EOL> else : <EOL> s . append ( '<STR_LIT>' + msgstr [ <NUM_LIT:1> ] + '<STR_LIT>' ) <EOL> else : <EOL> s . append ( '<STR_LIT>' + msgstr + '<STR_LIT>' ) <EOL> return '<STR_LIT>' . join ( s ) <EOL> def merge ( file1 , file2 , exact = False ) : <EOL> """<STR_LIT>""" <EOL> import shutil <EOL> if not os . path . exists ( file1 ) : <EOL> shutil . copyfile ( file2 , file1 ) <EOL> return <EOL> source = { } <EOL> f2 = file ( file2 ) <EOL> lineno = <NUM_LIT:1> <EOL> while <NUM_LIT:1> : <EOL> lineno , comments , msgid , msgstr = parse_translation ( f2 , lineno ) <EOL> if comments is None and msgid is None and msgstr is None : <EOL> break <EOL> if msgid is not None and msgid != '<STR_LIT>' and msgid != ( '<STR_LIT>' , '<STR_LIT>' ) : <EOL> source [ msgid ] = msgstr , split_comments ( comments ) [ <NUM_LIT:1> ] <EOL> f2 . close ( ) <EOL> infile = file1 + '<STR_LIT>' <EOL> shutil . copyfile ( file1 , infile ) <EOL> outfile = open ( file1 , '<STR_LIT:w>' ) <EOL> string_count = <NUM_LIT:0> <EOL> update_count = <NUM_LIT:0> <EOL> untranslated = <NUM_LIT:0> <EOL> removed = <NUM_LIT:0> <EOL> f1 = file ( infile ) <EOL> lineno = <NUM_LIT:1> <EOL> while <NUM_LIT:1> : <EOL> lineno , comments , msgid , msgstr = parse_translation ( f1 , lineno ) <EOL> if comments is None and msgid is None and msgstr is None : <EOL> break <EOL> if msgid is None : <EOL> continue <EOL> else : <EOL> if msgid != '<STR_LIT>' and msgid != ( '<STR_LIT>' , '<STR_LIT>' ) : <EOL> string_count += <NUM_LIT:1> <EOL> if msgid != '<STR_LIT>' : <EOL> if msgid in source : <EOL> new_flags , comments = split_comments ( comments ) <EOL> new_msgstr , new_comments = source [ msgid ] <EOL> del source [ msgid ] <EOL> if ( new_msgstr != msgstr and new_msgstr != '<STR_LIT>' and new_msgstr != ( '<STR_LIT>' , '<STR_LIT>' ) ) or new_comments != comments : <EOL> if new_msgstr != msgstr and new_msgstr != '<STR_LIT>' and new_msgstr != ( '<STR_LIT>' , '<STR_LIT>' ) : <EOL> update_count += <NUM_LIT:1> <EOL> msgstr = new_msgstr <EOL> else : <EOL> comments = new_comments <EOL> else : <EOL> if exact : <EOL> removed += <NUM_LIT:1> <EOL> continue <EOL> outfile . write ( write_msg ( comments , msgid , msgstr ) ) <EOL> if msgid != '<STR_LIT>' and ( msgstr == '<STR_LIT>' or msgstr == ( '<STR_LIT>' , '<STR_LIT>' ) ) : <EOL> untranslated += <NUM_LIT:1> <EOL> _len = len ( source ) <EOL> if _len : <EOL> update_count += _len <EOL> string_count += _len <EOL> for msgid , _v in source . iteritems ( ) : <EOL> msgstr , comments = _v <EOL> outfile . write ( write_msg ( comments , msgid , msgstr ) ) <EOL> if msgid != '<STR_LIT>' and msgstr == '<STR_LIT>' : <EOL> untranslated += <NUM_LIT:1> <EOL> f1 . close ( ) <EOL> outfile . close ( ) <EOL> if string_count == <NUM_LIT:0> : <EOL> print '<STR_LIT>' <EOL> else : <EOL> print ( '<STR_LIT>' <EOL> '<STR_LIT>' % <EOL> ( update_count , removed , untranslated , string_count , <EOL> <NUM_LIT> * untranslated / string_count ) ) <EOL> def main ( argv ) : <EOL> if len ( argv ) != <NUM_LIT:3> : <EOL> argv0 = os . path . basename ( argv [ <NUM_LIT:0> ] ) <EOL> sys . exit ( '<STR_LIT>' <EOL> '<STR_LIT:\n>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT:\n>' % argv0 ) <EOL> merge ( argv [ <NUM_LIT:1> ] , argv [ <NUM_LIT:2> ] ) <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> main ( sys . argv ) </s>
