Skip to content

Commit 794ef72

Browse files
committed
[processing] added support for QGIS variables in python scripts
1 parent 3e4e08b commit 794ef72

File tree

1 file changed

+14
-3
lines changed

1 file changed

+14
-3
lines changed

python/plugins/processing/script/ScriptAlgorithm.py

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@
2626
__revision__ = '$Format:%H$'
2727

2828
import os
29+
import re
30+
from qgis.core import *
2931
from PyQt.QtGui import QIcon
3032
from processing.core.GeoAlgorithm import GeoAlgorithm
3133
from processing.gui.Help2Html import getHtmlFromHelpFile
@@ -53,6 +55,7 @@
5355
from processing.core.outputs import OutputDirectory
5456
from processing.core.outputs import getOutputFromString
5557
from processing.script.WrongScriptException import WrongScriptException
58+
from processing.core.GeoAlgorithmExecutionException import GeoAlgorithmExecutionException
5659

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

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

309312
def processAlgorithm(self, progress):
310-
311-
script = 'import processing\n'
312-
313313
ns = {}
314314
ns['progress'] = progress
315315
ns['scriptDescriptionFile'] = self.descriptionFile
@@ -320,7 +320,18 @@ def processAlgorithm(self, progress):
320320
for out in self.outputs:
321321
ns[out.name] = out.value
322322

323+
variables = re.findall("@[a-zA-Z0-9_]*", self.script)
324+
print variables
325+
script = 'import processing\n'
323326
script += self.script
327+
328+
scope = QgsExpressionContextUtils.projectScope()
329+
for var in variables:
330+
varname = var[1:]
331+
if not scope.hasVariable(varname):
332+
raise GeoAlgorithmExecutionException("Wrong variable: %s" % varname)
333+
script = script.replace(var, scope.variable(varname))
334+
324335
exec((script), ns)
325336
for out in self.outputs:
326337
out.setValue(ns[out.name])

0 commit comments

Comments
 (0)