Skip to content

Commit b3142a0

Browse files
committed
Move provider algorithm handling to QgsProcessingProvider
1 parent 9c30a0e commit b3142a0

35 files changed

+403
-204
lines changed

python/core/processing/qgsprocessingalgorithm.sip

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010

1111

12+
1213
class QgsProcessingAlgorithm
1314
{
1415
%Docstring
@@ -85,6 +86,11 @@ class QgsProcessingAlgorithm
8586
Returns the flags indicating how and when the algorithm operates and should be exposed to users.
8687
%End
8788

89+
QgsProcessingProvider *provider() const;
90+
%Docstring
91+
Returns the provider to which this algorithm belongs.
92+
%End
93+
8894
};
8995
QFlags<QgsProcessingAlgorithm::Flag> operator|(QgsProcessingAlgorithm::Flag f1, QFlags<QgsProcessingAlgorithm::Flag> f2);
9096

python/core/processing/qgsprocessingprovider.sip

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,53 @@ class QgsProcessingProvider
9595
\see supportedOutputVectorLayerExtensions()
9696
%End
9797

98+
virtual bool load();
99+
%Docstring
100+
Loads the provider. This will be called when the plugin is being loaded, and any general
101+
setup actions should occur in an overridden version of this method.
102+
Subclasses should not load any algorithms in their load() implementations, as that must
103+
occur within the loadAlgorithms() method.
104+
\returns true if provider could be successfully loaded
105+
\see unload()
106+
%End
107+
108+
virtual void unload();
109+
%Docstring
110+
Unloads the provider. Any tear-down steps required by the provider should be implemented here.
111+
\see load()
112+
%End
113+
114+
void refreshAlgorithms();
115+
%Docstring
116+
Refreshes the algorithms available from the provider, causing it to re-populate with all associated algorithms.
117+
%End
118+
119+
QList< QgsProcessingAlgorithm * > algorithms() const;
120+
%Docstring
121+
Returns a list of algorithms supplied by this provider.
122+
\see algorithm()
123+
%End
124+
125+
QgsProcessingAlgorithm *algorithm( const QString &name ) const;
126+
%Docstring
127+
Returns the matching algorithm by name, or a nullptr if no matching
128+
algorithm is contained by this provider.
129+
\see algorithms()
130+
%End
131+
132+
protected:
133+
134+
virtual void loadAlgorithms() = 0;
135+
%Docstring
136+
Loads all algorithms belonging to this provider. Subclasses should implement this, calling
137+
addAlgorithm() to register all their associated algorithms.
138+
%End
139+
140+
bool addAlgorithm( QgsProcessingAlgorithm *algorithm /Transfer/ );
141+
%Docstring
142+
Adds an algorithm to the provider. Ownership of the algorithm is transferred to the provider.
143+
%End
144+
98145
private:
99146
QgsProcessingProvider( const QgsProcessingProvider &other );
100147
};

python/plugins/processing/algs/exampleprovider/ExampleAlgorithmProvider.py

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -40,23 +40,19 @@ def __init__(self):
4040
# Deactivate provider by default
4141
self.activate = False
4242

43-
# Load algorithms
44-
self.alglist = [ExampleAlgorithm()]
45-
for alg in self.alglist:
46-
alg.provider = self
47-
48-
def initializeSettings(self):
43+
def load(self):
4944
"""In this method we add settings needed to configure our
5045
provider.
5146
5247
Do not forget to call the parent method, since it takes care
5348
or automatically adding a setting for activating or
5449
deactivating the algorithms in the provider.
5550
"""
56-
AlgorithmProvider.initializeSettings(self)
51+
AlgorithmProvider.load(self)
5752
ProcessingConfig.addSetting(Setting('Example algorithms',
5853
ExampleAlgorithmProvider.MY_DUMMY_SETTING,
5954
'Example setting', 'Default value'))
55+
return True
6056

6157
def unload(self):
6258
"""Setting should be removed here, so they do not appear anymore
@@ -84,7 +80,7 @@ def icon(self):
8480
"""
8581
return AlgorithmProvider.icon(self)
8682

87-
def _loadAlgorithms(self):
83+
def loadAlgorithms(self):
8884
"""Here we fill the list of algorithms in self.algs.
8985
9086
This method is called whenever the list of algorithms should
@@ -98,4 +94,5 @@ def _loadAlgorithms(self):
9894
even if the list does not change, since the self.algs list is
9995
cleared before calling this method.
10096
"""
101-
self.algs = self.alglist
97+
for alg in [ExampleAlgorithm()]:
98+
self.addAlgorithm(alg)

python/plugins/processing/algs/gdal/GdalAlgorithmProvider.py

Lines changed: 20 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -98,15 +98,15 @@ class GdalAlgorithmProvider(AlgorithmProvider):
9898

9999
def __init__(self):
100100
super().__init__()
101-
self.createAlgsList()
102101

103-
def initializeSettings(self):
104-
AlgorithmProvider.initializeSettings(self)
102+
def load(self):
103+
AlgorithmProvider.load(self)
105104
ProcessingConfig.addSetting(Setting(
106105
self.name(),
107106
GdalUtils.GDAL_HELP_PATH,
108107
self.tr('Location of GDAL docs'),
109108
GdalUtils.gdalHelpPath()))
109+
return True
110110

111111
def unload(self):
112112
AlgorithmProvider.unload(self)
@@ -125,27 +125,23 @@ def icon(self):
125125
def svgIconPath(self):
126126
return QgsApplication.iconPath("providerGdal.svg")
127127

