Skip to content
Permalink
Browse files

[processing] added support for QGIS variables in python scripts

  • Loading branch information
volaya committed Apr 7, 2016
1 parent 3e4e08b commit 794ef725202825fa71d80d4733d5f554c4f8c4a9
Showing with 14 additions and 3 deletions.
  1. +14 −3 python/plugins/processing/script/ScriptAlgorithm.py
@@ -26,6 +26,8 @@
__revision__ = '$Format:%H$'

import os
import re
from qgis.core import *

This comment has been minimized.

Copy link
@luipir

luipir Apr 7, 2016

Contributor

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
@@ -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]

@@ -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
@@ -320,7 +320,18 @@ def processAlgorithm(self, progress):
for out in self.outputs:
ns[out.name] = 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

nyalldawson Apr 7, 2016

Collaborator

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

ie

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

nyalldawson Apr 7, 2016

Collaborator

@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 me@wheee.com

will attempt to be replaced by a variable.

This comment has been minimized.

Copy link
@volaya

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:
out.setValue(ns[out.name])

0 comments on commit 794ef72

Please sign in to comment.
You can’t perform that action at this time.