Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Merge pull request #7296 from wonder-sk/processing-python-escape-quotes
Fix escaping of quotes of map layer source in processing
  • Loading branch information
wonder-sk committed Jun 22, 2018
2 parents 7ab6597 + bc737ce commit 3f16335
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 17 deletions.
22 changes: 11 additions & 11 deletions src/core/processing/qgsprocessingparameters.cpp
Expand Up @@ -1645,7 +1645,7 @@ QString QgsProcessingParameterExtent::valueAsPythonString( const QVariant &value
p.insert( name(), value );
QgsMapLayer *layer = QgsProcessingParameters::parameterAsLayer( this, p, context );
if ( layer )
return QgsProcessingUtils::normalizeLayerSource( layer->source() ).prepend( '\'' ).append( '\'' );
return QgsProcessingUtils::stringToPythonLiteral( QgsProcessingUtils::normalizeLayerSource( layer->source() ) );

return QgsProcessingParameterDefinition::valueAsPythonString( value, context );
}
Expand Down Expand Up @@ -2418,7 +2418,7 @@ QString QgsProcessingParameterRasterLayer::valueAsPythonString( const QVariant &
QVariantMap p;
p.insert( name(), val );
QgsRasterLayer *layer = QgsProcessingParameters::parameterAsRasterLayer( this, p, context );
return layer ? QgsProcessingUtils::normalizeLayerSource( layer->source() ).prepend( '\'' ).append( '\'' ) : QString();
return layer ? QgsProcessingUtils::stringToPythonLiteral( QgsProcessingUtils::normalizeLayerSource( layer->source() ) ) : QString();
}

QgsProcessingParameterRasterLayer *QgsProcessingParameterRasterLayer::fromScriptCode( const QString &name, const QString &description, bool isOptional, const QString &definition )
Expand Down Expand Up @@ -2631,7 +2631,7 @@ QString QgsProcessingParameterString::valueAsPythonString( const QVariant &value

QString s = value.toString();
s.replace( '\n', QStringLiteral( "\\n" ) );
return s.prepend( '\'' ).append( '\'' );
return QgsProcessingUtils::stringToPythonLiteral( s );
}

QString QgsProcessingParameterString::asScriptCode() const
Expand Down Expand Up @@ -2716,7 +2716,7 @@ QString QgsProcessingParameterExpression::valueAsPythonString( const QVariant &v

QString s = value.toString();
s.replace( '\n', QStringLiteral( "\\n" ) );
return s.prepend( '\'' ).append( '\'' );
return QgsProcessingUtils::stringToPythonLiteral( s );
}

QStringList QgsProcessingParameterExpression::dependsOnOtherParameters() const
Expand Down Expand Up @@ -3155,7 +3155,7 @@ QString QgsProcessingParameterFeatureSource::valueAsPythonString( const QVariant
// prefer to use layer source instead of id if possible (since it's persistent)
if ( QgsVectorLayer *layer = qobject_cast< QgsVectorLayer * >( QgsProcessingUtils::mapLayerFromString( layerString, context ) ) )
layerString = layer->source();
return layerString.prepend( '\'' ).append( '\'' );
return QgsProcessingUtils::stringToPythonLiteral( layerString );
}
}
else
Expand All @@ -3172,10 +3172,10 @@ QString QgsProcessingParameterFeatureSource::valueAsPythonString( const QVariant
}
else if ( QgsVectorLayer *layer = qobject_cast< QgsVectorLayer * >( qvariant_cast<QObject *>( value ) ) )
{
return layer->source().prepend( '\'' ).append( '\'' );
return QgsProcessingUtils::stringToPythonLiteral( layer->source() );
}

return value.toString().prepend( '\'' ).append( '\'' );
return QgsProcessingUtils::stringToPythonLiteral( value.toString() );
}

QString QgsProcessingParameterFeatureSource::asScriptCode() const
Expand Down Expand Up @@ -3334,7 +3334,7 @@ QString QgsProcessingParameterFeatureSink::valueAsPythonString( const QVariant &
}
}

return value.toString().prepend( '\'' ).append( '\'' );
return QgsProcessingUtils::stringToPythonLiteral( value.toString() );
}

QString QgsProcessingParameterFeatureSink::asScriptCode() const
Expand Down Expand Up @@ -3559,7 +3559,7 @@ QString QgsProcessingParameterRasterDestination::valueAsPythonString( const QVar
}
}

return value.toString().prepend( '\'' ).append( '\'' );
return QgsProcessingUtils::stringToPythonLiteral( value.toString() );
}

