26
26
__revision__ = '$Format:%H$'
27
27
28
28
import os
29
+ import re
30
+ from qgis .core import *
29
31
from PyQt .QtGui import QIcon
30
32
from processing .core .GeoAlgorithm import GeoAlgorithm
31
33
from processing .gui .Help2Html import getHtmlFromHelpFile
53
55
from processing .core .outputs import OutputDirectory
54
56
from processing .core .outputs import getOutputFromString
55
57
from processing .script .WrongScriptException import WrongScriptException
58
+ from processing .core .GeoAlgorithmExecutionException import GeoAlgorithmExecutionException
56
59
57
60
pluginPath = os .path .split (os .path .dirname (__file__ ))[0 ]
58
61
@@ -307,9 +310,6 @@ def processDescriptionParameterLine(self, line):
307
310
'Problem with line %d' , 'ScriptAlgorithm' ) % (self .descriptionFile or '' , line ))
308
311
309
312
def processAlgorithm (self , progress ):
310
-
311
- script = 'import processing\n '
312
-
313
313
ns = {}
314
314
ns ['progress' ] = progress
315
315
ns ['scriptDescriptionFile' ] = self .descriptionFile
@@ -320,7 +320,18 @@ def processAlgorithm(self, progress):
320
320
for out in self .outputs :
321
321
ns [out .name ] = out .value
322
322
323
+ variables = re .findall ("@[a-zA-Z0-9_]*" , self .script )
324
+ print variables
325
+ script = 'import processing\n '
323
326
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
+
324
335
exec ((script ), ns )
325
336
for out in self .outputs :
326
337
out .setValue (ns [out .name ])
0 commit comments