@@ -359,6 +359,17 @@ QStringList QgsProcessingModelAlgorithm::asPythonCode( const QgsProcessing::Pyth
359
359
QStringList lines;
360
360
QString indent = QString ( ' ' ).repeated ( indentSize );
361
361
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
+
362
373
switch ( outputType )
363
374
{
364
375
case QgsProcessing::PythonQgsProcessingAlgorithmSubclass:
@@ -368,6 +379,7 @@ QStringList QgsProcessingModelAlgorithm::asPythonCode( const QgsProcessing::Pyth
368
379
// add specific parameter type imports
369
380
const auto params = parameterDefinitions ();
370
381
QStringList importLines; // not a set - we need regular ordering
382
+ importLines.reserve ( params.count () );
371
383
for ( const QgsProcessingParameterDefinition *def : params )
372
384
{
373
385
const QString importString = QgsApplication::processingRegistry ()->parameterType ( def->type () )->pythonImportString ();
@@ -378,53 +390,9 @@ QStringList QgsProcessingModelAlgorithm::asPythonCode( const QgsProcessing::Pyth
378
390
lines << QStringLiteral ( " import processing" );
379
391
lines << QString () << QString ();
380
392
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 () );
390
393
lines << QStringLiteral ( " class %1(QgsProcessingAlgorithm):" ).arg ( algorithmClassName );
391
394
lines << QString ();
392
395
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
-
428
396
// initAlgorithm, parameter definitions
429
397
lines << indent + QStringLiteral ( " def initAlgorithm(self, config=None):" );
430
398
lines.reserve ( lines.size () + params.size () );
@@ -568,6 +536,41 @@ QStringList QgsProcessingModelAlgorithm::asPythonCode( const QgsProcessing::Pyth
568
536
{
569
537
case QgsProcessing::PythonQgsProcessingAlgorithmSubclass:
570
538
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 );
571
574
break ;
572
575
}
573
576
0 commit comments