Skip to content
Permalink
Browse files

[processing] Use with ... when opening files

  • Loading branch information
nyalldawson committed Nov 7, 2016
1 parent b4bca5b commit 6a99017bf0657d989b960a7162cbcc29931fab91
Showing with 523 additions and 572 deletions.
  1. +12 −14 python/plugins/processing/algs/gdal/extractprojection.py
  2. +5 −6 python/plugins/processing/algs/gdal/information.py
  3. +57 −60 python/plugins/processing/algs/grass/GrassAlgorithm.py
  4. +50 −55 python/plugins/processing/algs/grass/GrassUtils.py
  5. +56 −58 python/plugins/processing/algs/grass7/Grass7Algorithm.py
  6. +25 −29 python/plugins/processing/algs/grass7/Grass7Utils.py
  7. +3 −4 python/plugins/processing/algs/lidar/fusion/FusionUtils.py
  8. +2 −2 python/plugins/processing/algs/otb/OTBAlgorithm.py
  9. +3 −4 python/plugins/processing/algs/otb/OTBUtils.py
  10. +8 −10 python/plugins/processing/algs/otb/maintenance/OTBHelper.py
  11. +8 −4 python/plugins/processing/algs/otb/maintenance/OTBTester.py
  12. +2 −3 python/plugins/processing/algs/qgis/BarPlot.py
  13. +7 −8 python/plugins/processing/algs/qgis/BasicStatisticsNumbers.py
  14. +6 −8 python/plugins/processing/algs/qgis/BasicStatisticsStrings.py
  15. +2 −3 python/plugins/processing/algs/qgis/MeanAndStdDevPlot.py
  16. +7 −8 python/plugins/processing/algs/qgis/NearestNeighbourAnalysis.py
  17. +20 −21 python/plugins/processing/algs/qgis/PointsToPaths.py
  18. +2 −3 python/plugins/processing/algs/qgis/PolarPlot.py
  19. +2 −3 python/plugins/processing/algs/qgis/RasterLayerHistogram.py
  20. +7 −8 python/plugins/processing/algs/qgis/RasterLayerStatistics.py
  21. +10 −11 python/plugins/processing/algs/qgis/UniqueValues.py
  22. +2 −3 python/plugins/processing/algs/qgis/VectorLayerHistogram.py
  23. +2 −3 python/plugins/processing/algs/qgis/VectorLayerScatterplot.py
  24. +4 −6 python/plugins/processing/algs/r/RAlgorithm.py
  25. +15 −16 python/plugins/processing/algs/r/RUtils.py
  26. +41 −43 python/plugins/processing/algs/saga/SagaAlgorithm212.py
  27. +7 −8 python/plugins/processing/algs/saga/SagaAlgorithm213.py
  28. +33 −37 python/plugins/processing/algs/saga/SagaDescriptionCreator.py
  29. +19 −20 python/plugins/processing/algs/saga/SagaUtils.py
  30. +26 −27 python/plugins/processing/algs/saga/versioncheck.py
  31. +4 −4 python/plugins/processing/core/GeoAlgorithm.py
  32. +6 −8 python/plugins/processing/core/ProcessingLog.py
  33. +8 −9 python/plugins/processing/gui/CommanderWindow.py
  34. +15 −17 python/plugins/processing/gui/RenderingStyles.py
  35. +2 −3 python/plugins/processing/modeler/ModelerDialog.py
  36. +10 −11 python/plugins/processing/script/ScriptAlgorithm.py
  37. +1 −1 python/plugins/processing/tests/testdata/expected/basic_statistics_string.html
  38. +34 −34 python/plugins/processing/tools/translation.py
@@ -75,17 +75,15 @@ def processAlgorithm(self, progress):
crs = tmp.ExportToWkt()
tmp = None

prj = open(outFileName + '.prj', 'wt')
prj.write(crs)
prj.close()

wld = open(outFileName + '.wld', 'wt')
wld.write('%0.8f\n' % geotransform[1])
wld.write('%0.8f\n' % geotransform[4])
wld.write('%0.8f\n' % geotransform[2])
wld.write('%0.8f\n' % geotransform[5])
wld.write('%0.8f\n' % (geotransform[0] + 0.5 * geotransform[1] + 0.5
* geotransform[2]))
wld.write('%0.8f\n' % (geotransform[3] + 0.5 * geotransform[4] + 0.5
* geotransform[5]))
wld.close()
with open(outFileName + '.prj', 'wt') as prj:
prj.write(crs)