128-
def _loadAlgorithms(self):
129-
self.algs = self.preloadedAlgs
130-
131-
def createAlgsList(self):
132-
# First we populate the list of algorithms with those created
133-
# extending GeoAlgorithm directly (those that execute GDAL
134-
# using the console)
135-
self.preloadedAlgs = [nearblack(), information(), warp(), translate(),
136-
rgb2pct(), pct2rgb(), merge(), buildvrt(), polygonize(), gdaladdo(),
137-
ClipByExtent(), ClipByMask(), contour(), rasterize(), proximity(),
138-
sieve(), fillnodata(), ExtractProjection(), gdal2xyz(),
139-
hillshade(), slope(), aspect(), tri(), tpi(), roughness(),
140-
ColorRelief(), GridInvDist(), GridAverage(), GridNearest(),
141-
GridDataMetrics(), gdaltindex(), gdalcalc(), rasterize_over(),
142-
retile(), gdal2tiles(), AssignProjection(),
143-
# ----- OGR tools -----
144-
OgrInfo(), Ogr2Ogr(), Ogr2OgrClip(), Ogr2OgrClipExtent(),
145-
Ogr2OgrToPostGis(), Ogr2OgrToPostGisList(), Ogr2OgrPointsOnLines(),
146-
Ogr2OgrBuffer(), Ogr2OgrDissolve(), OneSideBuffer(),
147-
OffsetCurve(), Ogr2OgrTableToPostGisList(), OgrSql(),
148-
]
128+
def loadAlgorithms(self):
129+
algs = [nearblack(), information(), warp(), translate(),
130+
rgb2pct(), pct2rgb(), merge(), buildvrt(), polygonize(), gdaladdo(),
131+
ClipByExtent(), ClipByMask(), contour(), rasterize(), proximity(),
132+
sieve(), fillnodata(), ExtractProjection(), gdal2xyz(),
133+
hillshade(), slope(), aspect(), tri(), tpi(), roughness(),
134+
ColorRelief(), GridInvDist(), GridAverage(), GridNearest(),
135+
GridDataMetrics(), gdaltindex(), gdalcalc(), rasterize_over(),
136+
retile(), gdal2tiles(), AssignProjection(),
137+
# ----- OGR tools -----
138+
OgrInfo(), Ogr2Ogr(), Ogr2OgrClip(), Ogr2OgrClipExtent(),
139+
Ogr2OgrToPostGis(), Ogr2OgrToPostGisList(), Ogr2OgrPointsOnLines(),
140+
Ogr2OgrBuffer(), Ogr2OgrDissolve(), OneSideBuffer(),
141+
OffsetCurve(), Ogr2OgrTableToPostGisList(), OgrSql(),
142+
]
143+
for a in algs:
144+
self.addAlgorithm(a)
149145

150146
def supportedOutputRasterLayerExtensions(self):
151147
return GdalUtils.getSupportedRasterExtensions()

python/plugins/processing/algs/grass7/Grass7Algorithm.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,6 @@ def __init__(self, descriptionfile):
9898

9999
def getCopy(self):
100100
newone = Grass7Algorithm(self.descriptionFile)
101-
newone.provider = self.provider
102101
return newone
103102

104103
def name(self):

python/plugins/processing/algs/grass7/Grass7AlgorithmProvider.py

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -44,10 +44,9 @@ class Grass7AlgorithmProvider(AlgorithmProvider):
4444

4545
def __init__(self):
4646
super().__init__()
47-
self.createAlgsList()
4847

49-
def initializeSettings(self):
50-
AlgorithmProvider.initializeSettings(self)
48+
def load(self):
49+
AlgorithmProvider.load(self)
5150
if isWindows() or isMac():
5251
ProcessingConfig.addSetting(Setting(
5352
self.name(),
@@ -66,6 +65,7 @@ def initializeSettings(self):
6665
Grass7Utils.GRASS_HELP_PATH,
6766
self.tr('Location of GRASS docs'),
6867
Grass7Utils.grassHelpPath()))
68+
return True
6969

7070
def unload(self):
7171
AlgorithmProvider.unload(self)
@@ -76,14 +76,14 @@ def unload(self):
7676
ProcessingConfig.removeSetting(Grass7Utils.GRASS_HELP_PATH)
7777

7878
def createAlgsList(self):
79-
self.preloadedAlgs = []
79+
algs = []
8080
folder = Grass7Utils.grassDescriptionPath()
8181
for descriptionFile in os.listdir(folder):
8282
if descriptionFile.endswith('txt'):
8383
try:
8484
alg = Grass7Algorithm(os.path.join(folder, descriptionFile))
8585
if alg.name().strip() != '':
86-
self.preloadedAlgs.append(alg)
86+
algs.append(alg)
8787
else:
8888
ProcessingLog.addToLog(
8989
ProcessingLog.LOG_ERROR,
@@ -92,10 +92,12 @@ def createAlgsList(self):
9292
ProcessingLog.addToLog(
9393
ProcessingLog.LOG_ERROR,
9494
self.tr('Could not open GRASS GIS 7 algorithm: {0}\n{1}').format(descriptionFile, str(e)))
95-
self.preloadedAlgs.append(nviz7())
95+
algs.append(nviz7())
96+
return algs
9697

97-
def _loadAlgorithms(self):
98-
self.algs = self.preloadedAlgs
98+
def loadAlgorithms(self):
99+
for a in self.createAlgsList():
100+
self.addAlgorithm(a)
99101

100102
def name(self):
101103
version = Grass7Utils.installedVersion()

python/plugins/processing/algs/grass7/ext/r_texture.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ def processOutputs(alg):
5353
else:
5454
angles = ['']
5555

56-
ext = alg.provider.supportedOutputRasterLayerExtensions()[0]
56+
ext = alg.provider().getSupportedOutputRasterLayerExtensions()[0]
5757
for method in methodList:
5858
out = alg.getOutputValue(u'output')
5959
for angle in angles:

0 commit comments

Comments
 (0)