Skip to content

Commit

Permalink
Model to Python script - Add annotations
Browse files Browse the repository at this point in the history
  • Loading branch information
Gustry authored and nyalldawson committed Dec 11, 2024
1 parent 6f4304b commit 0811b32
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 21 deletions.
27 changes: 15 additions & 12 deletions src/core/processing/models/qgsprocessingmodelalgorithm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -771,10 +771,13 @@ QStringList QgsProcessingModelAlgorithm::asPythonCode( const QgsProcessing::Pyth
{
// add specific parameter type imports
const auto params = parameterDefinitions();
importLines.reserve( params.count() + 3 );
importLines.reserve( params.count() + 6 );
importLines << QStringLiteral( "from typing import Any, Optional" );
importLines << QString();
importLines << QStringLiteral( "from qgis.core import QgsProcessing" );
importLines << QStringLiteral( "from qgis.core import QgsProcessingAlgorithm" );
importLines << QStringLiteral( "from qgis.core import QgsProcessingMultiStepFeedback" );
importLines << QStringLiteral( "from qgis.core import QgsProcessingContext" );
importLines << QStringLiteral( "from qgis.core import QgsProcessingFeedback, QgsProcessingMultiStepFeedback" );

bool hasAdvancedParams = false;
for ( const QgsProcessingParameterDefinition *def : params )
Expand All @@ -790,14 +793,14 @@ QStringList QgsProcessingModelAlgorithm::asPythonCode( const QgsProcessing::Pyth
if ( hasAdvancedParams )
importLines << QStringLiteral( "from qgis.core import QgsProcessingParameterDefinition" );

lines << QStringLiteral( "import processing" );
lines << QStringLiteral( "from qgis import processing" );
lines << QString() << QString();

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

// initAlgorithm, parameter definitions
lines << indent + QStringLiteral( "def initAlgorithm(self, config=None):" );
lines << indent + QStringLiteral( "def initAlgorithm(self, config: Optional[dict[str, Any]] = None):" );
if ( params.empty() )
{
lines << indent + indent + QStringLiteral( "pass" );
Expand Down Expand Up @@ -842,7 +845,7 @@ QStringList QgsProcessingModelAlgorithm::asPythonCode( const QgsProcessing::Pyth
}

lines << QString();
lines << indent + QStringLiteral( "def processAlgorithm(self, parameters, context, model_feedback):" );
lines << indent + QStringLiteral( "def processAlgorithm(self, parameters: dict[str, Any], context: QgsProcessingContext, model_feedback: QgsProcessingFeedback) -> dict[str, Any]:" );
currentIndent = indent + indent;

lines << currentIndent + QStringLiteral( "# Use a multi-step feedback, so that individual child algorithm progress reports are adjusted for the" );
Expand Down Expand Up @@ -988,38 +991,38 @@ QStringList QgsProcessingModelAlgorithm::asPythonCode( const QgsProcessing::Pyth
lines << QString();

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

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

// help
if ( !shortHelpString().isEmpty() )
{
lines << indent + QStringLiteral( "def shortHelpString(self):" );
lines << indent + QStringLiteral( "def shortHelpString(self) -> str:" );
lines << indent + indent + QStringLiteral( "return \"\"\"%1\"\"\"" ).arg( shortHelpString() );
lines << QString();
}
if ( !helpUrl().isEmpty() )
{
lines << indent + QStringLiteral( "def helpUrl(self):" );
lines << indent + QStringLiteral( "def helpUrl(self) -> str:" );
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 );
lines << indent + indent + QStringLiteral( "return self.__class__()" );

// additional import lines
static QMap< QString, QString > sAdditionalImports
Expand Down
21 changes: 12 additions & 9 deletions tests/src/analysis/testqgsprocessingmodelalgorithm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1454,22 +1454,25 @@ void TestQgsProcessingModelAlgorithm::modelExecution()
"Group : \n"
"With QGIS : %1\n"
"\"\"\"\n\n"
"from typing import Any, Optional\n"
"\n"
"from qgis.core import QgsProcessing\n"
"from qgis.core import QgsProcessingAlgorithm\n"
"from qgis.core import QgsProcessingMultiStepFeedback\n"
"from qgis.core import QgsProcessingContext\n"
"from qgis.core import QgsProcessingFeedback, QgsProcessingMultiStepFeedback\n"
"from qgis.core import QgsProcessingParameterFeatureSource\n"
"from qgis.core import QgsProcessingParameterNumber\n"
"from qgis.core import QgsProcessingParameterCrs\n"
"from qgis.core import QgsProcessingParameterFeatureSink\n"
"from qgis.core import QgsProcessingParameterDefinition\n"
"from qgis.core import QgsCoordinateReferenceSystem\n"
"from qgis.core import QgsExpression\n"
"import processing\n"
"from qgis import processing\n"
"\n"
"\n"
"class MyModel(QgsProcessingAlgorithm):\n"
"\n"
" def initAlgorithm(self, config=None):\n"
" def initAlgorithm(self, config: Optional[dict[str, Any]] = None):\n"
" # an input\n"
" self.addParameter(QgsProcessingParameterFeatureSource('SOURCE_LAYER', '', defaultValue=None))\n"
" self.addParameter(QgsProcessingParameterNumber('DIST', '', type=QgsProcessingParameterNumber.Double, defaultValue=None))\n"
Expand All @@ -1479,7 +1482,7 @@ void TestQgsProcessingModelAlgorithm::modelExecution()
" self.addParameter(QgsProcessingParameterFeatureSink('MyModelOutput', 'my model output', type=QgsProcessing.TypeVectorPolygon, createByDefault=True, supportsAppend=True, defaultValue=None))\n"
" self.addParameter(QgsProcessingParameterFeatureSink('MyOutput', 'My output', type=QgsProcessing.TypeVectorAnyGeometry, createByDefault=True, defaultValue=None))\n"
"\n"
" def processAlgorithm(self, parameters, context, model_feedback):\n"
" def processAlgorithm(self, parameters: dict[str, Any], context: QgsProcessingContext, model_feedback: QgsProcessingFeedback) -> dict[str, Any]:\n"
" # Use a multi-step feedback, so that individual child algorithm progress reports are adjusted for the\n"
" # overall progress through the model\n"
" feedback = QgsProcessingMultiStepFeedback(3, model_feedback)\n"
Expand Down Expand Up @@ -1523,20 +1526,20 @@ void TestQgsProcessingModelAlgorithm::modelExecution()
" results['MyOutput'] = outputs['cx3']['OUTPUT']\n"
" return results\n"
"\n"
" def name(self):\n"
" def name(self) -> str:\n"
" return '2my model'\n"
"\n"
" def displayName(self):\n"
" def displayName(self) -> str:\n"
" return '2my model'\n"
"\n"
" def group(self):\n"
" def group(self) -> str:\n"
" return ''\n"
"\n"
" def groupId(self):\n"
" def groupId(self) -> str:\n"
" return ''\n"
"\n"
" def createInstance(self):\n"
" return MyModel()\n" )
" return self.__class__()\n" )
.arg( Qgis::versionInt() )
.split( '\n' );
QCOMPARE( actualParts, expectedParts );
Expand Down

0 comments on commit 0811b32

Please sign in to comment.