Skip to content

Commit 3e65d8d

Browse files
committedMar 28, 2019
Ensure all public, stable API methods for Processing have nice docstrings
(cherry picked from commit f758d8b)
1 parent 01bae44 commit 3e65d8d

File tree

2 files changed

+48
-22
lines changed

2 files changed

+48
-22
lines changed
 

‎python/plugins/processing/tools/dataobjects.py

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -28,16 +28,12 @@
2828
import os
2929
import re
3030

31-
from qgis.core import (QgsVectorFileWriter,
32-
QgsMapLayer,
33-
QgsDataProvider,
31+
from qgis.core import (QgsDataProvider,
3432
QgsRasterLayer,
3533
QgsWkbTypes,
3634
QgsVectorLayer,
3735
QgsProject,
38-
QgsCoordinateReferenceSystem,
3936
QgsSettings,
40-
QgsProcessingUtils,
4137
QgsProcessingContext,
4238
QgsFeatureRequest,
4339
QgsExpressionContext,
@@ -48,9 +44,6 @@
4844
from qgis.utils import iface
4945

5046
from processing.core.ProcessingConfig import ProcessingConfig
51-
from processing.algs.gdal.GdalUtils import GdalUtils
52-
from processing.tools.system import (getTempFilename,
53-
removeInvalidChars)
5447

5548
ALL_TYPES = [-1]
5649

@@ -66,6 +59,12 @@
6659
def createContext(feedback=None):
6760
"""
6861
Creates a default processing context
62+
63+
:param feedback: Optional existing QgsProcessingFeedback object, or None to use a default feedback object
64+
:type feedback: Optional[QgsProcessingFeedback]
65+
66+
:returns: New QgsProcessingContext object
67+
:rtype: QgsProcessingContext
6968
"""
7069
context = QgsProcessingContext()
7170
context.setProject(QgsProject.instance())
@@ -132,7 +131,8 @@ def load(fileName, name=None, crs=None, style=None, isRaster=False):
132131
if prjSetting:
133132
settings.setValue('/Projections/defaultBehavior', prjSetting)
134133
raise RuntimeError(QCoreApplication.translate('dataobject',
135-
'Could not load layer: {0}\nCheck the processing framework log to look for errors.').format(fileName))
134+
'Could not load layer: {0}\nCheck the processing framework log to look for errors.').format(
135+
fileName))
136136
else:
137137
qgslayer = QgsVectorLayer(fileName, name, 'ogr')
138138
if qgslayer.isValid():
@@ -155,7 +155,6 @@ def load(fileName, name=None, crs=None, style=None, isRaster=False):
155155

156156

157157
def getRasterSublayer(path, param):
158-
159158
layer = QgsRasterLayer(path)
160159

161160
try:

‎python/plugins/processing/tools/general.py

Lines changed: 39 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,6 @@
2424
# This will get replaced with a git SHA1 when you do a git archive
2525

