File: generate_facts_doc.py

package info (click to toggle)
pyinfra 0.2.2%2Bgit20161227.ec708ef-1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 11,804 kB
  • ctags: 677
  • sloc: python: 5,944; sh: 71; makefile: 11
file content (95 lines) | stat: -rw-r--r-- 2,615 bytes parent folder | download
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
# pyinfra
# File: docs/generate_fact_docs.py
# Desc: generate rst docs from the fact classes

from types import MethodType
from inspect import getmembers, getargspec, isclass

from six.moves import range

from pyinfra import facts
from pyinfra.api.facts import FactBase
from pyinfra.api.util import underscore


def _title_line(char, string):
    return ''.join(char for _ in range(0, len(string)))


def build_facts():
    lines = []

    # Now we generate a facts.rst describing the use of the facts as:
    # host.data.<snake_case_fact>
    for module_name in facts.__all__:
        print('--> Doing fact module: {0}'.format(module_name))
        module = getattr(facts, module_name)

        lines.append(module_name.title())
        lines.append(_title_line('-', module_name))
        lines.append('')

        fact_classes = [
            (key, value)
            for key, value in getmembers(module)
            if (
                isclass(value)
                and issubclass(value, FactBase)
                and value.__module__ == module.__name__
                and value is not FactBase
            )
        ]

        for fact, cls in fact_classes:
            # FactClass -> fact_accessor on host object
            name = underscore(fact)

            # Does this fact take args?
            command_attr = getattr(cls, 'command', None)
            if isinstance(command_attr, MethodType):
                # Attach basic argspec to name
                # Note only supports facts with one arg as this is all that's possible,
                # will need to refactor to print properly in future.
                argspec = getargspec(command_attr)
                if len(argspec.args) > 1:
                    name = '{0}({1})'.format(name, argspec.args[1])

            name = ':code:`{0}`'.format(name)
            lines.append(name)

            # Underline name with -'s for title
            lines.append(_title_line('~', name))

            # Append any docstring
            doc = cls.__doc__
            if doc:
                lines.append('')
                lines.append('{0}'.format('\n'.join([
                    line for line in doc.split('\n')
                ])))

            lines.append('')
            lines.append('')

    # Write out the file
    print('--> Writing docs/facts.rst')

    out = '\n'.join(lines)
    out = '''
Facts Index
===========

.. include:: facts_.rst


{0}
    '''.format(out).strip()

    outfile = open('docs/facts.rst', 'w')
    outfile.write(out)
    outfile.close()


if __name__ == '__main__':
    print('### Building fact docs')
    build_facts()