QgsProcessingOutputDefinition *QgsProcessingParameterRasterDestination::toOutputDefinition() const
Expand Down Expand Up @@ -3675,7 +3675,7 @@ QString QgsProcessingParameterFileDestination::valueAsPythonString( const QVaria
}
}

return value.toString().prepend( '\'' ).append( '\'' );
return QgsProcessingUtils::stringToPythonLiteral( value.toString() );
}

QgsProcessingOutputDefinition *QgsProcessingParameterFileDestination::toOutputDefinition() const
Expand Down Expand Up @@ -3898,7 +3898,7 @@ QString QgsProcessingParameterVectorDestination::valueAsPythonString( const QVar
}
}

return value.toString().prepend( '\'' ).append( '\'' );
return QgsProcessingUtils::stringToPythonLiteral( value.toString() );
}

QString QgsProcessingParameterVectorDestination::asScriptCode() const
Expand Down
12 changes: 6 additions & 6 deletions tests/src/analysis/testqgsprocessing.cpp
Expand Up @@ -3805,7 +3805,7 @@ void TestQgsProcessing::parameterString()
QCOMPARE( def->valueAsPythonString( QStringLiteral( "abc" ), context ), QStringLiteral( "'abc'" ) );
QCOMPARE( def->valueAsPythonString( QStringLiteral( "abc\ndef" ), context ), QStringLiteral( "'abc\\ndef'" ) );
QCOMPARE( def->valueAsPythonString( QVariant::fromValue( QgsProperty::fromExpression( "\"a\"=1" ) ), context ), QStringLiteral( "QgsProperty.fromExpression('\"a\"=1')" ) );
QCOMPARE( def->valueAsPythonString( "uri='complex' username=\"complex\"", context ), QStringLiteral( "'uri=\'complex\' username=\"complex\"'" ) );
QCOMPARE( def->valueAsPythonString( "uri='complex' username=\"complex\"", context ), QStringLiteral( "'uri=\\'complex\\' username=\\\"complex\\\"'" ) );

QString code = def->asScriptCode();
QCOMPARE( code, QStringLiteral( "##non_optional=string" ) );
Expand Down Expand Up @@ -4368,7 +4368,7 @@ void TestQgsProcessing::parameterFeatureSource()
QCOMPARE( def->valueAsPythonString( QVariant::fromValue( QgsProcessingFeatureSourceDefinition( QgsProperty::fromExpression( "\"abc\" || \"def\"" ) ) ), context ), QStringLiteral( "QgsProperty.fromExpression('\"abc\" || \"def\"')" ) );
QCOMPARE( def->valueAsPythonString( QVariant::fromValue( QgsProperty::fromExpression( "\"a\"=1" ) ), context ), QStringLiteral( "QgsProperty.fromExpression('\"a\"=1')" ) );
QCOMPARE( def->valueAsPythonString( QVariant::fromValue( v2 ), context ), QStringLiteral( "'%1'" ).arg( vector2 ) );
QCOMPARE( def->valueAsPythonString( "uri='complex' username=\"complex\"", context ), QStringLiteral( "'uri=\'complex\' username=\"complex\"'" ) );
QCOMPARE( def->valueAsPythonString( "uri='complex' username=\"complex\"", context ), QStringLiteral( "'uri=\\'complex\\' username=\\\"complex\\\"'" ) );

QVariantMap map = def->toVariantMap();
QgsProcessingParameterFeatureSource fromMap( "x" );
Expand Down Expand Up @@ -4480,7 +4480,7 @@ void TestQgsProcessing::parameterFeatureSink()
QCOMPARE( def->valueAsPythonString( QVariant::fromValue( QgsProcessingOutputLayerDefinition( QgsProperty::fromValue( "abc" ) ) ), context ), QStringLiteral( "'abc'" ) );
QCOMPARE( def->valueAsPythonString( QVariant::fromValue( QgsProcessingOutputLayerDefinition( QgsProperty::fromExpression( "\"abc\" || \"def\"" ) ) ), context ), QStringLiteral( "QgsProperty.fromExpression('\"abc\" || \"def\"')" ) );
QCOMPARE( def->valueAsPythonString( QVariant::fromValue( QgsProperty::fromExpression( "\"a\"=1" ) ), context ), QStringLiteral( "QgsProperty.fromExpression('\"a\"=1')" ) );
QCOMPARE( def->valueAsPythonString( "uri='complex' username=\"complex\"", context ), QStringLiteral( "'uri=\'complex\' username=\"complex\"'" ) );
QCOMPARE( def->valueAsPythonString( "uri='complex' username=\"complex\"", context ), QStringLiteral( "'uri=\\'complex\\' username=\\\"complex\\\"'" ) );

