File: create_rst_api_reference.py

package info (click to toggle)
python-expyriment 0.7.0%2Bgit34-g55a4e7e-3.2
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 1,504 kB
  • ctags: 2,094
  • sloc: python: 12,766; makefile: 150
file content (167 lines) | stat: -rwxr-xr-x 4,686 bytes parent folder | download | duplicates (2)
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
#!/usr/bin/env python

"""
make rst files for the expyriment API reference

"""
import inspect

import expyriment

def inspect_members(item):
    members = inspect.getmembers(eval(item))
    modules = []
    classes = []
    methods = []
    functions = []
    attributes = []
    for member in members:
        if member[0][0:1] != '_':
            if inspect.ismodule(member[1]):
                modules.append(member)
            elif inspect.isclass(member[1]):
                classes.append(member)
            elif inspect.isfunction(member[1]):
                functions.append(member)
            elif inspect.ismethod(member[1]):
                methods.append(member)
            else:
                attributes.append(member)

    return modules, classes, methods, functions, attributes


def heading(txt, t="="):
    return txt + "\n" + len(txt)*t + "\n"


def create_class_rst(class_name):
    with open(class_name + ".rst", 'w') as fl:
        fl.write(heading(class_name))
        fl.write("\n.. autoclass:: " + class_name +"\n")
        fl.write("   :members:\n")
        fl.write("   :inherited-members:\n")
        fl.write("\n   .. automethod:: " + class_name + ".__init__\n")

def create_module_rst(mod_name, no_members=False):
    with open(mod_name + ".rst", 'w') as fl:
        fl.write(heading(mod_name))
        fl.write("\n.. automodule:: " + mod_name + "\n")
        fl.write("   :members:\n")
        fl.write("   :undoc-members:\n")
        fl.write("   :show-inheritance:\n")
        fl.write("   :inherited-members:\n")

        modules, classes, methods, functions, attributes = inspect_members(mod_name)
        if len(attributes)>0:
            fl.write(heading("\nAttributes", "-"))

            for att in attributes:
                att = mod_name + "." + att[0]
                fl.write(".. py:data:: " + att + "\n\n")
                #t = eval("type(" + att + ")")
                v = eval("repr(" + att + ")")
                fl.write("   default value: {0}\n\n".format(v))

        if len(modules)>0:
            fl.write(heading("\n\nModules", "-"))
            fl.write(".. toctree::\n   :maxdepth: 1\n   :titlesonly:\n")

            for m in modules:
                fl.write("\n   " + mod_name + "." + m[0])
                create_module_rst(mod_name + "." + m[0])

        if len(classes)>0:
            fl.write(heading("\n\nClasses", "-"))
            fl.write(".. toctree::\n   :titlesonly:\n")

            for cl in classes:
                fl.write("\n   " + mod_name + "." + cl[0])
                create_class_rst(mod_name + "." + cl[0])

        if len(functions)>0:
            fl.write(heading("\n\nFunctions", "-"))
            for func in functions:
                fl.write(".. autofunction:: " + mod_name + "." + func[0] + "\n")

        fl.write("\n\n")
        #fl.write("\n\n.. "+repr(modules) + "\n")
        #fl.write(".. "+repr(classes) + "\n")
        #fl.write(".. "+repr(methods) + "\n")
        #fl.write(".. "+repr(functions) + "\n")
        #fl.write(".. "+repr(attributes) + "\n")


def create_change_log_rst():
    """create well shaped Changelog.rst from CHANGES.md"""

    changes_md = "../../CHANGES.md"
    changelog_rst = "Changelog.rst"

    fl = open(changes_md, 'r')
    out = open(changelog_rst, 'w')

    out.write("""Changelog
==========

""")

    version_found = False
    for line in fl:
        if line.startswith("Version"):
            version_found = True
        if version_found:
            if line.startswith("New Feature") or line.startswith("Fixed") or\
                    line.startswith("Changes") or line.startswith("Changed"):
                out.write("\n" + line + "\n") # additonal blanklines
            elif line.startswith("--"):
                out.write(line + "\n")
            elif line.startswith("  - "):
                out.write("\n" + line)
            else:
                out.write(line)

    out.close()
    fl.close()



# main module
with open("expyriment.rst", 'w') as fl:
    fl.write("""
expyriment
==========

.. automodule:: expyriment


Packages
--------

.. toctree::
   :maxdepth: 1
   :titlesonly:

   expyriment.control
   expyriment.design
   expyriment.io
   expyriment.misc
   expyriment.stimuli

Functions
---------
.. autofunction:: expyriment.get_version
.. autofunction:: expyriment.show_documentation
.. autofunction:: expyriment.get_system_info
.. autofunction:: expyriment.get_experiment_secure_hash

""")

sub_modules = ["expyriment.io", "expyriment.design", "expyriment.stimuli",
               "expyriment.control", "expyriment.misc"]

#sub_modules
for mod_name in sub_modules:
    create_module_rst(mod_name)

create_change_log_rst()