Skip to content

Commit

Permalink
[processing] moved script syntax from script algorithm class to param…
Browse files Browse the repository at this point in the history
…eters classes
  • Loading branch information
volaya committed Oct 5, 2016
1 parent a69b358 commit 8cc9a50
Show file tree
Hide file tree
Showing 2 changed files with 206 additions and 144 deletions.
211 changes: 200 additions & 11 deletions python/plugins/processing/core/parameters.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,26 +30,31 @@

import sys
import os
import inspect

from qgis.PyQt.QtCore import QCoreApplication
from qgis.core import QgsRasterLayer, QgsVectorLayer
from processing.tools.vector import resolveFieldIndex, features
from processing.tools.system import isWindows
from processing.tools import dataobjects


def getParameterFromString(s):
tokens = s.split("|")
params = [t if str(t) != str(None) else None for t in tokens[1:]]
clazz = getattr(sys.modules[__name__], tokens[0])
return clazz(*params)


def parseBool(s):
if s is None or s == str(None).lower():
return None
return str(s).lower() == str(True).lower()

def _splitParameterOptions(line):
tokens = line.split('=', 1)
if tokens[1].lower().strip().startswith('optional'):
isOptional = True
definition = tokens[1].strip()[len('optional') + 1:]
else:
isOptional = False
definition = tokens[1]
return isOptional, tokens[0], definition

def _createDescriptiveName(s):
return s.replace('_', ' ')

class Parameter(object):

Expand Down Expand Up @@ -128,8 +133,7 @@ def tr(self, string, context=''):
if context == '':
context = 'Parameter'
return QCoreApplication.translate(context, string)



class ParameterBoolean(Parameter):

