Skip to content
Permalink
Browse files

[processing] Add API to get an import statement and classname for a p…

…arameter type
  • Loading branch information
nyalldawson committed Jan 25, 2019
1 parent 60f252c commit 3cb6a40ef2c624dba178b5cadd3e79156fa04692
@@ -51,6 +51,27 @@ A human readable and translatable short name for this parameter type.
This will be used in comboboxes and list widgets.
%End


virtual QString pythonImportString() const;
%Docstring
Returns a valid Python import string for importing the corresponding parameter type,
e.g. "from qgis.core import :py:class:`QgsProcessingParameterBoolean`".

.. seealso:: :py:func:`className`

.. versionadded:: 3.6
%End


virtual QString className() const;
%Docstring
Returns the corresponding class name for the parameter type.

.. seealso:: :py:func:`pythonImportString`

.. versionadded:: 3.6
%End

virtual QString id() const = 0;
%Docstring
A static id for this type which will be used for storing this parameter type.
@@ -167,6 +167,12 @@ def name(self):
def id(self):
return 'fields_mapping'

def pythonImportString(self):
return 'from processing.algs.qgis.FieldsMapper import FieldsMapper'

def className(self):
return 'FieldsMapper.ParameterFieldsMapping'

def description(self):
return QCoreApplication.translate('Processing', 'A mapping of field names to field type definitions and expressions. Used for the refactor fields algorithm.')

@@ -100,6 +100,14 @@ def testProcessingException(self):
results, ok = alg.run({}, context, feedback)
self.assertFalse(ok)

def testParameterPythonImport(self):
for t in QgsApplication.processingRegistry().parameterTypes():
import_string = t.pythonImportString()
# check that pythonImportString correctly imports
exec(import_string)
# and now we should be able to instantiate an object!
exec('test = {}(\'id\',\'name\')\nself.assertIsNotNone(test)'.format(t.className()))


if __name__ == '__main__':
nose2.main()
@@ -68,6 +68,30 @@ class CORE_EXPORT QgsProcessingParameterType
*/
virtual QString name() const = 0;

// TODO QGIS 4.0 -- make pure virtual

/**
* Returns a valid Python import string for importing the corresponding parameter type,
* e.g. "from qgis.core import QgsProcessingParameterBoolean".
*
* \see className()
* \since QGIS 3.6
*/
virtual QString pythonImportString() const { return QString(); }

// TODO QGIS 4.0 -- make pure virtual

/**
* Returns the corresponding class name for the parameter type.
*
* \see pythonImportString()
* \since QGIS 3.6
*/
virtual QString className() const
{
return name(); // this is wrong, but it's better than nothing for subclasses which don't implement this method
}

/**
* A static id for this type which will be used for storing this parameter type.
*/

0 comments on commit 3cb6a40

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