Skip to content

Commit

Permalink
[processing] simplify variables handling in scripts
Browse files Browse the repository at this point in the history
  • Loading branch information
alexbruy committed May 13, 2016
1 parent 0c8f6f7 commit 555fe3d
Showing 1 changed file with 8 additions and 9 deletions.
17 changes: 8 additions & 9 deletions python/plugins/processing/script/ScriptAlgorithm.py
Expand Up @@ -27,7 +27,7 @@

import os
import re
from qgis.core import QgsExpressionContextUtils
from qgis.core import QgsExpressionContextUtils, QgsExpressionContext
from qgis.PyQt.QtGui import QIcon
from processing.core.GeoAlgorithm import GeoAlgorithm
from processing.gui.Help2Html import getHtmlFromHelpFile
Expand Down Expand Up @@ -321,20 +321,19 @@ def processAlgorithm(self, progress):
for out in self.outputs:
ns[out.name] = out.value

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

projectScope = QgsExpressionContextUtils.projectScope()
globalScope = QgsExpressionContextUtils.globalScope()
context = QgsExpressionContext()
context.appendScope(QgsExpressionContextUtils.globalScope())
context.appendScope(QgsExpressionContextUtils.projectScope())
for var in variables:
varname = var[1:]
if projectScope.hasVariable(varname):
script = script.replace(var, projectScope.variable(varname))
elif globalScope.hasVariable(varname):
script = script.replace(var, globalScope.variable(varname))
if context.hasVariable(varname):
script = script.replace(var, context.variable(varname))
else:
ProcessingLog.addToLog(ProcessingLog.LOG_WARNING, "Cannot find variable: %s" % varname)
ProcessingLog.addToLog(ProcessingLog.LOG_WARNING, 'Cannot find variable: %s' % varname)

exec((script), ns)
for out in self.outputs:
Expand Down

0 comments on commit 555fe3d

Please sign in to comment.