Allow temporary folder location to be overridden through

QgsProcessingContext
This commit is contained in:
Nyall Dawson 2023-03-20 14:49:49 +10:00
parent 30598b9ad9
commit fa22256717
4 changed files with 66 additions and 1 deletions

View File

@ -585,6 +585,30 @@ Sets the logging ``level`` for algorithms to use when pushing feedback messages
.. seealso:: :py:func:`logLevel`
.. versionadded:: 3.20
%End
QString temporaryFolder() const;
%Docstring
Returns the (optional) temporary folder to use when running algorithms.
If set, this overrides the standard global Processing temporary folder and should be used
for all temporary files created during algorithm execution.
.. seealso:: :py:func:`setTemporaryFolder`
.. versionadded:: 3.32
%End
void setTemporaryFolder( const QString &folder );
%Docstring
Sets the (optional) temporary ``folder`` to use when running algorithms.
If set, this overrides the standard global Processing temporary folder and should be used
for all temporary files created during algorithm execution.
.. seealso:: :py:func:`temporaryFolder`
.. versionadded:: 3.32
%End
QVariantMap exportToMap() const;

View File

@ -147,6 +147,16 @@ void QgsProcessingContext::setLogLevel( LogLevel level )
mLogLevel = level;
}
QString QgsProcessingContext::temporaryFolder() const
{
return mTemporaryFolderOverride;
}
void QgsProcessingContext::setTemporaryFolder( const QString &folder )
{
mTemporaryFolderOverride = folder;
}
QVariantMap QgsProcessingContext::exportToMap() const
{
QVariantMap res;

View File

@ -100,6 +100,7 @@ class CORE_EXPORT QgsProcessingContext
mDistanceUnit = other.mDistanceUnit;
mAreaUnit = other.mAreaUnit;
mLogLevel = other.mLogLevel;
mTemporaryFolderOverride = other.mTemporaryFolderOverride;
}
/**
@ -657,6 +658,28 @@ class CORE_EXPORT QgsProcessingContext
*/
void setLogLevel( LogLevel level );
/**
* Returns the (optional) temporary folder to use when running algorithms.
*
* If set, this overrides the standard global Processing temporary folder and should be used
* for all temporary files created during algorithm execution.
*
* \see setTemporaryFolder()
* \since QGIS 3.32
*/
QString temporaryFolder() const;
/**
* Sets the (optional) temporary \a folder to use when running algorithms.
*
* If set, this overrides the standard global Processing temporary folder and should be used
* for all temporary files created during algorithm execution.
*
* \see temporaryFolder()
* \since QGIS 3.32
*/
void setTemporaryFolder( const QString &folder );
/**
* Exports the context's settings to a variant map.
*
@ -713,6 +736,8 @@ class CORE_EXPORT QgsProcessingContext
LogLevel mLogLevel = DefaultLevel;
QString mTemporaryFolderOverride;
#ifdef SIP_RUN
QgsProcessingContext( const QgsProcessingContext &other );
#endif

View File

@ -1037,7 +1037,13 @@ QString QgsProcessingUtils::tempFolder( const QgsProcessingContext *context )
static QString sFolder;
static QMutex sMutex;
QMutexLocker locker( &sMutex );
const QString basePath = QgsProcessing::settingsTempPath->value();
QString basePath;
if ( context )
basePath = context->temporaryFolder();
if ( basePath.isEmpty() )
basePath = QgsProcessing::settingsTempPath->value();
if ( basePath.isEmpty() )
{
// default setting -- automatically create a temp folder