Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
fix some PEP8 in Processing Model as python code
  • Loading branch information
Gustry authored and nyalldawson committed Feb 1, 2019
1 parent 6a8ba52 commit 122d5c3
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 19 deletions.
10 changes: 7 additions & 3 deletions src/core/processing/models/qgsprocessingmodelalgorithm.cpp
Expand Up @@ -22,6 +22,7 @@
#include "qgsxmlutils.h"
#include "qgsexception.h"
#include "qgsvectorlayer.h"
#include "qgsstringutils.h"
#include "qgsapplication.h"
#include "qgsprocessingparametertype.h"

Expand Down Expand Up @@ -385,8 +386,9 @@ QStringList QgsProcessingModelAlgorithm::asPythonCode( const QgsProcessing::Pyth
return n;
};

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

// createInstance
lines << indent + QStringLiteral( "def createInstance(self):" );
Expand Down Expand Up @@ -474,8 +476,8 @@ QStringList QgsProcessingModelAlgorithm::asPythonCode( const QgsProcessing::Pyth

}

lines << currentIndent + QStringLiteral( "results={}" );
lines << currentIndent + QStringLiteral( "outputs={}" );
lines << currentIndent + QStringLiteral( "results = {}" );
lines << currentIndent + QStringLiteral( "outputs = {}" );

QSet< QString > toExecute;
for ( auto childIt = mChildAlgorithms.constBegin(); childIt != mChildAlgorithms.constEnd(); ++childIt )
Expand Down Expand Up @@ -569,6 +571,8 @@ QStringList QgsProcessingModelAlgorithm::asPythonCode( const QgsProcessing::Pyth
break;
}

lines << QString();

return lines;
}

Expand Down
Expand Up @@ -201,11 +201,11 @@ QStringList QgsProcessingModelChildAlgorithm::asPythonCode( const QgsProcessing:
}
lines << baseIndent + QStringLiteral( "}" );

lines << baseIndent + QStringLiteral( "outputs['%1']=processing.run('%2', alg_params, context=context, feedback=feedback, is_child_algorithm=True)" ).arg( mId, mAlgorithmId );
lines << baseIndent + QStringLiteral( "outputs['%1'] = processing.run('%2', alg_params, context=context, feedback=feedback, is_child_algorithm=True)" ).arg( mId, mAlgorithmId );

for ( auto outputIt = mModelOutputs.constBegin(); outputIt != mModelOutputs.constEnd(); ++outputIt )
{
lines << baseIndent + QStringLiteral( "results['%1:%2']=outputs['%1']['%3']" ).arg( mId, outputIt.key(), outputIt.value().childOutputName() );
lines << baseIndent + QStringLiteral( "results['%1:%2'] = outputs['%1']['%3']" ).arg( mId, outputIt.key(), outputIt.value().childOutputName() );
}

return lines;
Expand Down
30 changes: 16 additions & 14 deletions tests/src/analysis/testqgsprocessing.cpp
Expand Up @@ -6152,11 +6152,11 @@ void TestQgsProcessing::modelerAlgorithm()
QVERIFY( child.setAlgorithmId( QStringLiteral( "native:centroids" ) ) );
QVERIFY( child.algorithm() );
QCOMPARE( child.algorithm()->id(), QStringLiteral( "native:centroids" ) );
QCOMPARE( child.asPythonCode( QgsProcessing::PythonQgsProcessingAlgorithmSubclass, QgsStringMap(), 4, 2 ).join( '\n' ), QStringLiteral( " alg_params = {\n }\n outputs['']=processing.run('native:centroids', alg_params, context=context, feedback=feedback, is_child_algorithm=True)" ) );
QCOMPARE( child.asPythonCode( QgsProcessing::PythonQgsProcessingAlgorithmSubclass, QgsStringMap(), 4, 2 ).join( '\n' ), QStringLiteral( " alg_params = {\n }\n outputs[''] = processing.run('native:centroids', alg_params, context=context, feedback=feedback, is_child_algorithm=True)" ) );
QgsStringMap extraParams;
extraParams[QStringLiteral( "SOMETHING" )] = QStringLiteral( "SOMETHING_ELSE" );
extraParams[QStringLiteral( "SOMETHING2" )] = QStringLiteral( "SOMETHING_ELSE2" );
QCOMPARE( child.asPythonCode( QgsProcessing::PythonQgsProcessingAlgorithmSubclass, extraParams, 4, 2 ).join( '\n' ), QStringLiteral( " alg_params = {\n 'SOMETHING':SOMETHING_ELSE,\n 'SOMETHING2':SOMETHING_ELSE2,\n }\n outputs['']=processing.run('native:centroids', alg_params, context=context, feedback=feedback, is_child_algorithm=True)" ) );
QCOMPARE( child.asPythonCode( QgsProcessing::PythonQgsProcessingAlgorithmSubclass, extraParams, 4, 2 ).join( '\n' ), QStringLiteral( " alg_params = {\n 'SOMETHING':SOMETHING_ELSE,\n 'SOMETHING2':SOMETHING_ELSE2,\n }\n outputs[''] = processing.run('native:centroids', alg_params, context=context, feedback=feedback, is_child_algorithm=True)" ) );
// bit of a hack -- but try to simulate an algorithm not originally available!
child.mAlgorithm.reset();
QVERIFY( !child.algorithm() );
Expand Down Expand Up @@ -6199,7 +6199,7 @@ void TestQgsProcessing::modelerAlgorithm()
QCOMPARE( child.parameterSources().value( QStringLiteral( "b" ) ).at( 0 ).staticValue().toInt(), 7 );
QCOMPARE( child.parameterSources().value( QStringLiteral( "b" ) ).at( 1 ).staticValue().toInt(), 9 );

