Skip to content


[processing] added support for QGIS variables in python scripts
Browse files Browse the repository at this point in the history
  • Loading branch information
volaya committed Apr 7, 2016
1 parent 3e4e08b commit 794ef72
Showing 1 changed file with 14 additions and 3 deletions.
17 changes: 14 additions & 3 deletions python/plugins/processing/script/
Expand Up @@ -26,6 +26,8 @@
__revision__ = '$Format:%H$'

import os
import re
from qgis.core import *

This comment has been minimized.

Copy link

luipir Apr 7, 2016


Hi victor,

can you please avoid import *
I can read you only added QgsExpressionContextUtils

from PyQt.QtGui import QIcon
from processing.core.GeoAlgorithm import GeoAlgorithm
from processing.gui.Help2Html import getHtmlFromHelpFile
Expand Down Expand Up @@ -53,6 +55,7 @@
from processing.core.outputs import OutputDirectory
from processing.core.outputs import getOutputFromString
from processing.script.WrongScriptException import WrongScriptException
from processing.core.GeoAlgorithmExecutionException import GeoAlgorithmExecutionException

pluginPath = os.path.split(os.path.dirname(__file__))[0]

Expand Down Expand Up @@ -307,9 +310,6 @@ def processDescriptionParameterLine(self, line):
'Problem with line %d', 'ScriptAlgorithm') % (self.descriptionFile or '', line))

def processAlgorithm(self, progress):

script = 'import processing\n'

ns = {}
ns['progress'] = progress
ns['scriptDescriptionFile'] = self.descriptionFile
Expand All @@ -320,7 +320,18 @@ def processAlgorithm(self, progress):
for out in self.outputs:
ns[] = out.value

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

scope = QgsExpressionContextUtils.projectScope()

This comment has been minimized.

Copy link

nyalldawson Apr 7, 2016


Why only the project scope? The global scope should also be used here.


context = QgsExpressionContext()
context.appendScope(  QgsExpressionContextUtils.globalScope() )
context.appendScope( QgsExpressionContextUtils.projectScope() )

This way global variables will correctly get overridden by any in the project scope.

for var in variables:
varname = var[1:]
if not scope.hasVariable(varname):
raise GeoAlgorithmExecutionException("Wrong variable: %s" % varname)
script = script.replace(var, scope.variable(varname))

This comment has been minimized.

Copy link

nyalldawson Apr 7, 2016


@volaya unless i'm reading this wrong, it looks to me like this change will auto replace every @.... instance in a script with a variable or throw an error.

Won't that break a lot of things? Even simple things like a comment

# Copyright

will attempt to be replaced by a variable.

This comment has been minimized.

Copy link

volaya Apr 26, 2016

Author Contributor

@nyalldawson see 8a9cb05

That should avoid the problems you mention since now it just writes a warning when a potential variable seems to be wrong

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

0 comments on commit 794ef72

Please sign in to comment.