-
-
Notifications
You must be signed in to change notification settings - Fork 79
/
Copy pathstring-fixer.py
298 lines (238 loc) · 10.1 KB
/
string-fixer.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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
import xml.etree.ElementTree as ET
import os
import re
should_fix_issues = True
show_warnings = True
def read_xml(file) -> ET.ElementTree:
ET.register_namespace('tools', 'http://schemas.android.com/tools')
parser = ET.XMLParser(target=ET.TreeBuilder(insert_comments=True))
return ET.parse(file, parser=parser)
def write_xml(tree: ET.ElementTree, file):
with open(file, 'r', encoding='utf-8') as f:
text = f.read()
closing_tag = '</resources>'
closing_tag_index = text.rfind(closing_tag)
if closing_tag_index > -1:
ending = text[(text.rfind(closing_tag) + len(closing_tag)):]
else:
ending = ''
tree.write(file, encoding='utf-8', xml_declaration=True, short_empty_elements=True)
# Replace the single quotes with double quotes
with open(file, 'r', encoding='utf-8') as f:
content = f.read()
with open(file, 'w', encoding='utf-8') as f:
f.write(content.replace("<?xml version='1.0' encoding='utf-8'?>", '<?xml version="1.0" encoding="utf-8"?>').strip() + ending)
def delete_element(tree, element):
root = tree.getroot()
root.remove(element)
def replace_text(element, text):
element.text = text
def add_attribute(element, name, value):
element.set(name, value)
def get_string_element(tree, name):
root = tree.getroot()
return root.find('string[@name="' + name + '"]')
def address_issue(tree, element, diagnostic, file):
diagnostic_name = diagnostic.__class__.__name__
file_parent_folder = file.split('/')[-2]
file_name = file.split('/')[-1]
if should_fix_issues and diagnostic.fix(tree, tree, element):
print(f'[Fixed] {diagnostic_name} {element.get("name")} in {file_parent_folder}/{file_name}')
elif not diagnostic.is_warning():
print(f'[Fail] {diagnostic_name} {element.get("name")} in {file_parent_folder}/{file_name}')
elif show_warnings:
print(f'[Warn] {diagnostic_name} {element.get("name")} in {file_parent_folder}/{file_name}')
class StringDiagnostic(object):
def check(self, source_tree, tree, element) -> bool:
return True
def fix(self, source_tree, tree, element) -> bool:
return False
def is_warning(self) -> bool:
return False
class NonTranslatableTranslated(StringDiagnostic):
def check(self, source_tree, tree, element) -> bool:
if tree == source_tree:
return False
source_element = get_string_element(source_tree, element.get('name'))
if source_element is None:
return False
return source_element.get('translatable') == 'false'
def fix(self, source_tree, tree, element) -> bool:
delete_element(tree, element)
return True
def is_warning(self) -> bool:
return True
class URLMismatch(StringDiagnostic):
def check(self, source_tree, tree, element) -> bool:
if source_tree != tree:
return False
source_string_value = get_string_element(source_tree, element.get('name')).text.strip()
string_value = element.text.strip()
source_urls = self.__get_urls(source_string_value)
urls = self.__get_urls(string_value)
if len(source_urls) != len(urls):
return True
for i in range(len(source_urls)):
if source_urls[i] != urls[i]:
return True
return False
def __get_urls(self, text):
# Regex to get all URLs
r = r'(https?://[^\s]+)'
# Find all matches
return re.findall(r, text)
def fix(self, source_tree, tree, element) -> bool:
delete_element(tree, element)
return True
def is_warning(self) -> bool:
return False
class PreferenceKeyTranslatable(StringDiagnostic):
def check(self, source_tree, tree, element) -> bool:
if source_tree != tree:
return False
string_value = element.text.strip()
if '_' in string_value and all(char.islower() or char.isdigit() or char == '_' for char in string_value):
if not string_elem.get('translatable') or string_elem.get('translatable').lower() != 'false':
return True
return False
def fix(self, source_tree, tree, element) -> bool:
add_attribute(element, 'translatable', 'false')
return True
def is_warning(self) -> bool:
return True
class FormattingDoesNotMatch(StringDiagnostic):
def check(self, source_tree, tree, element) -> bool:
if source_tree == tree:
return False
source_element = get_string_element(source_tree, element.get('name'))
if source_element is None:
return False
# Get the format arguments from the source string
source_format_args = self.__get_format_args(source_element.text)
# Get the format arguments from the string
format_args = self.__get_format_args(element.text)
# If the number of format arguments does not match, return true
if len(source_format_args) != len(format_args):
return True
# If the format arguments do not match, return true
remaining = format_args.copy()
for source_format_arg in source_format_args:
if source_format_arg in remaining:
remaining.remove(source_format_arg)
else:
return True
return False
def fix(self, source_tree, tree, element) -> bool:
delete_element(tree, element)
return True
def is_warning(self) -> bool:
return False
def __get_format_args(self, text):
# Avoid counting %% as a format argument
t = text.replace('%%', '')
# Get the format arguments from the source string (ex. %s, %d, %1$s, %0.2f) but %% is ignored
r = r'%[^a-zA-Z]*[a-zA-Z]'
# Find all matches
return re.findall(r, t)
class PositionalFormattingUnspecified(StringDiagnostic):
def check(self, source_tree, tree, element) -> bool:
# Regex to get all format arguments without a positional index
r = r'%[sd]'
# Find all matches
matches = re.findall(r, element.text)
# If there are no matches, continue
if not matches or len(matches) < 2:
return False
return True
def fix(self, source_tree, tree, element) -> bool:
# Regex to get all format arguments without a positional index
r = r'%[sd]'
# Find all matches
matches = re.findall(r, element.text)
# Add the positional index to the matches within the string
for i, match in enumerate(matches):
replace_text(element, element.text.replace(match, '%' + str(i + 1) + '$' + match[1], 1))
return True
def is_warning(self) -> bool:
return True
class NotInSource(StringDiagnostic):
def check(self, source_tree, tree, element) -> bool:
if source_tree == tree:
return False
source_element = get_string_element(source_tree, element.get('name'))
return source_element is None
def fix(self, source_tree, tree, element) -> bool:
delete_element(tree, element)
return True
def is_warning(self) -> bool:
return True
class TranslatedAppName(StringDiagnostic):
def check(self, source_tree, tree, element) -> bool:
app_name = get_string_element(source_tree, 'app_name').text
source_element = get_string_element(source_tree, element.get('name'))
if source_element is not None and app_name in source_element.text:
return app_name not in element.text
return False
def fix(self, source_tree, tree, element) -> bool:
return False
def is_warning(self) -> bool:
return True
class HardCodedAppName(StringDiagnostic):
def check(self, source_tree, tree, element) -> bool:
if source_tree != tree:
return False
app_name = get_string_element(source_tree, 'app_name').text
source_element = get_string_element(source_tree, element.get('name'))
if source_element is not None and app_name in source_element.text and element.attrib.get('translatable') != 'false':
return app_name in element.text
return False
def fix(self, source_tree, tree, element) -> bool:
return False
def is_warning(self) -> bool:
return True
class EmptyTranslation(StringDiagnostic):
def check(self, source_tree, tree, element) -> bool:
if source_tree == tree:
return False
return element.text is None or len(element.text.strip()) == 0
def fix(self, source_tree, tree, element) -> bool:
delete_element(tree, element)
return True
def is_warning(self) -> bool:
return True
script_dir = os.path.dirname(os.path.realpath(__file__)).replace('\\', '/')
reference_file = script_dir + '/../app/src/main/res/values/strings.xml'
reference_tree = read_xml(reference_file)
diagnostics = [
EmptyTranslation(),
PreferenceKeyTranslatable(),
NonTranslatableTranslated(),
FormattingDoesNotMatch(),
NotInSource(),
PositionalFormattingUnspecified(),
TranslatedAppName(),
HardCodedAppName(),
URLMismatch()
]
# Run diagnostics on reference file
for diagnostic in diagnostics:
for string_elem in reference_tree.iter('string'):
if diagnostic.check(reference_tree, reference_tree, string_elem):
address_issue(reference_tree, string_elem, diagnostic, reference_file)
if should_fix_issues:
write_xml(reference_tree, reference_file)
# Find all strings.xml files
for root, dirs, files in os.walk(script_dir + '/../app/src/main/res'):
for file in files:
if file == 'strings.xml':
strings_file_path = os.path.join(root, file).replace('\\', '/')
if strings_file_path == reference_file:
continue
tree = read_xml(strings_file_path)
root = tree.getroot()
for diagnostic in diagnostics:
for string_elem in root.iter('string'):
if diagnostic.check(reference_tree, tree, string_elem):
address_issue(tree, string_elem, diagnostic, strings_file_path)
if should_fix_issues:
write_xml(tree, strings_file_path)