Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
[processing] If model name starts with a digit, we need to remove tha…
…t when generating a class name
  • Loading branch information
nyalldawson committed Feb 5, 2019
1 parent 185855b commit 62e09d9
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 4 deletions.
2 changes: 2 additions & 0 deletions src/core/processing/models/qgsprocessingmodelalgorithm.cpp
Expand Up @@ -365,6 +365,8 @@ QStringList QgsProcessingModelAlgorithm::asPythonCode( const QgsProcessing::Pyth
QString n = name.toLower().trimmed();
QRegularExpression rx( QStringLiteral( "[^\\sa-z_A-Z0-9]" ) );
n.replace( rx, QString() );
QRegularExpression rx2( QStringLiteral( "^\\d*" ) ); // name can't start in a digit
n.replace( rx2, QString() );
return QgsStringUtils::capitalize( n, QgsStringUtils::UpperCamelCase );
};

Expand Down
9 changes: 5 additions & 4 deletions tests/src/analysis/testqgsprocessing.cpp
Expand Up @@ -6946,6 +6946,7 @@ void TestQgsProcessing::modelExecution()
QGSCOMPARENEAR( variables.value( "SOURCE_LAYER_maxx" ).value.toDouble(), -83.3333, 0.001 );
QGSCOMPARENEAR( variables.value( "SOURCE_LAYER_maxy" ).value.toDouble(), 46.8719, 0.001 );

model2.setName( QStringLiteral( "2my model" ) );
QStringList actualParts = model2.asPythonCode( QgsProcessing::PythonQgsProcessingAlgorithmSubclass, 2 );
QgsDebugMsg( actualParts.join( '\n' ) );
QStringList expectedParts = QStringLiteral( "from qgis.core import QgsProcessing\n"
Expand All @@ -6956,7 +6957,7 @@ void TestQgsProcessing::modelExecution()
"import processing\n"
"\n"
"\n"
"class Model(QgsProcessingAlgorithm):\n"
"class MyModel(QgsProcessingAlgorithm):\n"
"\n"
" def initAlgorithm(self, config=None):\n"
" self.addParameter(QgsProcessingParameterFeatureSource('SOURCE_LAYER', '', defaultValue=None))\n"
Expand Down Expand Up @@ -6994,10 +6995,10 @@ void TestQgsProcessing::modelExecution()
" return results\n"
"\n"
" def name(self):\n"
" return 'model'\n"
" return '2my model'\n"
"\n"
" def displayName(self):\n"
" return 'model'\n"
" return '2my model'\n"
"\n"
" def group(self):\n"
" return ''\n"
Expand All @@ -7006,7 +7007,7 @@ void TestQgsProcessing::modelExecution()
" return ''\n"
"\n"
" def createInstance(self):\n"
" return Model()\n" ).split( '\n' );
" return MyModel()\n" ).split( '\n' );
QCOMPARE( actualParts, expectedParts );
}

Expand Down

0 comments on commit 62e09d9

Please sign in to comment.