This repository has been archived by the owner on Feb 1, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7
/
LiveAPI_MakeDoc.py
273 lines (231 loc) · 8.97 KB
/
LiveAPI_MakeDoc.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
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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
# http://remotescripts.blogspot.com
"""
Copyright (C) 2011 Hanz Petrov <[email protected]>
MIDI Remote Script for generating Live API documentation,
based on realtime inspection of the Live module.
Writes two files to the userhome directory - Live.xml and Live.css.
Inspired in part by the following Live API exploration modules:
dumpXML by Nathan Ramella http://code.google.com/p/liveapi/source/browse/trunk/docs/Ableton+Live+API/makedocs
and LiveAPIGen by Patrick Mueller http://muellerware.org
Parts of the describe methods are based on "describe" by Anand, found at:
http://code.activestate.com/recipes/553262-list-classes-methods-and-functions-in-a-module/
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
"""
import Live
import os, sys, types
from _Support import inspect
from _Framework.ControlSurface import ControlSurface
class APIMakeDoc(ControlSurface):
def __init__(self, c_instance):
ControlSurface.__init__(self, c_instance)
module = Live
outfilename = (str(module.__name__) + ".xml")
outfilename = (os.path.join(os.path.expanduser('~'), outfilename))
cssfilename = "Live.css"
cssfilename = (os.path.join(os.path.expanduser('~'), cssfilename))
make_doc(module, outfilename, cssfilename)
def disconnect(self):
ControlSurface.disconnect(self)
def make_doc(module, outfilename, cssfilename):
if outfilename != None:
stdout_old = sys.stdout
outputfile = open(cssfilename, 'w')
sys.stdout = outputfile
print(css)
outputfile.close()
outputfile = open(outfilename, 'w')
sys.stdout = outputfile
print ('<?xml-stylesheet type="text/css" href="Live.css"?>') # set stylesheet to Live.css
print ('<Live>')
app = Live.Application.get_application() # get a handle to the App
maj = app.get_major_version() # get the major version from the App
min = app.get_minor_version() # get the minor version from the App
bug = app.get_bugfix_version() # get the bugfix version from the App
print ('Live API version ' + str(maj) + "." + str(min) + "." + str(bug)) # main title
print('<Doc>\t%s</Doc>\n' % header)
print('<Doc>\t%s</Doc>\n' % disclaimer)
describe_module(module)
print ("</Live>")
outputfile.close()
sys.stdout = stdout_old
def get_doc(obj):
""" Get object's doc string and remove \n's and clean up <'s and >'s for XML compatibility"""
doc = False
if obj.__doc__ != None:
doc = (obj.__doc__).replace("\n", "") #remove newlines from Live API docstings, for wrapped display
doc = doc.replace(" ", "") #Strip chunks of whitespace from docstrings, for wrapped display
doc = doc.replace("<", "<") #replace XML reserved characters
doc = doc.replace(">", ">")
doc = doc.replace("&", "&")
return doc
def print_obj_info(description, obj, name = None):
""" Print object's descriptor and name on one line, and docstring (if any) on the next """
if hasattr(obj, '__name__'):
name_str = obj.__name__
else:
name_str = name
if len(LINE) != 0:
LINE.append("." + name_str)
if inspect.ismethod(obj) or inspect.isbuiltin(obj):
LINE[-1] += "()"
else:
LINE.append(name_str)
line_str = ""
for item in LINE:
line_str += item
print ('<%s>%s<Description>%s</Description></%s>\n' % (description, line_str, description, description))
if hasattr(obj, '__doc__'):
if obj.__doc__ != None:
print('<Doc>\t%s</Doc>\n' % get_doc(obj))
def describe_obj(descr, obj):
""" Describe object passed as argument, and identify as Class, Method, Property, Value, or Built-In """
if obj.__name__ == "<unnamed Boost.Python function>" or obj.__name__.startswith('__'): #filter out descriptors
return
if (obj.__name__ == ("type")) or (obj.__name__ == ("class")): #filter out non-subclass type types
return
print_obj_info(descr, obj)
if inspect.ismethod(obj) or inspect.isbuiltin(obj): #go no further for these objects
LINE.pop()
return
else:
try:
members = inspect.getmembers(obj)
for (name, member) in members:
if inspect.isbuiltin(member):
describe_obj("Built-In", member)
for (name, member) in members:
if str(type(member)) == "<type 'property'>":
print_obj_info("Property", member, name)
LINE.pop()
for (name, member) in members:
if inspect.ismethod(member):
describe_obj("Method", member)
for (name, member) in members:
if (str(type(member)).startswith( "<class" )):
print_obj_info("Value", member, name)
LINE.pop()
for (name, member) in members:
if str(type(member)) == "<type 'object'>" or (str(type(member)) == "<type 'type'>" and not repr(obj).startswith("<class '")): #filter out unwanted types
continue
if inspect.isclass(member) and str(type(member)) == "<type 'type'>":
describe_obj("Sub-Class", member)
for (name, member) in members:
if str(type(member)) == "<type 'object'>" or (str(type(member)) == "<type 'type'>" and not repr(obj).startswith("<class '")): #filter out unwanted types
continue
if inspect.isclass(member) and not str(type(member)) == "<type 'type'>":
describe_obj("Class", member)
LINE.pop()
except:
return
def describe_module(module):
""" Describe the module object passed as argument
including its root classes and functions """
print_obj_info("Module", module)
for name in dir(module): #do the built-ins first
obj = getattr(module, name)
if inspect.isbuiltin(obj):
describe_obj("Built-In", obj)
for name in dir(module): #then the rest
obj = getattr(module, name)
if inspect.isclass(obj):
describe_obj("Class", obj)
elif (inspect.ismethod(obj) or inspect.isfunction(obj)):
describe_obj("Method", obj)
elif inspect.ismodule(obj):
describe_module(obj)
LINE.pop()
LINE = []
header = """Unofficial Live API documentation generated by the "API_MakeDoc" MIDI Remote Script.
<requirement xmlns:html="http://www.w3.org/1999/xhtml">
<html:a href="http://remotescripts.blogspot.com">http://remotescripts.blogspot.com</html:a></requirement>
"""
disclaimer = """This is unofficial documentation. Please do not contact Ableton with questions or problems relating to the use of this documentation."""
css = """/* Style Sheet for formatting XML output of Live API Inspector */
Live
{
background: #f8f8f8;
display: block;
margin-bottom: 10px;
margin-left: 20px;
margin-top: 10px;
padding: 4px;
font-family: "Lucida Sans Unicode", "Lucida Sans", "Lucida Grande", Verdana, sans-serif;
font-weight: bold;
color: #000000;
font-size: 10pt;
}
Module, Class, Sub-Class
{
display: block;
margin-bottom: 5px;
margin-top: 10px;
margin-left: -5px;
padding-left: 5px;
padding-top: 4px;
padding-bottom: 4px;
background: silver;
font-size: 12pt;
background-color: #DDD;
border: solid 1px #AAA;
color: #333;
}
Module
{
display: block;
color: #000;
background-color: #CCC;
}
Description
{
display: inline;
margin-left: 5px;
color: #000000;
font-family: Arial, Helvetica, sans-serif;
font-style: italic;
font-weight: normal;
font-size: 9pt;
}
Doc
{
display: block;
color: #408080;
margin-left: 20pt;
font-family: Arial, Helvetica, sans-serif;
font-style: italic;
font-weight: normal;
font-size: 9pt;
}
Method
{
display: block;
margin-top: 10px;
color: #000080;
}
Built-In
{
display: block;
margin-top: 10px;
color: #081798;
}
Property
{
display: block;
margin-top: 10px;
color: #0000AF;
}
Value
{
display: block;
margin-top: 10px;
color: #1C5A8D;
}
"""