Skip to content
Permalink
Browse files

Update processing.run() for new API

  • Loading branch information
nyalldawson committed Jun 27, 2017
1 parent 14347a4 commit f98bcb2b81131f6e359bc54e864e0b650404458f
Showing with 26 additions and 22 deletions.
  1. +24 −20 python/plugins/processing/core/Processing.py
  2. +2 −2 python/plugins/processing/tools/general.py
@@ -51,6 +51,7 @@
from processing.gui.Postprocessing import handleAlgorithmResults
from processing.gui.AlgorithmExecutor import execute
from processing.tools import dataobjects
from processing.core.GeoAlgorithmExecutionException import GeoAlgorithmExecutionException

from processing.algs.qgis.QGISAlgorithmProvider import QGISAlgorithmProvider # NOQA
#from processing.algs.grass7.Grass7AlgorithmProvider import Grass7AlgorithmProvider # NOQA
@@ -122,31 +123,31 @@ def removeScripts(folder):
provider.refreshAlgorithms()

@staticmethod
def runAlgorithm(algOrName, parameters, onFinish, feedback=None, context=None):
def runAlgorithm(algOrName, parameters, onFinish=None, feedback=None, context=None):
if isinstance(algOrName, QgsProcessingAlgorithm):
alg = algOrName
else:
alg = QgsApplication.processingRegistry().algorithmById(algOrName)

if feedback is None:
feedback = MessageBarProgress(alg.displayName() if alg else Processing.tr('Processing'))

if alg is None:
# fix_print_with_import
print('Error: Algorithm not found\n')
QgsMessageLog.logMessage(Processing.tr('Error: Algorithm {0} not found\n').format(algOrName),
Processing.tr("Processing"))
return
msg = Processing.tr('Error: Algorithm {0} not found\n').format(algOrName)
feedback.reportError(msg)
raise GeoAlgorithmExecutionException(msg)

# check for any manadatory parameters which were not specified
# check for any mandatory parameters which were not specified
for param in alg.parameterDefinitions():
if param.name() not in parameters:
if not param.flags() & QgsProcessingParameterDefinition.FlagOptional:
# fix_print_with_import
msg = Processing.tr('Error: Missing parameter value for parameter {0}.').format(param.name())
print('Error: Missing parameter value for parameter %s.' % param.name())
QgsMessageLog.logMessage(
Processing.tr('Error: Missing parameter value for parameter {0}.').format(param.name()),
Processing.tr("Processing"))
return

if feedback is None:
feedback = MessageBarProgress(alg.displayName())
feedback.reportError(msg)
raise GeoAlgorithmExecutionException(msg)

if context is None:
context = dataobjects.createContext(feedback)
@@ -155,24 +156,27 @@ def runAlgorithm(algOrName, parameters, onFinish, feedback=None, context=None):
if not ok:
# fix_print_with_import
print('Unable to execute algorithm\n' + str(msg))
QgsMessageLog.logMessage(Processing.tr('Unable to execute algorithm\n{0}').format(msg),
Processing.tr("Processing"))
return
msg = Processing.tr('Unable to execute algorithm\n{0}').format(msg)
feedback.reportError(msg)
raise GeoAlgorithmExecutionException(msg)

if not alg.validateInputCrs(parameters, context):
print('Warning: Not all input layers use the same CRS.\n' +
'This can cause unexpected results.')
QgsMessageLog.logMessage(
Processing.tr('Warning: Not all input layers use the same CRS.\nThis can cause unexpected results.'),
Processing.tr("Processing"))
feedback.pushInfo(
Processing.tr('Warning: Not all input layers use the same CRS.\nThis can cause unexpected results.'))

ret, results = execute(alg, parameters, context, feedback)
if ret:
feedback.pushInfo(
Processing.tr('Results: {}').format(results))

if onFinish is not None:
onFinish(alg, context, feedback)
else:
QgsMessageLog.logMessage(Processing.tr("There were errors executing the algorithm."),
Processing.tr("Processing"))
msg = Processing.tr("There were errors executing the algorithm.")
feedback.reportError(msg)
raise GeoAlgorithmExecutionException(msg)

if isinstance(feedback, MessageBarProgress):
feedback.close()
@@ -69,11 +69,11 @@ def algorithmHelp(id):
print('Algorithm "{}" not found.'.format(id))


def run(algOrName, *args, **kwargs):
def run(algOrName, parameters, onFinish=None, feedback=None, context=None):
"""Executes given algorithm and returns its outputs as dictionary
object.
"""
return Processing.runAlgorithm(algOrName, None, *args, **kwargs)
return Processing.runAlgorithm(algOrName, parameters, onFinish, feedback, context)


def runAndLoadResults(name, *args, **kwargs):

0 comments on commit f98bcb2

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