@@ -136,56 +136,53 @@ def createGrassScript(commands):
136
136
137
137
encoding = locale .getpreferredencoding ()
138
138
# 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 ' )
181
179
182
180
@staticmethod
183
181
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' )
189
186
190
187
@staticmethod
191
188
def grassMapsetFolder ():
@@ -213,17 +210,15 @@ def createTempMapset():
213
210
mkdir (os .path .join (folder , 'PERMANENT' ))
214
211
mkdir (os .path .join (folder , 'PERMANENT' , '.tmp' ))
215
212
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 ' )
220
216
221
217
GrassUtils .writeGrassWindow (os .path .join (folder , 'PERMANENT' , 'WIND' ))
222
218
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 ' )
227
222
228
223
@staticmethod
229
224
def writeGrassWindow (filename ):
@@ -282,7 +277,7 @@ def executeGrass(commands, progress, outputCommands=None):
282
277
command ,
283
278
shell = True ,
284
279
stdout = subprocess .PIPE ,
285
- stdin = open ( os . devnull ) ,
280
+ stdin = subprocess . DEVNULL ,
286
281
stderr = subprocess .STDOUT ,
287
282
universal_newlines = True ,
288
283
env = grassenv
@@ -312,7 +307,7 @@ def executeGrass(commands, progress, outputCommands=None):
312
307
command ,
313
308
shell = True ,
314
309
stdout = subprocess .PIPE ,
315
- stdin = open ( os . devnull ) ,
310
+ stdin = subprocess . DEVNULL ,
316
311
stderr = subprocess .STDOUT ,
317
312
universal_newlines = True ,
318
313
env = grassenv
0 commit comments