Skip to content

Commit 01f3808

Browse files
committedOct 5, 2016
[processing] improvements for scripts and R scripts
1 parent efd73a4 commit 01f3808

File tree

4 files changed

+65
-197
lines changed

4 files changed

+65
-197
lines changed
 

‎python/plugins/processing/algs/r/RAlgorithm.py

Lines changed: 10 additions & 118 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,8 @@
5656
from processing.core.outputs import OutputRaster
5757
from processing.core.outputs import OutputHTML
5858
from processing.core.outputs import OutputFile
59+
from processing.core.parameters import getParameterFromString
60+
from processing.core.outputs import getOutputFromString
5961
from processing.tools import dataobjects
6062
from processing.tools.system import isWindows
6163
from processing.script.WrongScriptException import WrongScriptException
@@ -148,7 +150,6 @@ def createDescriptiveName(self, s):
148150

149151
def processParameterLine(self, line):
150152
param = None
151-
out = None
152153
line = line.replace('#', '')
153154
if line.lower().strip().startswith('showplots'):
154155
self.showPlots = True
@@ -169,133 +170,24 @@ def processParameterLine(self, line):
169170
self.name = self.i18n_name = tokens[0]
170171
return
171172

172-
if tokens[1].lower().strip().startswith('output'):
173-
outToken = tokens[1].strip()[len('output') + 1:]
174-
out = self.processOutputParameterToken(outToken)
175-
176-
elif tokens[1].lower().strip().startswith('optional'):
177-
optToken = tokens[1].strip()[len('optional') + 1:]
178-
param = self.processInputParameterToken(optToken, tokens[0])
179-
if param:
180-
param.optional = True
181-
182-
else:
183-
param = self.processInputParameterToken(tokens[1], tokens[0])
173+
out = getOutputFromString(line)
174+
if out is None:
175+
param = getParameterFromString(line)
184176

185177
if param is not None:
186178
self.addParameter(param)
187179
elif out is not None:
188180
out.name = tokens[0]
189-
out.description = tokens[0]
181+
out.description = desc
190182
self.addOutput(out)
191183
else:
192184
raise WrongScriptException(
193-
self.tr('Could not load R script: %s.\n Problem with line %s' % (self.descriptionFile, line)))
194-
195-
def processInputParameterToken(self, token, name):
196-
param = None
197-
198-
desc = self.createDescriptiveName(name)
199-
200-
if token.lower().strip().startswith('raster'):
201-
param = ParameterRaster(name, desc, False)
202-
elif token.lower().strip() == 'vector':
203-
param = ParameterVector(name, desc,
204-
[dataobjects.TYPE_VECTOR_ANY])
205-
elif token.lower().strip() == 'vector point':
206-
param = ParameterVector(name, desc,
207-
[dataobjects.TYPE_VECTOR_POINT])
208-
elif token.lower().strip() == 'vector line':
209-
param = ParameterVector(name, desc,
210-
[dataobjects.TYPE_VECTOR_LINE])
211-
elif token.lower().strip() == 'vector polygon':
212-
param = ParameterVector(name, desc,
213-
[dataobjects.TYPE_VECTOR_POLYGON])
214-
elif token.lower().strip() == 'table':
215-
param = ParameterTable(name, desc, False)
216-
elif token.lower().strip().startswith('multiple raster'):
217-
param = ParameterMultipleInput(name, desc,
218-
dataobjects.TYPE_RASTER)
219-
param.optional = False
220-
elif token.lower().strip() == 'multiple vector':
221-
param = ParameterMultipleInput(name, desc,
222-
dataobjects.TYPE_VECTOR_ANY)
223-
param.optional = False
224-
elif token.lower().strip().startswith('selection'):
225-
options = token.strip()[len('selection'):].split(';')
226-
param = ParameterSelection(name, desc, options)
227-
elif token.lower().strip().startswith('boolean'):
228-
default = token.strip()[len('boolean') + 1:]
229-
if default:
230-
param = ParameterBoolean(name, desc, default)
231-
else:
232-
param = ParameterBoolean(name, desc)
233-
elif token.lower().strip().startswith('number'):
234-
default = token.strip()[len('number') + 1:]
235-
if default:
236-
param = ParameterNumber(name, desc, default=default)
237-
else:
238-
param = ParameterNumber(name, desc)
239-
elif token.lower().strip().startswith('field'):
240-
field = token.strip()[len('field') + 1:]
241-
found = False
242-
for p in self.parameters:
243-
if p.name == field:
244-
found = True
245-
break
246-
if found:
247-
param = ParameterTableField(name, desc, field)
248-
elif token.lower().strip().startswith('multiple field'):
249-
field = token.strip()[len('multiple field') + 1:]
250-
found = False
251-
for p in self.parameters:
252-
if p.name == field:
253-
found = True
254-
break
255-
if found:
256-
param = ParameterTableMultipleField(token, desc, field)
257-
elif token.lower().strip() == 'extent':
258-
param = ParameterExtent(name, desc)
259-
elif token.lower().strip() == 'point':
260-
param = ParameterPoint(name, desc)
261-
elif token.lower().strip() == 'file':
262-
param = ParameterFile(name, desc, False)
263-
elif token.lower().strip() == 'folder':
264-
param = ParameterFile(name, desc, True)
265-
elif token.lower().strip().startswith('string'):
266-
default = token.strip()[len('string') + 1:]
267-
if default:
268-
param = ParameterString(name, desc, default)
269-
else:
270-
param = ParameterString(name, desc)
271-
elif token.lower().strip().startswith('longstring'):
272-
default = token.strip()[len('longstring') + 1:]
273-
if default:
274-
param = ParameterString(name, desc, default, multiline=True)
275-
else:
276-
param = ParameterString(name, desc, multiline=True)
277-
elif token.lower().strip() == 'crs':
278-
default = token.strip()[len('crs') + 1:]
279-
if default:
280-
param = ParameterCrs(name, desc, default)
281-
else:
282-
param = ParameterCrs(name, desc)
185+
self.tr('Could not load script: %s.\n'
186+
'Problem with line "%s"', 'ScriptAlgorithm') % (self.descriptionFile or '', line))
283187

