QGIS/python/core/additions/providermetadata.py
Martin Dobias 90910b0447 Patch QgsProviderMetadata in Python to avoid API break
In C++ we do not support QgsProviderMetadata(key, description, createFunc) anymore,
but for python code we can have a patched class that still supports createFunc.

Why not keep the variant with createFunc in C++ as well? Because... SIP!
With newly added virtual methods in QgsProviderMetadata, SIP started to generate
a sip-specific subclass to handle derived classes in Python, however due to the variant
with PyObject* and custom MethodCode it was getting confused about what constructors
are available in C++ and failing to compile.
2019-06-21 18:27:36 +02:00

41 lines
1.8 KiB
Python

# -*- coding: utf-8 -*-
"""
***************************************************************************
providermetadata.py
---------------------
Date : June 2019
Copyright : (C) 2019 by Martin Dobias
Email : wonder dot sk at gmail dot com
***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************
"""
from qgis._core import QgsProviderMetadata
class PyProviderMetadata(QgsProviderMetadata):
""" wrapper around QgsProviderMetadata to keep the existing Python code running which registers
data providers by passing a custom python createProvider() function to QgsProviderMetadata
constructor. The proper new way of doing it is to subclass QgsProviderMetadata and implement
its virtual functions.
TODO: QGIS 4 - remove this wrapper (only subclassing of QgsProviderMetadata should be used)
"""
# this is a workaround to keep references to metadata classes
# so they are not removed when the variable gets out of scope
_kept_refs = []
def __init__(self, key, description, library_or_create_func=None):
super().__init__(key, description)
if callable(library_or_create_func):
self.createProvider = library_or_create_func
PyProviderMetadata._kept_refs.append(self)