Skip to content

Commit

Permalink
Move provider algorithm handling to QgsProcessingProvider
Browse files Browse the repository at this point in the history
  • Loading branch information
nyalldawson committed Apr 7, 2017
1 parent 9c30a0e commit b3142a0
Show file tree
Hide file tree
Showing 35 changed files with 403 additions and 204 deletions.
6 changes: 6 additions & 0 deletions python/core/processing/qgsprocessingalgorithm.sip
Expand Up @@ -9,6 +9,7 @@




class QgsProcessingAlgorithm
{
%Docstring
Expand Down Expand Up @@ -85,6 +86,11 @@ class QgsProcessingAlgorithm
Returns the flags indicating how and when the algorithm operates and should be exposed to users.
%End

QgsProcessingProvider *provider() const;
%Docstring
Returns the provider to which this algorithm belongs.
%End

};
QFlags<QgsProcessingAlgorithm::Flag> operator|(QgsProcessingAlgorithm::Flag f1, QFlags<QgsProcessingAlgorithm::Flag> f2);

Expand Down
47 changes: 47 additions & 0 deletions python/core/processing/qgsprocessingprovider.sip
Expand Up @@ -95,6 +95,53 @@ class QgsProcessingProvider
\see supportedOutputVectorLayerExtensions()
%End

virtual bool load();
%Docstring
Loads the provider. This will be called when the plugin is being loaded, and any general
setup actions should occur in an overridden version of this method.
Subclasses should not load any algorithms in their load() implementations, as that must
occur within the loadAlgorithms() method.
\returns true if provider could be successfully loaded
\see unload()
%End

virtual void unload();
%Docstring
Unloads the provider. Any tear-down steps required by the provider should be implemented here.
\see load()
%End

void refreshAlgorithms();
%Docstring
Refreshes the algorithms available from the provider, causing it to re-populate with all associated algorithms.
%End

QList< QgsProcessingAlgorithm * > algorithms() const;
%Docstring
Returns a list of algorithms supplied by this provider.
\see algorithm()
%End

QgsProcessingAlgorithm *algorithm( const QString &name ) const;
%Docstring
Returns the matching algorithm by name, or a nullptr if no matching
algorithm is contained by this provider.
\see algorithms()
%End

protected:

virtual void loadAlgorithms() = 0;
%Docstring
Loads all algorithms belonging to this provider. Subclasses should implement this, calling
addAlgorithm() to register all their associated algorithms.
%End

bool addAlgorithm( QgsProcessingAlgorithm *algorithm /Transfer/ );
%Docstring
Adds an algorithm to the provider. Ownership of the algorithm is transferred to the provider.
%End

private:
QgsProcessingProvider( const QgsProcessingProvider &other );
};
Expand Down
Expand Up @@ -40,23 +40,19 @@ def __init__(self):
# Deactivate provider by default
self.activate = False

# Load algorithms
self.alglist = [ExampleAlgorithm()]
for alg in self.alglist:
alg.provider = self

def initializeSettings(self):
def load(self):
"""In this method we add settings needed to configure our
provider.
Do not forget to call the parent method, since it takes care
or automatically adding a setting for activating or
deactivating the algorithms in the provider.
"""
AlgorithmProvider.initializeSettings(self)
AlgorithmProvider.load(self)
ProcessingConfig.addSetting(Setting('Example algorithms',
ExampleAlgorithmProvider.MY_DUMMY_SETTING,
'Example setting', 'Default value'))
return True

def unload(self):
"""Setting should be removed here, so they do not appear anymore
Expand Down Expand Up @@ -84,7 +80,7 @@ def icon(self):
"""
return AlgorithmProvider.icon(self)

def _loadAlgorithms(self):
def loadAlgorithms(self):
"""Here we fill the list of algorithms in self.algs.
This method is called whenever the list of algorithms should
Expand All @@ -98,4 +94,5 @@ def _loadAlgorithms(self):
even if the list does not change, since the self.algs list is
cleared before calling this method.
"""
self.algs = self.alglist
for alg in [ExampleAlgorithm()]:
self.addAlgorithm(alg)
44 changes: 20 additions & 24 deletions python/plugins/processing/algs/gdal/GdalAlgorithmProvider.py
Expand Up @@ -98,15 +98,15 @@ class GdalAlgorithmProvider(AlgorithmProvider):

def __init__(self):
super().__init__()
self.createAlgsList()

def initializeSettings(self):
AlgorithmProvider.initializeSettings(self)
def load(self):
AlgorithmProvider.load(self)
ProcessingConfig.addSetting(Setting(
self.name(),
GdalUtils.GDAL_HELP_PATH,
self.tr('Location of GDAL docs'),
GdalUtils.gdalHelpPath()))
return True

def unload(self):
AlgorithmProvider.unload(self)
Expand All @@ -125,27 +125,23 @@ def icon(self):
def svgIconPath(self):
return QgsApplication.iconPath("providerGdal.svg")

def _loadAlgorithms(self):
self.algs = self.preloadedAlgs