284-
return param
285-
286-
def processOutputParameterToken(self, token):
287-
out = None
288-
289-
if token.lower().strip().startswith('raster'):
290-
out = OutputRaster()
291-
elif token.lower().strip().startswith('vector'):
292-
out = OutputVector()
293-
elif token.lower().strip().startswith('table'):
294-
out = OutputTable()
295-
elif token.lower().strip().startswith('file'):
296-
out = OutputFile()
188+
raise WrongScriptException(
189+
self.tr('Could not load R script: %s.\n Problem with line %s' % (self.descriptionFile, line)))
297190

298-
return out
299191

300192
def processAlgorithm(self, progress):
301193
if isWindows():

‎python/plugins/processing/core/outputs.py

Lines changed: 46 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -39,13 +39,6 @@
3939
from processing.tools import dataobjects
4040

4141

42-
def getOutputFromString(s):
43-
tokens = s.split("|")
44-
params = [t if str(t) != "None" else None for t in tokens[1:]]
45-
clazz = getattr(sys.modules[__name__], tokens[0])
46-
return clazz(*params)
47-
48-
4942
class Output(object):
5043

5144
def __init__(self, name='', description='', hidden=False):
@@ -351,3 +344,49 @@ def getVectorWriter(self, fields, geomType, crs, options=None):
351344

352345
def dataType(self):
353346
return dataobjects.vectorDataType(self)
347+
348+
349+
350+
def getOutputFromString(s):
351+
try:
352+
if "|" in s:
353+
tokens = s.split("|")
354+
params = [t if unicode(t) != "None" else None for t in tokens[1:]]
355+
clazz = getattr(sys.modules[__name__], tokens[0])
356+
return clazz(*params)
357+
else:
358+
tokens = s.split("=")
359+
token = tokens[1].strip()[len('output') + 1:]
360+
out = None
361+
362+
if token.lower().strip().startswith('raster'):
363+
out = OutputRaster()
364+
elif token.lower().strip() == 'vector':
365+
out = OutputVector()
366+
elif token.lower().strip() == 'vector point':
367+
out = OutputVector(datatype=[dataobjects.TYPE_VECTOR_POINT])
368+
elif token.lower().strip() == 'vector line':
369+
out = OutputVector(datatype=[OutputVector.TYPE_VECTOR_LINE])
370+
elif token.lower().strip() == 'vector polygon':
371+
out = OutputVector(datatype=[OutputVector.TYPE_VECTOR_POLYGON])
372+
elif token.lower().strip().startswith('table'):
373+
out = OutputTable()
374+
elif token.lower().strip().startswith('html'):
375+
out = OutputHTML()
376+
elif token.lower().strip().startswith('file'):
377+
out = OutputFile()
378+
subtokens = token.split(' ')
379+
if len(subtokens) > 2:
380+
out.ext = subtokens[2]
381+
elif token.lower().strip().startswith('directory'):
382+
out = OutputDirectory()
383+
elif token.lower().strip().startswith('number'):
384+
out = OutputNumber()
385+
elif token.lower().strip().startswith('string'):
386+
out = OutputString()
387+
elif token.lower().strip().startswith('extent'):
388+
out = OutputExtent()
389+
390+
return out
391+
except:
392+
return None

‎python/plugins/processing/core/parameters.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1360,13 +1360,17 @@ def setValue(self, value):
13601360
paramClasses = [c for c in sys.modules[__name__].__dict__.values() if isclass(c) and issubclass(c, Parameter)]
13611361

