-
-
Notifications
You must be signed in to change notification settings - Fork 81
/
generator.py
305 lines (244 loc) · 10.1 KB
/
generator.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
import os
import sys
import json
import logging
import traceback
from abc import ABC, abstractmethod
from typing import Any, Dict, List, Type, Generic, Optional, cast
from pathlib import Path
from contextvars import ContextVar
from typing_extensions import override
from jinja2 import Environment, StrictUndefined, FileSystemLoader
from pydantic import BaseModel, ValidationError
from . import jsonrpc
from .. import __version__
from .types import PartialModel
from .utils import (
copy_tree,
is_same_path,
resolve_template_path,
)
from ..utils import DEBUG, DEBUG_GENERATOR
from .errors import PartialTypeGeneratorError
from .models import PythonData, DefaultData
from .._types import BaseModelT, InheritsGeneric, get_args
from .filters import quote
from .jsonrpc import Manifest
from .._compat import model_json, model_parse, cached_property
__all__ = (
'BASE_PACKAGE_DIR',
'GenericGenerator',
'BaseGenerator',
'Generator',
'render_template',
'cleanup_templates',
'partial_models_ctx',
)
log: logging.Logger = logging.getLogger(__name__)
BASE_PACKAGE_DIR = Path(__file__).parent.parent
GENERIC_GENERATOR_NAME = 'prisma.generator.generator.GenericGenerator'
# set of templates that should be rendered after every other template
DEFERRED_TEMPLATES = {'partials.py.jinja'}
DEFAULT_ENV = Environment(
trim_blocks=True,
lstrip_blocks=True,
loader=FileSystemLoader(Path(__file__).parent / 'templates'),
undefined=StrictUndefined,
)
# the type: ignore is required because Jinja2 filters are not typed
# and Pyright infers the type from the default builtin filters which
# results in an overly restrictive type
DEFAULT_ENV.filters['quote'] = quote # pyright: ignore
partial_models_ctx: ContextVar[List[PartialModel]] = ContextVar('partial_models_ctx', default=[])
class GenericGenerator(ABC, Generic[BaseModelT]):
@abstractmethod
def get_manifest(self) -> Manifest:
"""Get the metadata for this generator
This is used by prisma to display the post-generate message e.g.
✔ Generated Prisma Client Python to ./.venv/lib/python3.9/site-packages/prisma
"""
...
@abstractmethod
def generate(self, data: BaseModelT) -> None: ...
@classmethod
def invoke(cls) -> None:
"""Shorthand for calling BaseGenerator().run()"""
generator = cls()
generator.run()
def run(self) -> None:
"""Run the generation loop
This can only be called from a prisma generation, e.g.
```prisma
generator client {
provider = "python generator.py"
}
```
"""
if not os.environ.get('PRISMA_GENERATOR_INVOCATION'):
raise RuntimeError('Attempted to invoke a generator outside of Prisma generation')
request = None
try:
while True:
line = jsonrpc.readline()
if line is None:
log.debug('Prisma invocation ending')
break
request = jsonrpc.parse(line)
self._on_request(request)
except Exception as exc:
if request is None:
raise exc from None
# We don't care about being overly verbose or printing potentially redundant data here
if DEBUG or DEBUG_GENERATOR:
traceback.print_exc()
# Do not include the full stack trace for pydantic validation errors as they are typically
# the fault of the user.
if isinstance(exc, ValidationError):
message = str(exc)
elif isinstance(exc, PartialTypeGeneratorError):
# TODO: remove our internal frame from this stack trace
message = (
'An exception ocurred while running the partial type generator\n' + traceback.format_exc().strip()
)
else:
message = traceback.format_exc().strip()
response = jsonrpc.ErrorResponse(
id=request.id,
error={
# code copied from https://github.com/prisma/prisma/blob/main/packages/generator-helper/src/generatorHandler.ts
'code': -32000,
'message': message,
'data': {},
},
)
jsonrpc.reply(response)
def _on_request(self, request: jsonrpc.Request) -> None:
response = None
if request.method == 'getManifest':
response = jsonrpc.SuccessResponse(
id=request.id,
result=dict(
manifest=self.get_manifest(),
),
)
elif request.method == 'generate':
if request.params is None: # pragma: no cover
raise RuntimeError('Prisma JSONRPC did not send data to generate.')
if DEBUG_GENERATOR:
_write_debug_data('params', json.dumps(request.params, indent=2))
data = model_parse(self.data_class, request.params)
if DEBUG_GENERATOR:
_write_debug_data('data', model_json(data, indent=2))
self.generate(data)
response = jsonrpc.SuccessResponse(id=request.id, result=None)
else: # pragma: no cover
raise RuntimeError(f'JSON RPC received unexpected method: {request.method}')
jsonrpc.reply(response)
@cached_property
def data_class(self) -> Type[BaseModelT]:
"""Return the BaseModel used to parse the Prisma DMMF"""
# we need to cast to object as otherwise pyright correctly marks the code as unreachable,
# this is because __orig_bases__ is not present in the typeshed stubs as it is
# intended to be for internal use only, however I could not find a method
# for resolving generic TypeVars for inherited subclasses without using it.
# please create an issue or pull request if you know of a solution.
cls = cast(object, self.__class__)
if not isinstance(cls, InheritsGeneric):
raise RuntimeError('Could not resolve generic type arguments.')
typ: Optional[Any] = None
for base in cls.__orig_bases__:
if base.__origin__ == GenericGenerator:
typ = base
break
if typ is None: # pragma: no cover
raise RuntimeError(
'Could not find the GenericGenerator type;\n'
'This should never happen;\n'
f'Does {cls} inherit from {GenericGenerator} ?'
)
args = get_args(typ)
if not args:
raise RuntimeError(f'Could not resolve generic arguments from type: {typ}')
model = args[0]
if not issubclass(model, BaseModel):
raise TypeError(
f'Expected first generic type argument argument to be a subclass of {BaseModel} '
f'but got {model} instead.'
)
# we know the type we have resolved is the same as the first generic argument
# passed to GenericGenerator, safe to cast
return cast(Type[BaseModelT], model)
class BaseGenerator(GenericGenerator[DefaultData]):
pass
class Generator(GenericGenerator[PythonData]):
@override
def __init_subclass__(cls, *args: Any, **kwargs: Any) -> None:
raise TypeError(f'{Generator} cannot be subclassed, maybe you meant {BaseGenerator}?')
@override
def get_manifest(self) -> Manifest:
return Manifest(
name=f'Prisma Client Python (v{__version__})',
default_output=BASE_PACKAGE_DIR,
requires_engines=[
'queryEngine',
],
)
@override
def generate(self, data: PythonData) -> None:
config = data.generator.config
rootdir = Path(data.generator.output.value)
if not rootdir.exists():
rootdir.mkdir(parents=True, exist_ok=True)
if not is_same_path(BASE_PACKAGE_DIR, rootdir):
copy_tree(BASE_PACKAGE_DIR, rootdir)
# copy the Prisma Schema file used to generate the client to the
# package so we can use it to instantiate the query engine
packaged_schema = rootdir / 'schema.prisma'
if not is_same_path(data.schema_path, packaged_schema):
packaged_schema.write_text(data.datamodel)
params = data.to_params()
try:
for name in DEFAULT_ENV.list_templates():
if not name.endswith('.py.jinja') or name.startswith('_') or name in DEFERRED_TEMPLATES:
continue
render_template(rootdir, name, params)
if config.partial_type_generator:
log.debug('Generating partial types')
config.partial_type_generator.run()
params['partial_models'] = partial_models_ctx.get()
for name in DEFERRED_TEMPLATES:
render_template(rootdir, name, params)
except:
cleanup_templates(rootdir, env=DEFAULT_ENV)
raise
log.debug('Finished generating Prisma Client Python')
def cleanup_templates(rootdir: Path, *, env: Optional[Environment] = None) -> None:
"""Revert module to pre-generation state"""
if env is None:
env = DEFAULT_ENV
for name in env.list_templates():
file = resolve_template_path(rootdir=rootdir, name=name)
if file.exists():
log.debug('Removing rendered template at %s', file)
file.unlink()
def render_template(
rootdir: Path,
name: str,
params: Dict[str, Any],
*,
env: Optional[Environment] = None,
) -> None:
if env is None:
env = DEFAULT_ENV
template = env.get_template(name)
output = template.render(**params)
file = resolve_template_path(rootdir=rootdir, name=name)
if not file.parent.exists():
file.parent.mkdir(parents=True, exist_ok=True)
file.write_bytes(output.encode(sys.getdefaultencoding()))
log.debug('Rendered template to %s', file.absolute())
def _write_debug_data(name: str, output: str) -> None:
path = Path(__file__).parent.joinpath(f'debug-{name}.json')
with path.open('w') as file:
file.write(output)
log.debug('Wrote generator %s to %s', name, path.absolute())