MCPcopy Create free account
hub / github.com/dagger/dagger / generate

Function generate

sdk/python/codegen/src/codegen/generator.py:195–262  ·  view source on GitHub ↗

Code generation main function.

(schema: GraphQLSchema, schema_version: str = "")

Source from the content-addressed store, hash-verified

193
194@joiner
195def generate(schema: GraphQLSchema, schema_version: str = "") -> Iterator[str]:
196 """Code generation main function."""
197 yield textwrap.dedent(
198 """\
199 # Code generated by dagger. DO NOT EDIT.
200
201 import warnings # noqa: F401
202 from collections.abc import Callable
203 from dataclasses import dataclass
204 from typing import Protocol, runtime_checkable
205
206 from typing_extensions import Self
207
208 from dagger.client._core import Arg
209 from dagger.client._guards import typecheck
210 from dagger.client.base import Enum, Input, Root, Scalar, Type
211 """,
212 )
213
214 # Pre-create handy maps to make handler code simpler.
215 ids = frozenset(n for n, t in schema.type_map.items() if is_id_type(t))
216
217 # shared state between all handler instances
218 ctx = Context(ids=ids, schema=schema, schema_version=schema_version)
219
220 handlers: tuple[Handler, ...] = (
221 Scalar(ctx),
222 Enum(ctx),
223 Input(ctx),
224 InterfaceProtocol(ctx),
225 Object(ctx),
226 )
227
228 if ctx.legacy_sdk_compat:
229 for type_name in legacy_id_names(schema):
230 yield legacy_id_class(type_name)
231 ctx.defined.add(type_name)
232
233 # Split into two iterators to update ctx.remaining.
234 types_n, types_g = itertools.tee(get_grouped_types(handlers, schema.type_map))
235
236 # Track types that haven't been defined yet, to format as a forward reference.
237 ctx.remaining.update(name for _, name, _ in types_n)
238
239 for handler, type_name, named_type in types_g:
240 yield handler.render(named_type)
241 ctx.process_type(type_name)
242
243 yield ""
244 yield ""
245 yield "class Client(Query):"
246 yield indent(
247 '"""The Dagger client.\n'
248 "\n"
249 "Inherits all Query API methods and adds connection management.\n"
250 '"""'
251 )
252 ctx.defined.add("Client")

Callers 2

DecoratorsClass · 0.50

Calls 15

process_typeMethod · 0.95
InterfaceProtocolClass · 0.85
legacy_id_namesFunction · 0.85
legacy_id_classFunction · 0.85
get_grouped_typesFunction · 0.85
quoteFunction · 0.85
is_id_typeFunction · 0.70
ContextClass · 0.70
ScalarClass · 0.70
EnumClass · 0.70
InputClass · 0.70
ObjectClass · 0.70

Tested by 1