Skip to content
Permalink
Browse files

[processing] Fix some script parameter export/import issues

  • Loading branch information
m-kuhn committed Dec 22, 2016
1 parent ffd67f1 commit 55e3ea033aea3dab1212def7761d8b1efe6e101f
Showing with 33 additions and 9 deletions.
  1. +17 −9 python/plugins/processing/core/parameters.py
  2. +16 −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,17 @@ 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 + str(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
if default:
return ParameterString(name, descName, default, optional=isOptional)
else:
@@ -1255,15 +1261,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 +1421,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):

@@ -619,6 +634,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 55e3ea0

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