Skip to content

Commit

Permalink
add method to get parameter description for python script
Browse files Browse the repository at this point in the history
  • Loading branch information
Koyaani authored and nyalldawson committed Sep 24, 2021
1 parent 1b5b7d9 commit 968a5e8
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 0 deletions.
Expand Up @@ -507,6 +507,15 @@ parameter. Returns ``True`` if the value can be accepted.
The optional ``context`` parameter can be specified to allow a more stringent
check to be performed, capable of checking for the presence of required
layers and other factors within the context.
%End

QString descriptionAsPythonString() const;
%Docstring
Returns the description of the parameter with quotes around.
If there are quotes in the description, they are escaped if necessary.
The around quote could be single or double.

.. seealso:: :py:func:`description`
%End

virtual QString valueAsPythonString( const QVariant &value, QgsProcessingContext &context ) const;
Expand Down
15 changes: 15 additions & 0 deletions src/core/processing/qgsprocessingparameters.cpp
Expand Up @@ -2443,6 +2443,21 @@ QString QgsProcessingParameterDefinition::valueAsPythonComment( const QVariant &
return QString();
}

QString QgsProcessingParameterDefinition::descriptionAsPythonString( ) const
{
// if it possible to not use escaping for readability
if ( description().contains( "'" ) && !description().contains( "\"" ) )
{
return QStringLiteral( "\"%1\"" ).arg( description() );
}
else
{
// do the escape even if there is no apostrophe
const QString escapeDescription = description().replace( "\'", "\\\'" );
return QStringLiteral( "'%1'" ).arg( escapeDescription );
}
}

QString QgsProcessingParameterDefinition::asScriptCode() const
{
QString code = QStringLiteral( "##%1=" ).arg( mName );
Expand Down
8 changes: 8 additions & 0 deletions src/core/processing/qgsprocessingparameters.h
Expand Up @@ -606,6 +606,14 @@ class CORE_EXPORT QgsProcessingParameterDefinition
*/
virtual bool checkValueIsAcceptable( const QVariant &input, QgsProcessingContext *context = nullptr ) const;

/**
* Returns the description of the parameter with quotes around.
* If there are quotes in the description, they are escaped if necessary.
* The around quote could be single or double.
* \see description()
*/
QString descriptionAsPythonString() const;

/**
* Returns a string version of the parameter input \a value, which is suitable for use as an input
* parameter value when running an algorithm directly from a Python command.
Expand Down
13 changes: 13 additions & 0 deletions tests/src/analysis/testqgsprocessing.cpp
Expand Up @@ -2541,6 +2541,19 @@ void TestQgsProcessing::parameterGeneral()
QCOMPARE( fromMap.guiDefaultValueOverride(), param.guiDefaultValueOverride() );
QCOMPARE( fromMap.metadata(), param.metadata() );
QCOMPARE( fromMap.help(), QStringLiteral( "my help" ) );

// escaping quotes
param = QgsProcessingParameterBoolean( "param_name", "Param's name" );
QString pythonCode = param.asPythonString();
QCOMPARE( pythonCode, QStringLiteral( "QgsProcessingParameterBoolean('param_name', \"Param's name\", defaultValue=None)" ) );

param = QgsProcessingParameterBoolean( "param_name", "Param\"s name" );
pythonCode = param.asPythonString();
QCOMPARE( pythonCode, QStringLiteral( "QgsProcessingParameterBoolean('param_name', 'Param\"s name', defaultValue=None)" ) );

param = QgsProcessingParameterBoolean( "param_name", "Param's \" name" );
pythonCode = param.asPythonString();
QCOMPARE( pythonCode, QStringLiteral( "QgsProcessingParameterBoolean('param_name', 'Param\\'s \" name', defaultValue=None)" ) );
}

void TestQgsProcessing::parameterBoolean()
Expand Down

0 comments on commit 968a5e8

Please sign in to comment.