Skip to content

Commit 4e4bb2b

Browse files
authored
Merge pull request #3728 from nyalldawson/open
Cleanup processing file handling
2 parents 9b667d1 + 0484769 commit 4e4bb2b

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

41 files changed

+536
-585
lines changed

python/plugins/processing/algs/gdal/GdalUtils.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ def runGdal(commands, progress=None):
8787
fused_command,
8888
shell=True,
8989
stdout=subprocess.PIPE,
90-
stdin=open(os.devnull),
90+
stdin=subprocess.DEVNULL,
9191
stderr=subprocess.STDOUT,
9292
universal_newlines=True,
9393
).stdout

python/plugins/processing/algs/gdal/extractprojection.py

+12-14
Original file line numberDiff line numberDiff line change
@@ -75,17 +75,15 @@ def processAlgorithm(self, progress):
7575
crs = tmp.ExportToWkt()
7676
tmp = None
7777

78-
prj = open(outFileName + '.prj', 'wt')
79-
prj.write(crs)
80-
prj.close()
81-
82-
wld = open(outFileName + '.wld', 'wt')
83-
wld.write('%0.8f\n' % geotransform[1])
84-
wld.write('%0.8f\n' % geotransform[4])
85-
wld.write('%0.8f\n' % geotransform[2])
86-
wld.write('%0.8f\n' % geotransform[5])
87-
wld.write('%0.8f\n' % (geotransform[0] + 0.5 * geotransform[1] + 0.5
88-
* geotransform[2]))
89-
wld.write('%0.8f\n' % (geotransform[3] + 0.5 * geotransform[4] + 0.5
90-
* geotransform[5]))
91-
wld.close()
78+
with open(outFileName + '.prj', 'wt') as prj:
79+
prj.write(crs)
80+
81+
with open(outFileName + '.wld', 'wt') as wld:
82+
wld.write('%0.8f\n' % geotransform[1])
83+
wld.write('%0.8f\n' % geotransform[4])
84+
wld.write('%0.8f\n' % geotransform[2])
85+
wld.write('%0.8f\n' % geotransform[5])
86+
wld.write('%0.8f\n' % (geotransform[0] + 0.5 * geotransform[1] + 0.5
87+
* geotransform[2]))
88+
wld.write('%0.8f\n' % (geotransform[3] + 0.5 * geotransform[4] + 0.5
89+
* geotransform[5]))

python/plugins/processing/algs/gdal/information.py

+5-6
Original file line numberDiff line numberDiff line change
@@ -76,9 +76,8 @@ def getConsoleCommands(self):
7676
def processAlgorithm(self, progress):
7777
GdalUtils.runGdal(self.getConsoleCommands(), progress)
7878
output = self.getOutputValue(information.OUTPUT)
79-
f = open(output, 'w')
80-
f.write('<pre>')
81-
for s in GdalUtils.getConsoleOutput()[1:]:
82-
f.write(str(s))
83-
f.write('</pre>')
84-
f.close()
79+
with open(output, 'w') as f:
80+
f.write('<pre>')
81+
for s in GdalUtils.getConsoleOutput()[1:]:
82+
f.write(str(s))
83+
f.write('</pre>')

python/plugins/processing/algs/grass/GrassAlgorithm.py

+57-60
Original file line numberDiff line numberDiff line change
@@ -108,71 +108,68 @@ def getParameterDescriptions(self):
108108
descs = {}
109109
_, helpfile = self.help()
110110
try:
111-
infile = open(helpfile)
112-
lines = infile.readlines()
113-
for i in range(len(lines)):
114-
if lines[i].startswith('<DT><b>'):
115-
for param in self.parameters:
116-
searchLine = '<b>' + param.name + '</b>'
117-
if searchLine in lines[i]:
118-
i += 1
119-
descs[param.name] = (lines[i])[4:-6]
120-
break
121-
122-
infile.close()
111+
with open(helpfile) as infile:
112+
lines = infile.readlines()
113+
for i in range(len(lines)):
114+
if lines[i].startswith('<DT><b>'):
115+
for param in self.parameters:
116+
searchLine = '<b>' + param.name + '</b>'
117+
if searchLine in lines[i]:
118+
i += 1
119+
descs[param.name] = (lines[i])[4:-6]
120+
break
123121
except Exception:
124122
pass
125123
return descs
126124

