|
| 1 | +import subprocess |
| 2 | +import re |
| 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 | +def extract(lines, startline, first_line_regex=MSGID): |
| 24 | + first_line_match = re.match(first_line_regex, lines[startline]) |
| 25 | + current_string = "" |
| 26 | + if first_line_match: |
| 27 | + current_string += first_line_match.group(1) |
| 28 | + lineno = startline + 1 |
| 29 | + while lineno < len(lines): |
| 30 | + msg_match = re.match(STRING, lines[lineno]) |
| 31 | + if msg_match is None: |
| 32 | + return current_string, lines[startline:lineno], lineno |
| 33 | + else: |
| 34 | + current_string += msg_match.group(1) |
| 35 | + lineno += 1 |
| 36 | + else: |
| 37 | + return current_string, lines[startline:lineno], lineno |
| 38 | + return None, None, startline |
| 39 | + |
| 40 | +def get_msgs(lines): |
| 41 | + msgids = [] |
| 42 | + msgstrs = [] |
| 43 | + lineno = 0 |
| 44 | + while lineno < len(lines): |
| 45 | + string, raw_lines, lineno = extract(lines, lineno, MSGID) |
| 46 | + if string is not None: |
| 47 | + msgids.append((string, raw_lines)) |
| 48 | + continue |
| 49 | + string, raw_lines, lineno = extract(lines, lineno, MSGSTR) |
| 50 | + if string is not None: |
| 51 | + msgstrs.append((string, raw_lines)) |
| 52 | + continue |
| 53 | + lineno += 1 |
| 54 | + return msgids, msgstrs |
| 55 | + |
| 56 | +def main(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 | + with open(fp) as f: |
| 64 | + lines = f.read().splitlines() |
| 65 | + output_lines = [] |
| 66 | + lineno = 0 |
| 67 | + while lineno < len(lines): |
| 68 | + for first_line_regex, original_msgs in [(MSGID, msgids), (MSGSTR, msgstrs)]: |
| 69 | + string, raw_lines, lineno = extract(lines, lineno, first_line_regex) |
| 70 | + if string is not None: |
| 71 | + original_msg, original_lines = next(original_msgs) |
| 72 | + if original_msgs is msgstrs: |
| 73 | + lines_in_msg = string.split('\\n')[:-1] |
| 74 | + for line in lines_in_msg: |
| 75 | + if not any(line.startswith(h + ':') for h in HEADERS): |
| 76 | + break |
| 77 | + else: |
| 78 | + del raw_lines[1:] |
| 79 | + for header_line in lines_in_msg: |
| 80 | + if header_line.startswith('Language: '): |
| 81 | + raw_lines.append('"Language: zh-Hant\\n"') |
| 82 | + else: |
| 83 | + raw_lines.append('"{}\\n"'.format(header_line)) |
| 84 | + if string == 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 | + return output_lines |
| 93 | + |
| 94 | + |
| 95 | +if __name__ == '__main__': |
| 96 | + import sys |
| 97 | + if len(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 | + with open(fp, 'w') as f: |
| 104 | + f.writelines([s + '\n' for s in output_lines]) |
0 commit comments