Skip to content

Commit

Permalink
Review the Processing script template : add annotations, remove self.tr
Browse files Browse the repository at this point in the history
  • Loading branch information
Gustry authored and nyalldawson committed Dec 11, 2024
1 parent 0369b04 commit 6f4304b
Showing 1 changed file with 28 additions and 26 deletions.
54 changes: 28 additions & 26 deletions python/plugins/processing/script/ScriptTemplate.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,17 @@
***************************************************************************
"""

from qgis.PyQt.QtCore import QCoreApplication
from typing import Any, Optional

from qgis.core import (
QgsProcessing,
QgsFeatureSink,
QgsProcessingException,
QgsProcessing,
QgsProcessingAlgorithm,
QgsProcessingParameterFeatureSource,
QgsProcessingContext,
QgsProcessingException,
QgsProcessingFeedback,
QgsProcessingParameterFeatureSink,
QgsProcessingParameterFeatureSource,
)
from qgis import processing

Expand All @@ -42,16 +45,7 @@ class ExampleProcessingAlgorithm(QgsProcessingAlgorithm):
INPUT = "INPUT"
OUTPUT = "OUTPUT"

def tr(self, string):
"""
Returns a translatable string with the self.tr() function.
"""
return QCoreApplication.translate("Processing", string)

def createInstance(self):
return ExampleProcessingAlgorithm()

def name(self):
def name(self) -> str:
"""
Returns the algorithm name, used for identifying the algorithm. This
string should be fixed for the algorithm, and must not be localised.
Expand All @@ -61,21 +55,21 @@ def name(self):
"""
return "myscript"

def displayName(self):
def displayName(self) -> str:
"""
Returns the translated algorithm name, which should be used for any
user-visible display of the algorithm name.
"""
return self.tr("My Script")
return "My Script"

def group(self):
def group(self) -> str:
"""
Returns the name of the group this algorithm belongs to. This string
should be localised.
"""
return self.tr("Example scripts")
return "Example scripts"

def groupId(self):
def groupId(self) -> str:
"""
Returns the unique ID of the group this algorithm belongs to. This
string should be fixed for the algorithm, and must not be localised.
Expand All @@ -85,15 +79,15 @@ def groupId(self):
"""
return "examplescripts"

def shortHelpString(self):
def shortHelpString(self) -> str:
"""
Returns a localised short helper string for the algorithm. This string
should provide a basic description about what the algorithm does and the
parameters and outputs associated with it..
parameters and outputs associated with it.
"""
return self.tr("Example algorithm short description")
return "Example algorithm short description"

def initAlgorithm(self, config=None):
def initAlgorithm(self, config: Optional[dict[str, Any]] = None):
"""
Here we define the inputs and output of the algorithm, along
with some other properties.
Expand All @@ -104,7 +98,7 @@ def initAlgorithm(self, config=None):
self.addParameter(
QgsProcessingParameterFeatureSource(
self.INPUT,
self.tr("Input layer"),
"Input layer",
[QgsProcessing.SourceType.TypeVectorAnyGeometry],
)
)
Expand All @@ -113,10 +107,15 @@ def initAlgorithm(self, config=None):
# usually takes the form of a newly created vector layer when the
# algorithm is run in QGIS).
self.addParameter(
QgsProcessingParameterFeatureSink(self.OUTPUT, self.tr("Output layer"))
QgsProcessingParameterFeatureSink(self.OUTPUT, "Output layer")
)

def processAlgorithm(self, parameters, context, feedback):
def processAlgorithm(
self,
parameters: dict[str, Any],
context: QgsProcessingContext,
feedback: QgsProcessingFeedback,
) -> dict[str, Any]:
"""
Here is where the processing itself takes place.
"""
Expand Down Expand Up @@ -199,3 +198,6 @@ def processAlgorithm(self, parameters, context, feedback):
# dictionary, with keys matching the feature corresponding parameter
# or output names.
return {self.OUTPUT: dest_id}

def createInstance(self):
return self.__class__()

0 comments on commit 6f4304b

Please sign in to comment.