Skip to content

Commit d171ca7

Browse files
committedFeb 28, 2018
offer enterCategory in Python bindings by adding a context manager
1 parent 05ee1b0 commit d171ca7

File tree

4 files changed

+77
-10
lines changed

4 files changed

+77
-10
lines changed
 

‎python/core/__init__.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,25 @@ def __exit__(self, ex_type, ex_value, traceback):
211211
return False
212212

213213

214+
class ReadWriteContextEnterCategory():
215+
def __init__(self, context, category_name, details=None):
216+
self.context = context
217+
self.category_name = category_name
218+
self.details = details
219+
self.popper = None
220+
221+
def __enter__(self):
222+
self.popper = self.context._enterCategory(self.category_name, self.details)
223+
return self.context
224+
225+
def __exit__(self, ex_type, ex_value, traceback):
226+
del self.popper
227+
return True
228+
229+
230+
QgsReadWriteContext.enterCategory = ReadWriteContextEnterCategory
231+
232+
214233
class QgsTaskWrapper(QgsTask):
215234

216235
def __init__(self, description, flags, function, on_finished, *args, **kwargs):

‎python/core/qgsreadwritecontext.sip.in

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,25 @@ Append a message to the context
7171
.. versionadded:: 3.2
7272
%End
7373

74+
QgsReadWriteContextCategoryPopper enterCategory( const QString &category, const QString &details = QString() ) /PyName=_enterCategory/;
75+
%Docstring
76+
Push a category to the stack
77+
78+
.. note::
79+
80+
The return value should always be used so category can be automatically left.
81+
82+
.. note::
83+
84+
It is not aimed at being used in Python. Instead use the context manager.
85+
.. code-block:: python
86+
87+
context = QgsReadWriteContext()
88+
with QgsReadWriteContext.enterCategory(context, category, details):
89+
# do something
90+
91+
.. versionadded:: 3.2
92+
%End
7493

7594
QList<QgsReadWriteContext::ReadWriteMessage> takeMessages();
7695
%Docstring
@@ -83,6 +102,27 @@ Return the stored messages and remove them
83102
};
84103

85104

105+
class QgsReadWriteContextCategoryPopper
106+
{
107+
%Docstring
108+
QgsReadWriteContextCategoryPopper allows entering a context category
109+
and takes care of leaving this category on deletion of the class.
110+
This would happen when it gets out of scope.
111+
112+
.. versionadded:: 3.2
113+
%End
114+
115+
%TypeHeaderCode
116+
#include "qgsreadwritecontext.h"
117+
%End
118+
public:
119+
QgsReadWriteContextCategoryPopper( QgsReadWriteContext &context );
120+
%Docstring
121+
Creates a popper
122+
%End
123+
~QgsReadWriteContextCategoryPopper();
124+
};
125+
86126
/************************************************************************
87127
* This file has been generated automatically from *
88128
* *

‎scripts/sipify.pl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -847,6 +847,8 @@ sub detect_non_method_member{
847847
$LINE =~ s/^(\s*template\s*<)(?:class|typename) (\w+>)(.*)$/$1$2$3/;
848848
$LINE =~ s/\s*\boverride\b//;
849849
$LINE =~ s/\s*\bextern \b//;
850+
$LINE =~ s/\s*\bMAYBE_UNUSED \b//;
851+
$LINE =~ s/\s*\bNODISCARD \b//;
850852
$LINE =~ s/^(\s*)?(const )?(virtual |static )?inline /$1$2$3/;
851853
$LINE =~ s/\bconstexpr\b/const/;
852854
$LINE =~ s/\bnullptr\b/0/g;

‎src/core/qgsreadwritecontext.h

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -80,16 +80,18 @@ class CORE_EXPORT QgsReadWriteContext
8080
*/
8181
void pushMessage( const QString &message, Qgis::MessageLevel level = Qgis::Warning );
8282

83-
#ifndef SIP_RUN
84-
8583
/**
8684
* Push a category to the stack
8785
* \note The return value should always be used so category can be automatically left.
88-
* \note Not available in the Python bindings.
86+
* \note It is not aimed at being used in Python. Instead use the context manager.
87+
* \code{.py}
88+
* context = QgsReadWriteContext()
89+
* with QgsReadWriteContext.enterCategory(context, category, details):
90+
* # do something
91+
* \endcode
8992
* \since QGIS 3.2
9093
*/
91-
MAYBE_UNUSED NODISCARD QgsReadWriteContextCategoryPopper enterCategory( const QString &category, const QString &details = QString() );
92-
#endif
94+
MAYBE_UNUSED NODISCARD QgsReadWriteContextCategoryPopper enterCategory( const QString &category, const QString &details = QString() ) SIP_PYNAME( _enterCategory );
9395

9496
/**
9597
* Return the stored messages and remove them
@@ -109,17 +111,21 @@ class CORE_EXPORT QgsReadWriteContext
109111
friend class QgsReadWriteContextCategoryPopper;
110112
};
111113

112-
#ifndef SIP_RUN
113-
///@cond PRIVATE
114-
class QgsReadWriteContextCategoryPopper
114+
115+
/**
116+
* QgsReadWriteContextCategoryPopper allows entering a context category
117+
* and takes care of leaving this category on deletion of the class.
118+
* This would happen when it gets out of scope.
119+
* \since 3.2
120+
*/
121+
class CORE_EXPORT QgsReadWriteContextCategoryPopper
115122
{
116123
public:
124+
//! Creates a popper
117125
QgsReadWriteContextCategoryPopper( QgsReadWriteContext &context ) : mContext( context ) {}
118126
~QgsReadWriteContextCategoryPopper() {mContext.leaveCategory();}
119127
private:
120128
QgsReadWriteContext mContext;
121129
};
122-
///@endcond PRIVATE
123-
#endif
124130

125131
#endif // QGSREADWRITECONTEXT_H

0 commit comments

Comments
 (0)
Please sign in to comment.