-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathclean-up-base-localization.py
More file actions
executable file
·79 lines (62 loc) · 2.26 KB
/
clean-up-base-localization.py
File metadata and controls
executable file
·79 lines (62 loc) · 2.26 KB
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
#!/usr/bin/python
import argparse
import re
import operator
class LocalizationString:
keyval_re = re.compile(r"\"(.*)\"[ ]*=[ ]*\"(.*)\";\n")
def __init__(self):
self.comments = []
self.key = ""
self.value = ""
def parse(self, line):
keyval = LocalizationString.keyval_re.findall(line)
if keyval:
self.key = keyval[0][0]
self.value = keyval[0][1]
return True
else:
self.comments.append(line);
return False;
def validate(strings):
keys = [string.key for string in strings]
duplicate_keys = set([x for x in keys if keys.count(x) > 1])
if duplicate_keys:
print "ERROR: Following keys are not unique:"
for i in duplicate_keys:
print "\t%s" % i
vals = [string.value for string in strings]
duplicate_vals = set([x for x in vals if vals.count(x) > 1])
if duplicate_vals:
print "WARNING: Following values are not unique:"
for i in duplicate_vals:
print "\t%s" % i
def main():
parser = argparse.ArgumentParser(description='Parse sort strings in localizable file.')
parser.add_argument('--input', dest='input', required=True, help='a path to input localizable file.')
parser.add_argument('--output', dest='output', required=True, help='a path to output localizable file.')
args = parser.parse_args()
header = True
result = []
strings = []
current_localized_string = LocalizationString()
with open(args.input) as f:
for line in f:
if header:
result.append(line)
else:
if current_localized_string.parse(line):
strings.append(current_localized_string)
current_localized_string = LocalizationString()
if line == "/* ================= Application Strings ================= */\n":
header = False
strings.sort(key=operator.attrgetter('key'))
validate(strings)
for string in strings:
result.extend(string.comments)
result.append("\"%s\" = \"%s\";\n" % (string.key, string.value))
output = open(args.output, "w")
for item in result:
output.write(item)
output.close()
if __name__ == "__main__":
main()