Skip to content

Commit 69c991e

Browse files
committedAug 13, 2017
Restore ability to create processing tests from history panel
1 parent 5177972 commit 69c991e

File tree

1 file changed

+51
-60
lines changed

1 file changed

+51
-60
lines changed
 

‎python/plugins/processing/gui/TestTools.py

Lines changed: 51 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -30,38 +30,31 @@
3030
import re
3131
import yaml
3232
import hashlib
33+
import ast
3334

3435
from osgeo import gdal
3536
from osgeo.gdalconst import GA_ReadOnly
3637

3738
from numpy import nan_to_num
3839

3940
from qgis.core import (QgsApplication,
40-
QgsProcessingParameterDefinition)
41+
QgsProcessing,
42+
QgsProcessingParameterDefinition,
43+
QgsProcessingParameterBoolean,
44+
QgsProcessingParameterNumber,
45+
QgsProcessingParameterFile,
46+
QgsProcessingParameterString,
47+
QgsProcessingParameterVectorLayer,
48+
QgsProcessingParameterFeatureSource,
49+
QgsProcessingParameterRasterLayer,
50+
QgsProcessingParameterMultipleLayers,
51+
QgsProcessingParameterRasterDestination,
52+
QgsProcessingParameterFeatureSink,
53+
QgsProcessingParameterVectorDestination,
54+
QgsProcessingParameterFileDestination)
4155
from qgis.PyQt.QtCore import QCoreApplication, QMetaObject
4256
from qgis.PyQt.QtWidgets import QDialog, QVBoxLayout, QTextEdit, QMessageBox
4357

44-
from processing.core.Processing import Processing
45-
from processing.core.outputs import (
46-
OutputNumber,
47-
OutputString,
48-
OutputRaster,
49-
OutputVector,
50-
OutputHTML,
51-
OutputFile
52-
)
53-
54-
from processing.core.parameters import (
55-
ParameterRaster,
56-
ParameterVector,
57-
ParameterMultipleInput,
58-
ParameterFile,
59-
ParameterString,
60-
ParameterNumber,
61-
ParameterBoolean,
62-
ParameterTable
63-
)
64-
6558

6659
def extractSchemaPath(filepath):
6760
"""
@@ -132,15 +125,24 @@ def parseParameters(command):
132125
pos = m.end(0)
133126

134127

128+
def splitAlgIdAndParameters(command):
129+
"""
130+
Extracts the algorithm ID and input parameter list from a processing runalg command
131+
"""
132+
exp = re.compile(r"""['"](.*?)['"]\s*,\s*(.*)""")
133+
m = exp.search(command[len('processing.run('):-1])
134+
return m.group(1), ast.literal_eval(m.group(2))
135+
136+
135137
def createTest(text):
136138
definition = {}
137139

138-
tokens = list(parseParameters(text[len('processing.run('):-1]))
139-
cmdname = tokens[0]
140-
alg = QgsApplication.processingRegistry().createAlgorithmById(cmdname)
140+
alg_id, parameters = splitAlgIdAndParameters(text)
141+
142+
alg = QgsApplication.processingRegistry().createAlgorithmById(alg_id)
141143

142-
definition['name'] = 'Test ({})'.format(cmdname)
143-
definition['algorithm'] = cmdname
144+
definition['name'] = 'Test ({})'.format(alg_id)
145+
definition['algorithm'] = alg_id
144146

145147
params = {}
146148
results = {}
@@ -151,12 +153,12 @@ def createTest(text):
151153
continue
152154

153155
i += 1
154-
token = tokens[i]
156+
token = parameters[param.name()]
155157
# Handle empty parameters that are optionals
156158
if param.flags() & QgsProcessingParameterDefinition.FlagOptional and token is None:
157159
continue
158160

