Skip to content
Permalink
Browse files

[processing] Reorganise python script generated from model, to

move "guts" of script to top (initAlgorithm/processAlgorithm)
and boilerplate methods (createInstance, etc) to end
  • Loading branch information
nyalldawson committed Feb 3, 2019
1 parent 6aea483 commit 0250cab30748c8ce334b43cf014229f3ceec8d64
Showing with 63 additions and 61 deletions.
  1. +47 −44 src/core/processing/models/qgsprocessingmodelalgorithm.cpp
  2. +16 −17 tests/src/analysis/testqgsprocessing.cpp
@@ -359,6 +359,17 @@ QStringList QgsProcessingModelAlgorithm::asPythonCode( const QgsProcessing::Pyth
QStringList lines;
QString indent = QString( ' ' ).repeated( indentSize );
QString currentIndent;

auto safeName = []( const QString & name )->QString
{
QString n = name.toLower().trimmed();
QRegularExpression rx( QStringLiteral( "[^\\sa-z_A-Z0-9]" ) );
n.replace( rx, QString() );
return QgsStringUtils::capitalize( n, QgsStringUtils::UpperCamelCase );
};

const QString algorithmClassName = safeName( name() );

switch ( outputType )
{
case QgsProcessing::PythonQgsProcessingAlgorithmSubclass:
@@ -368,6 +379,7 @@ QStringList QgsProcessingModelAlgorithm::asPythonCode( const QgsProcessing::Pyth
// add specific parameter type imports
const auto params = parameterDefinitions();
QStringList importLines; // not a set - we need regular ordering
importLines.reserve( params.count() );
for ( const QgsProcessingParameterDefinition *def : params )
{
const QString importString = QgsApplication::processingRegistry()->parameterType( def->type() )->pythonImportString();
@@ -378,53 +390,9 @@ QStringList QgsProcessingModelAlgorithm::asPythonCode( const QgsProcessing::Pyth
lines << QStringLiteral( "import processing" );
lines << QString() << QString();

auto safeName = []( const QString & name )->QString
{
QString n = name.toLower().trimmed();
QRegularExpression rx( QStringLiteral( "[^\\sa-z_A-Z0-9]" ) );
n.replace( rx, QString() );
return QgsStringUtils::capitalize( n, QgsStringUtils::UpperCamelCase );
};

const QString algorithmClassName = safeName( name() );
lines << QStringLiteral( "class %1(QgsProcessingAlgorithm):" ).arg( algorithmClassName );
lines << QString();

// createInstance
lines << indent + QStringLiteral( "def createInstance(self):" );
lines << indent + indent + QStringLiteral( "return %1()" ).arg( algorithmClassName );
lines << QString();

// name, displayName
lines << indent + QStringLiteral( "def name(self):" );
lines << indent + indent + QStringLiteral( "return '%1'" ).arg( mModelName );
lines << QString();
lines << indent + QStringLiteral( "def displayName(self):" );
lines << indent + indent + QStringLiteral( "return '%1'" ).arg( mModelName );
lines << QString();

// group, groupId
lines << indent + QStringLiteral( "def group(self):" );
lines << indent + indent + QStringLiteral( "return '%1'" ).arg( mModelGroup );
lines << QString();
lines << indent + QStringLiteral( "def groupId(self):" );
lines << indent + indent + QStringLiteral( "return '%1'" ).arg( mModelGroupId );
lines << QString();

// help
if ( !shortHelpString().isEmpty() )
{
lines << indent + QStringLiteral( "def shortHelpString(self):" );
lines << indent + indent + QStringLiteral( "return \"\"\"%1\"\"\"" ).arg( shortHelpString() );
lines << QString();
}
if ( !helpUrl().isEmpty() )
{
lines << indent + QStringLiteral( "def helpUrl(self):" );
lines << indent + indent + QStringLiteral( "return '%1'" ).arg( helpUrl() );
lines << QString();
}

// initAlgorithm, parameter definitions
lines << indent + QStringLiteral( "def initAlgorithm(self, config=None):" );
lines.reserve( lines.size() + params.size() );
@@ -568,6 +536,41 @@ QStringList QgsProcessingModelAlgorithm::asPythonCode( const QgsProcessing::Pyth
{
case QgsProcessing::PythonQgsProcessingAlgorithmSubclass:
lines << currentIndent + QStringLiteral( "return results" );
lines << QString();

// name, displayName
lines << indent + QStringLiteral( "def name(self):" );
lines << indent + indent + QStringLiteral( "return '%1'" ).arg( mModelName );
lines << QString();
lines << indent + QStringLiteral( "def displayName(self):" );
lines << indent + indent + QStringLiteral( "return '%1'" ).arg( mModelName );
lines << QString();

// group, groupId
lines << indent + QStringLiteral( "def group(self):" );
lines << indent + indent + QStringLiteral( "return '%1'" ).arg( mModelGroup );
lines << QString();
lines << indent + QStringLiteral( "def groupId(self):" );
lines << indent + indent + QStringLiteral( "return '%1'" ).arg( mModelGroupId );
lines << QString();

// help
if ( !shortHelpString().isEmpty() )
{
lines << indent + QStringLiteral( "def shortHelpString(self):" );
lines << indent + indent + QStringLiteral( "return \"\"\"%1\"\"\"" ).arg( shortHelpString() );
lines << QString();
}
if ( !helpUrl().isEmpty() )
{
lines << indent + QStringLiteral( "def helpUrl(self):" );
lines << indent + indent + QStringLiteral( "return '%1'" ).arg( helpUrl() );
lines << QString();
}

// createInstance
lines << indent + QStringLiteral( "def createInstance(self):" );
lines << indent + indent + QStringLiteral( "return %1()" ).arg( algorithmClassName );
break;
}

@@ -6958,21 +6958,6 @@ void TestQgsProcessing::modelExecution()
"\n"
"class Model(QgsProcessingAlgorithm):\n"
"\n"
" def createInstance(self):\n"
" return Model()\n"
"\n"
" def name(self):\n"
" return 'model'\n"
"\n"
" def displayName(self):\n"
" return 'model'\n"
"\n"
" def group(self):\n"
" return ''\n"
"\n"
" def groupId(self):\n"
" return ''\n"
"\n"
" def initAlgorithm(self, config=None):\n"
" self.addParameter(QgsProcessingParameterFeatureSource('SOURCE_LAYER', '', defaultValue=None))\n"
" self.addParameter(QgsProcessingParameterNumber('DIST', '', type=QgsProcessingParameterNumber.Double, defaultValue=None))\n"
@@ -7006,8 +6991,22 @@ void TestQgsProcessing::modelExecution()
" }\n"
" outputs['cx3'] = processing.run('native:extractbyexpression', alg_params, context=context, feedback=feedback, is_child_algorithm=True)\n"
" results['cx3:MY_OUT'] = outputs['cx3']['OUTPUT']\n"
" return results"
"\n" ).split( '\n' );
" return results\n"
"\n"
" def name(self):\n"
" return 'model'\n"
"\n"
" def displayName(self):\n"
" return 'model'\n"
"\n"
" def group(self):\n"
" return ''\n"
"\n"
" def groupId(self):\n"
" return ''\n"
"\n"
" def createInstance(self):\n"
" return Model()\n" ).split( '\n' );
QCOMPARE( actualParts, expectedParts );
}

0 comments on commit 0250cab

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