File: help.py

package info (click to toggle)
qgis 2.14.11%2Bdfsg-3%2Bdeb9u1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 991,448 kB
  • ctags: 73,105
  • sloc: cpp: 535,362; python: 162,580; xml: 16,494; ansic: 8,031; sh: 1,788; perl: 1,559; sql: 727; yacc: 319; lex: 269; makefile: 251
file content (117 lines) | stat: -rw-r--r-- 4,422 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# -*- 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 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.getName().lower())
    mkdir(baseDir)

    groupName = alg.group.lower()
    groupName = groupName.replace('[', '').replace(']', '').replace(' - ', '_')
    groupName = groupName.replace(' ', '_')
    cmdLineName = alg.commandLineName()
    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.parameters:
            if isinstance(p, (ParameterMultipleInput, ParameterTableField, ParameterVector)):
                f.write('``{}`` [{}: {}]\n'.format(p.description, p.typeName(), p.dataType()))
            else:
                f.write('``{}`` [{}]\n'.format(p.description, p.typeName()))

            if hasattr(p, 'optional'):
                if p.optional:
                    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, 'default'):
                f.write('  Default: *{}*\n\n'.format(p.default if p.default != '' 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.typeName()))
            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.runalg('{}', ".format(alg.commandLineName())
        for p in alg.parameters:
            cmd += '{}, '.format(p.name.lower().strip())

        for o in alg.outputs:
            if not o.hidden:
                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.getName():
            continue

        for alg in provider.algs:
            baseHelpForAlgorithm(alg, folder)


def createAlgorithmHelp(algName, folder):
    alg = Processing.getAlgorithm(algName)
    baseHelpForAlgorithm(alg, folder)