Nyall Dawson 1e13d733c2 Move declaration of algorithm parameters/outputs to a new virtual
initAlgorithm() method

This allows 2 benefits:
- algorithms can be subclassed and have subclasses add additional
parameters/outputs to the algorithm. With the previous approach
of declaring parameters/outputs in the constructor, it's not
possible to call virtual methods to add additional parameters/
outputs (since you can't call virtual methods from a constructor).

- initAlgorithm takes a variant map argument, allowing the algorithm
to dynamically adjust its declared parameters and outputs according
to this configuration map. This potentially allows model algorithms which
can be configured to have variable numbers of parameters and
outputs at run time. E.g. a "router" algorithm which directs
features to one of any number of output sinks depending on some
user configured criteria.
2017-07-10 16:31:14 +10:00

120 lines
4.5 KiB
Python

# -*- coding: utf-8 -*-
"""
***************************************************************************
help.py
---------------------
Date : March 2013
Copyright : (C) 2013 by Victor Olaya
Email : volayaf at gmail dot com
***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************
"""
__author__ = 'Victor Olaya'
__date__ = 'March 2013'
__copyright__ = '(C) 2013, Victor Olaya'
# This will get replaced with a git SHA1 when you do a git archive
__revision__ = '$Format:%H$'
import os
import codecs
from qgis.core import (QgsApplication,
QgsProcessingParameterDefinition)
from processing.core.Processing import Processing
from processing.core.parameters import ParameterMultipleInput, ParameterTableField, ParameterVector, ParameterSelection
from processing.tools.system import mkdir
def baseHelpForAlgorithm(alg, folder):
baseDir = os.path.join(folder, alg.provider().id().lower())
mkdir(baseDir)
groupName = alg.group().lower()
groupName = groupName.replace('[', '').replace(']', '').replace(' - ', '_')
groupName = groupName.replace(' ', '_')
cmdLineName = alg.id()
algName = cmdLineName[cmdLineName.find(':') + 1:].lower()
validChars = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_'
safeGroupName = ''.join(c for c in groupName if c in validChars)
safeAlgName = ''.join(c for c in algName if c in validChars)
dirName = os.path.join(baseDir, safeGroupName)
mkdir(dirName)
filePath = os.path.join(dirName, safeAlgName + '.rst')
with codecs.open(filePath, 'w', encoding='utf-8') as f:
f.write('{}\n'.format(alg.name()))
f.write('{}\n\n'.format('=' * len(alg.name())))
f.write('Description\n')
f.write('-----------\n\n<put algorithm description here>\n\n')
# Algorithm parameters
f.write('Parameters\n')
f.write('----------\n\n')
for p in alg.parameterDefinitions():
if isinstance(p, (ParameterMultipleInput, ParameterTableField, ParameterVector)):
f.write('``{}`` [{}: {}]\n'.format(p.description(), p.type(), p.dataType()))
else:
f.write('``{}`` [{}]\n'.format(p.description(), p.type()))
if hasattr(p, 'optional'):
if p.flags() & QgsProcessingParameterDefinition.FlagOptional:
f.write(' Optional.\n\n')
f.write(' <put parameter description here>\n\n')
if isinstance(p, ParameterSelection):
f.write(' Options:\n\n')
for count, opt in enumerate(p.options):
f.write(' * {} --- {}\n'.format(count, opt))
f.write('\n')
if hasattr(p, 'defaultValue'):
f.write(' Default: *{}*\n\n'.format(p.defaultValue() if p.defaultValue() else '(not set)'))
# Algorithm outputs
f.write('Outputs\n')
f.write('-------\n\n')
for o in alg.outputs:
f.write('``{}`` [{}]\n'.format(o.description(), o.type()))
f.write(' <put output description here>\n\n')
# Console usage
f.write('Console usage\n')
f.write('-------------\n')
f.write('\n::\n\n')
cmd = " processing.run('{}', ".format(alg.id())
for p in alg.parameterDefinitions():
cmd += '{}, '.format(p.name().lower().strip())
for o in alg.outputs:
if not o.flags() & QgsProcessingParameterDefinition.FlagHidden:
cmd += '{}, '.format(o.name().lower().strip())
cmd = cmd[:-2] + ')\n\n'
f.write(cmd)
f.write('See also\n')
f.write('--------\n\n')
def createBaseHelpFiles(folder):
for provider in Processing.providers:
if 'grass' in provider.id():
continue
for alg in provider.algorithms():
baseHelpForAlgorithm(alg, folder)
def createAlgorithmHelp(algName, folder):
alg = QgsApplication.processingRegistry().createAlgorithmById(algName)
baseHelpForAlgorithm(alg, folder)