def createAlgsList(self):
# First we populate the list of algorithms with those created
# extending GeoAlgorithm directly (those that execute GDAL
# using the console)
self.preloadedAlgs = [nearblack(), information(), warp(), translate(),
rgb2pct(), pct2rgb(), merge(), buildvrt(), polygonize(), gdaladdo(),
ClipByExtent(), ClipByMask(), contour(), rasterize(), proximity(),
sieve(), fillnodata(), ExtractProjection(), gdal2xyz(),
hillshade(), slope(), aspect(), tri(), tpi(), roughness(),
ColorRelief(), GridInvDist(), GridAverage(), GridNearest(),
GridDataMetrics(), gdaltindex(), gdalcalc(), rasterize_over(),
retile(), gdal2tiles(), AssignProjection(),
# ----- OGR tools -----
OgrInfo(), Ogr2Ogr(), Ogr2OgrClip(), Ogr2OgrClipExtent(),
Ogr2OgrToPostGis(), Ogr2OgrToPostGisList(), Ogr2OgrPointsOnLines(),
Ogr2OgrBuffer(), Ogr2OgrDissolve(), OneSideBuffer(),
OffsetCurve(), Ogr2OgrTableToPostGisList(), OgrSql(),
]
def loadAlgorithms(self):
algs = [nearblack(), information(), warp(), translate(),
rgb2pct(), pct2rgb(), merge(), buildvrt(), polygonize(), gdaladdo(),
ClipByExtent(), ClipByMask(), contour(), rasterize(), proximity(),
sieve(), fillnodata(), ExtractProjection(), gdal2xyz(),
hillshade(), slope(), aspect(), tri(), tpi(), roughness(),
ColorRelief(), GridInvDist(), GridAverage(), GridNearest(),
GridDataMetrics(), gdaltindex(), gdalcalc(), rasterize_over(),
retile(), gdal2tiles(), AssignProjection(),
# ----- OGR tools -----
OgrInfo(), Ogr2Ogr(), Ogr2OgrClip(), Ogr2OgrClipExtent(),
Ogr2OgrToPostGis(), Ogr2OgrToPostGisList(), Ogr2OgrPointsOnLines(),
Ogr2OgrBuffer(), Ogr2OgrDissolve(), OneSideBuffer(),
OffsetCurve(), Ogr2OgrTableToPostGisList(), OgrSql(),
]
for a in algs:
self.addAlgorithm(a)

def supportedOutputRasterLayerExtensions(self):
return GdalUtils.getSupportedRasterExtensions()
1 change: 0 additions & 1 deletion python/plugins/processing/algs/grass7/Grass7Algorithm.py
Expand Up @@ -98,7 +98,6 @@ def __init__(self, descriptionfile):

def getCopy(self):
newone = Grass7Algorithm(self.descriptionFile)
newone.provider = self.provider
return newone

def name(self):
Expand Down
18 changes: 10 additions & 8 deletions python/plugins/processing/algs/grass7/Grass7AlgorithmProvider.py
Expand Up @@ -44,10 +44,9 @@ class Grass7AlgorithmProvider(AlgorithmProvider):

def __init__(self):
super().__init__()
self.createAlgsList()

def initializeSettings(self):
AlgorithmProvider.initializeSettings(self)
def load(self):
AlgorithmProvider.load(self)
if isWindows() or isMac():
ProcessingConfig.addSetting(Setting(
self.name(),
Expand All @@ -66,6 +65,7 @@ def initializeSettings(self):
Grass7Utils.GRASS_HELP_PATH,
self.tr('Location of GRASS docs'),
Grass7Utils.grassHelpPath()))
return True

def unload(self):
AlgorithmProvider.unload(self)
Expand All @@ -76,14 +76,14 @@ def unload(self):
ProcessingConfig.removeSetting(Grass7Utils.GRASS_HELP_PATH)

def createAlgsList(self):
self.preloadedAlgs = []
algs = []
folder = Grass7Utils.grassDescriptionPath()
for descriptionFile in os.listdir(folder):
if descriptionFile.endswith('txt'):
try:
alg = Grass7Algorithm(os.path.join(folder, descriptionFile))
if alg.name().strip() != '':
self.preloadedAlgs.append(alg)
algs.append(alg)
else:
ProcessingLog.addToLog(
ProcessingLog.LOG_ERROR,
Expand All @@ -92,10 +92,12 @@ def createAlgsList(self):
ProcessingLog.addToLog(
ProcessingLog.LOG_ERROR,
self.tr('Could not open GRASS GIS 7 algorithm: {0}\n{1}').format(descriptionFile, str(e)))
self.preloadedAlgs.append(nviz7())
algs.append(nviz7())
return algs

def _loadAlgorithms(self):
self.algs = self.preloadedAlgs
def loadAlgorithms(self):
for a in self.createAlgsList():
self.addAlgorithm(a)

def name(self):
version = Grass7Utils.installedVersion()
Expand Down
2 changes: 1 addition & 1 deletion python/plugins/processing/algs/grass7/ext/r_texture.py
Expand Up @@ -53,7 +53,7 @@ def processOutputs(alg):
else:
angles = ['']

ext = alg.provider.supportedOutputRasterLayerExtensions()[0]
ext = alg.provider().getSupportedOutputRasterLayerExtensions()[0]
for method in methodList:
out = alg.getOutputValue(u'output')
for angle in angles:
Expand Down

0 comments on commit b3142a0

Please sign in to comment.