Skip to content

Commit

Permalink
[processing] fixed text and html output in GRASS algorithms
Browse files Browse the repository at this point in the history
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
  • Loading branch information
volaya committed Jun 23, 2015
1 parent 77ca802 commit a842d47
Show file tree
Hide file tree
Showing 43 changed files with 36 additions and 967 deletions.
2 changes: 0 additions & 2 deletions python/plugins/processing/algs/grass/CMakeLists.txt
Expand Up @@ -2,7 +2,5 @@ FILE(GLOB PY_FILES *.py)
FILE(GLOB OTHER_FILES grass.txt)
FILE(GLOB DESCR_FILES description/*.txt)

ADD_SUBDIRECTORY(ext)

PLUGIN_INSTALL(processing algs/grass ${PY_FILES} ${OTHER_FILES})
PLUGIN_INSTALL(processing algs/grass/description ${DESCR_FILES})
42 changes: 15 additions & 27 deletions python/plugins/processing/algs/grass/GrassAlgorithm.py
Expand Up @@ -69,10 +69,6 @@ def __init__(self, descriptionfile):
self.defineCharacteristicsFromFile()
self.numExportedLayers = 0

# GRASS console output, needed to do postprocessing in case GRASS
# dumps results to the console
self.consoleOutput = []

def getCopy(self):
newone = GrassAlgorithm(self.descriptionFile)
newone.provider = self.provider
Expand Down Expand Up @@ -125,7 +121,7 @@ def defineCharacteristicsFromFile(self):
if isinstance(parameter, ParameterVector):
hasVectorInput = True
if isinstance(parameter, ParameterMultipleInput) \
and parameter.datatype < 3:
and parameter.datatype < 3:
hasVectorInput = True
elif line.startswith('*Parameter'):
param = getParameterFromString(line[1:])
Expand All @@ -138,6 +134,9 @@ def defineCharacteristicsFromFile(self):
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, e:
ProcessingLog.addToLog(ProcessingLog.LOG_ERROR,
Expand Down Expand Up @@ -320,17 +319,10 @@ def processAlgorithm(self, progress):
uniqueSufix = str(uuid.uuid4()).replace('-', '')
for out in self.outputs:
if isinstance(out, OutputFile):
if out.name == 'outputtext':
# The 'outputtext' file is generated by piping output
# from GRASS, is not an actual grass command
command += ' > ' + out.value
else:
command += ' ' + out.name + '="' + out.value + '"'
command += ' > ' + out.value
elif not isinstance(out, OutputHTML):
# Html files are not generated by GRASS, only by us to
# decorate GRASS output, so we skip them. An output name
# to make sure it is unique if the session uses this
# algorithm several times.
# We add an output name to make sure it is unique if the session
# uses this algorithm several times.
uniqueOutputName = out.name + uniqueSufix
command += ' ' + out.name + '=' + uniqueOutputName

Expand Down Expand Up @@ -394,9 +386,14 @@ def processAlgorithm(self, progress):
loglines.append(line)
if ProcessingConfig.getSetting(GrassUtils.GRASS_LOG_COMMANDS):
ProcessingLog.addToLog(ProcessingLog.LOG_INFO, loglines)
self.consoleOutput = GrassUtils.executeGrass(commands, progress,
outputCommands)
self.postProcessResults()
GrassUtils.executeGrass(commands, progress, outputCommands)

for out in self.outputs:
if isinstance(out, OutputHTML):
with open(self.getOutputFromName("rawoutput").value) as f:
rawOutput = "".join(f.readlines())
with open(out.value, "w") as f:
f.write("<pre>%s</pre>" % rawOutput)

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

def postProcessResults(self):
name = self.commandLineName().replace('.', '_')[len('grass:'):]
try:
module = importlib.import_module('processing.algs.grass.ext.' + name)
except ImportError:
return
if hasattr(module, 'postProcessResults'):
func = getattr(module, 'postProcessResults')
func(self)

def exportVectorLayer(self, orgFilename):

Expand Down
3 changes: 2 additions & 1 deletion python/plugins/processing/algs/grass/GrassUtils.py
Expand Up @@ -319,7 +319,8 @@ def executeGrass(commands, progress, outputCommands=None):

if ProcessingConfig.getSetting(GrassUtils.GRASS_LOG_CONSOLE):
ProcessingLog.addToLog(ProcessingLog.LOG_INFO, loglines)
return loglines



# GRASS session is used to hold the layers already exported or
# produced in GRASS between multiple calls to GRASS algorithms.
Expand Down
Expand Up @@ -9,4 +9,3 @@ ParameterNumber|maxdistance|The maximum distance to consider when finding the ho
ParameterString|dist|Sampling distance step coefficient (0.5-1.5)|1.0
ParameterBoolean|-d|Write output in degrees (default is radians)|False
OutputHTML|html|Horizon
OutputFile|outputtext|Horizon (text)
Expand Up @@ -4,5 +4,4 @@ Raster (r.*)
ParameterRaster|input|Input raster layer|False
ParameterNumber|quantiles|Number of quantiles|2|None|4
*ParameterBoolean|-r|Generate recode rules based on quantile-defined intervals|False
OutputHTML|html|Quantiles
OutputFile|outputtext|Quantiles (text)
OutputHTML|html|Quantiles
3 changes: 0 additions & 3 deletions python/plugins/processing/algs/grass/ext/CMakeLists.txt

This file was deleted.

This file was deleted.

Empty file.
32 changes: 0 additions & 32 deletions python/plugins/processing/algs/grass/ext/r_coin.py

This file was deleted.

32 changes: 0 additions & 32 deletions python/plugins/processing/algs/grass/ext/r_covar.py

This file was deleted.

32 changes: 0 additions & 32 deletions python/plugins/processing/algs/grass/ext/r_describe.py

This file was deleted.

32 changes: 0 additions & 32 deletions python/plugins/processing/algs/grass/ext/r_info.py

This file was deleted.

32 changes: 0 additions & 32 deletions python/plugins/processing/algs/grass/ext/r_regression_line.py

This file was deleted.

32 changes: 0 additions & 32 deletions python/plugins/processing/algs/grass/ext/r_report.py

This file was deleted.

32 changes: 0 additions & 32 deletions python/plugins/processing/algs/grass/ext/r_stats.py

This file was deleted.

0 comments on commit a842d47

Please sign in to comment.