aboutsummaryrefslogtreecommitdiff
path: root/docs/generate_autodocs.py
blob: 6138287b41d0d10ea349eb533ef624a6f957a713 (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
#!/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
from sphinx.application import Sphinx
from sphinx.environment import BuildEnvironment

BASE_DIR = '/home/gertjan/Dropbox/phd/research/msvm/python/start_here/'
DOCDIR = os.path.join(BASE_DIR, 'gensvm', 'docs')

CLASSES = [
        'GenSVMGridSearchCV',
        'GenSVM'
        ]

FULL_NAMES = {
        'GenSVM': 'gensvm.core.GenSVM',
        'GenSVMGridSearchCV': 'gensvm.gridsearch.GenSVMGridSearchCV'
        }

OUTPUT_FILES = {
        'GenSVMGridSearchCV': os.path.join(DOCDIR, 'cls_gridsearch.rst'),
        'GenSVM': os.path.join(DOCDIR, 'cls_gensvm.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_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:
        for line in ad.result:
            fid.write(line + '\n')

def main():
    app = load_app()
    for cls in CLASSES:
        generate_autodoc(app, cls)

if __name__ == '__main__':
    main()