generate_application_doc.py

Julien Malik, 2012-06-12 10:57 AM

Download (6.02 KB)

 
1
#!/usr/bin/python
2

    
3
import os, sys, subprocess
4
from optparse import OptionParser
5
import otbApplication
6

    
7
outputpath = os.path.join( os.environ["HOME"], ".qgis/python/plugins/sextante/otb/description" )
8
endl = os.linesep
9

    
10
docpath = os.path.join(outputpath, 'doc')
11
if not os.path.exists(docpath):
12
    os.makedirs(docpath)
13

    
14

    
15
def unique(seq): 
16
    # order preserving
17
    checked = []
18
    for e in seq:
19
        if e not in checked:
20
            checked.append(e)
21
    return checked
22

    
23
def ConvertString(s):
24
    '''Convert a string for compatibility in txt dump'''
25
    return s
26

    
27

    
28
def get_app_list():
29
    blackList = ["TestApplication"]
30
    appNames = [app for app in otbApplication.Registry.GetAvailableApplications() if app not in blackList]
31
    return appNames
32

    
33
def generate_all_app_docs( ) :
34
    appliIdx = 0
35
    for appliname in get_app_list():
36
        appliIdx = appliIdx+1
37
        appli = otbApplication.Registry.CreateApplication(appliname)
38
        appli.UpdateParameters() # TODO do we really need this ?
39
        generate_app_doc_rst( appli, os.path.join(outputpath, 'doc', appliname + '.rst') )
40

    
41
def generate_app_doc_rst( appli, outputrst ):
42

    
43
    rst = ''
44
    
45
    rst += '=' * 100 + endl
46
    rst += appli.GetDocName() + endl
47
    rst += '=' * 100 + 2*endl
48
    
49
    rst += appli.GetDescription() + endl
50
    rst += endl
51

    
52
    rst += '-' * 100 + endl
53
    rst += 'Detailed Description' + endl
54
    rst += '-' * 100 + 2*endl
55
    
56
    rst += appli.GetDocLongDescription() + endl
57
    rst += endl
58
    
59
    rst += '-' * 100 + endl
60
    rst += 'Parameters' + endl
61
    rst += '-' * 100 + 2*endl
62
    rst += endl
63
    
64
    depth = GetParametersDepth(appli.GetParametersKeys())
65
    deep = depth > 0
66
    rst += generate_param_doc(appli,appli.GetParametersKeys(),deep) + endl
67

    
68
    with open( outputrst, 'w' ) as rstfile:
69
        rstfile.write(rst)
70

    
71

    
72
def GetPixelType(value):
73
    # look for type
74
    foundcode = -1
75
    foundname = ""
76
    for ptypename, ptypecode in pixeltypes.iteritems():
77
        if value.endswith(ptypename):
78
            foundcode = ptypecode
79
            foundname = ptypename
80
            break
81
    return foundcode,foundname
82

    
83
def GetParametersDepth(paramlist):
84
    depth = 0
85
    for param in paramlist:
86
        depth = max(param.count("."),depth)
87
    return depth
88

    
89
def GenerateChoice(app,param,paramlist, level):
90
    output = indent(level) + "Available choices are: " + 2*endl
91
    for (choicekey,choicename) in zip(app.GetChoiceKeys(param),app.GetChoiceNames(param)):
92
        output +=  indent(level) + "* " + ConvertString(choicename) + endl
93
        choicedesc = app.GetParameterDescription(param+"."+choicekey)
94
        if len(choicedesc) >= 2:
95
            output += indent(level) + indent(1) + ConvertString(choicedesc) + 2*endl
96
        # List option associated to one choice
97
        options = []
98
        for p in paramlist:
99
            if p.startswith(param+"."+choicekey+"."):
100
                options.append(p)
101
        if len(options) > 0:
102
            for option in options:
103
                output += indent(level+1) +  "* " + ConvertString(app.GetParameterName(option)) + endl
104
                output += indent(level+1) + indent(1) + ConvertString(app.GetParameterDescription(option)) + 2*endl
105
    return output
106

    
107
def indent(n):
108
    return ' ' * 4 *  n
109

    
110
def generate_param_doc(app,paramlist,deep = False,current="", level=0):
111
    output = ""
112
    # First run
113
    if len(current)==0:
114
        firstlevelparams = []
115
        for param in paramlist:
116
            paramsplit = param.partition(".")
117
            firstlevelparams.append(paramsplit[0])
118
        firstlevelparams = unique(firstlevelparams)
119
        if deep:
120
            for param in firstlevelparams:
121
                output += indent(level) + "* " + ConvertString(app.GetParameterName(param)) + endl
122
                output += indent(level) + indent(1) + ConvertString(app.GetParameterDescription(param)) + 2*endl
123
                if app.GetParameterType(param) ==  otbApplication.ParameterType_Choice:
124
                    output += GenerateChoice(app,param,paramlist,level+1)
125
                else:
126
                    output += generate_param_doc(app,paramlist,deep,param,level+1)
127
        else:
128
            for param in firstlevelparams:
129
                output += indent(level) + "* " + ConvertString(app.GetParameterName(param)) + endl
130
                output+=  indent(level) + indent(1) + ConvertString(app.GetParameterDescription(param)) + 2*endl
131
                if app.GetParameterType(param) ==  otbApplication.ParameterType_Choice:
132
                    output += GenerateChoice(app,param,paramlist,level+1)
133
    else:
134
        currentlevelparams = []
135
        for param in paramlist:
136
            if param.startswith(current+".") and param.count(".") == current.count(".")+1:
137
                currentlevelparams.append(param)
138
        if len(currentlevelparams) > 0:
139
            for param in currentlevelparams:
140
                output += indent(level) + "* " + ConvertString(app.GetParameterName(param)) + endl
141
                output+=  indent(level) + indent(1) + ConvertString(app.GetParameterDescription(param)) + 2*endl
142
                output+= generate_param_doc(app,paramlist,deep,param,level+1) + endl
143
                if app.GetParameterType(param) ==  otbApplication.ParameterType_Choice:
144
                    output += GenerateChoice(app,param,paramlist,level+1) 
145
    return output
146

    
147

    
148
if __name__ == "__main__" :
149
    appli = otbApplication.Registry.CreateApplication("OrthoRectification")
150
    appli.UpdateParameters()
151
    
152
    rst = os.path.join(outputpath, 'doc', 'OrthoRectification.rst')
153
    generate_app_doc_rst(appli, rst)
154
    
155
    html = os.path.join(outputpath, 'doc', 'OrthoRectification.html')
156
    subprocess.call(['rst2html', '--stylesheet', os.path.join(outputpath, '..', 'helper','test.css'), rst, html])
157
    
158
    #latex = os.path.join(outputpath, 'doc', 'OrthoRectification.latex.tex')
159
    #pdflatex = os.path.join(outputpath, 'doc', 'OrthoRectification.latex.pdf')
160
    #subprocess.call(['rst2latex', rst, latex])
161
    #subprocess.call(['pdflatex', latex, pdflatex])
162
    
163
    #pdf = os.path.join(outputpath, 'doc', 'OrthoRectification.pdf')
164
    #subprocess.call(['rst2pdf', '-o', pdf, rst])
165