with open(outFileName + '.wld', 'wt') as wld:
wld.write('%0.8f\n' % geotransform[1])
wld.write('%0.8f\n' % geotransform[4])
wld.write('%0.8f\n' % geotransform[2])
wld.write('%0.8f\n' % geotransform[5])
wld.write('%0.8f\n' % (geotransform[0] + 0.5 * geotransform[1] + 0.5
* geotransform[2]))
wld.write('%0.8f\n' % (geotransform[3] + 0.5 * geotransform[4] + 0.5
* geotransform[5]))
@@ -76,9 +76,8 @@ def getConsoleCommands(self):
def processAlgorithm(self, progress):
GdalUtils.runGdal(self.getConsoleCommands(), progress)
output = self.getOutputValue(information.OUTPUT)
f = open(output, 'w')
f.write('<pre>')
for s in GdalUtils.getConsoleOutput()[1:]:
f.write(str(s))
f.write('</pre>')
f.close()
with open(output, 'w') as f:
f.write('<pre>')
for s in GdalUtils.getConsoleOutput()[1:]:
f.write(str(s))
f.write('</pre>')
@@ -108,71 +108,68 @@ def getParameterDescriptions(self):
descs = {}
_, helpfile = self.help()
try:
infile = open(helpfile)
lines = infile.readlines()
for i in range(len(lines)):
if lines[i].startswith('<DT><b>'):
for param in self.parameters:
searchLine = '<b>' + param.name + '</b>'
if searchLine in lines[i]:
i += 1
descs[param.name] = (lines[i])[4:-6]
break

infile.close()
with open(helpfile) as infile:
lines = infile.readlines()
for i in range(len(lines)):
if lines[i].startswith('<DT><b>'):
for param in self.parameters:
searchLine = '<b>' + param.name + '</b>'
if searchLine in lines[i]:
i += 1
descs[param.name] = (lines[i])[4:-6]
break
except Exception:
pass
return descs

def defineCharacteristicsFromFile(self):
lines = open(self.descriptionFile)
line = lines.readline().strip('\n').strip()
self.grassName = line
line = lines.readline().strip('\n').strip()
self.name = line
self.i18n_name = QCoreApplication.translate("GrassAlgorithm", line)
if " - " not in self.name:
self.name = self.grassName + " - " + self.name
self.i18n_name = self.grassName + " - " + self.i18n_name
line = lines.readline().strip('\n').strip()
self.group = line
self.i18n_group = QCoreApplication.translate("GrassAlgorithm", line)
hasRasterOutput = False
hasVectorInput = False
vectorOutputs = 0
line = lines.readline().strip('\n').strip()
while line != '':
try:
line = line.strip('\n').strip()
if line.startswith('Hardcoded'):
self.hardcodedStrings.append(line[len('Hardcoded|'):])
parameter = getParameterFromString(line)
if parameter is not None:
self.addParameter(parameter)
if isinstance(parameter, ParameterVector):
hasVectorInput = True
if isinstance(parameter, ParameterMultipleInput) \
and parameter.datatype < 3:
hasVectorInput = True
else:
output = getOutputFromString(line)
self.addOutput(output)
if isinstance(output, OutputRaster):
hasRasterOutput = True
elif isinstance(output, OutputVector):
vectorOutputs += 1
if isinstance(output, OutputHTML):
self.addOutput(OutputFile("rawoutput", output.description +
" (raw output)", "txt"))
line = lines.readline().strip('\n').strip()
except Exception as e:

