Skip to content

Commit a842d47

Browse files
committed
[processing] fixed text and html output in GRASS algorithms
This changes the way text outputs are handled. Now the output of the command is piped to a file, and with that file, the raw and HTML outputs are generated. More formatting for the HTML file should be added in the future, since it is now rather basic fixes #6379
1 parent 77ca802 commit a842d47

Some content is hidden

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

43 files changed

+36
-967
lines changed

python/plugins/processing/algs/grass/CMakeLists.txt

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,5 @@ FILE(GLOB PY_FILES *.py)
22
FILE(GLOB OTHER_FILES grass.txt)
33
FILE(GLOB DESCR_FILES description/*.txt)
44

5-
ADD_SUBDIRECTORY(ext)
6-
75
PLUGIN_INSTALL(processing algs/grass ${PY_FILES} ${OTHER_FILES})
86
PLUGIN_INSTALL(processing algs/grass/description ${DESCR_FILES})

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

Lines changed: 15 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -69,10 +69,6 @@ def __init__(self, descriptionfile):
6969
self.defineCharacteristicsFromFile()
7070
self.numExportedLayers = 0
7171

72-
# GRASS console output, needed to do postprocessing in case GRASS
73-
# dumps results to the console
74-
self.consoleOutput = []
75-
7672
def getCopy(self):
7773
newone = GrassAlgorithm(self.descriptionFile)
7874
newone.provider = self.provider
@@ -125,7 +121,7 @@ def defineCharacteristicsFromFile(self):
125121
if isinstance(parameter, ParameterVector):
126122
hasVectorInput = True
127123
if isinstance(parameter, ParameterMultipleInput) \
128-
and parameter.datatype < 3:
124+
and parameter.datatype < 3:
129125
hasVectorInput = True
130126
elif line.startswith('*Parameter'):
131127
param = getParameterFromString(line[1:])
@@ -138,6 +134,9 @@ def defineCharacteristicsFromFile(self):
138134
hasRasterOutput = True
139135
elif isinstance(output, OutputVector):
140136
vectorOutputs += 1
137+
if isinstance(output, OutputHTML):
138+
self.addOutput(OutputFile("rawoutput", output.description +
139+
" (raw output)", "txt"))
141140
line = lines.readline().strip('\n').strip()
142141
except Exception, e:
143142
ProcessingLog.addToLog(ProcessingLog.LOG_ERROR,
@@ -320,17 +319,10 @@ def processAlgorithm(self, progress):
320319
uniqueSufix = str(uuid.uuid4()).replace('-', '')
321320
for out in self.outputs:
322321
if isinstance(out, OutputFile):
323-
if out.name == 'outputtext':
324-
# The 'outputtext' file is generated by piping output
325-
# from GRASS, is not an actual grass command
326-
command += ' > ' + out.value
327-
else:
328-
command += ' ' + out.name + '="' + out.value + '"'
322+
command += ' > ' + out.value
329323
elif not isinstance(out, OutputHTML):
330-
# Html files are not generated by GRASS, only by us to
331-
# decorate GRASS output, so we skip them. An output name
332-
# to make sure it is unique if the session uses this
333-
# algorithm several times.
324+
# We add an output name to make sure it is unique if the session
325+
# uses this algorithm several times.
334326
uniqueOutputName = out.name + uniqueSufix
335327
command += ' ' + out.name + '=' + uniqueOutputName
336328

@@ -394,9 +386,14 @@ def processAlgorithm(self, progress):
394386
loglines.append(line)
395387
if ProcessingConfig.getSetting(GrassUtils.GRASS_LOG_COMMANDS):
396388
ProcessingLog.addToLog(ProcessingLog.LOG_INFO, loglines)
397-
self.consoleOutput = GrassUtils.executeGrass(commands, progress,
398-
outputCommands)
399-
self.postProcessResults()
389+
GrassUtils.executeGrass(commands, progress, outputCommands)
390+
391+
for out in self.outputs:
392+
if isinstance(out, OutputHTML):
393+
with open(self.getOutputFromName("rawoutput").value) as f:
394+
rawOutput = "".join(f.readlines())
395+
with open(out.value, "w") as f:
396+
f.write("<pre>%s</pre>" % rawOutput)
400397

401398
# If the session has been created outside of this algorithm, add
402399
# the new GRASS layers to it otherwise finish the session
@@ -405,15 +402,6 @@ def processAlgorithm(self, progress):
405402
else:
406403
GrassUtils.endGrassSession()
407404

408-
def postProcessResults(self):
409-
name = self.commandLineName().replace('.', '_')[len('grass:'):]
410-
try:
411-
module = importlib.import_module('processing.algs.grass.ext.' + name)
412-
except ImportError:
413-
return
414-
if hasattr(module, 'postProcessResults'):
415-
func = getattr(module, 'postProcessResults')
416-
func(self)
417405

418406
def exportVectorLayer(self, orgFilename):
419407

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -319,7 +319,8 @@ def executeGrass(commands, progress, outputCommands=None):
319319

320320
if ProcessingConfig.getSetting(GrassUtils.GRASS_LOG_CONSOLE):
321321
ProcessingLog.addToLog(ProcessingLog.LOG_INFO, loglines)
322-
return loglines
322+
323+
323324

324325
# GRASS session is used to hold the layers already exported or
325326
# produced in GRASS between multiple calls to GRASS algorithms.

python/plugins/processing/algs/grass/description/r.horizon.height.txt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,3 @@ ParameterNumber|maxdistance|The maximum distance to consider when finding the ho
99
ParameterString|dist|Sampling distance step coefficient (0.5-1.5)|1.0
1010
ParameterBoolean|-d|Write output in degrees (default is radians)|False
1111
OutputHTML|html|Horizon
12-
OutputFile|outputtext|Horizon (text)

python/plugins/processing/algs/grass/description/r.quantile.txt

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,4 @@ Raster (r.*)
44
ParameterRaster|input|Input raster layer|False
55
ParameterNumber|quantiles|Number of quantiles|2|None|4
66
*ParameterBoolean|-r|Generate recode rules based on quantile-defined intervals|False
7-
OutputHTML|html|Quantiles
8-
OutputFile|outputtext|Quantiles (text)
7+
OutputHTML|html|Quantiles

python/plugins/processing/algs/grass/ext/CMakeLists.txt

Lines changed: 0 additions & 3 deletions
This file was deleted.

python/plugins/processing/algs/grass/ext/HtmlReportPostProcessor.py

Lines changed: 0 additions & 39 deletions
This file was deleted.

python/plugins/processing/algs/grass/ext/__init__.py

Whitespace-only changes.

python/plugins/processing/algs/grass/ext/r_coin.py

Lines changed: 0 additions & 32 deletions
This file was deleted.

python/plugins/processing/algs/grass/ext/r_covar.py

Lines changed: 0 additions & 32 deletions
This file was deleted.

python/plugins/processing/algs/grass/ext/r_describe.py

Lines changed: 0 additions & 32 deletions
This file was deleted.

python/plugins/processing/algs/grass/ext/r_info.py

Lines changed: 0 additions & 32 deletions
This file was deleted.

python/plugins/processing/algs/grass/ext/r_regression_line.py

Lines changed: 0 additions & 32 deletions
This file was deleted.

python/plugins/processing/algs/grass/ext/r_report.py

Lines changed: 0 additions & 32 deletions
This file was deleted.

python/plugins/processing/algs/grass/ext/r_stats.py

Lines changed: 0 additions & 32 deletions
This file was deleted.

0 commit comments

Comments
 (0)