-
Notifications
You must be signed in to change notification settings - Fork 10
/
cmp_po.py
109 lines (85 loc) · 2.64 KB
/
cmp_po.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
from argparse import (
ArgumentParser
)
def get_msgstr(po_record):
riter = iter(po_record)
msgstr_code = ""
for rl in riter:
if rl.startswith("msgstr"):
tmp = rl[6:-1]
if tmp != '""':
msgstr_code += tmp
break
for rl in riter:
tmp = rl[:-1]
if tmp != '""':
msgstr_code += tmp
msgstr = eval(msgstr_code)
return msgstr
if __name__ == "__main__":
ap = ArgumentParser()
ap.add_argument("po", nargs = 2)
args = ap.parse_args()
pos = args.po
# print(pos)
for i in range(0, 2):
f = open(pos[i], "rb")
po_lines = list(f.readlines())
locals()["po" + str(i) + "_lines"] = po_lines
f.close()
# parse *.po
if po_lines[-1] == "\n":
liter = iter(po_lines)
else:
liter = iter(po_lines + ["\n"])
# skip first block
while next(liter) != "\n":
pass
po_content = {}
po_record = []
for l in liter:
if l != "\n":
po_record.append(l)
continue
if not po_record:
continue
riter = iter(po_record)
msgid_code = ""
for rl in riter:
if rl.startswith("msgid"):
tmp = rl[6:-1]
if tmp != '""':
msgid_code += tmp
break
for rl in riter:
if rl.startswith("msgstr"):
break
tmp = rl[:-1]
if tmp != '""':
msgid_code += tmp
msgid = eval(msgid_code)
if msgid in po_content:
print('Double record "%s" in file "%s"' % (msgid, pos[i]))
else:
po_content[msgid] = po_record
po_record = []
locals()["po" + str(i) + "_content"] = po_content
print("lines: " + str(len(po0_lines)) + "/" + str(len(po1_lines)))
print("records: " + str(len(po0_content)) + "/" + str(len(po1_content)))
both = set()
both |= set(po0_content) | set(po1_content)
for key in po0_content:
if key not in po1_content:
print("deleted: '%s'" % key)
both -= key
for key in po1_content:
if key not in po0_content:
print("added: '%s'" % key)
both -= key
for key in both:
msgstr0 = get_msgstr(po0_content[key])
msgstr1 = get_msgstr(po1_content[key])
if msgstr0 != msgstr1:
print("changed: '%s'" % key)
# else:
# print(key + "\n" + msgstr0 + "\n")