Skip to content

Commit

Permalink
[processing] Fix some script parameter export/import issues
Browse files Browse the repository at this point in the history
  • Loading branch information
m-kuhn committed Dec 22, 2016
1 parent ffd67f1 commit 55e3ea0
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 9 deletions.
26 changes: 17 additions & 9 deletions python/plugins/processing/core/parameters.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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):
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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):
Expand Down
16 changes: 16 additions & 0 deletions python/plugins/processing/tests/ParametersTest.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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):

Expand Down Expand Up @@ -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):
Expand Down

0 comments on commit 55e3ea0

Please sign in to comment.