ProcessingLog.addToLog(
ProcessingLog.LOG_ERROR,
traceback.format_exc())
#self.tr('Could not open GRASS algorithm: %s.\n%s' % (self.descriptionFile, line)))
raise e
lines.close()
with open(self.descriptionFile) as lines:
line = lines.readline().strip('\n').strip()
self.grassName = line
line = lines.readline().strip('\n').strip()
self.name = line
self.i18n_name = QCoreApplication.translate("GrassAlgorithm", line)
if " - " not in self.name:
self.name = self.grassName + " - " + self.name
self.i18n_name = self.grassName + " - " + self.i18n_name
line = lines.readline().strip('\n').strip()
self.group = line
self.i18n_group = QCoreApplication.translate("GrassAlgorithm", line)
hasRasterOutput = False
hasVectorInput = False
vectorOutputs = 0
line = lines.readline().strip('\n').strip()
while line != '':
try:
line = line.strip('\n').strip()
if line.startswith('Hardcoded'):
self.hardcodedStrings.append(line[len('Hardcoded|'):])
parameter = getParameterFromString(line)
if parameter is not None:
self.addParameter(parameter)
if isinstance(parameter, ParameterVector):
hasVectorInput = True
if isinstance(parameter, ParameterMultipleInput) \
and parameter.datatype < 3:
hasVectorInput = True
else:
output = getOutputFromString(line)
self.addOutput(output)
if isinstance(output, OutputRaster):
hasRasterOutput = True
elif isinstance(output, OutputVector):
vectorOutputs += 1
if isinstance(output, OutputHTML):
self.addOutput(OutputFile("rawoutput", output.description +
" (raw output)", "txt"))
line = lines.readline().strip('\n').strip()
except Exception as e:

ProcessingLog.addToLog(
ProcessingLog.LOG_ERROR,
traceback.format_exc())
#self.tr('Could not open GRASS algorithm: %s.\n%s' % (self.descriptionFile, line)))
raise e

