Skip to content
Permalink
Browse files

Merge pull request #3900 from m-kuhn/scriptParams

[processing] Fix some script parameter export/import issues
  • Loading branch information
alexbruy committed Dec 22, 2016
2 parents ffd67f1 + 3fba3ac commit 94cbc600103e02d4a3406a57bdafaac18b7a05c3
Showing with 52 additions and 9 deletions.
  1. +19 −9 python/plugins/processing/core/parameters.py
  2. +33 −0 python/plugins/processing/tests/ParametersTest.py
@@ -229,7 +229,9 @@ def fromScriptCode(self, line):
isOptional, name, definition = _splitParameterOptions(line)
if definition.startswith("boolean"):
descName = _createDescriptiveName(name)
default = definition.strip()[len('boolean') + 1:]
default = definition.strip()[len('boolean') + 1:] or None
if default == 'None':
default = None
if default:
param = ParameterBoolean(name, descName, default)
else:
@@ -292,6 +294,8 @@ def fromScriptCode(self, line):
if definition.startswith("crs"):
descName = _createDescriptiveName(name)
default = definition.strip()[len('crs') + 1:]
if default == 'None':
default = None
if default:
return ParameterCrs(name, descName, default, isOptional)
else:
@@ -574,7 +578,7 @@ def fromScriptCode(self, line):
if definition.startswith("fixedtable"):
descName = _createDescriptiveName(name)
default = definition.strip()[len('fixedtable') + 1:] or None
return ParameterFixedTable(name, descName, default, isOptional)
return ParameterFixedTable(name, descName, optional=isOptional)


class ParameterMultipleInput(ParameterDataObject):
@@ -1186,15 +1190,19 @@ def getAsScriptCode(self):
param_type = ''
if self.optional:
param_type += 'optional '
param_type += 'string'
return '##' + self.name + '=' + param_type + self.default
param_type += 'string '
return '##' + self.name + '=' + param_type + repr(self.default)

@classmethod
def fromScriptCode(self, line):
isOptional, name, definition = _splitParameterOptions(line)
descName = _createDescriptiveName(name)
if definition.lower().strip().startswith('string'):
default = definition.strip()[len('string') + 1:]
default = definition.strip()[len('string') + 1:] or None
if default == 'None':
default = None
elif default.startswith('"') or default.startswith('\''):
default = eval(default)
if default:
return ParameterString(name, descName, default, optional=isOptional)
else:
@@ -1255,15 +1263,17 @@ def getAsScriptCode(self):
param_type = ''
if self.optional:
param_type += 'optional '
param_type += 'expression'
return '##' + self.name + '=' + param_type + self.default
param_type += 'expression '
return '##' + self.name + '=' + param_type + str(self.default)

@classmethod
def fromScriptCode(self, line):
isOptional, name, definition = _splitParameterOptions(line)
if definition.lower().strip().startswith('expression'):
descName = _createDescriptiveName(name)
default = definition.strip()[len('expression') + 1:]
default = definition.strip()[len('expression') + 1:] or None
if default == 'None':
default = None
if default:
return ParameterExpression(name, descName, default, optional=isOptional)
else:
@@ -1413,7 +1423,7 @@ def getAsScriptCode(self):
if self.optional:
param_type += 'optional '
param_type += 'field'
return '##' + self.name + '=' + param_type + self.parent
return '##' + self.name + '=' + param_type + str(self.parent)

@classmethod
def fromScriptCode(self, line):
@@ -25,6 +25,8 @@

__revision__ = '$Format:%H$'

import sys
from inspect import isclass
from qgis.testing import start_app, unittest

from processing.core.parameters import (Parameter,
@@ -66,6 +68,19 @@ def testGetValueAsCommandLineParameter(self):
parameter.setValue(123)
self.assertEqual(parameter.getValueAsCommandLineParameter(), '123')

def testScriptCode(self):
"""Simple check that default constructed object export/import correctly"""
paramClasses = [c for c in list(sys.modules[__name__].__dict__.values())
if isclass(c) and issubclass(c, Parameter) and c != Parameter]

for paramClass in paramClasses:
param = paramClass()
if hasattr(param, 'getAsScriptCode'):
code = param.getAsScriptCode()
importedParam = paramClass.fromScriptCode(code)
self.assertEquals(param.optional, importedParam.optional)
self.assertEquals(param.default, importedParam.default, param)


class ParameterBooleanTest(unittest.TestCase):

@@ -578,12 +593,29 @@ def testScriptCode(self):
code = parameter.getAsScriptCode()
result = getParameterFromString(code)
self.assertIsInstance(result, ParameterString)
self.assertEqual(result.default, parameter.default)

parameter.default = None
parameter.optional = True
code = parameter.getAsScriptCode()
result = getParameterFromString(code)
self.assertIsInstance(result, ParameterString)
self.assertTrue(result.optional)
self.assertEqual(result.default, parameter.default)

parameter.default = 'None'
code = parameter.getAsScriptCode()
result = getParameterFromString(code)
self.assertIsInstance(result, ParameterString)
self.assertTrue(result.optional)
self.assertEqual(result.default, parameter.default)

parameter.default = 'It\'s Mario'
code = parameter.getAsScriptCode()
result = getParameterFromString(code)
self.assertIsInstance(result, ParameterString)
self.assertTrue(result.optional)
self.assertEqual(result.default, parameter.default)


class ParameterExpressionTest(unittest.TestCase):
@@ -619,6 +651,7 @@ def testScriptCode(self):
result = getParameterFromString(code)
self.assertIsInstance(result, ParameterExpression)
self.assertTrue(result.optional)
self.assertEquals(result.default, parameter.default)


class ParameterTableFieldTest(unittest.TestCase):

0 comments on commit 94cbc60

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