-
Notifications
You must be signed in to change notification settings - Fork 31
/
build-api.py
125 lines (111 loc) · 4.75 KB
/
build-api.py
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
import inspect
import textwrap
import sys
from numpydoc import docscrape
from pathlib import Path
def dedent_docstring(docstring):
if '\n' not in docstring:
return docstring
firstline, rest = docstring.expandtabs(4).split('\n', 1)
return firstline.lstrip() + '\n' + textwrap.dedent(rest)
def docstring_to_markdown(docstring):
if '{nodoc}' in docstring:
return
docstring = dedent_docstring(docstring)
parsed_docstring = docscrape.NumpyDocString(docstring)
summary = parsed_docstring['Summary'] + [''] + \
parsed_docstring['Extended Summary']
if not summary:
return
md = "\n".join(summary) + "\n\n"
if parsed_docstring['Parameters']:
md += "__Parameters__\n\n"
for name, type_, desc in parsed_docstring['Parameters']:
name = name.replace('*', r'\*')
desc = '\n'.join(desc)
md += f"- **{name}**: {desc}\n"
if parsed_docstring['Returns']:
md += "\n__Returns__\n\n"
for name, type_, desc in parsed_docstring['Returns']:
desc = '\n'.join(desc)
md += f"- {desc}\n"
if parsed_docstring['Examples']:
examples = parsed_docstring['Examples']
if examples[0].startswith('>>>'):
examples = [line[4:] for line in examples]
example = '\n'.join(examples)
md += f"\n__Example__\n\n~~~ .python\n{example.strip()}\n~~~\n\n"
else:
example = '\n'.join(examples)
md += f"\n__Example__\n\n{example.strip()}\n\n"
return md
def generate_markdown_docs(obj, name=None, descriptor=None):
methods = inspect.getmembers(obj, predicate=inspect.isfunction)
md = '<div class="ClassDoc YAMLDoc" markdown="1">\n\n'
if name is None:
name = obj.__name__
if descriptor is None:
descriptor = 'class'
if isinstance(obj, type):
md += f"# {descriptor} __{name}__\n\n"
md += docstring_to_markdown(obj.__doc__)
for method_name, method in methods:
if method_name.startswith('_') or method.__doc__ is None:
continue
npdoc = docstring_to_markdown(method.__doc__)
if npdoc is None:
continue
# Get the function signature but remove self as the first argument
method_sig = str(inspect.signature(method)).replace('self, ', '') \
.replace('*', r'\*')
if method_sig in ('self', '(self)'):
method_sig = '()'
md += f"## {method_name}{method_sig}\n\n{npdoc}\n\n"
return md + '</div>\n\n'
def gendoc(obj, path, name=None, descriptor=None):
root = Path('include/api')
Path(root / path).write_text(generate_markdown_docs(obj, name, descriptor))
sys.path.append('/home/sebastiaan/git/OpenSesame')
# sys.path.append('/home/sebastiaan/git/PyGaze')
from opensesame_plugins.core.joystick._libjoystick.basejoystick import BaseJoystick
gendoc(BaseJoystick, 'joystick.md', 'joystick', descriptor='instance')
from opensesame_plugins.core.srbox.libsrbox import LibSrbox
gendoc(LibSrbox, 'srbox.md', 'srbox', descriptor='instance')
from libopensesame.var_store import VarStore
gendoc(VarStore, 'var.md', 'var', descriptor='instance')
from libopensesame.file_pool_store import FilePoolStore
gendoc(FilePoolStore, 'pool.md', 'pool', descriptor='instance')
from libopensesame.item_store import ItemStore
gendoc(ItemStore, 'items.md', 'items', descriptor='instance')
from libopensesame.response_store import ResponseStore
gendoc(ResponseStore, 'responses.md', 'responses', descriptor='instance')
from libopensesame import python_workspace_api
gendoc(python_workspace_api, 'python_workspace_api.md')
from openexp._canvas.canvas import Canvas
gendoc(Canvas, 'canvas.md')
from openexp._sampler.sampler import Sampler
gendoc(Sampler, 'sampler.md')
from openexp._keyboard.keyboard import Keyboard
gendoc(Keyboard, 'keyboard.md')
from openexp._mouse.mouse import Mouse
gendoc(Mouse, 'mouse.md')
from openexp._clock.clock import Clock
gendoc(Clock, 'clock.md', 'clock', descriptor='instance')
from openexp._log.log import Log
gendoc(Log, 'log.md', 'log', descriptor='instance')
from libopensesame.widgets._form import Form
# gendoc(Form, 'form.md')
# from libopensesame.widgets._button import Button
# gendoc(Button, 'button.md')
# from libopensesame.widgets._image import Image
# gendoc(Image, 'image.md')
# from libopensesame.widgets._image_button import ImageButton
# gendoc(ImageButton, 'image_button.md')
# from libopensesame.widgets._checkbox import Checkbox
# gendoc(Checkbox, 'checkbox.md')
# from libopensesame.widgets._rating_scale import RatingScale
# gendoc(RatingScale, 'rating_scale.md')
# from libopensesame.widgets._label import Label
# gendoc(Label, 'label.md')
# from libopensesame.widgets._text_input import TextInput
# gendoc(TextInput, 'text_input.md')