159-
if isinstance(param, ParameterVector):
161+
if isinstance(param, (QgsProcessingParameterVectorLayer, QgsProcessingParameterFeatureSource)):
160162
schema, filepath = extractSchemaPath(token)
161163
p = {
162164
'type': 'vector',
@@ -166,7 +168,7 @@ def createTest(text):
166168
p['location'] = '[The source data is not in the testdata directory. Please use data in the processing/tests/testdata folder.]'
167169

168170
params[param.name()] = p
169-
elif isinstance(param, ParameterRaster):
171+
elif isinstance(param, QgsProcessingParameterRasterLayer):
170172
schema, filepath = extractSchemaPath(token)
171173
p = {
172174
'type': 'raster',
@@ -176,27 +178,18 @@ def createTest(text):
176178
p['location'] = '[The source data is not in the testdata directory. Please use data in the processing/tests/testdata folder.]'
177179

178180
params[param.name()] = p
179-
elif isinstance(param, ParameterTable):
180-
schema, filepath = extractSchemaPath(token)
181-
p = {
182-
'type': 'table',
183-
'name': filepath
184-
}
185-
if not schema:
186-
p['location'] = '[The source data is not in the testdata directory. Please use data in the processing/tests/testdata folder.]'
187-
188-
params[param.name()] = p
189-
elif isinstance(param, ParameterMultipleInput):
181+
elif isinstance(param, QgsProcessingParameterMultipleLayers):
190182
multiparams = token.split(';')
191183
newparam = []
192184

193185
# Handle datatype detection
194-
dataType = param.dataType()
195-
if dataType in ['points', 'lines', 'polygons', 'any vectors']:
186+
dataType = param.layerType()
187+
if dataType in [QgsProcessing.TypeVectorAny, QgsProcessing.TypeVectorPoint, QgsProcessing.TypeVectorLine, QgsProcessing.TypeVectorPolygon, QgsProcessing.TypeTable]:
196188
dataType = 'vector'
197189
else:
198190
dataType = 'raster'
199191

192+
schema = None
200193
for mp in multiparams:
201194
schema, filepath = extractSchemaPath(mp)
202195
newparam.append({
@@ -211,7 +204,7 @@ def createTest(text):
211204
p['location'] = '[The source data is not in the testdata directory. Please use data in the processing/tests/testdata folder.]'
212205

213206
params[param.name()] = p
214-
elif isinstance(param, ParameterFile):
207+
elif isinstance(param, QgsProcessingParameterFile):
215208
schema, filepath = extractSchemaPath(token)
216209
p = {
217210
'type': 'file',
@@ -221,12 +214,12 @@ def createTest(text):
221214
p['location'] = '[The source data is not in the testdata directory. Please use data in the processing/tests/testdata folder.]'
222215

223216
params[param.name()] = p
224-
elif isinstance(param, ParameterString):
217+
elif isinstance(param, QgsProcessingParameterString):
225218
params[param.name()] = token
226-
elif isinstance(param, ParameterBoolean):
219+
elif isinstance(param, QgsProcessingParameterBoolean):
227220
params[param.name()] = token
228-
elif isinstance(param, ParameterNumber):
229-
if param.isInteger:
221+
elif isinstance(param, QgsProcessingParameterNumber):
222+
if param.dataType() == QgsProcessingParameterNumber.Integer:
230223
params[param.name()] = int(token)
231224
else:
232225
params[param.name()] = float(token)
@@ -235,16 +228,14 @@ def createTest(text):
235228
token = token[1:]
236229
if token[-1] == '"':
237230
token = token[:-1]
238-
params[param.name] = token
231+
params[param.name()] = token
239232

240233
definition['params'] = params
241234

242235
for i, out in enumerate([out for out in alg.destinationParameterDefinitions() if not out.flags() & QgsProcessingParameterDefinition.FlagHidden]):
243-
token = tokens[i - len(alg.destinationParameterDefinitions())]
236+
token = parameters[out.name()]
244237

245-
if isinstance(out, (OutputNumber, OutputString)):
246-
results[out.name] = str(out)
247-
elif isinstance(out, OutputRaster):
238+
if isinstance(out, QgsProcessingParameterRasterDestination):
248239
if token is None:
249240
QMessageBox.warning(None,
250241
tr('Error'),
@@ -258,26 +249,26 @@ def createTest(text):
258249
dataArray = nan_to_num(dataset.ReadAsArray(0))
259250
strhash = hashlib.sha224(dataArray.data).hexdigest()
260251

261-
results[out.name] = {
252+
results[out.name()] = {
262253
'type': 'rasterhash',
263254
'hash': strhash
264255
}
265-
elif isinstance(out, OutputVector):
256+
elif isinstance(out, (QgsProcessingParameterVectorDestination, QgsProcessingParameterFeatureSink)):
266257
schema, filepath = extractSchemaPath(token)
267-
results[out.name] = {
258+
results[out.name()] = {
268259
'type': 'vector',
269260
'name': filepath
270261
}
271262
if not schema:
272-
results[out.name]['location'] = '[The expected result data is not in the testdata directory. Please write it to processing/tests/testdata/expected. Prefer gml files.]'
273-
elif isinstance(out, OutputHTML) or isinstance(out, OutputFile):
263+
results[out.name()]['location'] = '[The expected result data is not in the testdata directory. Please write it to processing/tests/testdata/expected. Prefer gml files.]'
264+
elif isinstance(out, QgsProcessingParameterFileDestination):
274265
schema, filepath = extractSchemaPath(token)
275-
results[out.name] = {
266+
results[out.name()] = {
276267
'type': 'file',
277268
'name': filepath
278269
}
279270
if not schema:
280-
results[out.name]['location'] = '[The expected result file is not in the testdata directory. Please redirect the output to processing/tests/testdata/expected.]'
271+
results[out.name()]['location'] = '[The expected result file is not in the testdata directory. Please redirect the output to processing/tests/testdata/expected.]'
281272

282273
definition['results'] = results
283274
dlg = ShowTestDialog(yaml.dump([definition], default_flow_style=False))

0 commit comments

Comments
 (0)
Please sign in to comment.