mirror of
https://github.com/qgis/QGIS.git
synced 2025-04-14 00:07:35 -04:00
offer enterCategory in Python bindings by adding a context manager
This commit is contained in:
parent
05ee1b02c3
commit
d171ca7ae4
@ -211,6 +211,25 @@ class edit(object):
|
||||
return False
|
||||
|
||||
|
||||
class ReadWriteContextEnterCategory():
|
||||
def __init__(self, context, category_name, details=None):
|
||||
self.context = context
|
||||
self.category_name = category_name
|
||||
self.details = details
|
||||
self.popper = None
|
||||
|
||||
def __enter__(self):
|
||||
self.popper = self.context._enterCategory(self.category_name, self.details)
|
||||
return self.context
|
||||
|
||||
def __exit__(self, ex_type, ex_value, traceback):
|
||||
del self.popper
|
||||
return True
|
||||
|
||||
|
||||
QgsReadWriteContext.enterCategory = ReadWriteContextEnterCategory
|
||||
|
||||
|
||||
class QgsTaskWrapper(QgsTask):
|
||||
|
||||
def __init__(self, description, flags, function, on_finished, *args, **kwargs):
|
||||
|
@ -71,6 +71,25 @@ Append a message to the context
|
||||
.. versionadded:: 3.2
|
||||
%End
|
||||
|
||||
QgsReadWriteContextCategoryPopper enterCategory( const QString &category, const QString &details = QString() ) /PyName=_enterCategory/;
|
||||
%Docstring
|
||||
Push a category to the stack
|
||||
|
||||
.. note::
|
||||
|
||||
The return value should always be used so category can be automatically left.
|
||||
|
||||
.. note::
|
||||
|
||||
It is not aimed at being used in Python. Instead use the context manager.
|
||||
.. code-block:: python
|
||||
|
||||
context = QgsReadWriteContext()
|
||||
with QgsReadWriteContext.enterCategory(context, category, details):
|
||||
# do something
|
||||
|
||||
.. versionadded:: 3.2
|
||||
%End
|
||||
|
||||
QList<QgsReadWriteContext::ReadWriteMessage> takeMessages();
|
||||
%Docstring
|
||||
@ -83,6 +102,27 @@ Return the stored messages and remove them
|
||||
};
|
||||
|
||||
|
||||
class QgsReadWriteContextCategoryPopper
|
||||
{
|
||||
%Docstring
|
||||
QgsReadWriteContextCategoryPopper allows entering a context category
|
||||
and takes care of leaving this category on deletion of the class.
|
||||
This would happen when it gets out of scope.
|
||||
|
||||
.. versionadded:: 3.2
|
||||
%End
|
||||
|
||||
%TypeHeaderCode
|
||||
#include "qgsreadwritecontext.h"
|
||||
%End
|
||||
public:
|
||||
QgsReadWriteContextCategoryPopper( QgsReadWriteContext &context );
|
||||
%Docstring
|
||||
Creates a popper
|
||||
%End
|
||||
~QgsReadWriteContextCategoryPopper();
|
||||
};
|
||||
|
||||
/************************************************************************
|
||||
* This file has been generated automatically from *
|
||||
* *
|
||||
|
@ -847,6 +847,8 @@ while ($LINE_IDX < $LINE_COUNT){
|
||||
$LINE =~ s/^(\s*template\s*<)(?:class|typename) (\w+>)(.*)$/$1$2$3/;
|
||||
$LINE =~ s/\s*\boverride\b//;
|
||||
$LINE =~ s/\s*\bextern \b//;
|
||||
$LINE =~ s/\s*\bMAYBE_UNUSED \b//;
|
||||
$LINE =~ s/\s*\bNODISCARD \b//;
|
||||
$LINE =~ s/^(\s*)?(const )?(virtual |static )?inline /$1$2$3/;
|
||||
$LINE =~ s/\bconstexpr\b/const/;
|
||||
$LINE =~ s/\bnullptr\b/0/g;
|
||||
|
@ -80,16 +80,18 @@ class CORE_EXPORT QgsReadWriteContext
|
||||
*/
|
||||
void pushMessage( const QString &message, Qgis::MessageLevel level = Qgis::Warning );
|
||||
|
||||
#ifndef SIP_RUN
|
||||
|
||||
/**
|
||||
* Push a category to the stack
|
||||
* \note The return value should always be used so category can be automatically left.
|
||||
* \note Not available in the Python bindings.
|
||||
* \note It is not aimed at being used in Python. Instead use the context manager.
|
||||
* \code{.py}
|
||||
* context = QgsReadWriteContext()
|
||||
* with QgsReadWriteContext.enterCategory(context, category, details):
|
||||
* # do something
|
||||
* \endcode
|
||||
* \since QGIS 3.2
|
||||
*/
|
||||
MAYBE_UNUSED NODISCARD QgsReadWriteContextCategoryPopper enterCategory( const QString &category, const QString &details = QString() );
|
||||
#endif
|
||||
MAYBE_UNUSED NODISCARD QgsReadWriteContextCategoryPopper enterCategory( const QString &category, const QString &details = QString() ) SIP_PYNAME( _enterCategory );
|
||||
|
||||
/**
|
||||
* Return the stored messages and remove them
|
||||
@ -109,17 +111,21 @@ class CORE_EXPORT QgsReadWriteContext
|
||||
friend class QgsReadWriteContextCategoryPopper;
|
||||
};
|
||||
|
||||
#ifndef SIP_RUN
|
||||
///@cond PRIVATE
|
||||
class QgsReadWriteContextCategoryPopper
|
||||
|
||||
/**
|
||||
* QgsReadWriteContextCategoryPopper allows entering a context category
|
||||
* and takes care of leaving this category on deletion of the class.
|
||||
* This would happen when it gets out of scope.
|
||||
* \since 3.2
|
||||
*/
|
||||
class CORE_EXPORT QgsReadWriteContextCategoryPopper
|
||||
{
|
||||
public:
|
||||
//! Creates a popper
|
||||
QgsReadWriteContextCategoryPopper( QgsReadWriteContext &context ) : mContext( context ) {}
|
||||
~QgsReadWriteContextCategoryPopper() {mContext.leaveCategory();}
|
||||
private:
|
||||
QgsReadWriteContext mContext;
|
||||
};
|
||||
///@endcond PRIVATE
|
||||
#endif
|
||||
|
||||
#endif // QGSREADWRITECONTEXT_H
|
||||
|
Loading…
x
Reference in New Issue
Block a user