@@ -114,58 +114,55 @@ def createGrass7Script(commands):
114
114
folder = Grass7Utils .grassPath ()
115
115
116
116
script = Grass7Utils .grassScriptFilename ()
117
- gisrc = userFolder () + os . sep + 'processing.gisrc7' # FIXME: use temporary file
117
+ gisrc = os . path . join ( userFolder (), 'processing.gisrc7' ) # FIXME: use temporary file
118
118
119
119
# Temporary gisrc file
120
- output = open (gisrc , 'w' )
121
- location = 'temp_location'
122
- gisdbase = Grass7Utils .grassDataFolder ()
123
-
124
- output .write ('GISDBASE: ' + gisdbase + '\n ' )
125
- output .write ('LOCATION_NAME: ' + location + '\n ' )
126
- output .write ('MAPSET: PERMANENT \n ' )
127
- output .write ('GRASS_GUI: text\n ' )
128
- output .close ()
129
-
130
- output = open (script , 'w' )
131
- output .write ('set HOME=' + os .path .expanduser ('~' ) + '\n ' )
132
- output .write ('set GISRC=' + gisrc + '\n ' )
133
- output .write ('set WINGISBASE=' + folder + '\n ' )
134
- output .write ('set GISBASE=' + folder + '\n ' )
135
- output .write ('set GRASS_PROJSHARE=' + folder + os .sep + 'share'
136
- + os .sep + 'proj' + '\n ' )
137
- output .write ('set GRASS_MESSAGE_FORMAT=plain\n ' )
138
-
139
- # Replacement code for etc/Init.bat
140
- output .write ('if "%GRASS_ADDON_PATH%"=="" set PATH=%WINGISBASE%\\ bin;%WINGISBASE%\\ lib;%PATH%\n ' )
141
- output .write ('if not "%GRASS_ADDON_PATH%"=="" set PATH=%WINGISBASE%\\ bin;%WINGISBASE%\\ lib;%GRASS_ADDON_PATH%;%PATH%\n ' )
142
- output .write ('\n ' )
143
- output .write ('set GRASS_VERSION=' + Grass7Utils .getGrassVersion () + '\n ' )
144
- output .write ('if not "%LANG%"=="" goto langset\n ' )
145
- output .write ('FOR /F "usebackq delims==" %%i IN (`"%WINGISBASE%\\ etc\\ winlocale"`) DO @set LANG=%%i\n ' )
146
- output .write (':langset\n ' )
147
- output .write ('\n ' )
148
- output .write ('set PATHEXT=%PATHEXT%;.PY\n ' )
149
- output .write ('set PYTHONPATH=%PYTHONPATH%;%WINGISBASE%\\ etc\\ python;%WINGISBASE%\\ etc\\ wxpython\\ n' )
150
- output .write ('\n ' )
151
- output .write ('g.gisenv.exe set="MAPSET=PERMANENT"\n ' )
152
- output .write ('g.gisenv.exe set="LOCATION=' + location + '"\n ' )
153
- output .write ('g.gisenv.exe set="LOCATION_NAME=' + location + '"\n ' )
154
- output .write ('g.gisenv.exe set="GISDBASE=' + gisdbase + '"\n ' )
155
- output .write ('g.gisenv.exe set="GRASS_GUI=text"\n ' )
156
- for command in commands :
157
- output .write (command .encode ('utf8' ) + '\n ' )
158
- output .write ('\n ' )
159
- output .write ('exit\n ' )
160
- output .close ()
120
+ with open (gisrc , 'w' ) as output :
121
+ location = 'temp_location'
122
+ gisdbase = Grass7Utils .grassDataFolder ()
123
+
124
+ output .write ('GISDBASE: ' + gisdbase + '\n ' )
125
+ output .write ('LOCATION_NAME: ' + location + '\n ' )
126
+ output .write ('MAPSET: PERMANENT \n ' )
127
+ output .write ('GRASS_GUI: text\n ' )
128
+
129
+ with open (script , 'w' ) as output :
130
+ output .write ('set HOME=' + os .path .expanduser ('~' ) + '\n ' )
131
+ output .write ('set GISRC=' + gisrc + '\n ' )
132
+ output .write ('set WINGISBASE=' + folder + '\n ' )
133
+ output .write ('set GISBASE=' + folder + '\n ' )
134
+ output .write ('set GRASS_PROJSHARE=' + folder + os .sep + 'share'
135
+ + os .sep + 'proj' + '\n ' )
136
+ output .write ('set GRASS_MESSAGE_FORMAT=plain\n ' )
137
+
138
+ # Replacement code for etc/Init.bat
139
+ output .write ('if "%GRASS_ADDON_PATH%"=="" set PATH=%WINGISBASE%\\ bin;%WINGISBASE%\\ lib;%PATH%\n ' )
140
+ output .write ('if not "%GRASS_ADDON_PATH%"=="" set PATH=%WINGISBASE%\\ bin;%WINGISBASE%\\ lib;%GRASS_ADDON_PATH%;%PATH%\n ' )
141
+ output .write ('\n ' )
142
+ output .write ('set GRASS_VERSION=' + Grass7Utils .getGrassVersion () + '\n ' )
143
+ output .write ('if not "%LANG%"=="" goto langset\n ' )
144
+ output .write ('FOR /F "usebackq delims==" %%i IN (`"%WINGISBASE%\\ etc\\ winlocale"`) DO @set LANG=%%i\n ' )
145
+ output .write (':langset\n ' )
146
+ output .write ('\n ' )
147
+ output .write ('set PATHEXT=%PATHEXT%;.PY\n ' )
148
+ output .write ('set PYTHONPATH=%PYTHONPATH%;%WINGISBASE%\\ etc\\ python;%WINGISBASE%\\ etc\\ wxpython\\ n' )
149
+ output .write ('\n ' )
150
+ output .write ('g.gisenv.exe set="MAPSET=PERMANENT"\n ' )
151
+ output .write ('g.gisenv.exe set="LOCATION=' + location + '"\n ' )
152
+ output .write ('g.gisenv.exe set="LOCATION_NAME=' + location + '"\n ' )
153
+ output .write ('g.gisenv.exe set="GISDBASE=' + gisdbase + '"\n ' )
154
+ output .write ('g.gisenv.exe set="GRASS_GUI=text"\n ' )
155
+ for command in commands :
156
+ Grass7Utils .writeCommand (output , command )
157
+ output .write ('\n ' )
158
+ output .write ('exit\n ' )
161
159
162
160
@staticmethod
163
161
def createGrass7BatchJobFileFromGrass7Commands (commands ):
164
- fout = open (Grass7Utils .grassBatchJobFilename (), 'w' )
165
- for command in commands :
166
- fout .write (command .encode ('utf8' ) + '\n ' )
167
- fout .write ('exit' )
168
- fout .close ()
162
+ with open (Grass7Utils .grassBatchJobFilename (), 'w' ) as fout :
163
+ for command in commands :
164
+ Grass7Utils .writeCommand (fout , command )
165
+ fout .write ('exit' )
169
166
170
167
@staticmethod
171
168
def grassMapsetFolder ():
@@ -393,3 +390,12 @@ def tr(string, context=''):
393
390
if context == '' :
394
391
context = 'Grass7Utils'
395
392
return QCoreApplication .translate (context , string )
393
+
394
+ @staticmethod
395
+ def writeCommand (output , command ):
396
+ try :
397
+ # Python 2
398
+ output .write (command .encode ('utf8' ) + '\n ' )
399
+ except TypeError :
400
+ # Python 3
401
+ output .write (command + '\n ' )
0 commit comments