127125
def defineCharacteristicsFromFile(self):
128-
lines = open(self.descriptionFile)
129-
line = lines.readline().strip('\n').strip()
130-
self.grassName = line
131-
line = lines.readline().strip('\n').strip()
132-
self.name = line
133-
self.i18n_name = QCoreApplication.translate("GrassAlgorithm", line)
134-
if " - " not in self.name:
135-
self.name = self.grassName + " - " + self.name
136-
self.i18n_name = self.grassName + " - " + self.i18n_name
137-
line = lines.readline().strip('\n').strip()
138-
self.group = line
139-
self.i18n_group = QCoreApplication.translate("GrassAlgorithm", line)
140-
hasRasterOutput = False
141-
hasVectorInput = False
142-
vectorOutputs = 0
143-
line = lines.readline().strip('\n').strip()
144-
while line != '':
145-
try:
146-
line = line.strip('\n').strip()
147-
if line.startswith('Hardcoded'):
148-
self.hardcodedStrings.append(line[len('Hardcoded|'):])
149-
parameter = getParameterFromString(line)
150-
if parameter is not None:
151-
self.addParameter(parameter)
152-
if isinstance(parameter, ParameterVector):
153-
hasVectorInput = True
154-
if isinstance(parameter, ParameterMultipleInput) \
155-
and parameter.datatype < 3:
156-
hasVectorInput = True
157-
else:
158-
output = getOutputFromString(line)
159-
self.addOutput(output)
160-
if isinstance(output, OutputRaster):
161-
hasRasterOutput = True
162-
elif isinstance(output, OutputVector):
163-
vectorOutputs += 1
164-
if isinstance(output, OutputHTML):
165-
self.addOutput(OutputFile("rawoutput", output.description +
166-
" (raw output)", "txt"))
167-
line = lines.readline().strip('\n').strip()
168-
except Exception as e:
169-
170-
ProcessingLog.addToLog(
171-
ProcessingLog.LOG_ERROR,
172-
traceback.format_exc())
173-
#self.tr('Could not open GRASS algorithm: %s.\n%s' % (self.descriptionFile, line)))
174-
raise e
175-
lines.close()
126+
with open(self.descriptionFile) as lines:
127+
line = lines.readline().strip('\n').strip()
128+
self.grassName = line
129+
line = lines.readline().strip('\n').strip()
130+
self.name = line
131+
self.i18n_name = QCoreApplication.translate("GrassAlgorithm", line)
132+
if " - " not in self.name:
133+
self.name = self.grassName + " - " + self.name
134+
self.i18n_name = self.grassName + " - " + self.i18n_name
135+
line = lines.readline().strip('\n').strip()
136+
self.group = line
137+
self.i18n_group = QCoreApplication.translate("GrassAlgorithm", line)
138+
hasRasterOutput = False
139+
hasVectorInput = False
140+
vectorOutputs = 0
141+
line = lines.readline().strip('\n').strip()
142+
while line != '':
143+
try:
144+
line = line.strip('\n').strip()
145+
if line.startswith('Hardcoded'):
146+
self.hardcodedStrings.append(line[len('Hardcoded|'):])
147+
parameter = getParameterFromString(line)
148+
if parameter is not None:
149+
self.addParameter(parameter)
150+
if isinstance(parameter, ParameterVector):
151+
hasVectorInput = True
152+
if isinstance(parameter, ParameterMultipleInput) \
153+
and parameter.datatype < 3:
154+
hasVectorInput = True
155+
else:
156+
output = getOutputFromString(line)
157+
self.addOutput(output)
158+
if isinstance(output, OutputRaster):
159+
hasRasterOutput = True
160+
elif isinstance(output, OutputVector):
161+
vectorOutputs += 1
162+
if isinstance(output, OutputHTML):
163+
self.addOutput(OutputFile("rawoutput", output.description +
164+
" (raw output)", "txt"))
165+
line = lines.readline().strip('\n').strip()
166+
except Exception as e:
167+
168+
ProcessingLog.addToLog(
169+
ProcessingLog.LOG_ERROR,
170+
traceback.format_exc())
171+
#self.tr('Could not open GRASS algorithm: %s.\n%s' % (self.descriptionFile, line)))
172+
raise e
176173

