Skip to content

Commit 0250cab

Browse files
committed
[processing] Reorganise python script generated from model, to
move "guts" of script to top (initAlgorithm/processAlgorithm) and boilerplate methods (createInstance, etc) to end
1 parent 6aea483 commit 0250cab

File tree

2 files changed

+63
-61
lines changed

2 files changed

+63
-61
lines changed

src/core/processing/models/qgsprocessingmodelalgorithm.cpp

Lines changed: 47 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -359,6 +359,17 @@ QStringList QgsProcessingModelAlgorithm::asPythonCode( const QgsProcessing::Pyth
359359
QStringList lines;
360360
QString indent = QString( ' ' ).repeated( indentSize );
361361
QString currentIndent;
362+
363+
auto safeName = []( const QString & name )->QString
364+
{
365+
QString n = name.toLower().trimmed();
366+
QRegularExpression rx( QStringLiteral( "[^\\sa-z_A-Z0-9]" ) );
367+
n.replace( rx, QString() );
368+
return QgsStringUtils::capitalize( n, QgsStringUtils::UpperCamelCase );
369+
};
370+
371+
const QString algorithmClassName = safeName( name() );
372+
362373
switch ( outputType )
363374
{
364375
case QgsProcessing::PythonQgsProcessingAlgorithmSubclass:
@@ -368,6 +379,7 @@ QStringList QgsProcessingModelAlgorithm::asPythonCode( const QgsProcessing::Pyth
368379
// add specific parameter type imports
369380
const auto params = parameterDefinitions();
370381
QStringList importLines; // not a set - we need regular ordering
382+
importLines.reserve( params.count() );
371383
for ( const QgsProcessingParameterDefinition *def : params )
372384
{
373385
const QString importString = QgsApplication::processingRegistry()->parameterType( def->type() )->pythonImportString();
@@ -378,53 +390,9 @@ QStringList QgsProcessingModelAlgorithm::asPythonCode( const QgsProcessing::Pyth
378390
lines << QStringLiteral( "import processing" );
379391
lines << QString() << QString();
380392

381-
auto safeName = []( const QString & name )->QString
382-
{
383-
QString n = name.toLower().trimmed();
384-
QRegularExpression rx( QStringLiteral( "[^\\sa-z_A-Z0-9]" ) );
385-
n.replace( rx, QString() );
386-
return QgsStringUtils::capitalize( n, QgsStringUtils::UpperCamelCase );
387-
};
388-
389-
const QString algorithmClassName = safeName( name() );
390393
lines << QStringLiteral( "class %1(QgsProcessingAlgorithm):" ).arg( algorithmClassName );
391394
lines << QString();
392395

393-
// createInstance
394-
lines << indent + QStringLiteral( "def createInstance(self):" );
395-
lines << indent + indent + QStringLiteral( "return %1()" ).arg( algorithmClassName );
396-
lines << QString();
397-
398-
// name, displayName
399-
lines << indent + QStringLiteral( "def name(self):" );
400-
lines << indent + indent + QStringLiteral( "return '%1'" ).arg( mModelName );
401-
lines << QString();
402-
lines << indent + QStringLiteral( "def displayName(self):" );
403-
lines << indent + indent + QStringLiteral( "return '%1'" ).arg( mModelName );
404-
lines << QString();
405-
406-
// group, groupId
407-
lines << indent + QStringLiteral( "def group(self):" );
408-
lines << indent + indent + QStringLiteral( "return '%1'" ).arg( mModelGroup );
409-
lines << QString();
410-
lines << indent + QStringLiteral( "def groupId(self):" );
411-
lines << indent + indent + QStringLiteral( "return '%1'" ).arg( mModelGroupId );
412-
lines << QString();
413-
414-
// help
415-
if ( !shortHelpString().isEmpty() )
416-
{
417-
lines << indent + QStringLiteral( "def shortHelpString(self):" );
418-
lines << indent + indent + QStringLiteral( "return \"\"\"%1\"\"\"" ).arg( shortHelpString() );
419-
lines << QString();
420-
}
421-
if ( !helpUrl().isEmpty() )
422-
{
423-
lines << indent + QStringLiteral( "def helpUrl(self):" );
424-
lines << indent + indent + QStringLiteral( "return '%1'" ).arg( helpUrl() );
425-
lines << QString();
426-
}
427-
428396
// initAlgorithm, parameter definitions
429397
lines << indent + QStringLiteral( "def initAlgorithm(self, config=None):" );
430398
lines.reserve( lines.size() + params.size() );
@@ -568,6 +536,41 @@ QStringList QgsProcessingModelAlgorithm::asPythonCode( const QgsProcessing::Pyth
568536
{
569537
case QgsProcessing::PythonQgsProcessingAlgorithmSubclass:
570538
lines << currentIndent + QStringLiteral( "return results" );
539+
lines << QString();
540+
541+
// name, displayName
542+
lines << indent + QStringLiteral( "def name(self):" );
543+
lines << indent + indent + QStringLiteral( "return '%1'" ).arg( mModelName );
544+
lines << QString();
545+
lines << indent + QStringLiteral( "def displayName(self):" );
546+
lines << indent + indent + QStringLiteral( "return '%1'" ).arg( mModelName );
547+
lines << QString();
548+
549+
// group, groupId
550+
lines << indent + QStringLiteral( "def group(self):" );
551+
lines << indent + indent + QStringLiteral( "return '%1'" ).arg( mModelGroup );
552+
lines << QString();
553+
lines << indent + QStringLiteral( "def groupId(self):" );
554+
lines << indent + indent + QStringLiteral( "return '%1'" ).arg( mModelGroupId );
555+
lines << QString();
556+
557+
// help
558+
if ( !shortHelpString().isEmpty() )
559+
{
560+
lines << indent + QStringLiteral( "def shortHelpString(self):" );
561+
lines << indent + indent + QStringLiteral( "return \"\"\"%1\"\"\"" ).arg( shortHelpString() );
562+
lines << QString();
563+
}
564+
if ( !helpUrl().isEmpty() )
565+
{
566+
lines << indent + QStringLiteral( "def helpUrl(self):" );
567+
lines << indent + indent + QStringLiteral( "return '%1'" ).arg( helpUrl() );
568+
lines << QString();
569+
}
570+
571+
// createInstance
572+
lines << indent + QStringLiteral( "def createInstance(self):" );
573+
lines << indent + indent + QStringLiteral( "return %1()" ).arg( algorithmClassName );
571574
break;
572575
}
573576

tests/src/analysis/testqgsprocessing.cpp

Lines changed: 16 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -6958,21 +6958,6 @@ void TestQgsProcessing::modelExecution()
69586958
"\n"
69596959
"class Model(QgsProcessingAlgorithm):\n"
69606960
"\n"
6961-
" def createInstance(self):\n"
6962-
" return Model()\n"
6963-
"\n"
6964-
" def name(self):\n"
6965-
" return 'model'\n"
6966-
"\n"
6967-
" def displayName(self):\n"
6968-
" return 'model'\n"
6969-
"\n"
6970-
" def group(self):\n"
6971-
" return ''\n"
6972-
"\n"
6973-
" def groupId(self):\n"
6974-
" return ''\n"
6975-
"\n"
69766961
" def initAlgorithm(self, config=None):\n"
69776962
" self.addParameter(QgsProcessingParameterFeatureSource('SOURCE_LAYER', '', defaultValue=None))\n"
69786963
" self.addParameter(QgsProcessingParameterNumber('DIST', '', type=QgsProcessingParameterNumber.Double, defaultValue=None))\n"
@@ -7006,8 +6991,22 @@ void TestQgsProcessing::modelExecution()
70066991
" }\n"
70076992
" outputs['cx3'] = processing.run('native:extractbyexpression', alg_params, context=context, feedback=feedback, is_child_algorithm=True)\n"
70086993
" results['cx3:MY_OUT'] = outputs['cx3']['OUTPUT']\n"
7009-
" return results"
7010-
"\n" ).split( '\n' );
6994+
" return results\n"
6995+
"\n"
6996+
" def name(self):\n"
6997+
" return 'model'\n"
6998+
"\n"
6999+
" def displayName(self):\n"
7000+
" return 'model'\n"
7001+
"\n"
7002+
" def group(self):\n"
7003+
" return ''\n"
7004+
"\n"
7005+
" def groupId(self):\n"
7006+
" return ''\n"
7007+
"\n"
7008+
" def createInstance(self):\n"
7009+
" return Model()\n" ).split( '\n' );
70117010
QCOMPARE( actualParts, expectedParts );
70127011
}
70137012

0 commit comments

Comments
 (0)