Skip to content

Commit

Permalink
Rename more references to grass7 to version-less "grass"
Browse files Browse the repository at this point in the history
  • Loading branch information
nyalldawson committed Feb 6, 2024
1 parent 0ab66b7 commit ea3a865
Show file tree
Hide file tree
Showing 19 changed files with 59 additions and 60 deletions.
8 changes: 4 additions & 4 deletions .ci/test_blocklist_qt6.txt
Original file line number Diff line number Diff line change
Expand Up @@ -138,9 +138,9 @@ ProcessingGdalAlgorithmsGeneralTest
ProcessingGdalAlgorithmsRasterTest
ProcessingGdalAlgorithmsVectorTest
ProcessingCheckValidityAlgorithmTest
ProcessingGrass7AlgorithmsImageryTest
ProcessingGrass7AlgorithmsRasterTestPt1
ProcessingGrass7AlgorithmsRasterTestPt2
ProcessingGrass7AlgorithmsVectorTest
ProcessingGrassAlgorithmsImageryTest
ProcessingGrassAlgorithmsRasterTestPt1
ProcessingGrassAlgorithmsRasterTestPt2
ProcessingGrassAlgorithmsVectorTest
PyQgsCoordinateReferenceSystemModel
PyQgsProviderRegistry
8 changes: 4 additions & 4 deletions python/plugins/grassprovider/ext/i.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ def regroupRasters(alg, parameters, context, src, group, subgroup=None, extFile=
# extFileName = new_parameters[ext]
# if extFileName:
# shortFileName = os.path.basename(extFileName)
# destPath = os.path.join(Grass7Utils.grassMapsetFolder(),
# destPath = os.path.join(GrassUtils.grassMapsetFolder(),
# 'PERMANENT',
# 'group', new_parameters[group.name()],
# 'subgroup', new_parameters[subgroup.name()],
Expand Down Expand Up @@ -184,7 +184,7 @@ def verifyRasterNum(alg, parameters, context, rasters, mini, maxi=None):


def createDestDir(alg, toFile):
""" Generates an mkdir command for GRASS7 script """
""" Generates an mkdir command for GRASS script """
# Creates the destination directory
command = "{} \"{}\"".format(
"MD" if isWindows() else "mkdir -p",
Expand All @@ -194,7 +194,7 @@ def createDestDir(alg, toFile):


def moveFile(alg, fromFile, toFile):
""" Generates a move command for GRASS7 script """
""" Generates a move command for GRASS script """
createDestDir(alg, toFile)
command = "{} \"{}\" \"{}\"".format(
"MOVE /Y" if isWindows() else "mv -f",
Expand All @@ -205,7 +205,7 @@ def moveFile(alg, fromFile, toFile):


def copyFile(alg, fromFile, toFile):
""" Generates a copy command for GRASS7 script """
""" Generates a copy command for GRASS script """
createDestDir(alg, toFile)
command = "{} \"{}\" \"{}\"".format(
"COPY /Y" if isWindows() else "cp -f",
Expand Down
22 changes: 11 additions & 11 deletions python/plugins/grassprovider/ext/r_li.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@


def rliPath():
"""Return r.li GRASS7 user dir"""
"""Return r.li GRASS user dir"""
if isWindows():
homeDir = win32api.GetShortPathName(os.path.expanduser('~'))
return os.path.join(homeDir, 'AppData', 'Roaming', 'GRASS7', 'r.li')
Expand Down Expand Up @@ -82,17 +82,17 @@ def configFile(alg, parameters, context, feedback, outputTxt=False):
"""Handle inline configuration
:param parameters:
"""
# Where is the GRASS7 user directory ?
userGrass7Path = rliPath()
if not os.path.isdir(userGrass7Path):
mkdir(userGrass7Path)
if not os.path.isdir(os.path.join(userGrass7Path, 'output')):
mkdir(os.path.join(userGrass7Path, 'output'))
# Where is the GRASS user directory ?
user_grass_path = rliPath()
if not os.path.isdir(user_grass_path):
mkdir(user_grass_path)
if not os.path.isdir(os.path.join(user_grass_path, 'output')):
mkdir(os.path.join(user_grass_path, 'output'))

# If we have a configuration file, we need to copy it into user dir
if parameters['config']:
fileName = alg.parameterAsString(parameters, 'config', context)
configFilePath = os.path.join(userGrass7Path, os.path.basename(fileName))
configFilePath = os.path.join(user_grass_path, os.path.basename(fileName))
# Copy the file
shutil.copy(parameters['config'], configFilePath)
# Change the parameter value
Expand All @@ -101,7 +101,7 @@ def configFile(alg, parameters, context, feedback, outputTxt=False):
elif parameters['config_txt']:
# Creates a temporary txt file in user r.li directory
tempConfig = os.path.basename(getTempFilename(context=context))
configFilePath = os.path.join(userGrass7Path, tempConfig)
configFilePath = os.path.join(user_grass_path, tempConfig)
# Inject rules into temporary txt file
with open(configFilePath, "w") as f:
f.write(alg.parameterAsString(parameters, 'config_txt', context))
Expand All @@ -128,9 +128,9 @@ def configFile(alg, parameters, context, feedback, outputTxt=False):
def moveOutputTxtFile(alg, parameters, context):
# Find output file name:
txtPath = alg.parameterAsString(parameters, 'output_txt', context)
userGrass7Path = rliPath()
user_grass_path = rliPath()

output = os.path.join(userGrass7Path, 'output',
output = os.path.join(user_grass_path, 'output',
alg.parameterAsString(parameters, 'output', context))
# move the file
if isWindows():
Expand Down
2 changes: 1 addition & 1 deletion python/plugins/grassprovider/ext/v_net.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
* *
***************************************************************************
This Python module handles pre-treatment operations for v.net.* GRASS7 modules.
This Python module handles pre-treatment operations for v.net.* GRASS modules.
Before using a v.net module you often have to incorporate a points layer into
the network vector map.
"""
Expand Down
24 changes: 12 additions & 12 deletions python/plugins/grassprovider/grass.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ A short guide for creating and editing GRASS GIS processing algorithms:
-----------------------------------------------------------------------

For a GRASS command to be executed within QGIS a plain text description file is needed
to define the inputs required by the command, the output it generates and the parameters
that are used to configure it. Each command is described in a separate text file, but some
to define the inputs required by the command, the output it generates and the parameters
that are used to configure it. Each command is described in a separate text file, but some
commands can be split into several algorithms by using more than one description file,
so that more than one entry is shown in the algorithms list. Splitting a grass command
was originally done because the processing provider did not support optional parameters,
Expand All @@ -15,7 +15,7 @@ create new algorithms or improve existing algorithms.
Each file starts with three lines containing:

- The name of the grass command to call to execute the algorithm (e.g. v.buffer)
- The description of the algorithm to show to the user. For split commands you must
- The description of the algorithm to show to the user. For split commands you must
include the algorithm id first, e.g.:
r.sun.insoltime - Solar irradiance and irradiation model (daily sums).
and
Expand Down Expand Up @@ -155,7 +155,7 @@ Example: *QgsProcessingParameterBoolean|-i|Output raster map as integer|False

RELOADING ALGORITHM DESCRIPTIONS
---------------------------------------
You do not need to restart QGIS after editing or creating an algorithm description - simply click the wrench icon in the processing toolbox, then click OK, and QGIS will reload the descriptions.
You do not need to restart QGIS after editing or creating an algorithm description - simply click the wrench icon in the processing toolbox, then click OK, and QGIS will reload the descriptions.

ADVANCED PROCESSING
--------------------------
Expand All @@ -165,13 +165,13 @@ To save the console output from GRASS to file, simply create a QgsProcessingPara
Example: QgsProcessingParameterFileDestination|html|List of addons|Html files (*.html)|addons_list.html|False

To add additional logic to an algorithm, like a preliminary check on data, the use of more than one GRASS command,
or a transformation of output data, then you need to use the ext mechanism.
or a transformation of output data, then you need to use the ext mechanism.

There are 4 different levels where you can add logic:
- Checking the input parameters, e.g. if you want to verify that two mutually exclusive options have not been both enabled.
- Processing inputs import: if you need to do more than importing input layers.
- Processing the command itself: if you need to chain more than one GRASS command for your algorithm.
- Processing the outputs: if you need to do special things before exporting layers or if you need special export methods.
There are 4 different levels where you can add logic:
- Checking the input parameters, e.g. if you want to verify that two mutually exclusive options have not been both enabled.
- Processing inputs import: if you need to do more than importing input layers.
- Processing the command itself: if you need to chain more than one GRASS command for your algorithm.
- Processing the outputs: if you need to do special things before exporting layers or if you need special export methods.

To add some logic on one (or more) level(s), you have to create a .py file named according to the algorithm name in python/plugins/grassprovider/ext, replacing '.' with '_'.
Then you need to create methods using the respective names:
Expand All @@ -180,10 +180,10 @@ Then you need to create methods using the respective names:
- Command: processCommand
- Outputs: processOutputs

If there is a Python file with the algorithm name in the ext directory, methods will be imported from the file and run instead of the common methods (there are "standard" processCommand/processInputs/processOutputs/checkParameterValuesBeforeExecuting methods in the code of the GRASS provider for QGIS Processing, in python/plugins/grassprovider/Grass7Algorithm.py).
If there is a Python file with the algorithm name in the ext directory, methods will be imported from the file and run instead of the common methods (there are "standard" processCommand/processInputs/processOutputs/checkParameterValuesBeforeExecuting methods in the code of the GRASS provider for QGIS Processing, in python/plugins/grassprovider/grass_algorithm.py).

If we take the example of v.what.rast, there is an ext file: ext/v_what_rast.py.
In this file there is a processCommand method. It just launches the standard processCommand but with the delOutputs option set to True (we do not want to have standard outputs).
Then there is also a customized processOutputs which exports the input vector as an output for QGIS. We need to do this because v.what.rast modifies values directly in the input vector layer instead of generating a new output, so we have to build this output ourself.
Then there is also a customized processOutputs which exports the input vector as an output for QGIS. We need to do this because v.what.rast modifies values directly in the input vector layer instead of generating a new output, so we have to build this output ourself.

If you want to do special things in the ext mechanism, you will need to read (and understand) the GRASS provider code standard methods in Grass7Algorithm.py.
14 changes: 7 additions & 7 deletions python/plugins/grassprovider/grass_algorithm.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ def __init__(self,
self._short_description = ''
self._group = ''
self._groupId = ''
self.grass7Name = ''
self.grass_name = ''
self.params = []
self.hardcodedStrings = []
self.inputLayers = []
Expand Down Expand Up @@ -216,9 +216,9 @@ def helpUrl(self):
return None

if os.path.exists(helpPath):
return QUrl.fromLocalFile(os.path.join(helpPath, '{}.html'.format(self.grass7Name))).toString()
return QUrl.fromLocalFile(os.path.join(helpPath, '{}.html'.format(self.grass_name))).toString()
else:
return helpPath + '{}.html'.format(self.grass7Name)
return helpPath + '{}.html'.format(self.grass_name)

def initAlgorithm(self, config=None):
"""
Expand Down Expand Up @@ -254,7 +254,7 @@ def _define_characteristics_from_parsed_description(
"""
Create algorithm parameters and outputs from parsed description
"""
self.grass7Name = description.grass_command
self.grass_name = description.grass_command
self._name = description.name
self._short_description = description.short_description
self._display_name = description.display_name
Expand Down Expand Up @@ -588,7 +588,7 @@ def postInputs(self, context: QgsProcessingContext):
# Add the default parameters commands
self.commands.append(command)

QgsMessageLog.logMessage(self.tr('processInputs end. Commands: {}').format(self.commands), 'Grass7', Qgis.MessageLevel.Info)
QgsMessageLog.logMessage(self.tr('processInputs end. Commands: {}').format(self.commands), 'Grass', Qgis.MessageLevel.Info)

def processCommand(self, parameters, context, feedback, delOutputs=False):
"""
Expand All @@ -598,7 +598,7 @@ def processCommand(self, parameters, context, feedback, delOutputs=False):
:param delOutputs: do not add outputs to commands.
"""
noOutputs = [o for o in self.parameterDefinitions() if o not in self.destinationParameterDefinitions()]
command = '{} '.format(self.grass7Name)
command = '{} '.format(self.grass_name)
command += '{}'.join(self.hardcodedStrings)

# Add algorithm command
Expand Down Expand Up @@ -743,7 +743,7 @@ def processCommand(self, parameters, context, feedback, delOutputs=False):

command += ' --overwrite'
self.commands.append(command)
QgsMessageLog.logMessage(self.tr('processCommands end. Commands: {}').format(self.commands), 'Grass7', Qgis.MessageLevel.Info)
QgsMessageLog.logMessage(self.tr('processCommands end. Commands: {}').format(self.commands), 'Grass', Qgis.MessageLevel.Info)

def vectorOutputType(self, parameters, context):
"""Determine vector output types for outputs"""
Expand Down
2 changes: 1 addition & 1 deletion python/plugins/grassprovider/grass_provider.py
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ def defaultVectorFileExtension(self, hasGeometry=True):

def supportsNonFileBasedOutput(self):
"""
GRASS7 Provider doesn't support non file based outputs
GRASS Provider doesn't support non file based outputs
"""
return False

Expand Down
8 changes: 4 additions & 4 deletions python/plugins/grassprovider/grass_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ def installedVersion(run=False):
def grassBin():
"""
Find GRASS binary path on the operating system.
Sets global variable Grass7Utils.command
Sets global variable GrassUtils.command
"""

def searchFolder(folder):
Expand Down Expand Up @@ -223,7 +223,7 @@ def searchFolder(folder):
def grassPath():
"""
Find GRASS path on the operating system.
Sets global variable Grass7Utils.path
Sets global variable GrassUtils.path
"""
if GrassUtils.path is not None:
return GrassUtils.path
Expand Down Expand Up @@ -543,7 +543,7 @@ def startGrassSession():
# the layers.
@staticmethod
def endGrassSession():
# shutil.rmtree(Grass7Utils.grassMapsetFolder(), True)
# shutil.rmtree(GrassUtils.grassMapsetFolder(), True)
GrassUtils.sessionRunning = False
GrassUtils.sessionLayers = {}
GrassUtils.projectionSet = False
Expand All @@ -564,7 +564,7 @@ def checkGrassIsInstalled(ignorePreviousState=False):
if GrassUtils.isGrassInstalled:
return

# We check the version of Grass7
# We check the version of Grass
if GrassUtils.installedVersion() is not None:
# For Ms-Windows, we check GRASS binaries
if isWindows():
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
from grassprovider.grass_utils import GrassUtils


class TestGrass7AlgorithmsImageryTest(QgisTestCase, AlgorithmsTestBase.AlgorithmsTest):
class TestGrassAlgorithmsImageryTest(QgisTestCase, AlgorithmsTestBase.AlgorithmsTest):

@classmethod
def setUpClass(cls):
Expand All @@ -51,7 +51,7 @@ def tearDownClass(cls):
shutil.rmtree(path)

def test_definition_file(self):
return 'grass7_algorithms_imagery_tests.yaml'
return 'grass_algorithms_imagery_tests.yaml'


if __name__ == '__main__':
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
from grassprovider.grass_utils import GrassUtils


class TestGrass7AlgorithmsRasterTest(QgisTestCase, AlgorithmsTestBase.AlgorithmsTest):
class TestGrassAlgorithmsRasterTest(QgisTestCase, AlgorithmsTestBase.AlgorithmsTest):

@classmethod
def setUpClass(cls):
Expand All @@ -52,7 +52,7 @@ def tearDownClass(cls):
shutil.rmtree(path)

def test_definition_file(self):
return 'grass7_algorithms_raster_tests1.yaml'
return 'grass_algorithms_raster_tests1.yaml'


if __name__ == '__main__':
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
testDataPath = os.path.join(os.path.dirname(__file__), 'testdata')


class TestGrass7AlgorithmsRasterTest(QgisTestCase, AlgorithmsTestBase.AlgorithmsTest):
class TestGrassAlgorithmsRasterTest(QgisTestCase, AlgorithmsTestBase.AlgorithmsTest):

@classmethod
def setUpClass(cls):
Expand All @@ -63,13 +63,13 @@ def tearDownClass(cls):
shutil.rmtree(path)

def test_definition_file(self):
return 'grass7_algorithms_raster_tests2.yaml'
return 'grass_algorithms_raster_tests2.yaml'

def testNeighbors(self):
context = QgsProcessingContext()
input_raster = os.path.join(testDataPath, 'custom', 'grass7', 'float_raster.tif')

alg = QgsApplication.processingRegistry().createAlgorithmById('grass7:r.neighbors')
alg = QgsApplication.processingRegistry().createAlgorithmById('grass:r.neighbors')
self.assertIsNotNone(alg)

temp_file = os.path.join(self.temp_dir, 'grass_output.tif')
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@
testDataPath = os.path.join(os.path.dirname(__file__), 'testdata')


class TestGrass7AlgorithmsVectorTest(QgisTestCase, AlgorithmsTestBase.AlgorithmsTest):
class TestGrassAlgorithmsVectorTest(QgisTestCase, AlgorithmsTestBase.AlgorithmsTest):

@classmethod
def setUpClass(cls):
Expand All @@ -68,7 +68,7 @@ def tearDownClass(cls):
shutil.rmtree(path)

def test_definition_file(self):
return 'grass7_algorithms_vector_tests.yaml'
return 'grass_algorithms_vector_tests.yaml'

def testMemoryLayerInput(self):
# create a memory layer and add to project and context
Expand All @@ -88,7 +88,7 @@ def testMemoryLayerInput(self):
context = QgsProcessingContext()
context.setProject(QgsProject.instance())

alg = QgsApplication.processingRegistry().createAlgorithmById('grass7:v.buffer')
alg = QgsApplication.processingRegistry().createAlgorithmById('grass:v.buffer')
self.assertIsNotNone(alg)

temp_file = os.path.join(self.temp_dir, 'grass_output.shp')
Expand Down Expand Up @@ -148,7 +148,7 @@ def testFeatureSourceInput(self):
context = QgsProcessingContext()
context.setProject(QgsProject.instance())

alg = QgsApplication.processingRegistry().createAlgorithmById('grass7:v.buffer')
alg = QgsApplication.processingRegistry().createAlgorithmById('grass:v.buffer')
self.assertIsNotNone(alg)
temp_file = os.path.join(self.temp_dir, 'grass_output_sel.shp')
parameters = {'input': QgsProcessingFeatureSourceDefinition('testmem', True),
Expand Down Expand Up @@ -202,7 +202,7 @@ def testOutputToGeopackage(self):
context = QgsProcessingContext()
context.setProject(QgsProject.instance())

alg = QgsApplication.processingRegistry().createAlgorithmById('grass7:v.buffer')
alg = QgsApplication.processingRegistry().createAlgorithmById('grass:v.buffer')
self.assertIsNotNone(alg)

temp_file = os.path.join(self.temp_dir, 'grass_output.gpkg')
Expand Down
4 changes: 2 additions & 2 deletions python/plugins/grassprovider/tests/grass_todo.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Here is a list of grass7 algorithms without test units
# Here is a list of grass algorithms without test units

## Raster algorithms

Expand Down Expand Up @@ -99,4 +99,4 @@

## Vector algorithms

We need to handle gml for output and input for GRASS7 algorithms before creating tests !
We need to handle gml for output and input for GRASS algorithms before creating tests !
Loading

0 comments on commit ea3a865

Please sign in to comment.