177174
self.addParameter(ParameterExtent(
178175
self.GRASS_REGION_EXTENT_PARAMETER,

python/plugins/processing/algs/grass/GrassUtils.py

+52-57
Original file line numberDiff line numberDiff line change
@@ -136,56 +136,53 @@ def createGrassScript(commands):
136136

137137
encoding = locale.getpreferredencoding()
138138
# Temporary gisrc file
139-
output = codecs.open(gisrc, 'w', encoding=encoding)
140-
location = 'temp_location'
141-
gisdbase = GrassUtils.grassDataFolder()
142-
143-
output.write('GISDBASE: ' + gisdbase + '\n')
144-
output.write('LOCATION_NAME: ' + location + '\n')
145-
output.write('MAPSET: PERMANENT \n')
146-
output.write('GRASS_GUI: text\n')
147-
output.close()
148-
149-
output = codecs.open(script, 'w', encoding=encoding)
150-
output.write('set HOME=' + os.path.expanduser('~') + '\n')
151-
output.write('set GISRC=' + gisrc + '\n')
152-
output.write('set GRASS_SH=' + shell + '\\bin\\sh.exe\n')
153-
output.write('set PATH=' + os.path.join(shell, 'bin') + ';' + os.path.join(shell, 'lib') + ';' + '%PATH%\n')
154-
output.write('set WINGISBASE=' + folder + '\n')
155-
output.write('set GISBASE=' + folder + '\n')
156-
output.write('set GRASS_PROJSHARE=' + os.path.join(folder, 'share', 'proj') + '\n')
157-
output.write('set GRASS_MESSAGE_FORMAT=gui\n')
158-
159-
# Replacement code for etc/Init.bat
160-
output.write('if "%GRASS_ADDON_PATH%"=="" set PATH=%WINGISBASE%\\bin;%WINGISBASE%\\lib;%PATH%\n')
161-
output.write('if not "%GRASS_ADDON_PATH%"=="" set PATH=%WINGISBASE%\\bin;%WINGISBASE%\\lib;%GRASS_ADDON_PATH%;%PATH%\n')
162-
output.write('\n')
163-
output.write('set GRASS_VERSION=' + GrassUtils.getGrassVersion() + '\n')
164-
output.write('if not "%LANG%"=="" goto langset\n')
165-
output.write('FOR /F "usebackq delims==" %%i IN (`"%WINGISBASE%\\etc\\winlocale"`) DO @set LANG=%%i\n')
166-
output.write(':langset\n')
167-
output.write('\n')
168-
output.write('set PATHEXT=%PATHEXT%;.PY\n')
169-
output.write('set PYTHONPATH=%PYTHONPATH%;%WINGISBASE%\\etc\\python;%WINGISBASE%\\etc\\wxpython\\n')
170-
output.write('\n')
171-
output.write('g.gisenv.exe set="MAPSET=PERMANENT"\n')
172-
output.write('g.gisenv.exe set="LOCATION=' + location + '"\n')
173-
output.write('g.gisenv.exe set="LOCATION_NAME=' + location + '"\n')
174-
output.write('g.gisenv.exe set="GISDBASE=' + gisdbase + '"\n')
175-
output.write('g.gisenv.exe set="GRASS_GUI=text"\n')
176-
for command in commands:
177-
output.write(command + u'\n')
178-
output.write('\n')
179-
output.write('exit\n')
180-
output.close()
139+
with codecs.open(gisrc, 'w', encoding=encoding) as output:
140+
location = 'temp_location'
141+
gisdbase = GrassUtils.grassDataFolder()
142+
143+
output.write('GISDBASE: ' + gisdbase + '\n')
144+
output.write('LOCATION_NAME: ' + location + '\n')
145+
output.write('MAPSET: PERMANENT \n')
146+
output.write('GRASS_GUI: text\n')
147+
148+
with codecs.open(script, 'w', encoding=encoding) as output:
149+
output.write('set HOME=' + os.path.expanduser('~') + '\n')
150+
output.write('set GISRC=' + gisrc + '\n')
151+
output.write('set GRASS_SH=' + shell + '\\bin\\sh.exe\n')
152+
output.write('set PATH=' + os.path.join(shell, 'bin') + ';' + os.path.join(shell, 'lib') + ';' + '%PATH%\n')
153+
output.write('set WINGISBASE=' + folder + '\n')
154+
output.write('set GISBASE=' + folder + '\n')
155+
output.write('set GRASS_PROJSHARE=' + os.path.join(folder, 'share', 'proj') + '\n')
156+
output.write('set GRASS_MESSAGE_FORMAT=gui\n')
157+
158+
# Replacement code for etc/Init.bat
159+
output.write('if "%GRASS_ADDON_PATH%"=="" set PATH=%WINGISBASE%\\bin;%WINGISBASE%\\lib;%PATH%\n')
160+
output.write('if not "%GRASS_ADDON_PATH%"=="" set PATH=%WINGISBASE%\\bin;%WINGISBASE%\\lib;%GRASS_ADDON_PATH%;%PATH%\n')
161+
output.write('\n')
162+
output.write('set GRASS_VERSION=' + GrassUtils.getGrassVersion() + '\n')
163+
output.write('if not "%LANG%"=="" goto langset\n')
164+
output.write('FOR /F "usebackq delims==" %%i IN (`"%WINGISBASE%\\etc\\winlocale"`) DO @set LANG=%%i\n')
165+
output.write(':langset\n')
166+
output.write('\n')
167+
output.write('set PATHEXT=%PATHEXT%;.PY\n')
168+
output.write('set PYTHONPATH=%PYTHONPATH%;%WINGISBASE%\\etc\\python;%WINGISBASE%\\etc\\wxpython\\n')
169+
output.write('\n')
170+
output.write('g.gisenv.exe set="MAPSET=PERMANENT"\n')
171+
output.write('g.gisenv.exe set="LOCATION=' + location + '"\n')
172+
output.write('g.gisenv.exe set="LOCATION_NAME=' + location + '"\n')
173+
output.write('g.gisenv.exe set="GISDBASE=' + gisdbase + '"\n')
174+
output.write('g.gisenv.exe set="GRASS_GUI=text"\n')
175+
for command in commands:
176+
output.write(command + u'\n')
177+
output.write('\n')
178+
output.write('exit\n')
181179

182180
@staticmethod
183181
def createGrassBatchJobFileFromGrassCommands(commands):
184-
fout = codecs.open(GrassUtils.grassBatchJobFilename(), 'w', encoding='utf-8')
185-
for command in commands:
186-
fout.write(command + u'\n')
187-
fout.write('exit')
188-
fout.close()
182+
with codecs.open(GrassUtils.grassBatchJobFilename(), 'w', encoding='utf-8') as fout:
183+
for command in commands:
184+
fout.write(command + u'\n')
185+
fout.write('exit')
189186

190187
@staticmethod
191188
def grassMapsetFolder():
@@ -213,17 +210,15 @@ def createTempMapset():
213210
mkdir(os.path.join(folder, 'PERMANENT'))
214211
mkdir(os.path.join(folder, 'PERMANENT', '.tmp'))
215212
GrassUtils.writeGrassWindow(os.path.join(folder, 'PERMANENT', 'DEFAULT_WIND'))
216-
outfile = codecs.open(os.path.join(folder, 'PERMANENT', 'MYNAME'), 'w', encoding='utf-8')
217-
outfile.write(
218-
'QGIS GRASS interface: temporary data processing location.\n')
219-
outfile.close()
213+
with codecs.open(os.path.join(folder, 'PERMANENT', 'MYNAME'), 'w', encoding='utf-8') as outfile:
214+
outfile.write(
215+
'QGIS GRASS interface: temporary data processing location.\n')
220216

221217
GrassUtils.writeGrassWindow(os.path.join(folder, 'PERMANENT', 'WIND'))
222218
mkdir(os.path.join(folder, 'PERMANENT', 'dbf'))
223-
outfile = codecs.open(os.path.join(folder, 'PERMANENT', 'VAR'), 'w', encoding='utf-8')
224-
outfile.write('DB_DRIVER: dbf\n')
225-
outfile.write('DB_DATABASE: $GISDBASE/$LOCATION_NAME/$MAPSET/dbf/\n')
226-
outfile.close()
219+
with codecs.open(os.path.join(folder, 'PERMANENT', 'VAR'), 'w', encoding='utf-8') as outfile:
220+
outfile.write('DB_DRIVER: dbf\n')
221+
outfile.write('DB_DATABASE: $GISDBASE/$LOCATION_NAME/$MAPSET/dbf/\n')
227222

228223
@staticmethod
229224
def writeGrassWindow(filename):
@@ -282,7 +277,7 @@ def executeGrass(commands, progress, outputCommands=None):
282277
command,
283278
shell=True,
284279
stdout=subprocess.PIPE,
285-
stdin=open(os.devnull),
280+
stdin=subprocess.DEVNULL,
286281
stderr=subprocess.STDOUT,
287282
universal_newlines=True,
288283
env=grassenv
@@ -312,7 +307,7 @@ def executeGrass(commands, progress, outputCommands=None):
312307
command,
313308
shell=True,
314309
stdout=subprocess.PIPE,
315-
stdin=open(os.devnull),
310+
stdin=subprocess.DEVNULL,
316311
stderr=subprocess.STDOUT,
317312
universal_newlines=True,
318313
env=grassenv

0 commit comments

Comments
 (0)