Skip to content

Commit

Permalink
Restore ability to run script algorithms
Browse files Browse the repository at this point in the history
  • Loading branch information
nyalldawson committed Jun 26, 2017
1 parent df329bc commit 3bf9ea3
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 15 deletions.
8 changes: 8 additions & 0 deletions python/plugins/processing/core/parameters.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
QgsProject,
QgsRectangle,
QgsVectorFileWriter,
QgsProcessingParameters,
QgsProcessingParameterDefinition)

from processing.tools.vector import resolveFieldIndex
Expand Down Expand Up @@ -602,6 +603,13 @@ def getParameterFromString(s):
except:
return None
else: # try script syntax

# try native method
param = QgsProcessingParameters.parameterFromScriptCode(s)
if param:
return param

# try Python duck-typed method
for paramClass in paramClasses:
try:
param = paramClass.fromScriptCode(s)
Expand Down
38 changes: 23 additions & 15 deletions python/plugins/processing/script/ScriptAlgorithm.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ def __init__(self, descriptionFile, script=None):
self._name = ''
self._display_name = ''
self._group = ''
self._flags = 0
self._flags = None

self.script = script
self.allowEdit = True
Expand All @@ -88,7 +88,10 @@ def group(self):
return self._group

def flags(self):
return self._flags
if self._flags is not None:
return QgsProcessingAlgorithm.Flags(self._flags)
else:
return QgsProcessingAlgorithm.flags(self)

def svgIconPath(self):
return QgsApplication.iconPath("processingScript.svg")
Expand All @@ -98,6 +101,7 @@ def defineCharacteristicsFromFile(self):
self.script = ''
filename = os.path.basename(self.descriptionFile)
self._name = filename[:filename.rfind('.')].replace('_', ' ')
self._display_name = self._name
self._group = self.tr('User scripts', 'ScriptAlgorithm')
with open(self.descriptionFile) as lines:
line = lines.readline()
Expand Down Expand Up @@ -163,8 +167,8 @@ def processParameterLine(self, line):
if param is not None:
self.addParameter(param)
elif out is not None:
out.name = tokens[0]
out.description = desc
out.setName(tokens[0])
out.setDescription(desc)
self.addOutput(out)
else:
raise WrongScriptException(
Expand All @@ -173,33 +177,37 @@ def processParameterLine(self, line):

def processAlgorithm(self, parameters, context, feedback):
ns = {}
ns['feedback'] = feedback

ns['scriptDescriptionFile'] = self.descriptionFile
ns['context'] = context

for param in self.parameterDefinitions():
ns[param.name] = parameters[param.name()]
ns[param.name()] = parameters[param.name()]

for out in self.outputs:
ns[out.name] = out.value
ns['self'] = self
ns['parameters'] = parameters
ns['feedback'] = feedback
ns['context'] = context

# for out in self.outputDefinitions():
# ns[out.name()] = out.value

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

context = QgsExpressionContext()
context.appendScope(QgsExpressionContextUtils.globalScope())
context.appendScope(QgsExpressionContextUtils.projectScope(QgsProject.instance()))
context = self.createExpressionContext(parameters, context)
for var in variables:
varname = var[1:]
if context.hasVariable(varname):
script = script.replace(var, context.variable(varname))
else:
# messy - it's probably NOT a variable, and instead an email address or some other string containing '@'
QgsMessageLog.logMessage(self.tr('Cannot find variable: {0}').format(varname), self.tr('Processing'), QgsMessageLog.WARNING)

exec((script), ns)
for out in self.outputs:
out.setValue(ns[out.name])
results = {}
for out in self.outputDefinitions():
results[out.name()] = ns[out.name()]
return results

def helpUrl(self):
if self.descriptionFile is None:
Expand Down

0 comments on commit 3bf9ea3

Please sign in to comment.