Skip to content

Commit f86d88c

Browse files
committed
Fix inconsistent file endings in string fixer
1 parent d1f50d7 commit f86d88c

File tree

1 file changed

+27
-18
lines changed

1 file changed

+27
-18
lines changed

scripts/string-fixer.py

Lines changed: 27 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,21 @@ def read_xml(file) -> ET.ElementTree:
1212
return ET.parse(file, parser=parser)
1313

1414
def write_xml(tree: ET.ElementTree, file):
15+
with open(file, 'r', encoding='utf-8') as f:
16+
text = f.read()
17+
closing_tag = '</resources>'
18+
closing_tag_index = text.rfind(closing_tag)
19+
if closing_tag_index > -1:
20+
ending = text[(text.rfind(closing_tag) + len(closing_tag)):]
21+
else:
22+
ending = ''
23+
1524
tree.write(file, encoding='utf-8', xml_declaration=True, short_empty_elements=True)
1625
# Replace the single quotes with double quotes
1726
with open(file, 'r', encoding='utf-8') as f:
1827
content = f.read()
1928
with open(file, 'w', encoding='utf-8') as f:
20-
f.write(content.replace("<?xml version='1.0' encoding='utf-8'?>", '<?xml version="1.0" encoding="utf-8"?>'))
29+
f.write(content.replace("<?xml version='1.0' encoding='utf-8'?>", '<?xml version="1.0" encoding="utf-8"?>').strip() + ending)
2130

2231
def delete_element(tree, element):
2332
root = tree.getroot()
@@ -50,7 +59,7 @@ def check(self, source_tree, tree, element) -> bool:
5059

5160
def fix(self, source_tree, tree, element) -> bool:
5261
return False
53-
62+
5463
def is_warning(self) -> bool:
5564
return False
5665

@@ -66,7 +75,7 @@ def check(self, source_tree, tree, element) -> bool:
6675
def fix(self, source_tree, tree, element) -> bool:
6776
delete_element(tree, element)
6877
return True
69-
78+
7079
def is_warning(self) -> bool:
7180
return True
7281

@@ -82,13 +91,13 @@ def check(self, source_tree, tree, element) -> bool:
8291

8392
if len(source_urls) != len(urls):
8493
return True
85-
94+
8695
for i in range(len(source_urls)):
8796
if source_urls[i] != urls[i]:
8897
return True
89-
98+
9099
return False
91-
100+
92101
def __get_urls(self, text):
93102
# Regex to get all URLs
94103
r = r'(https?://[^\s]+)'
@@ -98,7 +107,7 @@ def __get_urls(self, text):
98107
def fix(self, source_tree, tree, element) -> bool:
99108
delete_element(tree, element)
100109
return True
101-
110+
102111
def is_warning(self) -> bool:
103112
return False
104113

@@ -115,7 +124,7 @@ def check(self, source_tree, tree, element) -> bool:
115124
def fix(self, source_tree, tree, element) -> bool:
116125
add_attribute(element, 'translatable', 'false')
117126
return True
118-
127+
119128
def is_warning(self) -> bool:
120129
return True
121130

@@ -126,7 +135,7 @@ def check(self, source_tree, tree, element) -> bool:
126135
source_element = get_string_element(source_tree, element.get('name'))
127136
if source_element is None:
128137
return False
129-
138+
130139
# Get the format arguments from the source string
131140
source_format_args = self.__get_format_args(source_element.text)
132141

@@ -136,21 +145,21 @@ def check(self, source_tree, tree, element) -> bool:
136145
# If the number of format arguments does not match, return true
137146
if len(source_format_args) != len(format_args):
138147
return True
139-
148+
140149
# If the format arguments do not match, return true
141150
remaining = format_args.copy()
142151
for source_format_arg in source_format_args:
143152
if source_format_arg in remaining:
144153
remaining.remove(source_format_arg)
145154
else:
146155
return True
147-
156+
148157
return False
149158

150159
def fix(self, source_tree, tree, element) -> bool:
151160
delete_element(tree, element)
152161
return True
153-
162+
154163
def is_warning(self) -> bool:
155164
return False
156165

@@ -184,7 +193,7 @@ def fix(self, source_tree, tree, element) -> bool:
184193
for i, match in enumerate(matches):
185194
replace_text(element, element.text.replace(match, '%' + str(i + 1) + '$' + match[1], 1))
186195
return True
187-
196+
188197
def is_warning(self) -> bool:
189198
return True
190199

@@ -198,7 +207,7 @@ def check(self, source_tree, tree, element) -> bool:
198207
def fix(self, source_tree, tree, element) -> bool:
199208
delete_element(tree, element)
200209
return True
201-
210+
202211
def is_warning(self) -> bool:
203212
return True
204213

@@ -212,7 +221,7 @@ def check(self, source_tree, tree, element) -> bool:
212221

213222
def fix(self, source_tree, tree, element) -> bool:
214223
return False
215-
224+
216225
def is_warning(self) -> bool:
217226
return True
218227

@@ -228,7 +237,7 @@ def check(self, source_tree, tree, element) -> bool:
228237

229238
def fix(self, source_tree, tree, element) -> bool:
230239
return False
231-
240+
232241
def is_warning(self) -> bool:
233242
return True
234243

@@ -241,7 +250,7 @@ def check(self, source_tree, tree, element) -> bool:
241250
def fix(self, source_tree, tree, element) -> bool:
242251
delete_element(tree, element)
243252
return True
244-
253+
245254
def is_warning(self) -> bool:
246255
return True
247256

@@ -286,4 +295,4 @@ def is_warning(self) -> bool:
286295
if diagnostic.check(reference_tree, tree, string_elem):
287296
address_issue(tree, string_elem, diagnostic, strings_file_path)
288297
if should_fix_issues:
289-
write_xml(tree, strings_file_path)
298+
write_xml(tree, strings_file_path)

0 commit comments

Comments
 (0)