13621362
def getParameterFromString(s):
1363-
print s
13641363
# Try the parameter definitions used in description files
13651364
if '|' in s:
1365+
isAdvanced = False
1366+
if s.startswith("*"):
1367+
s = s[1:]
1368+
isAdvanced = True
13661369
tokens = s.split("|")
13671370
params = [t if unicode(t) != unicode(None) else None for t in tokens[1:]]
13681371
clazz = getattr(sys.modules[__name__], tokens[0])
1369-
return clazz(*params)
1372+
param = clazz(*params)
1373+
param.isAdvanced = isAdvanced
13701374
else: # try script syntax
13711375
for paramClass in paramClasses:
13721376
try:

‎python/plugins/processing/script/ScriptAlgorithm.py

Lines changed: 3 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -33,21 +33,11 @@
3333
from qgis.PyQt.QtGui import QIcon
3434
from processing.core.GeoAlgorithm import GeoAlgorithm
3535
from processing.gui.Help2Html import getHtmlFromHelpFile
36-
from processing.core.parameters import getParameterFromString, paramClasses
37-
from processing.core.outputs import OutputTable
38-
from processing.core.outputs import OutputVector
39-
from processing.core.outputs import OutputRaster
40-
from processing.core.outputs import OutputNumber
41-
from processing.core.outputs import OutputString
42-
from processing.core.outputs import OutputHTML
43-
from processing.core.outputs import OutputFile
44-
from processing.core.outputs import OutputDirectory
36+
from processing.core.parameters import getParameterFromString
4537
from processing.core.outputs import getOutputFromString
4638
from processing.core.ProcessingLog import ProcessingLog
4739
from processing.script.WrongScriptException import WrongScriptException
4840

49-
from processing.tools import dataobjects
50-
5141
pluginPath = os.path.split(os.path.dirname(__file__))[0]
5242

5343

@@ -128,7 +118,6 @@ def createDescriptiveName(self, s):
128118

129119
def processParameterLine(self, line):
130120
param = None
131-
out = None
132121
line = line.replace('#', '')
133122

134123
if line == "nomodeler":
@@ -146,10 +135,8 @@ def processParameterLine(self, line):
146135
self.name = self.i18n_name = tokens[0]
147136
return
148137

149-
if tokens[1].lower().strip().startswith('output'):
150-
outToken = tokens[1].strip()[len('output') + 1:]
151-
out = self.processOutputParameterToken(outToken)
152-
else:
138+
out = getOutputFromString(line)
139+
if out is None:
153140
param = getParameterFromString(line)
154141

155142
if param is not None:
@@ -163,60 +150,6 @@ def processParameterLine(self, line):
163150
self.tr('Could not load script: %s.\n'
164151
'Problem with line "%s"', 'ScriptAlgorithm') % (self.descriptionFile or '', line))
165152

166-
def processInputParameterLine(self, line):
167-
for paramClass in paramClasses:
168-
param = paramClass.fromScriptCode(line)
169-
if param is not None:
170-
return param
171-
172-
def processOutputParameterToken(self, token):
173-
out = None
174-
175-
if token.lower().strip().startswith('raster'):
176-
out = OutputRaster()
177-
elif token.lower().strip() == 'vector':
178-
out = OutputVector()
179-
elif token.lower().strip() == 'vector point':
180-
out = OutputVector(datatype=[dataobjects.TYPE_VECTOR_POINT])
181-
elif token.lower().strip() == 'vector line':
182-
out = OutputVector(datatype=[OutputVector.TYPE_VECTOR_LINE])
183-
elif token.lower().strip() == 'vector polygon':
184-
out = OutputVector(datatype=[OutputVector.TYPE_VECTOR_POLYGON])
185-
elif token.lower().strip().startswith('table'):
186-
out = OutputTable()
187-
elif token.lower().strip().startswith('html'):
188-
out = OutputHTML()
189-
elif token.lower().strip().startswith('file'):
190-
out = OutputFile()
191-
subtokens = token.split(' ')
192-
if len(subtokens) > 2:
193-
out.ext = subtokens[2]
194-
elif token.lower().strip().startswith('directory'):
195-
out = OutputDirectory()
196-
elif token.lower().strip().startswith('number'):
197-
out = OutputNumber()
198-
elif token.lower().strip().startswith('string'):
199-
out = OutputString()
200-
elif token.lower().strip().startswith('extent'):
201-
out = OutputExtent()
202-
203-
return out
204-
205-
def processDescriptionParameterLine(self, line):
206-
try:
207-
if line.startswith('Parameter'):
208-
self.addParameter(getParameterFromString(line))
209-
elif line.startswith('*Parameter'):
210-
param = getParameterFromString(line[1:])
211-
param.isAdvanced = True
212-
self.addParameter(param)
213-
else:
214-
self.addOutput(getOutputFromString(line))
215-
except Exception:
216-
raise WrongScriptException(
217-
self.tr('Could not load script: %s.\n'
218-
'Problem with line %d', 'ScriptAlgorithm') % (self.descriptionFile or '', line))
219-
220153
def processAlgorithm(self, progress):
221154
ns = {}
222155
ns['progress'] = progress

0 commit comments

Comments
 (0)
Please sign in to comment.