-
Notifications
You must be signed in to change notification settings - Fork 53
/
Copy pathscript.py
311 lines (254 loc) · 11 KB
/
script.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
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
import argparse
import importlib
import inspect
import json
import logging
import os
import shutil
import sys
from importlib.util import module_from_spec, spec_from_file_location
from tempfile import mkdtemp
from types import ModuleType
from typing import Any, Dict, List, Tuple, Type, Optional
from uuid import uuid4
from pydantic import BaseModel, Extra, create_model
from pydantic.fields import ModelField
try:
from pydantic.generics import GenericModel
except ImportError:
GenericModel = None
logger = logging.getLogger("pydantic2ts")
def import_module(path: str) -> ModuleType:
"""
Helper which allows modules to be specified by either dotted path notation or by filepath.
If we import by filepath, we must also assign a name to it and add it to sys.modules BEFORE
calling 'spec.loader.exec_module' because there is code in pydantic which requires that the
definition exist in sys.modules under that name.
"""
try:
if os.path.exists(path):
name = uuid4().hex
spec = spec_from_file_location(name, path, submodule_search_locations=[])
module = module_from_spec(spec)
sys.modules[name] = module
spec.loader.exec_module(module)
return module
else:
return importlib.import_module(path)
except Exception as e:
logger.error(
"The --module argument must be a module path separated by dots or a valid filepath"
)
raise e
def is_submodule(obj, module_name: str) -> bool:
"""
Return true if an object is a submodule
"""
return inspect.ismodule(obj) and getattr(obj, "__name__", "").startswith(
f"{module_name}."
)
def is_concrete_pydantic_model(obj) -> bool:
"""
Return true if an object is a concrete subclass of pydantic's BaseModel.
'concrete' meaning that it's not a GenericModel.
"""
if not inspect.isclass(obj):
return False
elif obj is BaseModel:
return False
elif GenericModel and issubclass(obj, GenericModel):
return bool(obj.__concrete__)
else:
return issubclass(obj, BaseModel)
def extract_pydantic_models(module: ModuleType) -> List[Type[BaseModel]]:
"""
Given a module, return a list of the pydantic models contained within it.
"""
models = []
module_name = module.__name__
for _, model in inspect.getmembers(module, is_concrete_pydantic_model):
models.append(model)
for _, submodule in inspect.getmembers(
module, lambda obj: is_submodule(obj, module_name)
):
models.extend(extract_pydantic_models(submodule))
return models
def clean_output_file(output_filename: str) -> None:
"""
Clean up the output file typescript definitions were written to by:
1. Removing the 'master model'.
This is a faux pydantic model with references to all the *actual* models necessary for generating
clean typescript definitions without any duplicates. We don't actually want it in the output, so
this function removes it from the generated typescript file.
2. Adding a banner comment with clear instructions for how to regenerate the typescript definitions.
"""
with open(output_filename, "r") as f:
lines = f.readlines()
start, end = None, None
for i, line in enumerate(lines):
if line.rstrip("\r\n") == "export interface _Master_ {":
start = i
elif (start is not None) and line.rstrip("\r\n") == "}":
end = i
break
banner_comment_lines = [
"/* tslint:disable */\n",
"/* eslint-disable */\n",
"/**\n",
"/* This file was automatically generated from pydantic models by running pydantic2ts.\n",
"/* Do not modify it by hand - just update the pydantic models and then re-run the script\n",
"*/\n\n",
]
new_lines = banner_comment_lines + lines[:start] + lines[(end + 1) :]
with open(output_filename, "w") as f:
f.writelines(new_lines)
def clean_schema(schema: Dict[str, Any]) -> None:
"""
Clean up the resulting JSON schemas by:
1) Removing titles from JSON schema properties.
If we don't do this, each property will have its own interface in the
resulting typescript file (which is a LOT of unnecessary noise).
2) Getting rid of the useless "An enumeration." description applied to Enums
which don't have a docstring.
"""
for prop in schema.get("properties", {}).values():
prop.pop("title", None)
if "enum" in schema and schema.get("description") == "An enumeration.":
del schema["description"]
def generate_json_schema(models: List[Type[BaseModel]], readonly_interfaces: bool) -> str:
"""
Create a top-level '_Master_' model with references to each of the actual models.
Generate the schema for this model, which will include the schemas for all the
nested models. Then clean up the schema.
One weird thing we do is we temporarily override the 'extra' setting in models,
changing it to 'forbid' UNLESS it was explicitly set to 'allow'. This prevents
'[k: string]: any' from being added to every interface. This change is reverted
once the schema has been generated.
"""
def find_model(name: str) -> Optional[Type[BaseModel]]:
return next((m for m in models if m.__name__ == name), None)
def find_field(prop: str, model_: Type[BaseModel]) -> ModelField:
return next(f for f in model_.__fields__.values() if f.alias == prop)
model_extras = [getattr(m.Config, "extra", None) for m in models]
try:
for m in models:
if getattr(m.Config, "extra", None) != Extra.allow:
m.Config.extra = Extra.forbid
master_model = create_model(
"_Master_", **{m.__name__: (m, ...) for m in models}
)
master_model.Config.extra = Extra.forbid
master_model.Config.schema_extra = staticmethod(clean_schema)
schema = json.loads(master_model.schema_json())
for d in schema.get("definitions", {}).values():
clean_schema(d)
if readonly_interfaces:
model = find_model(d["title"])
if model is not None:
props = d.get("properties", {}).keys()
d["required"] = list(prop for prop in props if not find_field(prop, model).allow_none)
return json.dumps(schema, indent=2)
finally:
for m, x in zip(models, model_extras):
if x is not None:
m.Config.extra = x
def generate_typescript_defs(
module: str, output: str, exclude: Tuple[str] = (), readonly_interfaces: bool = False, json2ts_cmd: str = "json2ts",
) -> None:
"""
Convert the pydantic models in a python module into typescript interfaces.
:param module: python module containing pydantic model definitions, ex: my_project.api.schemas
:param output: file that the typescript definitions will be written to
:param exclude: optional, a tuple of names for pydantic models which should be omitted from the typescript output.
:param json2ts_cmd: optional, the command that will execute json2ts. Provide this if the executable is not
discoverable or if it's locally installed (ex: 'yarn json2ts').
:param readonly_interfaces: optional, do not mark non-optional properties with default values as optional
in the generated interfaces.
"""
if " " not in json2ts_cmd and not shutil.which(json2ts_cmd):
raise Exception(
"json2ts must be installed. Instructions can be found here: "
"https://www.npmjs.com/package/json-schema-to-typescript"
)
logger.info("Finding pydantic models...")
models = extract_pydantic_models(import_module(module))
if exclude:
models = [m for m in models if m.__name__ not in exclude]
logger.info("Generating JSON schema from pydantic models...")
schema = generate_json_schema(models, readonly_interfaces)
schema_dir = mkdtemp()
schema_file_path = os.path.join(schema_dir, "schema.json")
with open(schema_file_path, "w") as f:
f.write(schema)
logger.info("Converting JSON schema to typescript definitions...")
json2ts_exit_code = os.system(
f'{json2ts_cmd} -i {schema_file_path} -o {output} --bannerComment ""'
)
shutil.rmtree(schema_dir)
if json2ts_exit_code == 0:
clean_output_file(output)
logger.info(f"Saved typescript definitions to {output}.")
else:
raise RuntimeError(
f'"{json2ts_cmd}" failed with exit code {json2ts_exit_code}.'
)
def parse_cli_args(args: List[str] = None) -> argparse.Namespace:
"""
Parses the command-line arguments passed to pydantic2ts.
"""
parser = argparse.ArgumentParser(
prog="pydantic2ts",
description=main.__doc__,
formatter_class=argparse.RawTextHelpFormatter,
)
parser.add_argument(
"--module",
help="name or filepath of the python module you would like to convert.\n"
"All the pydantic models within it will be converted to typescript interfaces.\n"
"Discoverable submodules will also be checked.",
)
parser.add_argument(
"--output",
help="name of the file the typescript definitions should be written to. Ex: './frontend/apiTypes.ts'",
)
parser.add_argument(
"--exclude",
action="append",
default=[],
help="name of a pydantic model which should be omitted from the resulting typescript definitions.\n"
"This option can be defined multiple times,\n"
"ex: `--exclude Foo --exclude Bar` to exclude both the Foo and Bar models from the output.",
)
parser.add_argument(
"--readonly-interfaces",
dest="readonly_interfaces",
action="store_true",
help="do not mark non-optional properties with default values as optional in the generated interfaces.\n"
"This is useful if you want an interface for data that is returned by an API (default values are not empty),\n"
"in contrast to an interface for data that is sent to an API (default values may be empty).",
)
parser.add_argument(
"--json2ts-cmd",
dest="json2ts_cmd",
default="json2ts",
help="optional, the command used to invoke json2ts.\n"
"Specify this if you have json-schema-to-typescript installed locally (ex: 'yarn json2ts')\n"
"or if the exact path to the executable is required (ex: /myproject/node_modules/bin/json2ts).\n"
"(default: json2ts)",
)
return parser.parse_args(args)
def main() -> None:
"""
CLI entrypoint to run :func:`generate_typescript_defs`
"""
logging.basicConfig(level=logging.DEBUG, format="%(asctime)s %(message)s")
args = parse_cli_args()
return generate_typescript_defs(
args.module,
args.output,
exclude=tuple(args.exclude),
readonly_interfaces=args.readonly_interfaces,
json2ts_cmd=args.json2ts_cmd,
)
if __name__ == "__main__":
main()