aboutsummaryrefslogtreecommitdiff
path: root/docs/generate_autodocs.py
blob: a2f7e49d693d1dcfd998dab611f55ef1eb40be6a (plain)
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
#!/usr/bin/env python

"""
This script manually generates the autodoc RST files for the classes we want to 
document. By doing this, we can generate the documentation on Read The Docs 
(RTD).  If we try to use vanilla autodoc, we run into the problem that a 
working Blas installation is necessary to install the GenSVM python package and 
this is not available in the RTD VM.

Author: Gertjan van den Burg

"""

import os

from docutils.statemachine import StringList, ViewList

from sphinx.ext.autodoc import (
    AutoDirective,
    ClassDocumenter,
    Options,
    FunctionDocumenter,
)
from sphinx.application import Sphinx
from sphinx.environment import BuildEnvironment

HERE_DIR = os.path.dirname(os.path.abspath(__file__))
BASE_DIR = os.path.abspath(os.path.join(HERE_DIR, "..", ".."))

DOCDIR = os.path.join(BASE_DIR, "gensvm", "docs")

CLASSES = ["GenSVMGridSearchCV", "GenSVM"]

FUNCTIONS = ["load_grid_tiny", "load_grid_small", "load_grid_full"]

FULL_NAMES = {
    "GenSVM": "gensvm.core.GenSVM",
    "GenSVMGridSearchCV": "gensvm.gridsearch.GenSVMGridSearchCV",
    "load_grid_tiny": "gensvm.gridsearch.load_grid_tiny",
    "load_grid_small": "gensvm.gridsearch.load_grid_small",
    "load_grid_full": "gensvm.gridsearch.load_grid_full",
}

OUTPUT_FILES = {
    "GenSVMGridSearchCV": os.path.join(DOCDIR, "cls_gridsearch.rst"),
    "GenSVM": os.path.join(DOCDIR, "cls_gensvm.rst"),
    "load_grid_tiny": os.path.join(DOCDIR, "auto_functions.rst"),
    "load_grid_small": os.path.join(DOCDIR, "auto_functions.rst"),
    "load_grid_full": os.path.join(DOCDIR, "auto_functions.rst"),
}


def load_app():
    srcdir = DOCDIR[:]
    confdir = DOCDIR[:]
    outdir = os.path.join(BASE_DIR, "gensvm_docs", "html")
    doctreedir = os.path.join(BASE_DIR, "gensvm_docs", "doctrees")
    buildername = "html"

    app = Sphinx(srcdir, confdir, outdir, doctreedir, buildername)
    return app


def generate_class_autodoc(app, cls):
    ad = AutoDirective(
        name="autoclass",
        arguments=[FULL_NAMES[cls]],
        options={"noindex": True},
        content=StringList([], items=[]),
        lineno=0,
        content_offset=1,
        block_text="",
        state=None,
        state_machine=None,
    )

    ad.env = BuildEnvironment(app)
    ad.genopt = Options(noindex=True)
    ad.filename_set = set()
    ad.result = ViewList()

    documenter = ClassDocumenter(ad, ad.arguments[0])
    documenter.generate(all_members=True)

    with open(OUTPUT_FILES[cls], "w") as fid:
        fid.write(cls + '\n')
        fid.write(''.join(['=']*len(cls)) + '\n')
        for line in ad.result:
            fid.write(line + "\n")


def generate_func_autodoc(app, func, add_title=True):
    ad = AutoDirective(
        name="autofunc",
        arguments=[FULL_NAMES[func]],
        options={"noindex": True},
        content=StringList([], items=[]),
        lineno=0,
        content_offset=1,
        block_text="",
        state=None,
        state_machine=None,
    )

    ad.env = BuildEnvironment(app)
    ad.genopt = Options(noindex=True)
    ad.filename_set = set()
    ad.result = ViewList()

    documenter = FunctionDocumenter(ad, ad.arguments[0])
    documenter.generate(all_members=True)

    title = 'Parameter Grids'
    with open(OUTPUT_FILES[func], "a") as fid:
        if add_title:
            fid.write(title + '\n')
            fid.write(''.join(['=']*len(title)) + '\n')
        for line in ad.result:
            fid.write(line + "\n")


def main():
    for of in OUTPUT_FILES:
        fname = OUTPUT_FILES[of]
        if os.path.exists(fname):
            os.unlink(fname)
    app = load_app()
    for cls in CLASSES:
        generate_class_autodoc(app, cls)
    add_title = True
    for func in FUNCTIONS:
        generate_func_autodoc(app, func, add_title=add_title)
        add_title = False


if __name__ == "__main__":
    main()