default_metadata = {
Expand Down Expand Up @@ -158,6 +162,20 @@ def getAsScriptCode(self):
param_type += 'optional '
param_type += 'boolean '
return '##' + self.name + '=' + param_type + str(self.default)

@classmethod
def fromScriptCode(self, line):
isOptional, name, definition = _splitParameterOptions(line)
if definition.startswith("boolean"):
descName = _createDescriptiveName(name)
default = definition.strip()[len('boolean') + 1:]
if default:
param = ParameterBoolean(name, descName, default)
else:
param = ParameterBoolean(name, descName)
param.optional = isOptional
return param



class ParameterCrs(Parameter):
Expand Down Expand Up @@ -194,6 +212,16 @@ def getAsScriptCode(self):
param_type += 'crs '
return '##' + self.name + '=' + param_type + str(self.default)

@classmethod
def fromScriptCode(self, line):
isOptional, name, definition = _splitParameterOptions(line)
if definition.startswith("crs"):
descName = _createDescriptiveName(name)
default = definition.strip()[len('crs') + 1:]
if default:
return ParameterCrs(name, descName, default, isOptional)
else:
return ParameterCrs(name, descName, None, isOptional)

class ParameterDataObject(Parameter):

Expand Down Expand Up @@ -243,6 +271,14 @@ def getAsScriptCode(self):
param_type += 'optional '
param_type += 'extent'
return '##' + self.name + '=' + param_type

@classmethod
def fromScriptCode(self, line):
isOptional, name, definition = _splitParameterOptions(line)
if definition.startswith("extent"):
descName = _createDescriptiveName(name)
default = definition.strip()[len('extent') + 1:] or None
return ParameterExtent(name, descName, default, isOptional)


class ParameterPoint(Parameter):
Expand Down Expand Up @@ -279,6 +315,14 @@ def getAsScriptCode(self):
param_type += 'point'
return '##' + self.name + '=' + param_type

@classmethod
def fromScriptCode(self, line):
isOptional, name, definition = _splitParameterOptions(line)
if definition.startswith("point"):
descName = _createDescriptiveName(name)
default = definition.strip()[len('point') + 1:] or None
return ParameterPoint(name, descName, default, isOptional)


class ParameterFile(Parameter):

Expand Down Expand Up @@ -318,6 +362,13 @@ def getAsScriptCode(self):
param_type += 'file'
return '##' + self.name + '=' + param_type

@classmethod
def fromScriptCode(self, line):
isOptional, name, definition = _splitParameterOptions(line)
if definition.startswith("file") or definition.startswith("folder"):
descName = _createDescriptiveName(name)
return ParameterFile(name, descName, definition.startswith("folder"), isOptional)


class ParameterFixedTable(Parameter):

Expand Down Expand Up @@ -356,6 +407,14 @@ def tableToString(table):
tablestring = tablestring[:-1]
return tablestring

@classmethod
def fromScriptCode(self, line):
isOptional, name, definition = _splitParameterOptions(line)
if definition.startswith("point"):
descName = _createDescriptiveName(name)
default = definition.strip()[len('point') + 1:] or None
return ParameterPoint(name, descName, default, isOptional)


class ParameterMultipleInput(ParameterDataObject):

Expand Down Expand Up @@ -542,6 +601,17 @@ def getAsScriptCode(self):
param_type += 'multiple vector'
return '##' + self.name + '=' + param_type

@classmethod
def fromScriptCode(self, line):
isOptional, name, definition = _splitParameterOptions(line)
descName = _createDescriptiveName(name)
if definition.lower().strip() == 'multiple raster':
return ParameterMultipleInput(name, descName,
dataobjects.TYPE_RASTER, isOptional)
elif definition.lower().strip() == 'multiple vector':
return ParameterMultipleInput(name, definition,
dataobjects.TYPE_VECTOR_ANY, isOptional)


class ParameterNumber(Parameter):

Expand Down Expand Up @@ -599,6 +669,13 @@ def getAsScriptCode(self):
param_type += 'number'
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('number'):
default = definition.strip()[len('number') + 1:] or None
return ParameterNumber(name, descName, default=default, optional=isOptional)

class ParameterRange(Parameter):

Expand Down Expand Up @@ -701,6 +778,13 @@ def getAsScriptCode(self):
param_type += 'raster'
return '##' + self.name + '=' + param_type

@classmethod
def fromScriptCode(self, line):
isOptional, name, definition = _splitParameterOptions(line)
descName = _createDescriptiveName(name)
print isOptional, name, definition
if definition.lower().strip().startswith('raster'):
return ParameterRaster(name, descName, optional=isOptional)

class ParameterSelection(Parameter):

Expand Down Expand Up @@ -744,7 +828,18 @@ def setValue(self, n):
except:
return False


@classmethod
def fromScriptCode(self, line):
isOptional, name, definition = _splitParameterOptions(line)
descName = _createDescriptiveName(name)
if definition.lower().strip().startswith('selectionfromfile'):
options = definition.strip()[len('selectionfromfile '):].split(';')
return ParameterSelection(name, descName, options, isSource=True, optional=isOptional)
elif definition.lower().strip().startswith('selection'):
options = definition.strip()[len('selection '):].split(';')
return ParameterSelection(name, descName, options, optional=isOptional)


class ParameterString(Parameter):

NEWLINE = '\n'
Expand Down Expand Up @@ -781,6 +876,22 @@ def getAsScriptCode(self):
param_type += 'string'
return '##' + self.name + '=' + param_type + 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:]
if default:
return ParameterString(name, descName, default, optional=isOptional)
else:
return ParameterString(name, descName, optional=isOptional)
elif definition.lower().strip().startswith('longstring'):
default = definition.strip()[len('longstring') + 1:]
if default:
return ParameterString(name, descName, default, multiline=True, optional=isOptional)
else:
return ParameterString(name, descName, multiline=True, optional=isOptional)

class ParameterTable(ParameterDataObject):

Expand Down Expand Up @@ -853,6 +964,13 @@ def getAsScriptCode(self):
param_type += 'table'
return '##' + self.name + '=' + param_type

