Skip to content

Commit 8a9cb05

Browse files
committed
[processing] better handling of variables in scripts
1 parent 5cc2dcd commit 8a9cb05

File tree

1 file changed

+9
-5
lines changed

1 file changed

+9
-5
lines changed

python/plugins/processing/script/ScriptAlgorithm.py

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@
5454
from processing.core.outputs import OutputFile
5555
from processing.core.outputs import OutputDirectory
5656
from processing.core.outputs import getOutputFromString
57+
from processing.core.ProcessingLog import ProcessingLog
5758
from processing.script.WrongScriptException import WrongScriptException
5859
from processing.core.GeoAlgorithmExecutionException import GeoAlgorithmExecutionException
5960

@@ -321,16 +322,19 @@ def processAlgorithm(self, progress):
321322
ns[out.name] = out.value
322323

323324
variables = re.findall("@[a-zA-Z0-9_]*", self.script)
324-
print variables
325325
script = 'import processing\n'
326326
script += self.script
327327

328-
scope = QgsExpressionContextUtils.projectScope()
328+
projectScope = QgsExpressionContextUtils.projectScope()
329+
globalScope = QgsExpressionContextUtils.globalScope()
329330
for var in variables:
330331
varname = var[1:]
331-
if not scope.hasVariable(varname):
332-
raise GeoAlgorithmExecutionException("Wrong variable: %s" % varname)
333-
script = script.replace(var, scope.variable(varname))
332+
if projectScope.hasVariable(varname):
333+
script = script.replace(var, projectScope.variable(varname))
334+
elif globalScope.hasVariable(varname):
335+
script = script.replace(var, globalScope.variable(varname))
336+
else:
337+
ProcessingLog.addToLog(ProcessingLog.LOG_WARNING, "Cannot find variable: %s" % varname)
334338

335339
exec((script), ns)
336340
for out in self.outputs:

0 commit comments

Comments
 (0)