Skip to content

Commit 507aeb0

Browse files
radosuavvolaya
radosuav
authored andcommittedNov 4, 2015
[Processing] Allow "progress" to be specified when calling processing.runalg
This is mostly so that algorithms executed in Processing scripts can display messages in the same way as if they were executed directly from Processing toolbox. Also fixes a small issue with busy cursor being reset too early when algorithms were executed from Processing scripts.
1 parent e497e8b commit 507aeb0

File tree

2 files changed

+24
-8
lines changed

2 files changed

+24
-8
lines changed
 

‎python/plugins/processing/core/Processing.py

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -265,12 +265,13 @@ def runandload(name, *args):
265265
Processing.runAlgorithm(name, handleAlgorithmResults, *args)
266266

267267
@staticmethod
268-
def runAlgorithm(algOrName, onFinish, *args):
268+
def runAlgorithm(algOrName, onFinish, *args, **kwargs):
269269
if isinstance(algOrName, GeoAlgorithm):
270270
alg = algOrName
271271
else:
272272
alg = Processing.getAlgorithm(algOrName)
273273
if alg is None:
274+
print 'Error: Algorithm not found\n'
274275
QgsMessageLog.logMessage(Processing.tr('Error: Algorithm {0} not found\n').format(algOrName), Processing.tr("Processing"))
275276
return
276277
alg = alg.getCopy()
@@ -287,6 +288,7 @@ def runAlgorithm(algOrName, onFinish, *args):
287288
output = alg.getOutputFromName(name)
288289
if output and output.setValue(value):
289290
continue
291+
print 'Error: Wrong parameter value %s for parameter %s.' % (value, name)
290292
QgsMessageLog.logMessage(Processing.tr('Error: Wrong parameter value {0} for parameter {1}.').format(value, name), Processing.tr("Processing"))
291293
ProcessingLog.addToLog(
292294
ProcessingLog.LOG_ERROR,
@@ -298,6 +300,7 @@ def runAlgorithm(algOrName, onFinish, *args):
298300
for param in alg.parameters:
299301
if param.name not in setParams:
300302
if not param.setValue(None):
303+
print ('Error: Missing parameter value for parameter %s.' % (param.name))
301304
QgsMessageLog.logMessage(Processing.tr('Error: Missing parameter value for parameter {0}.').format(param.name), Processing.tr("Processing"))
302305
ProcessingLog.addToLog(
303306
ProcessingLog.LOG_ERROR,
@@ -307,53 +310,66 @@ def runAlgorithm(algOrName, onFinish, *args):
307310
return
308311
else:
309312
if len(args) != alg.getVisibleParametersCount() + alg.getVisibleOutputsCount():
313+
print 'Error: Wrong number of parameters'
310314
QgsMessageLog.logMessage(Processing.tr('Error: Wrong number of parameters'), Processing.tr("Processing"))
311315
processing.alghelp(algOrName)
312316
return
313317
i = 0
314318
for param in alg.parameters:
315319
if not param.hidden:
316320
if not param.setValue(args[i]):
321+
print 'Error: Wrong parameter value: ' \
322+
+ unicode(args[i])
317323
QgsMessageLog.logMessage(Processing.tr('Error: Wrong parameter value: ') + unicode(args[i]), Processing.tr("Processing"))
318324
return
319325
i = i + 1
320326

321327
for output in alg.outputs:
322328
if not output.hidden:
323329
if not output.setValue(args[i]):
330+
print 'Error: Wrong output value: ' + unicode(args[i])
324331
QgsMessageLog.logMessage(Processing.tr('Error: Wrong output value: ') + unicode(args[i]), Processing.tr("Processing"))
325332
return
326333
i = i + 1
327334

328335
msg = alg._checkParameterValuesBeforeExecuting()
329336
if msg:
337+
print 'Unable to execute algorithm\n' + msg
330338
QgsMessageLog.logMessage(Processing.tr('Unable to execute algorithm\n{0}').format(msg), Processing.tr("Processing"))
331339
return
332340

333341
if not alg.checkInputCRS():
342+
print 'Warning: Not all input layers use the same CRS.\n' \
343+
+ 'This can cause unexpected results.'
334344
QgsMessageLog.logMessage(Processing.tr('Warning: Not all input layers use the same CRS.\nThis can cause unexpected results.'), Processing.tr("Processing"))
335345

336346
if iface is not None:
337347
# Don't set the wait cursor twice, because then when you
338348
# restore it, it will still be a wait cursor.
349+
overrideCursor = False
339350
cursor = QApplication.overrideCursor()
340351
if cursor is None or cursor == 0:
341352
QApplication.setOverrideCursor(QCursor(Qt.WaitCursor))
353+
overrideCursor = True
342354
elif cursor.shape() != Qt.WaitCursor:
343355
QApplication.setOverrideCursor(QCursor(Qt.WaitCursor))
356+
overrideCursor = True
344357

345358
progress = None
346-
if iface is not None:
347-
progress = MessageBarProgress(alg.name)
359+
if kwargs is not None and "progress" in kwargs.keys():
360+
progress = kwargs["progress"]
361+
elif iface is not None :
362+
progress = MessageBarProgress()
348363
ret = runalg(alg, progress)
349364
if ret:
350365
if onFinish is not None:
351366
onFinish(alg, progress)
352367
else:
353368
QgsMessageLog.logMessage(Processing.tr("There were errors executing the algorithm."), Processing.tr("Processing"))
354369

355-
if iface is not None:
370+
if iface is not None and overrideCursor:
356371
QApplication.restoreOverrideCursor()
372+
if isinstance(progress, MessageBarProgress):
357373
progress.close()
358374
return alg
359375

‎python/plugins/processing/tools/general.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -67,11 +67,11 @@ def alghelp(name):
6767
print 'Algorithm not found'
6868

6969

70-
def runalg(algOrName, *args):
71-
alg = Processing.runAlgorithm(algOrName, None, *args)
70+
def runalg(algOrName, *args, **kwargs):
71+
alg = Processing.runAlgorithm(algOrName, None, *args, **kwargs)
7272
if alg is not None:
7373
return alg.getOutputValuesAsDictionary()
7474

7575

76-
def runandload(name, *args):
77-
return Processing.runAlgorithm(name, handleAlgorithmResults, *args)
76+
def runandload(name, *args, **kwargs):
77+
return Processing.runAlgorithm(name, handleAlgorithmResults, *args, **kwargs)

0 commit comments

Comments
 (0)
Please sign in to comment.