2626
__revision__ = '$Format:%H$'
27-
28-
import os
29-
import configparser
30-
3127
from qgis.core import (QgsApplication,
3228
QgsProcessingAlgorithm,
3329
QgsProcessingParameterEnum,
@@ -43,8 +39,13 @@
4339

4440

4541
def algorithmHelp(id):
46-
"""Prints algorithm parameters with their types. Also
47-
provides information about options if any.
42+
"""
43+
Prints algorithm parameters with their types. Also
44+
provides information about parameters and outputs,
45+
and their acceptable values.
46+
47+
:param id: An algorithm's ID
48+
:type id: str
4849
"""
4950
alg = QgsApplication.processingRegistry().algorithmById(id)
5051
if alg is not None:
@@ -100,6 +101,9 @@ def run(algOrName, parameters, onFinish=None, feedback=None, context=None, is_ch
100101
:param context: Processing context object
101102
:param is_child_algorithm: Set to True if this algorithm is being run as part of a larger algorithm,
102103
i.e. it is a sub-part of an algorithm which calls other Processing algorithms.
104+
105+
:returns algorithm results as a dictionary, or None if execution failed
106+
:rtype: Union[dict, None]
103107
"""
104108
if onFinish or not is_child_algorithm:
105109
return Processing.runAlgorithm(algOrName, parameters, onFinish, feedback, context)
@@ -114,8 +118,17 @@ def post_process(_alg, _context, _feedback):
114118

115119

116120
def runAndLoadResults(algOrName, parameters, feedback=None, context=None):
117-
"""Executes given algorithm and load its results into QGIS project
121+
"""
122+
Executes given algorithm and load its results into the current QGIS project
118123
when possible.
124+
125+
:param algOrName: Either an instance of an algorithm, or an algorithm's ID
126+
:param parameters: Algorithm parameters dictionary
127+
:param feedback: Processing feedback object
128+
:param context: Processing context object
129+
130+
:returns algorithm results as a dictionary, or None if execution failed
131+
:rtype: Union[dict, None]
119132
"""
120133
if isinstance(algOrName, QgsProcessingAlgorithm):
121134
alg = algOrName
@@ -127,21 +140,30 @@ def runAndLoadResults(algOrName, parameters, feedback=None, context=None):
127140
if not param.name() in parameters:
128141
continue
129142

130-
if isinstance(param, (QgsProcessingParameterFeatureSink, QgsProcessingParameterVectorDestination, QgsProcessingParameterRasterDestination)):
143+
if isinstance(param, (QgsProcessingParameterFeatureSink, QgsProcessingParameterVectorDestination,
144+
QgsProcessingParameterRasterDestination)):
131145
p = parameters[param.name()]
132146
if not isinstance(p, QgsProcessingOutputLayerDefinition):
133147
parameters[param.name()] = QgsProcessingOutputLayerDefinition(p, QgsProject.instance())
134148
else:
135149
p.destinationProject = QgsProject.instance()
136150
parameters[param.name()] = p
137151

138-
return Processing.runAlgorithm(alg, parameters=parameters, onFinish=handleAlgorithmResults, feedback=feedback, context=context)
152+
return Processing.runAlgorithm(alg, parameters=parameters, onFinish=handleAlgorithmResults, feedback=feedback,
153+
context=context)
139154

140155

141156
def createAlgorithmDialog(algOrName, parameters={}):
142-
"""Creates and returns an algorithm dialog for the specified algorithm, prepopulated
157+
"""
158+
Creates and returns an algorithm dialog for the specified algorithm, prepopulated
143159
with a given set of parameters. It is the caller's responsibility to execute
144160
and delete this dialog.
161+
162+
:param algOrName: Either an instance of an algorithm, or an algorithm's ID
163+
:param parameters: Initial algorithm parameters dictionary
164+
165+
:returns algorithm results as a dictionary, or None if execution failed
166+
:rtype: Union[dict, None]
145167
"""
146168
if isinstance(algOrName, QgsProcessingAlgorithm):
147169
alg = algOrName.create()
@@ -162,10 +184,15 @@ def createAlgorithmDialog(algOrName, parameters={}):
162184

163185

164186
def execAlgorithmDialog(algOrName, parameters={}):
165-
"""Executes an algorithm dialog for the specified algorithm, prepopulated
187+
"""
188+
Executes an algorithm dialog for the specified algorithm, prepopulated
166189
with a given set of parameters.
167190
168-
Returns the algorithm's results.
191+
:param algOrName: Either an instance of an algorithm, or an algorithm's ID
192+
:param parameters: Initial algorithm parameters dictionary
193+
194+
:returns algorithm results as a dictionary, or None if execution failed
195+
:rtype: Union[dict, None]
169196
"""
170197
dlg = createAlgorithmDialog(algOrName, parameters)
171198
if dlg is None:

0 commit comments

Comments
 (0)
Please sign in to comment.