QCOMPARE( def->defaultFileExtension(), QStringLiteral( "shp" ) );
QCOMPARE( def->generateTemporaryDestination(), QStringLiteral( "memory:" ) );
Expand Down Expand Up @@ -4608,7 +4608,7 @@ void TestQgsProcessing::parameterVectorOut()
QCOMPARE( def->valueAsPythonString( QVariant::fromValue( QgsProcessingOutputLayerDefinition( QgsProperty::fromValue( "abc" ) ) ), context ), QStringLiteral( "'abc'" ) );
QCOMPARE( def->valueAsPythonString( QVariant::fromValue( QgsProcessingOutputLayerDefinition( QgsProperty::fromExpression( "\"abc\" || \"def\"" ) ) ), context ), QStringLiteral( "QgsProperty.fromExpression('\"abc\" || \"def\"')" ) );
QCOMPARE( def->valueAsPythonString( QVariant::fromValue( QgsProperty::fromExpression( "\"a\"=1" ) ), context ), QStringLiteral( "QgsProperty.fromExpression('\"a\"=1')" ) );
QCOMPARE( def->valueAsPythonString( "uri='complex' username=\"complex\"", context ), QStringLiteral( "'uri=\'complex\' username=\"complex\"'" ) );
QCOMPARE( def->valueAsPythonString( "uri='complex' username=\"complex\"", context ), QStringLiteral( "'uri=\\'complex\\' username=\\\"complex\\\"'" ) );

QCOMPARE( def->defaultFileExtension(), QStringLiteral( "shp" ) );
QVERIFY( def->generateTemporaryDestination().endsWith( QStringLiteral( ".shp" ) ) );
Expand Down Expand Up @@ -4724,7 +4724,7 @@ void TestQgsProcessing::parameterRasterOut()
QCOMPARE( def->valueAsPythonString( QVariant::fromValue( QgsProcessingOutputLayerDefinition( QgsProperty::fromValue( "abc" ) ) ), context ), QStringLiteral( "'abc'" ) );
QCOMPARE( def->valueAsPythonString( QVariant::fromValue( QgsProcessingOutputLayerDefinition( QgsProperty::fromExpression( "\"abc\" || \"def\"" ) ) ), context ), QStringLiteral( "QgsProperty.fromExpression('\"abc\" || \"def\"')" ) );
QCOMPARE( def->valueAsPythonString( QVariant::fromValue( QgsProperty::fromExpression( "\"a\"=1" ) ), context ), QStringLiteral( "QgsProperty.fromExpression('\"a\"=1')" ) );
QCOMPARE( def->valueAsPythonString( "uri='complex' username=\"complex\"", context ), QStringLiteral( "'uri=\'complex\' username=\"complex\"'" ) );
QCOMPARE( def->valueAsPythonString( "uri='complex' username=\"complex\"", context ), QStringLiteral( "'uri=\\'complex\\' username=\\\"complex\\\"'" ) );

QVariantMap map = def->toVariantMap();
QgsProcessingParameterRasterDestination fromMap( "x" );
Expand Down Expand Up @@ -4847,7 +4847,7 @@ void TestQgsProcessing::parameterFileOut()
QCOMPARE( def->valueAsPythonString( QVariant::fromValue( QgsProcessingOutputLayerDefinition( QgsProperty::fromValue( "abc" ) ) ), context ), QStringLiteral( "'abc'" ) );
QCOMPARE( def->valueAsPythonString( QVariant::fromValue( QgsProcessingOutputLayerDefinition( QgsProperty::fromExpression( "\"abc\" || \"def\"" ) ) ), context ), QStringLiteral( "QgsProperty.fromExpression('\"abc\" || \"def\"')" ) );
QCOMPARE( def->valueAsPythonString( QVariant::fromValue( QgsProperty::fromExpression( "\"a\"=1" ) ), context ), QStringLiteral( "QgsProperty.fromExpression('\"a\"=1')" ) );
QCOMPARE( def->valueAsPythonString( "uri='complex' username=\"complex\"", context ), QStringLiteral( "'uri=\'complex\' username=\"complex\"'" ) );
QCOMPARE( def->valueAsPythonString( "uri='complex' username=\"complex\"", context ), QStringLiteral( "'uri=\\'complex\\' username=\\\"complex\\\"'" ) );

QVariantMap map = def->toVariantMap();
QgsProcessingParameterFileDestination fromMap( "x" );
Expand Down

0 comments on commit 3f16335

Please sign in to comment.