Skip to content

Commit 2a92481

Browse files
committedMay 24, 2017
[processing] Use with ... when opening files
(partially cherry-picked from 6a99017)
1 parent 65f571c commit 2a92481

File tree

4 files changed

+23
-22
lines changed

4 files changed

+23
-22
lines changed
 

‎python/plugins/processing/algs/otb/OTBAlgorithm.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -127,8 +127,8 @@ def get_list_from_node(self, myet):
127127
return all_params
128128

129129
def defineCharacteristicsFromFile(self):
130-
content = open(self.descriptionFile).read()
131-
dom_model = ET.fromstring(content)
130+
with open(self.descriptionFile) as content:
131+
dom_model = ET.fromstring(content.read())
132132

133133
self.appkey = dom_model.find('key').text
134134
self.cliName = dom_model.find('exec').text

‎python/plugins/processing/algs/otb/OTBUtils.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -274,10 +274,9 @@ def remove_parameter_by_criteria(doc, criteria):
274274

275275

276276
def defaultWrite(available_app, original_dom_document):
277-
fh = open("description/%s.xml" % available_app, "w")
278-
the_root = original_dom_document
279-
ET.ElementTree(the_root).write(fh)
280-
fh.close()
277+
with open("description/%s.xml" % available_app, "w") as fh:
278+
the_root = original_dom_document
279+
ET.ElementTree(the_root).write(fh)
281280

282281

283282
def defaultSplit(available_app, original_dom_document, parameter):

‎python/plugins/processing/algs/otb/maintenance/OTBHelper.py

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -656,16 +656,15 @@ def create_xml_descriptors():
656656
if available_app in white_list and available_app not in black_list:
657657
logger.warning("There is no adaptor for %s, check white list and versions" % available_app)
658658
# TODO Remove this default code when all apps are tested...
659-
fh = open("description/%s.xml" % available_app, "w")
660-
the_root = get_xml_description_from_application_name(available_app)
661-
ET.ElementTree(the_root).write(fh)
662-
fh.close()
659+
with open("description/%s.xml" % available_app, "w") as fh:
660+
the_root = get_xml_description_from_application_name(available_app)
661+
ET.ElementTree(the_root).write(fh)
663662
try:
664663
get_automatic_ut_from_xml_description(the_root)
665664
except:
666665
logger.error("Unit test for command %s must be fixed: %s" % (available_app, traceback.format_exc()))
667666
else:
668-
logger.warning("%s (not custom app) is not in white list." % available_app)
667+
logger.warning("%s (not custom app) is not in white list." % available_app)
669668
# except Exception, e:
670669
# logger.error(traceback.format_exc())
671670

@@ -678,12 +677,11 @@ def create_html_description():
678677

679678
for available_app in otbApplication.Registry.GetAvailableApplications():
680679
try:
681-
fh = open("description/doc/%s.html" % available_app, "w")
682-
app_instance = otbApplication.Registry.CreateApplication(available_app)
683-
app_instance.UpdateParameters()
684-
ct = describe_app(app_instance)
685-
fh.write(ct)
686-
fh.close()
680+
with open("description/doc/%s.html" % available_app, "w") as fh:
681+
app_instance = otbApplication.Registry.CreateApplication(available_app)
682+
app_instance.UpdateParameters()
683+
ct = describe_app(app_instance)
684+
fh.write(ct)
687685
except Exception:
688686
logger.error(traceback.format_exc())
689687

@@ -694,7 +692,7 @@ def create_html_description():
694692
if __name__ == "__main__":
695693
# Prepare the environment
696694
from qgis.core import QgsApplication
697-
695+
698696
app = QgsApplication([], True)
699697
QgsApplication.initQgis()
700698

‎python/plugins/processing/algs/otb/maintenance/OTBTester.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,8 @@ def templatize(item):
120120
self.fail(unicode(e))
121121

122122
def add_make(self, previous_context, new_file):
123-
input = open(new_file).read()
123+
with open(new_file) as f:
124+
input = f.read()
124125
output = parse(input)
125126
apps = [each for each in output if 'Command' in unicode(type(each))]
126127
setcommands = [each for each in apps if 'SET' in each.name.upper()]
@@ -163,21 +164,24 @@ def mini_clean(item):
163164
return environment
164165

165166
def get_apps(self, the_makefile, the_dict):
166-
input = open(the_makefile).read()
167+
with open(the_makefile) as f:
168+
input = f.read()
167169
output = parse(input)
168170
apps = [each for each in output if 'Command' in unicode(type(each))]
169171
otb_apps = [each for each in apps if 'OTB_TEST_APPLICATION' in each.name.upper()]
170172
return otb_apps
171173

172174
def get_tests(self, the_makefile, the_dict):
173-
input = open(the_makefile).read()
175+
with open(the_makefile) as f:
176+
input = f.read()
174177
output = parse(input)
175178
apps = [each for each in output if 'Command' in unicode(type(each))]
176179
otb_tests = [each for each in apps if 'ADD_TEST' in each.name.upper()]
177180
return otb_tests
178181

179182
def get_apps_with_context(self, the_makefile, the_dict):
180-
input = open(the_makefile).read()
183+
with open(the_makefile) as f:
184+
input = f.read()
181185
output = parse(input)
182186

183187
def is_a_command(item):

0 commit comments

Comments
 (0)
Please sign in to comment.