-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuilder.py
More file actions
287 lines (240 loc) · 8.49 KB
/
Copy pathbuilder.py
File metadata and controls
287 lines (240 loc) · 8.49 KB
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
274
275
276
277
278
279
280
281
282
283
284
285
286
287
import click
import json
from jinja2 import Environment, FileSystemLoader
import os
import importlib
import re
import requests
def extract_type(element):
if "type" in element:
if "format" in element and element["format"] is not None:
tipe = element["format"]
else:
tipe = element["type"].lower()
if tipe == "void":
return "None"
elif tipe in ["integer", "int32", "int64", "long"]:
return "int"
elif tipe in ["double", "float", "number"]:
return "float"
elif tipe == "string":
return "str"
elif tipe == "byte":
return "bytearray"
elif tipe == "date":
return "datetime.date"
elif tipe == "datetime":
return "datetime.datetime"
elif tipe == "boolean":
return "bool"
elif tipe == "array":
if "items" in element:
subtipe = extract_type(element["items"])
return "[{}]".format(subtipe)
else:
return "[]"
elif tipe == "map":
if "key" in element and "value" in element:
keytipe = extract_type(element["key"])
valuetipe = extract_type(element["value"])
return "{{{}:{}}}".format(keytipe, valuetipe)
else:
return "{}"
else:
# No type, check for $ref
if "$ref" in element:
return "model:{}".format(element["$ref"].capitalize())
def startswith(s, v):
return str(s).startswith(v)
def required_attributes(m):
return [r for r in m["required"]]
def optional_attributes(m):
return [
r for r in (y for y in list(m["properties"].keys()) if y not in m["required"])
]
def fix_param_name(name):
if name[-1:] == "]":
return name.replace("[", "_").replace("]", "")
else:
return name
def service_param_string(params):
"""Takes a param section from a metadata class and returns a param string for the service method"""
p = []
k = []
for param in params:
name = fix_param_name(param["name"])
if "required" in param and param["required"] is True:
p.append(name)
else:
if "default" in param:
k.append("{name}={default}".format(name=name, default=param["default"]))
else:
k.append("{name}=None".format(name=name))
p.sort()
k.sort()
a = p + k
return ", ".join(a)
def metadata_path(name):
return "py3canvas/meta/{}.py".format(name)
def model_path(name):
return "py3canvas/models/{}.py".format(name)
def api_path(name):
return "py3canvas/{}.py".format(name)
def get_jinja_env():
loader = FileSystemLoader(
os.path.join(os.path.abspath(os.path.dirname(__file__)), "templates")
)
env = Environment(loader=loader, trim_blocks=True, lstrip_blocks=True)
env.filters["fix_param_name"] = fix_param_name
env.filters["extract_type"] = extract_type
env.filters["startswith"] = startswith
env.filters["required_attributes"] = required_attributes
env.filters["optional_attributes"] = optional_attributes
env.filters["service_param_string"] = service_param_string
env.globals["getattr"] = getattr
return env
@click.command()
@click.option("--specfile", help="Canvas Swagger Spec (JSON Format)")
def build_metadata_class(specfile):
"""Generate a metadata class for the specified specfile."""
with open(specfile) as f:
spec = json.load(f)
name = os.path.basename(specfile).split(".")[0]
spec["name"] = name
env = get_jinja_env()
metadata_template = env.get_template("metadata.py.jinja2")
with open("py3canvas/meta/{}.py".format(name), "w") as t:
t.write(metadata_template.render(spec=spec))
@click.command()
@click.option("--metadata", help="Path to the python Model Metadata class")
def build_model_classes(metadata):
"""Generate a model class for any models contained in the specified spec file."""
i = importlib.import_module(metadata)
env = get_jinja_env()
model_template = env.get_template("model.py.jinja2")
for model in i.models:
with open(model_path(model.name.lower()), "w") as t:
t.write(model_template.render(model_md=model))
@click.command()
@click.option("--metadata", help="Path to the python Service Metadata class")
def build_service_class(metadata):
"""Generate a service class for the service contained in the specified metadata class."""
i = importlib.import_module(metadata)
service = i.service
env = get_jinja_env()
service_template = env.get_template("service.py.jinja2")
with open(api_path(service.name.lower()), "w") as t:
t.write(service_template.render(service_md=service))
@click.command()
@click.option(
"--specfile", required=True, type=click.File(mode="r"), help="The json specfile."
)
@click.option("--api-name", type=str, help="The name of the api class. Defaults to ")
@click.option(
"--output-folder",
required=True,
type=click.Path(file_okay=False, writable=True),
help="Path to output the API file to.",
)
def build_api_from_specfile(specfile, api_name, output_folder):
base_name = os.path.basename(specfile.name)
click.echo("Generating code for specfile: {}".format(base_name))
output_file_name = base_name.replace(".json", ".py")
module_path = "py3canvas.apis.{}".format(base_name[: base_name.find(".")])
if api_name is None:
raw_api_name = list(base_name[: base_name.find(".")])
api_name = ""
capitalize = True
for i in raw_api_name:
if i == "_":
capitalize = True
continue
if capitalize is True:
api_name += i.capitalize()
capitalize = False
else:
api_name += i
spec = json.load(specfile)
env = get_jinja_env()
api_template = env.get_template("api.py.jinja2")
with open(os.path.join(output_folder, output_file_name), "w") as api:
api.write(
api_template.render(
spec=spec,
api_name=api_name,
base_name=base_name,
module_path=module_path,
)
)
api_tests_template = env.get_template("api_tests.py.jinja2")
with open(
os.path.join(output_folder, "../tests/{}".format(output_file_name)), "w"
) as api_tests:
api_tests.write(
api_tests_template.render(
spec=spec,
api_name=api_name,
base_name=base_name,
module_path=module_path,
)
)
@click.command()
@click.option(
"--specfile-path",
required=True,
type=click.Path(file_okay=False, readable=True),
help="Path for specfiles",
)
@click.option(
"--output-folder",
required=True,
type=click.Path(file_okay=False, writable=True),
help="Path to output the API file to.",
)
@click.pass_context
def build_all_apis(ctx, specfile_path, output_folder):
specs = os.listdir(specfile_path)
for spec in specs:
specfile = os.path.join(specfile_path, spec)
with open(specfile, "r") as f:
ctx.invoke(
build_api_from_specfile,
specfile=f,
api_name=None,
output_folder=output_folder,
)
@click.command()
@click.option(
"--specfile-path",
required=True,
type=click.Path(file_okay=False, readable=True),
help="Path for specfiles",
)
def update_spec_files(specfile_path):
"""Update spec files from Instructure API docs"""
docsFile = "api-docs.json"
baseUrl = "https://canvas.instructure.com/doc/api/"
specs = requests.get(f"{baseUrl}{docsFile}").json()
for spec in specs["apis"]:
specName = spec["path"][1:]
r = requests.get(f"{baseUrl}{specName}")
if r.status_code == 200:
specFile = os.path.join(specfile_path, specName)
with open(specFile, "wb") as f:
f.write(r.content)
click.echo(f"Updated {specFile}")
else:
click.echo(
f"Something went wrong trying to retrieve {specName}. Status Code: {r.status_code}"
)
@click.group()
def cli():
pass
cli.add_command(build_metadata_class)
cli.add_command(build_model_classes)
cli.add_command(build_service_class)
cli.add_command(build_api_from_specfile)
cli.add_command(build_all_apis)
cli.add_command(update_spec_files)
if __name__ == "__main__":
cli()