Skip to content

Commit f6710b0

Browse files
committed
python3 fixes
1 parent 5e36de7 commit f6710b0

File tree

2 files changed

+59
-48
lines changed

2 files changed

+59
-48
lines changed

python/plugins/processing/algs/grass7/Grass7Utils.py

Lines changed: 53 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -114,58 +114,55 @@ def createGrass7Script(commands):
114114
folder = Grass7Utils.grassPath()
115115

116116
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
118118

119119
# 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')
161159

162160
@staticmethod
163161
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')
169166

170167
@staticmethod
171168
def grassMapsetFolder():
@@ -393,3 +390,12 @@ def tr(string, context=''):
393390
if context == '':
394391
context = 'Grass7Utils'
395392
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')

python/plugins/processing/algs/saga/SagaUtils.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,12 @@ def createSagaBatchJobFileFromSagaCommands(commands):
9898
else:
9999
pass
100100
for command in commands:
101-
fout.write('saga_cmd ' + command.encode('utf8') + '\n')
101+
try:
102+
# Python 2
103+
fout.write('saga_cmd ' + command.encode('utf8') + '\n')
104+
except TypeError:
105+
# Python 3
106+
fout.write('saga_cmd ' + command + '\n')
102107

103108
fout.write('exit')
104109
fout.close()

0 commit comments

Comments
 (0)