|
| 1 | +importsubprocess |
| 2 | +importre |
| 3 | + |
| 4 | +STRING=r'"((?:[^\"]|\\|\")*)"' |
| 5 | +MSGID=r'^msgid '+STRING |
| 6 | +MSGSTR=r'^msgstr '+STRING |
| 7 | + |
| 8 | +HEADERS= [ |
| 9 | +'Project-Id-Version', |
| 10 | +'Report-Msgid-Bugs-To', |
| 11 | +'POT-Creation-Date', |
| 12 | +'PO-Revision-Date', |
| 13 | +'Last-Translator', |
| 14 | +'Language-Team', |
| 15 | +'Language', |
| 16 | +'MIME-Version', |
| 17 | +'Content-Type', |
| 18 | +'Content-Transfer-Encoding', |
| 19 | +'Plural-Forms', |
| 20 | +'X-Generator', |
| 21 | +] |
| 22 | + |
| 23 | +defextract(lines,startline,first_line_regex=MSGID): |
| 24 | +first_line_match=re.match(first_line_regex,lines[startline]) |
| 25 | +current_string="" |
| 26 | +iffirst_line_match: |
| 27 | +current_string+=first_line_match.group(1) |
| 28 | +lineno=startline+1 |
| 29 | +whilelineno<len(lines): |
| 30 | +msg_match=re.match(STRING,lines[lineno]) |
| 31 | +ifmsg_matchisNone: |
| 32 | +returncurrent_string,lines[startline:lineno],lineno |
| 33 | +else: |
| 34 | +current_string+=msg_match.group(1) |
| 35 | +lineno+=1 |
| 36 | +else: |
| 37 | +returncurrent_string,lines[startline:lineno],lineno |
| 38 | +returnNone,None,startline |
| 39 | + |
| 40 | +defget_msgs(lines): |
| 41 | +msgids= [] |
| 42 | +msgstrs= [] |
| 43 | +lineno=0 |
| 44 | +whilelineno<len(lines): |
| 45 | +string,raw_lines,lineno=extract(lines,lineno,MSGID) |
| 46 | +ifstringisnotNone: |
| 47 | +msgids.append((string,raw_lines)) |
| 48 | +continue |
| 49 | +string,raw_lines,lineno=extract(lines,lineno,MSGSTR) |
| 50 | +ifstringisnotNone: |
| 51 | +msgstrs.append((string,raw_lines)) |
| 52 | +continue |
| 53 | +lineno+=1 |
| 54 | +returnmsgids,msgstrs |
| 55 | + |
| 56 | +defmain(fp): |
| 57 | +p=subprocess.Popen(['git','show','HEAD:'+fp],stdout=subprocess.PIPE) |
| 58 | +out,err=p.communicate() |
| 59 | +head_po=out.decode().splitlines() |
| 60 | +msgids,msgstrs=get_msgs(head_po) |
| 61 | +msgids=iter(msgids) |
| 62 | +msgstrs=iter(msgstrs) |
| 63 | +withopen(fp)asf: |
| 64 | +lines=f.read().splitlines() |
| 65 | +output_lines= [] |
| 66 | +lineno=0 |
| 67 | +whilelineno<len(lines): |
| 68 | +forfirst_line_regex,original_msgsin [(MSGID,msgids), (MSGSTR,msgstrs)]: |
| 69 | +string,raw_lines,lineno=extract(lines,lineno,first_line_regex) |
| 70 | +ifstringisnotNone: |
| 71 | +original_msg,original_lines=next(original_msgs) |
| 72 | +iforiginal_msgsismsgstrs: |
| 73 | +lines_in_msg=string.split('\\n')[:-1] |
| 74 | +forlineinlines_in_msg: |
| 75 | +ifnotany(line.startswith(h+':')forhinHEADERS): |
| 76 | +break |
| 77 | +else: |
| 78 | +delraw_lines[1:] |
| 79 | +forheader_lineinlines_in_msg: |
| 80 | +ifheader_line.startswith('Language: '): |
| 81 | +raw_lines.append('"Language: zh-Hant\\n"') |
| 82 | +else: |
| 83 | +raw_lines.append('"{}\\n"'.format(header_line)) |
| 84 | +ifstring==original_msg: |
| 85 | +output_lines.extend(original_lines) |
| 86 | +else: |
| 87 | +output_lines.extend(raw_lines) |
| 88 | +break |
| 89 | +else: |
| 90 | +output_lines.append(lines[lineno]) |
| 91 | +lineno+=1 |
| 92 | +returnoutput_lines |
| 93 | + |
| 94 | + |
| 95 | +if__name__=='__main__': |
| 96 | +importsys |
| 97 | +iflen(sys.argv)<2: |
| 98 | +print('Usage: python fix_diffs.py <po_file_path>') |
| 99 | + |
| 100 | +fp=sys.argv[1] |
| 101 | +output_lines=main(fp) |
| 102 | + |
| 103 | +withopen(fp,'w')asf: |
| 104 | +f.writelines([s+'\n'forsinoutput_lines]) |