QCOMPARE( child.asPythonCode( QgsProcessing::PythonQgsProcessingAlgorithmSubclass, extraParams, 4, 2 ).join( '\n' ), QStringLiteral( " alg_params = {\n 'a':5,\n 'b':[7,9],\n 'SOMETHING':SOMETHING_ELSE,\n 'SOMETHING2':SOMETHING_ELSE2,\n }\n outputs['my_id']=processing.run('native:centroids', alg_params, context=context, feedback=feedback, is_child_algorithm=True)" ) );
QCOMPARE( child.asPythonCode( QgsProcessing::PythonQgsProcessingAlgorithmSubclass, extraParams, 4, 2 ).join( '\n' ), QStringLiteral( " alg_params = {\n 'a':5,\n 'b':[7,9],\n 'SOMETHING':SOMETHING_ELSE,\n 'SOMETHING2':SOMETHING_ELSE2,\n }\n outputs['my_id'] = processing.run('native:centroids', alg_params, context=context, feedback=feedback, is_child_algorithm=True)" ) );

QgsProcessingModelOutput testModelOut;
testModelOut.setChildId( QStringLiteral( "my_id" ) );
Expand Down Expand Up @@ -6235,7 +6235,7 @@ void TestQgsProcessing::modelerAlgorithm()
QCOMPARE( child.modelOutput( "a" ).description(), QStringLiteral( "my output" ) );
child.modelOutput( "a" ).setDescription( QStringLiteral( "my output 2" ) );
QCOMPARE( child.modelOutput( "a" ).description(), QStringLiteral( "my output 2" ) );
QCOMPARE( child.asPythonCode( QgsProcessing::PythonQgsProcessingAlgorithmSubclass, extraParams, 4, 2 ).join( '\n' ), QStringLiteral( " alg_params = {\n 'a':5,\n 'b':[7,9],\n 'SOMETHING':SOMETHING_ELSE,\n 'SOMETHING2':SOMETHING_ELSE2,\n }\n outputs['my_id']=processing.run('native:centroids', alg_params, context=context, feedback=feedback, is_child_algorithm=True)\n results['my_id:a']=outputs['my_id']['']" ) );
QCOMPARE( child.asPythonCode( QgsProcessing::PythonQgsProcessingAlgorithmSubclass, extraParams, 4, 2 ).join( '\n' ), QStringLiteral( " alg_params = {\n 'a':5,\n 'b':[7,9],\n 'SOMETHING':SOMETHING_ELSE,\n 'SOMETHING2':SOMETHING_ELSE2,\n }\n outputs['my_id'] = processing.run('native:centroids', alg_params, context=context, feedback=feedback, is_child_algorithm=True)\n results['my_id:a'] = outputs['my_id']['']" ) );

// no existent
child.modelOutput( "b" ).setDescription( QStringLiteral( "my output 3" ) );
Expand Down Expand Up @@ -6956,9 +6956,10 @@ void TestQgsProcessing::modelExecution()
"import processing\n"
"\n"
"\n"
"class model(QgsProcessingAlgorithm):\n"
"class Model(QgsProcessingAlgorithm):\n"
"\n"
" def createInstance(self):\n"
" return model()\n"
" return Model()\n"
"\n"
" def name(self):\n"
" return 'model'\n"
Expand All @@ -6979,8 +6980,8 @@ void TestQgsProcessing::modelExecution()
" self.addParameter(QgsProcessingParameterFeatureSink('cx3:MY_OUT', '', type=QgsProcessing.TypeVectorAnyGeometry, createByDefault=True, defaultValue=None))\n"
"\n"
" def processAlgorithm(self, parameters, context, feedback):\n"
" results={}\n"
" outputs={}\n"
" results = {}\n"
" outputs = {}\n"
" alg_params = {\n"
" 'DISSOLVE':False,\n"
" 'DISTANCE':parameters['DIST'],\n"
Expand All @@ -6990,22 +6991,23 @@ void TestQgsProcessing::modelExecution()
" 'SEGMENTS':QgsExpression('@myvar*2').evaluate(),\n"
" 'OUTPUT':parameters['cx1:MODEL_OUT_LAYER'],\n"
" }\n"
" outputs['cx1']=processing.run('native:buffer', alg_params, context=context, feedback=feedback, is_child_algorithm=True)\n"
" results['cx1:MODEL_OUT_LAYER']=outputs['cx1']['OUTPUT']\n"
" outputs['cx1'] = processing.run('native:buffer', alg_params, context=context, feedback=feedback, is_child_algorithm=True)\n"
" results['cx1:MODEL_OUT_LAYER'] = outputs['cx1']['OUTPUT']\n"
" alg_params = {\n"
" 'INPUT':outputs['cx1']['OUTPUT'],\n"
" 'OUTPUT':QgsProcessing.TEMPORARY_OUTPUT,\n"
" }\n"
" outputs['cx2']=processing.run('native:centroids', alg_params, context=context, feedback=feedback, is_child_algorithm=True)\n"
" outputs['cx2'] = processing.run('native:centroids', alg_params, context=context, feedback=feedback, is_child_algorithm=True)\n"
" alg_params = {\n"
" 'EXPRESSION':'true',\n"
" 'INPUT':outputs['cx1']['OUTPUT'],\n"
" 'OUTPUT':parameters['MY_OUT'],\n"
" 'OUTPUT':parameters['cx3:MY_OUT'],\n"
" }\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" ).split( '\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' );
QCOMPARE( actualParts, expectedParts );
}

Expand Down

0 comments on commit 122d5c3

Please sign in to comment.