self.addParameter(ParameterExtent(
self.GRASS_REGION_EXTENT_PARAMETER,
@@ -136,56 +136,53 @@ def createGrassScript(commands):

encoding = locale.getpreferredencoding()
# Temporary gisrc file
output = codecs.open(gisrc, 'w', encoding=encoding)
location = 'temp_location'
gisdbase = GrassUtils.grassDataFolder()

output.write('GISDBASE: ' + gisdbase + '\n')
output.write('LOCATION_NAME: ' + location + '\n')
output.write('MAPSET: PERMANENT \n')
output.write('GRASS_GUI: text\n')
output.close()

output = codecs.open(script, 'w', encoding=encoding)
output.write('set HOME=' + os.path.expanduser('~') + '\n')
output.write('set GISRC=' + gisrc + '\n')
output.write('set GRASS_SH=' + shell + '\\bin\\sh.exe\n')
output.write('set PATH=' + os.path.join(shell, 'bin') + ';' + os.path.join(shell, 'lib') + ';' + '%PATH%\n')
output.write('set WINGISBASE=' + folder + '\n')
output.write('set GISBASE=' + folder + '\n')
output.write('set GRASS_PROJSHARE=' + os.path.join(folder, 'share', 'proj') + '\n')
output.write('set GRASS_MESSAGE_FORMAT=gui\n')

# Replacement code for etc/Init.bat
output.write('if "%GRASS_ADDON_PATH%"=="" set PATH=%WINGISBASE%\\bin;%WINGISBASE%\\lib;%PATH%\n')
output.write('if not "%GRASS_ADDON_PATH%"=="" set PATH=%WINGISBASE%\\bin;%WINGISBASE%\\lib;%GRASS_ADDON_PATH%;%PATH%\n')
output.write('\n')
output.write('set GRASS_VERSION=' + GrassUtils.getGrassVersion() + '\n')
output.write('if not "%LANG%"=="" goto langset\n')
output.write('FOR /F "usebackq delims==" %%i IN (`"%WINGISBASE%\\etc\\winlocale"`) DO @set LANG=%%i\n')
output.write(':langset\n')
output.write('\n')
output.write('set PATHEXT=%PATHEXT%;.PY\n')
output.write('set PYTHONPATH=%PYTHONPATH%;%WINGISBASE%\\etc\\python;%WINGISBASE%\\etc\\wxpython\\n')
output.write('\n')
output.write('g.gisenv.exe set="MAPSET=PERMANENT"\n')
output.write('g.gisenv.exe set="LOCATION=' + location + '"\n')
output.write('g.gisenv.exe set="LOCATION_NAME=' + location + '"\n')
output.write('g.gisenv.exe set="GISDBASE=' + gisdbase + '"\n')
output.write('g.gisenv.exe set="GRASS_GUI=text"\n')
for command in commands:
output.write(command + u'\n')
output.write('\n')
output.write('exit\n')
output.close()
with codecs.open(gisrc, 'w', encoding=encoding) as output:
location = 'temp_location'
gisdbase = GrassUtils.grassDataFolder()

output.write('GISDBASE: ' + gisdbase + '\n')
output.write('LOCATION_NAME: ' + location + '\n')
output.write('MAPSET: PERMANENT \n')
output.write('GRASS_GUI: text\n')

with codecs.open(script, 'w', encoding=encoding) as output:
output.write('set HOME=' + os.path.expanduser('~') + '\n')
output.write('set GISRC=' + gisrc + '\n')
output.write('set GRASS_SH=' + shell + '\\bin\\sh.exe\n')
output.write('set PATH=' + os.path.join(shell, 'bin') + ';' + os.path.join(shell, 'lib') + ';' + '%PATH%\n')
output.write('set WINGISBASE=' + folder + '\n')
output.write('set GISBASE=' + folder + '\n')
output.write('set GRASS_PROJSHARE=' + os.path.join(folder, 'share', 'proj') + '\n')
output.write('set GRASS_MESSAGE_FORMAT=gui\n')

# Replacement code for etc/Init.bat
output.write('if "%GRASS_ADDON_PATH%"=="" set PATH=%WINGISBASE%\\bin;%WINGISBASE%\\lib;%PATH%\n')
output.write('if not "%GRASS_ADDON_PATH%"=="" set PATH=%WINGISBASE%\\bin;%WINGISBASE%\\lib;%GRASS_ADDON_PATH%;%PATH%\n')
output.write('\n')
output.write('set GRASS_VERSION=' + GrassUtils.getGrassVersion() + '\n')
output.write('if not "%LANG%"=="" goto langset\n')
output.write('FOR /F "usebackq delims==" %%i IN (`"%WINGISBASE%\\etc\\winlocale"`) DO @set LANG=%%i\n')
output.write(':langset\n')
output.write('\n')
output.write('set PATHEXT=%PATHEXT%;.PY\n')
output.write('set PYTHONPATH=%PYTHONPATH%;%WINGISBASE%\\etc\\python;%WINGISBASE%\\etc\\wxpython\\n')
output.write('\n')
output.write('g.gisenv.exe set="MAPSET=PERMANENT"\n')
output.write('g.gisenv.exe set="LOCATION=' + location + '"\n')
output.write('g.gisenv.exe set="LOCATION_NAME=' + location + '"\n')
output.write('g.gisenv.exe set="GISDBASE=' + gisdbase + '"\n')
output.write('g.gisenv.exe set="GRASS_GUI=text"\n')
for command in commands:
output.write(command + u'\n')
output.write('\n')
output.write('exit\n')

@staticmethod
def createGrassBatchJobFileFromGrassCommands(commands):
fout = codecs.open(GrassUtils.grassBatchJobFilename(), 'w', encoding='utf-8')
for command in commands:
fout.write(command + u'\n')
fout.write('exit')
fout.close()
with codecs.open(GrassUtils.grassBatchJobFilename(), 'w', encoding='utf-8') as fout:
for command in commands:
fout.write(command + u'\n')
fout.write('exit')

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

GrassUtils.writeGrassWindow(os.path.join(folder, 'PERMANENT', 'WIND'))
mkdir(os.path.join(folder, 'PERMANENT', 'dbf'))
outfile = codecs.open(os.path.join(folder, 'PERMANENT', 'VAR'), 'w', encoding='utf-8')
outfile.write('DB_DRIVER: dbf\n')
outfile.write('DB_DATABASE: $GISDBASE/$LOCATION_NAME/$MAPSET/dbf/\n')
outfile.close()
with codecs.open(os.path.join(folder, 'PERMANENT', 'VAR'), 'w', encoding='utf-8') as outfile:
outfile.write('DB_DRIVER: dbf\n')
outfile.write('DB_DATABASE: $GISDBASE/$LOCATION_NAME/$MAPSET/dbf/\n')

@staticmethod
def writeGrassWindow(filename):

0 comments on commit 6a99017

Please sign in to comment.
You can’t perform that action at this time.