Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

support grass algorithms in profile folder #53246

Merged
merged 10 commits into from
Jul 6, 2023
17 changes: 12 additions & 5 deletions python/plugins/grassprovider/Grass7Algorithm.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import uuid
import math
import importlib
from pathlib import Path

from qgis.PyQt.QtCore import QCoreApplication, QUrl

Expand Down Expand Up @@ -139,11 +140,17 @@ def __init__(self, descriptionfile):

# Use the ext mechanism
name = self.name().replace('.', '_')
self.module = None
try:
self.module = importlib.import_module(
'grassprovider.ext.{}'.format(name))
except ImportError:
self.module = None
extpath = self.descriptionFile.parents[1].joinpath('ext', name + '.py')
# this check makes it a bit faster
if extpath.exists():
spec = importlib.util.spec_from_file_location('grassprovider.ext.' + name, extpath)
self.module = importlib.util.module_from_spec(spec)
spec.loader.exec_module(self.module)
except Exception as e:
QgsMessageLog.logMessage(self.tr('Failed to load: {0}\n{1}').format(extpath, e), 'Processing', Qgis.Critical)
pass

def createInstance(self):
return self.__class__(self.descriptionFile)
Expand Down Expand Up @@ -200,7 +207,7 @@ def defineCharacteristicsFromFile(self):
"""
Create algorithm parameters and outputs from a text file.
"""
with open(self.descriptionFile) as lines:
with self.descriptionFile.open() as lines:
# First line of the file is the Grass algorithm name
line = lines.readline().strip('\n').strip()
self.grass7Name = line
Expand Down
13 changes: 7 additions & 6 deletions python/plugins/grassprovider/Grass7AlgorithmProvider.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
__copyright__ = '(C) 2014, Victor Olaya'

import os
from pathlib import Path
from qgis.PyQt.QtCore import QCoreApplication
from qgis.core import (Qgis,
QgsApplication,
Expand All @@ -35,7 +36,7 @@


class Grass7AlgorithmProvider(QgsProcessingProvider):
descriptionFolder = Grass7Utils.grassDescriptionPath()
descriptionFolders = Grass7Utils.grassDescriptionFolders()

def __init__(self):
super().__init__()
Expand Down Expand Up @@ -86,18 +87,18 @@ def unload(self):

def createAlgsList(self):
algs = []
folder = self.descriptionFolder
for descriptionFile in os.listdir(folder):
if descriptionFile.endswith('txt'):
folders = self.descriptionFolders
for folder in folders:
for descriptionFile in folder.glob('*.txt'):
try:
alg = Grass7Algorithm(os.path.join(folder, descriptionFile))
alg = Grass7Algorithm(descriptionFile)
if alg.name().strip() != '':
algs.append(alg)
else:
QgsMessageLog.logMessage(self.tr('Could not open GRASS GIS 7 algorithm: {0}').format(descriptionFile), self.tr('Processing'), Qgis.Critical)
except Exception as e:
QgsMessageLog.logMessage(
self.tr('Could not open GRASS GIS 7 algorithm: {0}\n{1}').format(descriptionFile, str(e)), self.tr('Processing'), Qgis.Critical)
self.tr('Could not open GRASS GIS 7 algorithm: {0}\n{1}').format(descriptionFile, e), self.tr('Processing'), Qgis.Critical)
return algs

def loadAlgorithms(self):
Expand Down
20 changes: 18 additions & 2 deletions python/plugins/grassprovider/Grass7Utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import subprocess
import os
import sys
from pathlib import Path

from qgis.core import (Qgis,
QgsApplication,
Expand Down Expand Up @@ -254,8 +255,23 @@ def grassPath():
return folder or ''

@staticmethod
def grassDescriptionPath():
return os.path.join(os.path.dirname(__file__), 'description')
def userDescriptionFolder():
"""
Creates and returns a directory for users to create additional algorithm descriptions.
Or modified versions of stock algorithm descriptions shipped with QGIS.
AlisterH marked this conversation as resolved.
Show resolved Hide resolved
"""
folder = Path(userFolder(), 'grassaddons', 'description')
folder.mkdir(parents=True, exist_ok=True)
return folder

@staticmethod
def grassDescriptionFolders():
"""
Returns the directories to search for algorithm descriptions.
Note that the provider will load from these in sequence, so we put the userDescriptionFolder first
to allow users to create modified versions of stock algorithms shipped with QGIS.
"""
return [Grass7Utils.userDescriptionFolder(), Path(__file__).parent.joinpath('description')]

@staticmethod
def getWindowsCodePage():
Expand Down