Skip to content
Permalink
Browse files

offer enterCategory in Python bindings by adding a context manager

  • Loading branch information
3nids committed Feb 28, 2018
1 parent 05ee1b0 commit d171ca7ae457eb23a11e4aff956abcff8989ef04
Showing with 77 additions and 10 deletions.
  1. +19 −0 python/core/__init__.py
  2. +40 −0 python/core/qgsreadwritecontext.sip.in
  3. +2 −0 scripts/sipify.pl
  4. +16 −10 src/core/qgsreadwritecontext.h
@@ -211,6 +211,25 @@ def __exit__(self, ex_type, ex_value, traceback):
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 @@ sub detect_non_method_member{
$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

0 comments on commit d171ca7

Please sign in to comment.
You can’t perform that action at this time.