@classmethod
def fromScriptCode(self, line):
isOptional, name, definition = _splitParameterOptions(line)
descName = _createDescriptiveName(name)
if definition.lower().strip().startswith('table'):
return ParameterTable(name, descName, isOptional)


class ParameterTableField(Parameter):

Expand Down Expand Up @@ -908,6 +1026,24 @@ def getAsScriptCode(self):
return '##' + self.name + '=' + param_type + self.parent


@classmethod
def fromScriptCode(self, line):
isOptional, name, definition = _splitParameterOptions(line)
descName = _createDescriptiveName(name)
if definition.lower().strip().startswith('field'):
if definition.lower().strip().startswith('field number'):
parent = definition.strip()[len('field number') + 1:]
datatype = ParameterTableField.DATA_TYPE_NUMBER
elif definition.lower().strip().startswith('field string'):
parent = definition.strip()[len('field string') + 1:]
datatype = ParameterTableField.DATA_TYPE_STRING
else:
parent = definition.strip()[len('field') + 1:]
datatype = ParameterTableField.DATA_TYPE_ANY

return ParameterTableField(name, descName, parent, datatype, isOptional)


class ParameterTableMultipleField(Parameter):

"""A parameter representing several table fields.
Expand Down Expand Up @@ -974,6 +1110,23 @@ def getAsScriptCode(self):
param_type += 'multiple field '
return '##' + self.name + '=' + param_type + self.parent

@classmethod
def fromScriptCode(self, line):
isOptional, name, definition = _splitParameterOptions(line)
if definition.lower().strip().startswith('multiple field'):
descName = _createDescriptiveName(name)
if definition.lower().strip().startswith('multiple field number'):
field = definition.strip()[len('multiple field number') + 1:]
datatype = ParameterTableMultipleField.DATA_TYPE_NUMBER
elif definition.lower().strip().startswith('multiple field string'):
field = definition.strip()[len('multiple field string') + 1:]
datatype = ParameterTableMultipleField.DATA_TYPE_STRING
else:
field = definition.strip()[len('multiple field') + 1:]
datatype = ParameterTableMultipleField.DATA_TYPE_ANY

return ParameterTableMultipleField(name, descName, field, datatype, isOptional)


class ParameterVector(ParameterDataObject):

Expand Down Expand Up @@ -1050,6 +1203,22 @@ def getAsScriptCode(self):
param_type += 'vector'
return '##' + self.name + '=' + param_type

@classmethod
def fromScriptCode(self, line):
isOptional, name, definition = _splitParameterOptions(line)
descName = _createDescriptiveName(name)
if definition.lower().strip() == 'vector':
return ParameterVector(name, descName,
[dataobjects.TYPE_VECTOR_ANY], isOptional)
elif definition.lower().strip() == 'vector point':
return ParameterVector(name, descName,
[dataobjects.TYPE_VECTOR_POINT], isOptional)
elif definition.lower().strip() == 'vector line':
return ParameterVector(name, descName,
[dataobjects.TYPE_VECTOR_LINE], isOptional)
elif definition.lower().strip() == 'vector polygon':
return ParameterVector(name, descName,
[dataobjects.TYPE_VECTOR_POLYGON], isOptional)

class ParameterGeometryPredicate(Parameter):

Expand Down Expand Up @@ -1089,3 +1258,23 @@ def setValue(self, value):
else:
self.value = value
return True


paramClasses = [c for c in sys.modules[__name__].__dict__.values() if inspect.isclass(c) and issubclass(c, Parameter)]

def getParameterFromString(s):
print s
#Try the parameter definitions used in description files
if '|' in s:
tokens = s.split("|")
params = [t if unicode(t) != unicode(None) else None for t in tokens[1:]]
clazz = getattr(sys.modules[__name__], tokens[0])
return clazz(*params)
else: #try script syntax
for paramClass in paramClasses:
try:
param = paramClass.fromScriptCode(s)
if param is not None:
return param
except AttributeError:
pass
Loading

0 comments on commit 8cc9a50

Please sign in to comment.