-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathgenerate.py
executable file
·229 lines (177 loc) · 6.1 KB
/
generate.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
#!/usr/bin/env python
import sys
import json
import textwrap
from io import StringIO
from collections import defaultdict
from datetime import datetime
import requests
from lxml import etree
def now():
return datetime.utcnow().isoformat() + 'Z'
def generate_spec(root='http://www.last.fm'):
"""
Generate structured JSON with description of all the endpoints
listed on http://www.last.fm/api/intro.
It crawlers the documentation pages and extracts resource names,
parameter types, etc.
"""
def get_doc(path):
return etree.fromstring(
requests.get(root + path).text.replace('<=', '<='),
parser=etree.HTMLParser()
)
paths = get_doc('/api/intro').xpath(
'//a[starts-with(@href, "/api/show")]/@href')
spec = defaultdict(dict)
spec['__generated__'] = now()
requires_auth = None
for path in paths:
sys.stderr.write(path + '\n')
params = {}
doc = get_doc(path)
package, method = path.split('/')[-1].split('.')
for param, desc in zip(
doc.xpath('//div[@id="wsdescriptor"]/span[@class="param"]'),
doc.xpath('//div[@id="wsdescriptor"]'
'/span[@class="param"]/following-sibling::text()[1]')
):
if not param.text:
# <span class="param"></span> <br />
continue
name = param.text.strip()
requires_auth = not doc.xpath('contains(normalize-space(/), '
'"does not require authentication")')
if name in {'api_key', 'api_sig', 'sk'}:
if name == 'sk':
assert requires_auth
continue
boolean = False
multiple = False
brackets = ''
if '[' in name:
i = name.index('[')
brackets = name[i:]
name = name[:i]
if brackets == '[0|1]':
boolean = True
elif brackets in ['[i]', '[1|2]']:
multiple = True
else:
pass
brackets += ' '
params[name] = {
'description': brackets + ' '.join(desc.split()),
'required': 'Required' in desc,
'boolean': boolean,
'multiple': multiple,
}
desc = ' '.join(
doc.xpath('string(//div[@class="wsdescription"])').strip().split())
spec[package][method] = {
'documentation': root + path,
'description': desc,
'auth': requires_auth,
'http': (
'POST' if doc.xpath('contains(normalize-space(/),'
' "HTTP POST request")')
else 'GET'
),
'params': params
}
print json.dumps(spec, indent=4, sort_keys=True)
def generate_code(specfile='api.json'):
"""Take a path to a spec file and generate the actual Python code."""
spec = json.load(open(specfile))
del spec['__generated__']
out = StringIO()
out.write(u'# Generated code. Do not edit.\n')
out.write(u'# %s\n' % now())
out.write(u'from .package import Package\n\n\n')
packages = sorted(spec.keys())
out.write(u'class BaseClient(object):\n\n')
out.write(u' def __init__(self):\n')
for package in packages:
out.write(u' self.%s = %s(self)\n' % (
package, package.capitalize())
)
out.write(u'\n\n')
for package in packages:
out.write(u'class %s(Package):\n\n' % package.capitalize())
for method in sorted(spec[package].keys()):
method_spec = spec[package][method]
args, doc, call = args_doc_call(method, method_spec)
out.write(u' def %s(%s):\n%s\n%s' % (
uncamel(method),
args,
doc,
call
))
out.write(u'\n')
print out.getvalue()
def prefix(text, p=' '):
return '\n'.join((p + line) for line in text.splitlines())
def uncamel(name):
"""helloWorld => hello_world"""
n = ''
for c in name:
if c.isupper():
n += '_' + c.lower()
else:
n += c
return n
def args_doc_call(method_name, spec):
"""Return args code, docstring, and call code."""
def make_safe(name):
if name in {'from', }:
return name + '_'
return name
def q(s):
return "'%s'" % s
params = spec['params']
safe = {name: make_safe(name) for name in params}
required = [safe[name] for name in params if params[name]['required']]
optional = [safe[name] for name in params if not params[name]['required']]
params = sorted(required)
for name in optional:
params.append(name + '=None')
args = ', '.join(['self'] + params)
doc = [
'"""',
] + textwrap.wrap(spec['description']) + [
'',
('Authorization required.'
if spec['auth']
else 'Authorization not required.'),
'',
spec['documentation'],
'',
]
for name in sorted(required) + sorted(optional):
param = spec['params'][name.rstrip('_')]
type_spec = ['required' if param['required'] else 'optional']
if param['multiple']:
type_spec.append('multiple')
if param['boolean']:
type_spec.append('boolean')
doc.append(':param %s: %s' % (
name,
', '.join(type_spec))
)
doc.extend(' ' + line for line
in textwrap.wrap(param['description']))
doc.append('\n"""\n')
doc = prefix('\n'.join(doc), ' ' * 8)
call = '%sreturn self._call(%s)\n' %(
' ' * 8,
', '.join([q(spec['http']), q(method_name), 'auth=%s' % spec['auth']] +
[name + '=' + name
for name in
sorted(required) + sorted(optional)])
)
return args, doc, call
if __name__ == '__main__':
{
'spec': generate_spec,
'code': generate_code
}[sys.argv[1]]()