Skip to content
Permalink
Browse files

[processing] Automatically load the native and 3d providers

when a script calls Processing.initialize() if they are not
already loaded

This means the following ugly code can be avoided:

    Processing.initialize()
    QgsApplication.processingRegistry().addProvider(QgsNativeAlgorithms(QgsApplication.processingRegistry()))
    QgsApplication.processingRegistry().addProvider(Qgs3DAlgorithms(QgsApplication.processingRegistry()))

and instead the call to Processing.initialize() is sufficient to load
ALL providers

Fixes #41310

(cherry picked from commit b402544)
  • Loading branch information
nyalldawson committed Feb 19, 2021
1 parent 0afe729 commit 5bdda19f58779ba556faadbbd16f71d901d06230
@@ -42,6 +42,7 @@
QgsProcessingOutputMultipleLayers,
QgsProcessingFeedback,
QgsRuntimeProfiler)
from qgis.analysis import QgsNativeAlgorithms

import processing
from processing.core.ProcessingConfig import ProcessingConfig
@@ -99,6 +100,20 @@ def initialize():
return

with QgsRuntimeProfiler.profile('Initialize'):

# add native provider if not already added
if "native" not in [p.id() for p in QgsApplication.processingRegistry().providers()]:
QgsApplication.processingRegistry().addProvider(QgsNativeAlgorithms(QgsApplication.processingRegistry()))

# add 3d provider if available and not already added
if "3d" not in [p.id() for p in QgsApplication.processingRegistry().providers()]:
try:
from qgis._3d import Qgs3DAlgorithms
QgsApplication.processingRegistry().addProvider(Qgs3DAlgorithms(QgsApplication.processingRegistry()))
except ImportError:
# no 3d library available
pass

# Add the basic providers
for c in [
QgisAlgorithmProvider,
@@ -404,7 +404,6 @@ def setUpClass(cls):
start_app()
from processing.core.Processing import Processing
Processing.initialize()
QgsApplication.processingRegistry().addProvider(QgsNativeAlgorithms())
cls.cleanup_paths = []

@classmethod
@@ -54,7 +54,6 @@ def setUpClass(cls):
"QGIS_TestPyQgsProcessingCheckValidity")
QgsSettings().clear()
Processing.initialize()
QgsApplication.processingRegistry().addProvider(QgsNativeAlgorithms())
cls.registry = QgsApplication.instance().processingRegistry()

def _make_layer(self, layer_wkb_name):
@@ -44,7 +44,6 @@ def setUpClass(cls):
start_app()
from processing.core.Processing import Processing
Processing.initialize()
QgsApplication.processingRegistry().addProvider(QgsNativeAlgorithms())
cls.cleanup_paths = []
cls.in_place_layers = {}
cls.vector_layer_params = {}
@@ -67,20 +67,16 @@ def setUpClass(cls):
start_app()
from processing.core.Processing import Processing
Processing.initialize()
ProcessingConfig.setSettingValue(ModelerUtils.MODELS_FOLDER, os.path.join(os.path.dirname(__file__), 'models'))
QgsApplication.processingRegistry().addProvider(QgsNativeAlgorithms())
cls.cleanup_paths = []
cls.in_place_layers = {}
cls.vector_layer_params = {}
cls._original_models_folder = ProcessingConfig.getSetting(ModelerUtils.MODELS_FOLDER)

@classmethod
def tearDownClass(cls):
from processing.core.Processing import Processing
Processing.deinitialize()
for path in cls.cleanup_paths:
shutil.rmtree(path)
ProcessingConfig.setSettingValue(ModelerUtils.MODELS_FOLDER, cls._original_models_folder)

def test_definition_file(self):
return 'qgis_algorithm_tests1.yaml'
@@ -42,20 +42,16 @@ def setUpClass(cls):
start_app()
from processing.core.Processing import Processing
Processing.initialize()
ProcessingConfig.setSettingValue(ModelerUtils.MODELS_FOLDER, os.path.join(os.path.dirname(__file__), 'models'))
QgsApplication.processingRegistry().addProvider(QgsNativeAlgorithms())
cls.cleanup_paths = []
cls.in_place_layers = {}
cls.vector_layer_params = {}
cls._original_models_folder = ProcessingConfig.getSetting(ModelerUtils.MODELS_FOLDER)

@classmethod
def tearDownClass(cls):
from processing.core.Processing import Processing
Processing.deinitialize()
for path in cls.cleanup_paths:
shutil.rmtree(path)
ProcessingConfig.setSettingValue(ModelerUtils.MODELS_FOLDER, cls._original_models_folder)

def test_definition_file(self):
return 'qgis_algorithm_tests2.yaml'
@@ -42,20 +42,16 @@ def setUpClass(cls):
start_app()
from processing.core.Processing import Processing
Processing.initialize()
ProcessingConfig.setSettingValue(ModelerUtils.MODELS_FOLDER, os.path.join(os.path.dirname(__file__), 'models'))
QgsApplication.processingRegistry().addProvider(QgsNativeAlgorithms())
cls.cleanup_paths = []
cls.in_place_layers = {}
cls.vector_layer_params = {}
cls._original_models_folder = ProcessingConfig.getSetting(ModelerUtils.MODELS_FOLDER)

@classmethod
def tearDownClass(cls):
from processing.core.Processing import Processing
Processing.deinitialize()
for path in cls.cleanup_paths:
shutil.rmtree(path)
ProcessingConfig.setSettingValue(ModelerUtils.MODELS_FOLDER, cls._original_models_folder)

def test_definition_file(self):
return 'qgis_algorithm_tests3.yaml'
@@ -42,8 +42,13 @@ def setUpClass(cls):
start_app()
from processing.core.Processing import Processing
Processing.initialize()

# change the model provider folder so that it looks in the test directory for models
ProcessingConfig.setSettingValue(ModelerUtils.MODELS_FOLDER, os.path.join(os.path.dirname(__file__), 'models'))
QgsApplication.processingRegistry().addProvider(QgsNativeAlgorithms())
for p in QgsApplication.processingRegistry().providers():
if p.id() == "model":
p.refreshAlgorithms()

cls.cleanup_paths = []
cls.in_place_layers = {}
cls.vector_layer_params = {}

0 comments on commit 5bdda19

Please sign in to comment.