-
Notifications
You must be signed in to change notification settings - Fork 1
/
clean_android_strings.py
77 lines (63 loc) · 3.95 KB
/
clean_android_strings.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
import sys
import re
def clean_android_string(android_string):
#\\" -> \"
transformed = re.sub(r'\\\\"', '\\"', android_string)
#... -> ellipse, remove whitespaces before ellipse
transformed = re.sub(r'\s*\.\.\.', '…', transformed)
#trim whitespace around \n
transformed = re.sub(r'\s*\\n\s*', re.escape('\\') + 'n', transformed)
#place quotes around strings inside xml tags that do not have them
# handle edge case of [string \"quote at end\"] --> ["string \"quote at end\""]
transformed = re.sub(r'\\\"</string>', '\\\"\"</string>', transformed)
transformed = re.sub(r'\"?</string>', '\"</string>', transformed)
transformed = re.sub(r'">"?', '">"', transformed)
return transformed
def clean_android_strings(input_file_name, output_file_name):
inputfile = open(input_file_name,'r')
outputfile = open(output_file_name, 'w')
outputfile.write(clean_android_string(inputfile.read()))
outputfile.close()
inputfile.close()
def assert_true(condition, message):
if condition:
print "OK " + message
else:
print "FAIL " + message
def test_trim_newline_whitespace():
assert_true(clean_android_string("""<string name="test_newline">" \\n "</string>""") == """<string name="test_newline">"\\n"</string>""", "newline before after")
assert_true(clean_android_string("""<string name="test_newline">"\\n "</string>""") == """<string name="test_newline">"\\n"</string>""", "newline after")
assert_true(clean_android_string("""<string name="test_newline">" \\n"</string>""") == """<string name="test_newline">"\\n"</string>""", "newline before")
assert_true(clean_android_string("""<string name="test_newline">" \\n \\n \\n "</string>""") == """<string name="test_newline">"\\n\\n\\n"</string>""", "newline multiple")
def test_add_quotes():
assert_true(clean_android_string("""<string name="test_add_quotes">add quotes around this</string>""") == """<string name="test_add_quotes">"add quotes around this"</string>""", "add quotes")
assert_true(clean_android_string("""<string name="test_add_quotes">add quotes around \\"this\\"</string>""") == """<string name="test_add_quotes">"add quotes around \\"this\\""</string>""", "add quotes escaped quote at end")
def test_ellipse_conversion():
assert_true(clean_android_string("""<string name="test_ellipse">"..."</string>""") == """<string name="test_ellipse">"…"</string>""", "ellipse conversion")
assert_true(clean_android_string("""<string name="test_ellipse">" ..."</string>""") == """<string name="test_ellipse">"…"</string>""", "ellipse prior space")
# note: is this next test a bug or a feature, removing all preceeding whitespace with multiple ellipses? :)
assert_true(clean_android_string("""<string name="test_ellipse">"... ... ......"</string>""") == """<string name="test_ellipse">"…………"</string>""", "ellipse multiple")
def test_escape_quotes():
assert_true(clean_android_string("""<string name="test_quotes">"\\\\"slash quote\\\\""</string>""") == """<string name="test_quotes">"\\"slash quote\\""</string>""", "escaped quotes")
assert_true(clean_android_string("""<string name="test_quotes">"\\"slash quote\\""</string>""") == """<string name="test_quotes">"\\"slash quote\\""</string>""", "escaped quotes no action")
assert_true(clean_android_string("""<string name="test_quotes">"\\"slash quote\\" \\\\""</string>""") == """<string name="test_quotes">"\\"slash quote\\" \\""</string>""", "escaped quotes multiple")
def run_all_tests():
test_trim_newline_whitespace()
test_ellipse_conversion()
test_add_quotes()
test_escape_quotes()
# Very simple tool to cleanup an android strings.xml file
def main(argv):
num_args = len(argv)
if num_args == 1 and argv[0] == "-test":
run_all_tests()
sys.exit(0)
if num_args != 3 and num_args != 1:
print 'Cleans an Android strings.xml'
print 'Usage: <input.xml> <output.xml>'
print ' or'
print 'Usage: -test (run all tests)'
sys.exit(2)
clean_android_strings(argv[0], argv[1])
if __name__ == "__main__":
main(sys.argv[1:])