sdkloom 0.0.0-stage → 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +202 -0
- package/README.md +239 -2
- package/kits/allowed-sources.json +24 -0
- package/kits/csharp/kit.json +6 -0
- package/kits/csharp/src/Runtime/Client.cs +295 -0
- package/kits/csharp/src/Runtime/Context.cs +216 -0
- package/kits/csharp/src/Runtime/Errors.cs +121 -0
- package/kits/csharp/src/Runtime/Http.cs +218 -0
- package/kits/csharp/src/Runtime/Json.cs +650 -0
- package/kits/csharp/src/Runtime/Lang.cs +212 -0
- package/kits/csharp/src/Runtime/Models.cs +206 -0
- package/kits/csharp/src/Runtime/Multipart.cs +108 -0
- package/kits/csharp/src/Runtime/Pagination.cs +122 -0
- package/kits/csharp/src/Runtime/Sse.cs +197 -0
- package/kits/csharp/src/Runtime/Testing.cs +375 -0
- package/kits/csharp/src/Runtime/Transport.cs +212 -0
- package/kits/csharp/src/Runtime/Wait.cs +57 -0
- package/kits/csharp/src/Runtime/Webhooks.cs +148 -0
- package/kits/csharp/tests/ClientTests.cs +128 -0
- package/kits/csharp/tests/HttpTests.cs +44 -0
- package/kits/csharp/tests/JsonTests.cs +132 -0
- package/kits/csharp/tests/LangTests.cs +57 -0
- package/kits/csharp/tests/MultipartTests.cs +31 -0
- package/kits/csharp/tests/PaginationTests.cs +93 -0
- package/kits/csharp/tests/SseTests.cs +122 -0
- package/kits/csharp/tests/TestRunner.cs +93 -0
- package/kits/csharp/tests/TestingTests.cs +80 -0
- package/kits/csharp/tests/TransportTests.cs +145 -0
- package/kits/csharp/tests/WaitTests.cs +39 -0
- package/kits/csharp/tests/WebhooksTests.cs +46 -0
- package/kits/csharp/versions.json +12 -0
- package/kits/go/kit.json +6 -0
- package/kits/go/src/sdkruntime/client.go +322 -0
- package/kits/go/src/sdkruntime/context.go +207 -0
- package/kits/go/src/sdkruntime/errors.go +74 -0
- package/kits/go/src/sdkruntime/http.go +261 -0
- package/kits/go/src/sdkruntime/json.go +527 -0
- package/kits/go/src/sdkruntime/lang.go +462 -0
- package/kits/go/src/sdkruntime/multipart.go +75 -0
- package/kits/go/src/sdkruntime/pagination.go +150 -0
- package/kits/go/src/sdkruntime/sse.go +267 -0
- package/kits/go/src/sdkruntime/testing.go +343 -0
- package/kits/go/src/sdkruntime/transport.go +168 -0
- package/kits/go/src/sdkruntime/wait.go +49 -0
- package/kits/go/src/sdkruntime/webhooks.go +129 -0
- package/kits/go/tests/client_test.go +185 -0
- package/kits/go/tests/http_test.go +78 -0
- package/kits/go/tests/json_test.go +127 -0
- package/kits/go/tests/lang_test.go +98 -0
- package/kits/go/tests/multipart_test.go +29 -0
- package/kits/go/tests/pagination_test.go +69 -0
- package/kits/go/tests/sse_test.go +129 -0
- package/kits/go/tests/testing_test.go +121 -0
- package/kits/go/tests/transport_test.go +133 -0
- package/kits/go/tests/wait_test.go +46 -0
- package/kits/go/tests/webhooks_test.go +62 -0
- package/kits/go/versions.json +12 -0
- package/kits/java/kit.json +6 -0
- package/kits/java/src/dev/sdkloom/runtime/ApiException.java +63 -0
- package/kits/java/src/dev/sdkloom/runtime/ArgumentException.java +24 -0
- package/kits/java/src/dev/sdkloom/runtime/Assert.java +72 -0
- package/kits/java/src/dev/sdkloom/runtime/ClientRuntime.java +220 -0
- package/kits/java/src/dev/sdkloom/runtime/Clock.java +39 -0
- package/kits/java/src/dev/sdkloom/runtime/Context.java +79 -0
- package/kits/java/src/dev/sdkloom/runtime/Decode.java +200 -0
- package/kits/java/src/dev/sdkloom/runtime/DecodeException.java +26 -0
- package/kits/java/src/dev/sdkloom/runtime/Decoder.java +16 -0
- package/kits/java/src/dev/sdkloom/runtime/Emitter.java +56 -0
- package/kits/java/src/dev/sdkloom/runtime/ErrorFactory.java +18 -0
- package/kits/java/src/dev/sdkloom/runtime/EventListener.java +20 -0
- package/kits/java/src/dev/sdkloom/runtime/EventStream.java +232 -0
- package/kits/java/src/dev/sdkloom/runtime/FakeClock.java +37 -0
- package/kits/java/src/dev/sdkloom/runtime/Headers.java +42 -0
- package/kits/java/src/dev/sdkloom/runtime/HttpRequest.java +88 -0
- package/kits/java/src/dev/sdkloom/runtime/HttpResponse.java +69 -0
- package/kits/java/src/dev/sdkloom/runtime/JdkTransport.java +97 -0
- package/kits/java/src/dev/sdkloom/runtime/Js.java +230 -0
- package/kits/java/src/dev/sdkloom/runtime/Json.java +281 -0
- package/kits/java/src/dev/sdkloom/runtime/JsonValue.java +15 -0
- package/kits/java/src/dev/sdkloom/runtime/Links.java +62 -0
- package/kits/java/src/dev/sdkloom/runtime/Middleware.java +16 -0
- package/kits/java/src/dev/sdkloom/runtime/MiddlewareEntry.java +43 -0
- package/kits/java/src/dev/sdkloom/runtime/MockRoute.java +111 -0
- package/kits/java/src/dev/sdkloom/runtime/MockTransport.java +62 -0
- package/kits/java/src/dev/sdkloom/runtime/MultipartForm.java +111 -0
- package/kits/java/src/dev/sdkloom/runtime/Next.java +16 -0
- package/kits/java/src/dev/sdkloom/runtime/OperationInfo.java +35 -0
- package/kits/java/src/dev/sdkloom/runtime/OperationMock.java +105 -0
- package/kits/java/src/dev/sdkloom/runtime/OperationSpec.java +125 -0
- package/kits/java/src/dev/sdkloom/runtime/Pages.java +55 -0
- package/kits/java/src/dev/sdkloom/runtime/Pipeline.java +29 -0
- package/kits/java/src/dev/sdkloom/runtime/RawResult.java +31 -0
- package/kits/java/src/dev/sdkloom/runtime/RecordedEvent.java +36 -0
- package/kits/java/src/dev/sdkloom/runtime/SdkInfo.java +38 -0
- package/kits/java/src/dev/sdkloom/runtime/SendOptions.java +34 -0
- package/kits/java/src/dev/sdkloom/runtime/ServerSentEvent.java +58 -0
- package/kits/java/src/dev/sdkloom/runtime/State.java +58 -0
- package/kits/java/src/dev/sdkloom/runtime/Test.java +21 -0
- package/kits/java/src/dev/sdkloom/runtime/TestRunner.java +63 -0
- package/kits/java/src/dev/sdkloom/runtime/Transport.java +16 -0
- package/kits/java/src/dev/sdkloom/runtime/TransportException.java +30 -0
- package/kits/java/src/dev/sdkloom/runtime/UrlConnectionTransport.java +195 -0
- package/kits/java/src/dev/sdkloom/runtime/Urls.java +116 -0
- package/kits/java/src/dev/sdkloom/runtime/Waits.java +53 -0
- package/kits/java/src/dev/sdkloom/runtime/WebhookSignatureException.java +24 -0
- package/kits/java/src/dev/sdkloom/runtime/Webhooks.java +146 -0
- package/kits/java/tests/dev/sdkloom/runtime/ClientTest.java +266 -0
- package/kits/java/tests/dev/sdkloom/runtime/EventStreamTest.java +131 -0
- package/kits/java/tests/dev/sdkloom/runtime/HttpTest.java +95 -0
- package/kits/java/tests/dev/sdkloom/runtime/JdkTransportTest.java +12 -0
- package/kits/java/tests/dev/sdkloom/runtime/JsTest.java +102 -0
- package/kits/java/tests/dev/sdkloom/runtime/JsonTest.java +87 -0
- package/kits/java/tests/dev/sdkloom/runtime/LinksTest.java +22 -0
- package/kits/java/tests/dev/sdkloom/runtime/MultipartFormTest.java +30 -0
- package/kits/java/tests/dev/sdkloom/runtime/PagesTest.java +75 -0
- package/kits/java/tests/dev/sdkloom/runtime/TestData.java +53 -0
- package/kits/java/tests/dev/sdkloom/runtime/TestingTest.java +156 -0
- package/kits/java/tests/dev/sdkloom/runtime/TransportContract.java +201 -0
- package/kits/java/tests/dev/sdkloom/runtime/UrlConnectionTransportTest.java +49 -0
- package/kits/java/tests/dev/sdkloom/runtime/WaitsTest.java +43 -0
- package/kits/java/tests/dev/sdkloom/runtime/WebhooksTest.java +45 -0
- package/kits/java/versions.json +41 -0
- package/kits/python/kit.json +6 -0
- package/kits/python/src/sdkloom_runtime/__init__.py +1 -0
- package/kits/python/src/sdkloom_runtime/client.py +232 -0
- package/kits/python/src/sdkloom_runtime/compat.py +195 -0
- package/kits/python/src/sdkloom_runtime/context.py +134 -0
- package/kits/python/src/sdkloom_runtime/errors.py +65 -0
- package/kits/python/src/sdkloom_runtime/http.py +192 -0
- package/kits/python/src/sdkloom_runtime/lang.py +207 -0
- package/kits/python/src/sdkloom_runtime/models.py +221 -0
- package/kits/python/src/sdkloom_runtime/multipart.py +69 -0
- package/kits/python/src/sdkloom_runtime/pagination.py +61 -0
- package/kits/python/src/sdkloom_runtime/pipeline.py +39 -0
- package/kits/python/src/sdkloom_runtime/sse.py +121 -0
- package/kits/python/src/sdkloom_runtime/testing.py +224 -0
- package/kits/python/src/sdkloom_runtime/transport.py +120 -0
- package/kits/python/src/sdkloom_runtime/wait.py +44 -0
- package/kits/python/src/sdkloom_runtime/webhooks.py +84 -0
- package/kits/python/tests/support.py +28 -0
- package/kits/python/tests/test_client.py +275 -0
- package/kits/python/tests/test_compat.py +87 -0
- package/kits/python/tests/test_http.py +94 -0
- package/kits/python/tests/test_lang.py +104 -0
- package/kits/python/tests/test_models.py +110 -0
- package/kits/python/tests/test_multipart.py +37 -0
- package/kits/python/tests/test_pagination.py +70 -0
- package/kits/python/tests/test_sse.py +61 -0
- package/kits/python/tests/test_testing.py +130 -0
- package/kits/python/tests/test_transport.py +124 -0
- package/kits/python/tests/test_wait.py +46 -0
- package/kits/python/tests/test_webhooks.py +57 -0
- package/kits/python/versions.json +33 -0
- package/kits/rust/kit.json +6 -0
- package/kits/rust/src/runtime/client.rs +212 -0
- package/kits/rust/src/runtime/context.rs +182 -0
- package/kits/rust/src/runtime/crypto.rs +84 -0
- package/kits/rust/src/runtime/errors.rs +147 -0
- package/kits/rust/src/runtime/http.rs +248 -0
- package/kits/rust/src/runtime/json.rs +424 -0
- package/kits/rust/src/runtime/lang.rs +296 -0
- package/kits/rust/src/runtime/mod.rs +30 -0
- package/kits/rust/src/runtime/models.rs +266 -0
- package/kits/rust/src/runtime/multipart.rs +81 -0
- package/kits/rust/src/runtime/pagination.rs +117 -0
- package/kits/rust/src/runtime/sse.rs +195 -0
- package/kits/rust/src/runtime/testing.rs +308 -0
- package/kits/rust/src/runtime/transport.rs +324 -0
- package/kits/rust/src/runtime/wait.rs +29 -0
- package/kits/rust/src/runtime/webhooks.rs +142 -0
- package/kits/rust/tests/client.rs +152 -0
- package/kits/rust/tests/http.rs +44 -0
- package/kits/rust/tests/json.rs +82 -0
- package/kits/rust/tests/lang.rs +74 -0
- package/kits/rust/tests/multipart.rs +21 -0
- package/kits/rust/tests/pagination.rs +61 -0
- package/kits/rust/tests/sse.rs +77 -0
- package/kits/rust/tests/testing.rs +90 -0
- package/kits/rust/tests/transport.rs +218 -0
- package/kits/rust/tests/wait.rs +32 -0
- package/kits/rust/tests/webhooks.rs +73 -0
- package/kits/rust/versions.json +6 -0
- package/kits/typescript/kit.json +6 -0
- package/kits/typescript/src/client.ts +223 -0
- package/kits/typescript/src/context.ts +108 -0
- package/kits/typescript/src/errors.ts +63 -0
- package/kits/typescript/src/helpers.ts +49 -0
- package/kits/typescript/src/http.ts +192 -0
- package/kits/typescript/src/models.ts +139 -0
- package/kits/typescript/src/multipart.ts +66 -0
- package/kits/typescript/src/pagination.ts +54 -0
- package/kits/typescript/src/pipeline.ts +25 -0
- package/kits/typescript/src/sse.ts +102 -0
- package/kits/typescript/src/testing.ts +217 -0
- package/kits/typescript/src/transport.ts +101 -0
- package/kits/typescript/src/wait.ts +36 -0
- package/kits/typescript/src/webhooks.ts +168 -0
- package/kits/typescript/tests/client.test.ts +269 -0
- package/kits/typescript/tests/helpers.test.ts +27 -0
- package/kits/typescript/tests/http.test.ts +80 -0
- package/kits/typescript/tests/models.test.ts +71 -0
- package/kits/typescript/tests/multipart.test.ts +28 -0
- package/kits/typescript/tests/pagination.test.ts +76 -0
- package/kits/typescript/tests/sse.test.ts +82 -0
- package/kits/typescript/tests/testing.test.ts +103 -0
- package/kits/typescript/tests/transport.test.ts +127 -0
- package/kits/typescript/tests/wait.test.ts +31 -0
- package/kits/typescript/tests/webhooks.test.ts +53 -0
- package/kits/typescript/versions.json +15 -0
- package/package.json +37 -4
- package/packages/cli/recipes/apiKeyHeader.ts +16 -0
- package/packages/cli/recipes/apiVersionHeader.ts +16 -0
- package/packages/cli/recipes/basic.ts +13 -0
- package/packages/cli/recipes/bearer.ts +10 -0
- package/packages/cli/recipes/hmacSign.ts +23 -0
- package/packages/cli/recipes/idempotencyKey.ts +19 -0
- package/packages/cli/recipes/oauthClientCredentials.ts +50 -0
- package/packages/cli/recipes/retry.ts +42 -0
- package/packages/cli/recipes/timing.ts +12 -0
- package/packages/cli/recipes/userAgent.ts +8 -0
- package/packages/cli/src/build.js +93 -0
- package/packages/cli/src/commands/build.js +31 -0
- package/packages/cli/src/commands/conformance-check.js +50 -0
- package/packages/cli/src/commands/contract.js +181 -0
- package/packages/cli/src/commands/diff.js +113 -0
- package/packages/cli/src/commands/doctor.js +43 -0
- package/packages/cli/src/commands/import-openapi.js +42 -0
- package/packages/cli/src/commands/init.js +27 -0
- package/packages/cli/src/commands/kit-check.js +145 -0
- package/packages/cli/src/commands/publish.js +81 -0
- package/packages/cli/src/commands/recipe.js +49 -0
- package/packages/cli/src/commands/record.js +181 -0
- package/packages/cli/src/commands/serve.js +156 -0
- package/packages/cli/src/commands/spec-check.js +57 -0
- package/packages/cli/src/commands/test.js +177 -0
- package/packages/cli/src/commands/variants.js +32 -0
- package/packages/cli/src/commands/verify.js +57 -0
- package/packages/cli/src/commands/version.js +63 -0
- package/packages/cli/src/containers.js +82 -0
- package/packages/cli/src/coverage.js +72 -0
- package/packages/cli/src/git.js +25 -0
- package/packages/cli/src/io.js +50 -0
- package/packages/cli/src/java/ScenarioHarness.java +297 -0
- package/packages/cli/src/java/SchemaRoundTrip.java +63 -0
- package/packages/cli/src/java.js +70 -0
- package/packages/cli/src/js/harness.mjs +120 -0
- package/packages/cli/src/kits.js +103 -0
- package/packages/cli/src/languages/csharp.js +274 -0
- package/packages/cli/src/languages/go.js +185 -0
- package/packages/cli/src/languages/index.js +18 -0
- package/packages/cli/src/languages/java.js +187 -0
- package/packages/cli/src/languages/python.js +120 -0
- package/packages/cli/src/languages/rust.js +189 -0
- package/packages/cli/src/languages/types.js +93 -0
- package/packages/cli/src/languages/typescript.js +112 -0
- package/packages/cli/src/main.js +13 -0
- package/packages/cli/src/packaging.js +39 -0
- package/packages/cli/src/portability.js +45 -0
- package/packages/cli/src/profiles.js +55 -0
- package/packages/cli/src/project.js +56 -0
- package/packages/cli/src/python/scenario_harness.py +162 -0
- package/packages/cli/src/python/schema_roundtrip.py +42 -0
- package/packages/cli/src/registries.js +214 -0
- package/packages/cli/src/resolve-core.js +39 -0
- package/packages/cli/src/run.js +133 -0
- package/packages/cli/src/runtimes.js +61 -0
- package/packages/cli/src/scenarios-java.js +82 -0
- package/packages/cli/src/scenarios-javascript.js +54 -0
- package/packages/cli/src/scenarios-python.js +90 -0
- package/packages/cli/src/scenarios.js +183 -0
- package/packages/cli/src/schema.js +84 -0
- package/packages/cli/src/templates.js +85 -0
- package/packages/cli/src/toolchains.js +68 -0
- package/packages/cli/src/transpiled-tests.js +92 -0
- package/packages/codegen/src/canonical-json.js +31 -0
- package/packages/codegen/src/capabilities.js +50 -0
- package/packages/codegen/src/code-writer.js +54 -0
- package/packages/codegen/src/file-tree.js +25 -0
- package/packages/codegen/src/hash.js +6 -0
- package/packages/codegen/src/index.js +16 -0
- package/packages/codegen/src/lock.js +106 -0
- package/packages/conformance/src/case.js +143 -0
- package/packages/conformance/src/checks.js +40 -0
- package/packages/conformance/src/index.js +3 -0
- package/packages/conformance/src/matrix.js +48 -0
- package/packages/conformance/src/suite.js +48 -0
- package/packages/differ/src/diff.js +500 -0
- package/packages/differ/src/index.js +3 -0
- package/packages/differ/src/notes.js +112 -0
- package/packages/differ/src/support.js +34 -0
- package/packages/gen-csharp/src/emit-client.js +397 -0
- package/packages/gen-csharp/src/emit-harness.js +333 -0
- package/packages/gen-csharp/src/emit-mock.js +111 -0
- package/packages/gen-csharp/src/emit-models.js +119 -0
- package/packages/gen-csharp/src/emit-webhooks.js +57 -0
- package/packages/gen-csharp/src/generate.js +245 -0
- package/packages/gen-csharp/src/index.js +5 -0
- package/packages/gen-csharp/src/names.js +284 -0
- package/packages/gen-go/src/emit-client.js +405 -0
- package/packages/gen-go/src/emit-harness.js +294 -0
- package/packages/gen-go/src/emit-mock.js +112 -0
- package/packages/gen-go/src/emit-models.js +146 -0
- package/packages/gen-go/src/emit-webhooks.js +58 -0
- package/packages/gen-go/src/generate.js +205 -0
- package/packages/gen-go/src/index.js +5 -0
- package/packages/gen-go/src/names.js +197 -0
- package/packages/gen-java/src/emit-client.js +441 -0
- package/packages/gen-java/src/emit-models.js +342 -0
- package/packages/gen-java/src/emit-testing.js +123 -0
- package/packages/gen-java/src/emit-webhooks.js +70 -0
- package/packages/gen-java/src/generate.js +244 -0
- package/packages/gen-java/src/index.js +4 -0
- package/packages/gen-java/src/names.js +197 -0
- package/packages/gen-python/src/emit-client.js +272 -0
- package/packages/gen-python/src/emit-models.js +139 -0
- package/packages/gen-python/src/emit-testing.js +93 -0
- package/packages/gen-python/src/emit-webhooks.js +48 -0
- package/packages/gen-python/src/generate.js +237 -0
- package/packages/gen-python/src/index.js +2 -0
- package/packages/gen-python/src/names.js +161 -0
- package/packages/gen-python/src/style.js +67 -0
- package/packages/gen-rust/src/emit-client.js +379 -0
- package/packages/gen-rust/src/emit-harness.js +278 -0
- package/packages/gen-rust/src/emit-mock.js +121 -0
- package/packages/gen-rust/src/emit-models.js +117 -0
- package/packages/gen-rust/src/emit-webhooks.js +58 -0
- package/packages/gen-rust/src/generate.js +243 -0
- package/packages/gen-rust/src/index.js +5 -0
- package/packages/gen-rust/src/names.js +223 -0
- package/packages/gen-typescript/src/emit-client.js +434 -0
- package/packages/gen-typescript/src/emit-models.js +93 -0
- package/packages/gen-typescript/src/emit-package.js +90 -0
- package/packages/gen-typescript/src/emit-testing.js +138 -0
- package/packages/gen-typescript/src/emit-webhooks.js +52 -0
- package/packages/gen-typescript/src/generate.js +70 -0
- package/packages/gen-typescript/src/index.js +3 -0
- package/packages/gen-typescript/src/naming.js +64 -0
- package/packages/gen-typescript/src/runtime.js +81 -0
- package/packages/gen-typescript/src/types.js +136 -0
- package/packages/ir/src/api-surface.js +22 -0
- package/packages/ir/src/checks.js +52 -0
- package/packages/ir/src/index.js +32 -0
- package/packages/ir/src/ir.js +214 -0
- package/packages/ir/src/pages.js +18 -0
- package/packages/ir/src/validate-ir.js +462 -0
- package/packages/ir/src/validate-types.js +140 -0
- package/packages/kit/src/block-parser.js +196 -0
- package/packages/kit/src/index.js +15 -0
- package/packages/kit/src/kit-check.js +203 -0
- package/packages/kit/src/sources.js +53 -0
- package/packages/kit/src/version-policy.js +133 -0
- package/packages/kit/src/version-range.js +132 -0
- package/packages/openapi/src/import.js +292 -0
- package/packages/openapi/src/index.js +1 -0
- package/packages/openapi/src/names.js +50 -0
- package/packages/openapi/src/schemas.js +264 -0
- package/packages/packager/src/archive.js +106 -0
- package/packages/packager/src/crate.js +39 -0
- package/packages/packager/src/gomodule.js +11 -0
- package/packages/packager/src/index.js +9 -0
- package/packages/packager/src/maven.js +85 -0
- package/packages/packager/src/metadata.js +34 -0
- package/packages/packager/src/npm.js +71 -0
- package/packages/packager/src/nuget.js +78 -0
- package/packages/packager/src/sbom.js +56 -0
- package/packages/packager/src/wheel.js +62 -0
- package/packages/spec/src/index.js +11 -0
- package/packages/spec/src/requirements.js +139 -0
- package/packages/transpile/src/ast.js +51 -0
- package/packages/transpile/src/check.js +669 -0
- package/packages/transpile/src/emit-csharp.js +469 -0
- package/packages/transpile/src/emit-go.js +529 -0
- package/packages/transpile/src/emit-java.js +360 -0
- package/packages/transpile/src/emit-python.js +320 -0
- package/packages/transpile/src/emit-rust.js +499 -0
- package/packages/transpile/src/errors.js +15 -0
- package/packages/transpile/src/index.js +13 -0
- package/packages/transpile/src/lexer.js +161 -0
- package/packages/transpile/src/parser.js +543 -0
- package/packages/transpile/src/tests/ast.js +46 -0
- package/packages/transpile/src/tests/parse.js +376 -0
- package/packages/transpile/src/tests/translate.js +849 -0
- package/packages/transpile/src/transpile.js +57 -0
|
@@ -0,0 +1,237 @@
|
|
|
1
|
+
// Generates the Python SDK as an in-memory file tree.
|
|
2
|
+
|
|
3
|
+
import { emitWebhooks, webhookFunction } from "./emit-webhooks.ts";
|
|
4
|
+
import { createFileTree, sha256, } from "@sdkloom/codegen";
|
|
5
|
+
|
|
6
|
+
import { parseBlocks, } from "@sdkloom/kit";
|
|
7
|
+
import { PYTHON_HELPERS, pythonName, transpileLogic, transpileMiddleware, TranspileError } from "@sdkloom/transpile";
|
|
8
|
+
import { clientName, emitClient } from "./emit-client.ts";
|
|
9
|
+
import { emitErrors, emitModels, errorClass, python } from "./emit-models.ts";
|
|
10
|
+
import { emitTesting } from "./emit-testing.ts";
|
|
11
|
+
import { comment, snake, str } from "./names.ts";
|
|
12
|
+
import { preamble, PY3, } from "./style.ts";
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
/** ">=2.7 <3" → ">=2.7, <3", the form requires-python and Requires-Python take (PEP 440). */
|
|
25
|
+
export function pep440(range ) {
|
|
26
|
+
return range.trim().split(/\s+/).join(", ");
|
|
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
|
+
const TAG_LINE = /^\s*#\s*@([a-z][a-z-]*)\b/;
|
|
54
|
+
|
|
55
|
+
function blockDoc(block , kit ) {
|
|
56
|
+
return [
|
|
57
|
+
block.summary ?? "",
|
|
58
|
+
"",
|
|
59
|
+
`Supported versions: Python ${block.versions ?? kit.supports}`,
|
|
60
|
+
"Sources:",
|
|
61
|
+
...block.sources.flatMap((source) => [` [${source.index}] ${source.url}`, ...block.quotes.filter((quote) => quote.index === source.index).map((quote) => ` "${quote.text}"`)]),
|
|
62
|
+
` Retrieved: ${block.retrieved ?? "unknown"}`,
|
|
63
|
+
`Provenance: block ${block.id} · kit ${kit.name} ${kit.version} · sha256:${sha256(block.code).slice(0, 12)}`,
|
|
64
|
+
];
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
function vendor(file , kit , header ) {
|
|
68
|
+
const parsed = parseBlocks(file.text, { commentPrefix: "#", file: file.path });
|
|
69
|
+
if (parsed.errors.length > 0) throw new Error(`kit file ${file.path} is invalid: ${parsed.errors.map((error) => error.message).join("; ")}`);
|
|
70
|
+
const writer = python();
|
|
71
|
+
header.forEach((line) => writer.line(line));
|
|
72
|
+
let index = 0;
|
|
73
|
+
let inMetadata = false;
|
|
74
|
+
for (const line of file.text.split("\n")) {
|
|
75
|
+
const tag = TAG_LINE.exec(line)?.[1];
|
|
76
|
+
if (tag === "block") {
|
|
77
|
+
const block = parsed.blocks[index];
|
|
78
|
+
index += 1;
|
|
79
|
+
inMetadata = true;
|
|
80
|
+
if (block !== undefined) comment(writer, blockDoc(block, kit));
|
|
81
|
+
continue;
|
|
82
|
+
}
|
|
83
|
+
if (tag === "end" || (inMetadata && tag !== undefined)) continue;
|
|
84
|
+
inMetadata = false;
|
|
85
|
+
writer.line(line);
|
|
86
|
+
}
|
|
87
|
+
return { text: writer.toString(), blocks: parsed.blocks };
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
const CUSTOM_FILE = "custom/python/middleware.py";
|
|
91
|
+
|
|
92
|
+
function transpile(ir , name , sources ) {
|
|
93
|
+
const middleware = ir.middlewares.find((candidate) => candidate.name === name);
|
|
94
|
+
const source = sources.get(name);
|
|
95
|
+
if (middleware === undefined || source === undefined) throw new Error(`middleware ${name} has no source to transpile`);
|
|
96
|
+
try {
|
|
97
|
+
return transpileMiddleware({ name, source, config: middleware.config }, "python", { configKey: snake, functions: ir.logic ?? [], logicName: LOGIC_NAME });
|
|
98
|
+
} catch (error ) {
|
|
99
|
+
if (!(error instanceof TranspileError)) throw error;
|
|
100
|
+
throw new Error(`${error.message}\nTo keep this middleware as written, write def ${pythonName(name)}(request, call_next, ctx) in ${CUSTOM_FILE}; it replaces the transpiled one.`);
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
/** Transpiled middleware first, then the author's native functions, which replace transpiled ones of the same name. */
|
|
105
|
+
function emitMiddleware(ir , sources , custom , header , style ) {
|
|
106
|
+
const native = (name ) => custom !== undefined && new RegExp(`^def ${pythonName(name)}\\(`, "m").test(custom);
|
|
107
|
+
const writer = preamble(python().line(header), style, false)
|
|
108
|
+
.line("# Middleware transpiled from the TypeScript definition (spec/subset.md).")
|
|
109
|
+
.line(`# Functions in ${CUSTOM_FILE} replace transpiled ones of the same name.`)
|
|
110
|
+
.line("import math # noqa: F401")
|
|
111
|
+
.line("import random # noqa: F401")
|
|
112
|
+
.line()
|
|
113
|
+
.line(`from ._runtime.lang import ${[...PYTHON_HELPERS, "js_message"].sort().join(", ")} # noqa: F401`)
|
|
114
|
+
.line("from ._runtime.compat import json_loads # noqa: F401");
|
|
115
|
+
// Middleware may call the project's logic functions.
|
|
116
|
+
if ((ir.logic ?? []).length > 0) writer.line("from . import logic as _logic # noqa: F401");
|
|
117
|
+
for (const middleware of ir.middlewares.filter((candidate) => !native(candidate.name))) {
|
|
118
|
+
writer.line().line();
|
|
119
|
+
transpile(ir, middleware.name, sources).trimEnd().split("\n").forEach((line) => writer.line(line));
|
|
120
|
+
}
|
|
121
|
+
if (custom === undefined) return writer.toString();
|
|
122
|
+
return `${writer.line().line().line(`# Copied from ${CUSTOM_FILE}.`).toString()}${custom}`;
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
/** Calls of logic functions are qualified, so no parameter or local of the caller can hide them. */
|
|
126
|
+
const LOGIC_NAME = (name ) => `_logic.${pythonName(name)}`;
|
|
127
|
+
|
|
128
|
+
/** logic.py: the definition's logic functions, transpiled; parameters are the inputs in snake_case. */
|
|
129
|
+
function emitLogic(ir , sources , header , style ) {
|
|
130
|
+
const writer = preamble(python().line(header), style, false)
|
|
131
|
+
.line("# Logic functions transpiled from the TypeScript definition (spec/subset.md).")
|
|
132
|
+
.line("import math # noqa: F401")
|
|
133
|
+
.line("import random # noqa: F401")
|
|
134
|
+
.line("import sys")
|
|
135
|
+
.line()
|
|
136
|
+
.line(`from ._runtime.lang import ${[...PYTHON_HELPERS, "js_message"].sort().join(", ")} # noqa: F401`)
|
|
137
|
+
.line("from ._runtime.compat import json_loads # noqa: F401")
|
|
138
|
+
.line()
|
|
139
|
+
.line("_logic = sys.modules[__name__]");
|
|
140
|
+
for (const logic of ir.logic ?? []) {
|
|
141
|
+
const source = sources.get(logic.name);
|
|
142
|
+
if (source === undefined) throw new Error(`logic function ${logic.name} has no source to transpile`);
|
|
143
|
+
const [signature, ...body] = transpileLogic(logic, source, "python", { functions: ir.logic ?? [], logicName: LOGIC_NAME }).trimEnd().split("\n");
|
|
144
|
+
writer.line().line().line(signature ?? "");
|
|
145
|
+
if (logic.doc !== undefined) writer.line(` """${logic.doc.replace(/\\/g, "\\\\").replace(/"/g, '\\"')}"""`);
|
|
146
|
+
body.forEach((line) => writer.line(line));
|
|
147
|
+
}
|
|
148
|
+
return writer.toString();
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
function emitInit(ir , header ) {
|
|
152
|
+
const errors = [...new Set(ir.operations.flatMap((operation) => operation.errors.map((error) => errorClass(error.name))))].sort();
|
|
153
|
+
const models = ir.types.flatMap((named) => (named.type.kind === "union" ? [`Unknown${named.name}`, named.name] : [named.name]));
|
|
154
|
+
const hooks = (ir.webhooks ?? []).map(webhookFunction);
|
|
155
|
+
const logic = (ir.logic ?? []).map((entry) => pythonName(entry.name));
|
|
156
|
+
const names = [clientName(ir), "SERVERS", ...logic, ...errors, ...models, "ApiError", "TransportError", "DecodeError", "UNSET", "CallOptions", "MiddlewareEntry", "RawResult", "entry", "HttpRequest", "HttpResponse", "ServerSentEvent", "WaitTimeoutError", "MultipartForm", ...(hooks.length === 0 ? [] : [...hooks, "WebhookSignatureError"])];
|
|
157
|
+
const writer = python().line(header);
|
|
158
|
+
writer.line(`from .client import ${clientName(ir)}, SERVERS`);
|
|
159
|
+
if (errors.length > 0) writer.line(`from .errors import ${errors.join(", ")}`);
|
|
160
|
+
if (models.length > 0) writer.line(`from .models import ${models.join(", ")}`);
|
|
161
|
+
writer.line("from ._runtime.errors import ApiError, TransportError");
|
|
162
|
+
writer.line("from ._runtime.models import UNSET, DecodeError");
|
|
163
|
+
writer.line("from ._runtime.client import CallOptions, MiddlewareEntry, RawResult, entry");
|
|
164
|
+
writer.line("from ._runtime.http import HttpRequest, HttpResponse");
|
|
165
|
+
writer.line("from ._runtime.sse import ServerSentEvent");
|
|
166
|
+
writer.line("from ._runtime.wait import WaitTimeoutError");
|
|
167
|
+
writer.line("from ._runtime.multipart import MultipartForm");
|
|
168
|
+
if (logic.length > 0) writer.line(`from .logic import ${logic.join(", ")}`);
|
|
169
|
+
if (hooks.length > 0) writer.line(`from .webhooks import ${[...hooks, "WebhookSignatureError"].join(", ")}`);
|
|
170
|
+
writer.line();
|
|
171
|
+
writer.line(`__all__ = [${names.map(str).join(", ")}]`);
|
|
172
|
+
return writer.toString();
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
function emitPyproject(input ) {
|
|
176
|
+
return python()
|
|
177
|
+
.line("[project]")
|
|
178
|
+
.line(`name = ${str(input.packageName)}`)
|
|
179
|
+
.line(`version = ${str(input.version)}`)
|
|
180
|
+
.line(`description = ${str(input.ir.doc ?? input.ir.name)}`)
|
|
181
|
+
.line('readme = "README.md"')
|
|
182
|
+
.line(`requires-python = ${str(pep440(input.kit.supports))}`)
|
|
183
|
+
.line("dependencies = []")
|
|
184
|
+
.toString();
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
function emitReadme(input ) {
|
|
188
|
+
const client = clientName(input.ir);
|
|
189
|
+
return python()
|
|
190
|
+
.line(`# ${input.packageName}`)
|
|
191
|
+
.line()
|
|
192
|
+
.line(input.ir.doc ?? "")
|
|
193
|
+
.line()
|
|
194
|
+
.line(`> Generated by sdkloom ${input.sdkloomVersion} from \`${input.source}\`. Do not edit by hand: run \`sdkloom build\`.`)
|
|
195
|
+
.line()
|
|
196
|
+
.line("```python")
|
|
197
|
+
.line(`from ${input.module} import ${client}`)
|
|
198
|
+
.line()
|
|
199
|
+
.line(`client = ${client}(...)`)
|
|
200
|
+
.line("```")
|
|
201
|
+
.line()
|
|
202
|
+
.line(`Standard library only. Python ${input.kit.supports}. Test helpers: \`from ${input.module}.testing import mock_client, examples\`.`)
|
|
203
|
+
.toString();
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
export function generatePython(input ) {
|
|
207
|
+
const { ir } = input;
|
|
208
|
+
const header = `# Generated by sdkloom ${input.sdkloomVersion} from ${input.source}. Do not edit: run \`sdkloom build\` to regenerate.`;
|
|
209
|
+
const files = [];
|
|
210
|
+
const trace = new Map ();
|
|
211
|
+
const add = (path , text , from ) => {
|
|
212
|
+
files.push([`${path.startsWith("/") ? path.slice(1) : path}`, text]);
|
|
213
|
+
trace.set(path, from);
|
|
214
|
+
};
|
|
215
|
+
const operations = ir.operations.map((operation) => `ir:operations.${operation.name}`);
|
|
216
|
+
const types = ir.types.map((named) => `ir:types.${named.name}`);
|
|
217
|
+
|
|
218
|
+
const style = input.kit.syntax === "py27" ? { py27: true } : PY3;
|
|
219
|
+
const blocks = [];
|
|
220
|
+
for (const file of [...input.kit.files].sort((a, b) => (a.path < b.path ? -1 : 1))) {
|
|
221
|
+
const vendored = vendor(file, input.kit, style.py27 ? [header, "# -*- coding: utf-8 -*-"] : [header]);
|
|
222
|
+
add(`${input.module}/_runtime/${file.path}`, vendored.text, vendored.blocks.map((block) => `block:${block.id}`));
|
|
223
|
+
vendored.blocks.forEach((block) => blocks.push({ id: block.id, versions: block.versions ?? "*", code: block.code }));
|
|
224
|
+
}
|
|
225
|
+
add(`${input.module}/models.py`, emitModels(ir, header, style), types);
|
|
226
|
+
add(`${input.module}/errors.py`, emitErrors(ir, header, style), operations);
|
|
227
|
+
add(`${input.module}/middleware.py`, emitMiddleware(ir, input.middlewareSources, input.middleware, header, style), ir.middlewares.map((entry) => `middleware:${entry.name}`));
|
|
228
|
+
add(`${input.module}/client.py`, emitClient(ir, header, { name: input.packageName, version: input.version }, style), ["ir:servers", ...operations]);
|
|
229
|
+
add(`${input.module}/testing.py`, emitTesting(ir, header, style), [...types, ...operations]);
|
|
230
|
+
if ((ir.logic ?? []).length > 0) add(`${input.module}/logic.py`, emitLogic(ir, input.logicSources ?? new Map(), header, style), (ir.logic ?? []).map((entry) => `ir:logic.${entry.name}`));
|
|
231
|
+
if ((ir.webhooks ?? []).length > 0) add(`${input.module}/webhooks.py`, emitWebhooks(ir, header, style), (ir.webhooks ?? []).map((hook) => `ir:webhooks.${hook.name}`));
|
|
232
|
+
add(`${input.module}/__init__.py`, emitInit(ir, header), ["ir"]);
|
|
233
|
+
add(`${input.module}/py.typed`, "", ["config"]);
|
|
234
|
+
add("pyproject.toml", emitPyproject(input), ["config"]);
|
|
235
|
+
add("README.md", emitReadme(input), ["config", "ir"]);
|
|
236
|
+
return { files: createFileTree(files), trace, blocks };
|
|
237
|
+
}
|
|
@@ -0,0 +1,161 @@
|
|
|
1
|
+
// Python names, literals, comments and the IR → Python type mapping.
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
const KEYWORDS = new Set([
|
|
7
|
+
"False", "None", "True", "and", "as", "assert", "async", "await", "break", "class", "continue", "def", "del", "elif", "else", "except",
|
|
8
|
+
"finally", "for", "from", "global", "if", "import", "in", "is", "lambda", "nonlocal", "not", "or", "pass", "raise", "return", "try",
|
|
9
|
+
"while", "with", "yield",
|
|
10
|
+
]);
|
|
11
|
+
|
|
12
|
+
/** getOrder → get_order, x-request-id → x_request_id, class → class_ */
|
|
13
|
+
export function snake(name ) {
|
|
14
|
+
const base = name
|
|
15
|
+
.replace(/([a-z0-9])([A-Z])/g, "$1_$2")
|
|
16
|
+
.replace(/[^A-Za-z0-9]+/g, "_")
|
|
17
|
+
.replace(/^_+|_+$/g, "")
|
|
18
|
+
.toLowerCase();
|
|
19
|
+
const safe = /^[0-9]/.test(base) ? `_${base}` : base;
|
|
20
|
+
return KEYWORDS.has(safe) ? `${safe}_` : safe;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
/** A Python string literal (JSON string syntax is valid Python). */
|
|
24
|
+
export function str(text ) {
|
|
25
|
+
return JSON.stringify(text);
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
export function comment(writer , lines ) {
|
|
29
|
+
lines.forEach((line) => writer.line(line === "" ? "#" : `# ${line}`));
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
export function docstring(writer , lines ) {
|
|
33
|
+
const text = lines.filter((line, index, all) => line !== "" || index < all.length - 1).map((line) => line.replaceAll("\\", "\\\\").replaceAll('"', '\\"'));
|
|
34
|
+
if (text.length === 0) return;
|
|
35
|
+
if (text.length === 1) {
|
|
36
|
+
writer.line(`"""${text[0]}"""`);
|
|
37
|
+
return;
|
|
38
|
+
}
|
|
39
|
+
writer.line(`"""${text[0]}`);
|
|
40
|
+
text.slice(1).forEach((line) => writer.line(line));
|
|
41
|
+
writer.line('"""');
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
const PY_TYPE = {
|
|
45
|
+
string: "str",
|
|
46
|
+
secret: "str",
|
|
47
|
+
datetime: "str",
|
|
48
|
+
decimal: "Decimal",
|
|
49
|
+
int: "int",
|
|
50
|
+
number: "float",
|
|
51
|
+
bool: "bool",
|
|
52
|
+
bytes: "bytes",
|
|
53
|
+
stream: "Iterable[bytes]",
|
|
54
|
+
multipart: "MultipartForm",
|
|
55
|
+
};
|
|
56
|
+
|
|
57
|
+
const DECODER = {
|
|
58
|
+
string: "as_string",
|
|
59
|
+
secret: "as_string",
|
|
60
|
+
datetime: "as_string",
|
|
61
|
+
decimal: "as_decimal",
|
|
62
|
+
int: "as_integer",
|
|
63
|
+
number: "as_number",
|
|
64
|
+
bool: "as_boolean",
|
|
65
|
+
};
|
|
66
|
+
|
|
67
|
+
const EXAMPLE = {
|
|
68
|
+
string: str("string"),
|
|
69
|
+
secret: str("secret"),
|
|
70
|
+
datetime: str("2026-01-01T00:00:00Z"),
|
|
71
|
+
decimal: 'Decimal("1.00")',
|
|
72
|
+
int: "1",
|
|
73
|
+
number: "1.5",
|
|
74
|
+
bool: "True",
|
|
75
|
+
};
|
|
76
|
+
|
|
77
|
+
function only(name , path ) {
|
|
78
|
+
throw new Error(`${path}: ${name} values are only supported as an operation body or result`);
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
function inline(path ) {
|
|
82
|
+
throw new Error(`${path}: inline object and union types are not supported; use model() or union()`);
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
export function namedKind(ir , name ) {
|
|
86
|
+
const found = ir.types.find((named) => named.name === name);
|
|
87
|
+
if (found === undefined) throw new Error(`unknown type ${name}`);
|
|
88
|
+
return found.type.kind;
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
export function pyType(node ) {
|
|
92
|
+
switch (node.kind) {
|
|
93
|
+
case "primitive":
|
|
94
|
+
return PY_TYPE[node.name];
|
|
95
|
+
case "enum":
|
|
96
|
+
return "str";
|
|
97
|
+
case "list":
|
|
98
|
+
return `List[${pyType(node.item)}]`;
|
|
99
|
+
case "map":
|
|
100
|
+
return `Dict[str, ${pyType(node.value)}]`;
|
|
101
|
+
case "ref":
|
|
102
|
+
return node.name;
|
|
103
|
+
case "object":
|
|
104
|
+
case "union":
|
|
105
|
+
return inline("type");
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
export function decoder(ir , node , path ) {
|
|
110
|
+
switch (node.kind) {
|
|
111
|
+
case "primitive":
|
|
112
|
+
return DECODER[node.name] ?? only(node.name, path);
|
|
113
|
+
case "enum":
|
|
114
|
+
return `lambda value, path: as_enum(value, path, (${node.values.map(str).join(", ")},))`;
|
|
115
|
+
case "list":
|
|
116
|
+
return `lambda value, path: as_list(value, path, ${decoder(ir, node.item, path)})`;
|
|
117
|
+
case "map":
|
|
118
|
+
return `lambda value, path: as_map(value, path, ${decoder(ir, node.value, path)})`;
|
|
119
|
+
case "ref":
|
|
120
|
+
return namedKind(ir, node.name) === "union" ? `decode_${snake(node.name)}` : `${node.name}.from_json`;
|
|
121
|
+
case "object":
|
|
122
|
+
case "union":
|
|
123
|
+
return inline(path);
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
/** Expression that turns a field value into JSON-ready data; only unions need help (they add their tag). */
|
|
128
|
+
export function encoder(ir , node , expr ) {
|
|
129
|
+
switch (node.kind) {
|
|
130
|
+
case "list": {
|
|
131
|
+
const item = encoder(ir, node.item, "item");
|
|
132
|
+
return item === "item" ? expr : `[None if item is None else ${item} for item in ${expr}]`;
|
|
133
|
+
}
|
|
134
|
+
case "map": {
|
|
135
|
+
const item = encoder(ir, node.value, "item");
|
|
136
|
+
return item === "item" ? expr : `{key: None if item is None else ${item} for key, item in ${expr}.items()}`;
|
|
137
|
+
}
|
|
138
|
+
case "ref":
|
|
139
|
+
return namedKind(ir, node.name) === "union" ? `encode_${snake(node.name)}(${expr})` : expr;
|
|
140
|
+
default:
|
|
141
|
+
return expr;
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
export function example(node , path ) {
|
|
146
|
+
switch (node.kind) {
|
|
147
|
+
case "primitive":
|
|
148
|
+
return EXAMPLE[node.name] ?? only(node.name, path);
|
|
149
|
+
case "enum":
|
|
150
|
+
return str(node.values[0] ?? "");
|
|
151
|
+
case "list":
|
|
152
|
+
return "[]";
|
|
153
|
+
case "map":
|
|
154
|
+
return "{}";
|
|
155
|
+
case "ref":
|
|
156
|
+
return `self.${node.name}()`;
|
|
157
|
+
case "object":
|
|
158
|
+
case "union":
|
|
159
|
+
return inline(path);
|
|
160
|
+
}
|
|
161
|
+
}
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
// How generated Python is written for the kit line being built: annotated Python 3 (the default package), or plain code
|
|
2
|
+
// that also runs on Python 2.7 (the py27 variant, strategy "syntax": "py27").
|
|
3
|
+
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
export const PY3 = { py27: false };
|
|
11
|
+
|
|
12
|
+
/** A parameter: [name, annotation, default]. "*" starts keyword-only parameters; "**name" collects the rest. */
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
function plain(param ) {
|
|
16
|
+
const [name, , value] = param;
|
|
17
|
+
return value === undefined ? name : `${name}=${value}`;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
function annotated(param ) {
|
|
21
|
+
const [name, type, value] = param;
|
|
22
|
+
const typed = type === undefined ? name : `${name}: ${type}`;
|
|
23
|
+
return value === undefined ? typed : `${typed} = ${value}`;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* The `def` line. Python 2.7 has no keyword-only parameters and no annotations: the "*" marker is dropped, and
|
|
28
|
+
* parameters without a default move ahead of those with one so the order stays valid (callers pass them by name).
|
|
29
|
+
*/
|
|
30
|
+
export function def(style , name , params , returns ) {
|
|
31
|
+
if (!style.py27) {
|
|
32
|
+
const list = params.map((param) => (param === "*" ? "*" : annotated(param)));
|
|
33
|
+
return `def ${name}(${list.join(", ")})${returns === undefined ? "" : ` -> ${returns}`}:`;
|
|
34
|
+
}
|
|
35
|
+
const real = params.filter((param) => param !== "*");
|
|
36
|
+
const rest = real.filter((param) => param[0].startsWith("**"));
|
|
37
|
+
const named = real.filter((param) => !param[0].startsWith("**"));
|
|
38
|
+
const required = named.filter((param) => param[2] === undefined);
|
|
39
|
+
const optional = named.filter((param) => param[2] !== undefined);
|
|
40
|
+
return `def ${name}(${[...required, ...optional, ...rest].map(plain).join(", ")}):`;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
/** `name: Type = value` on Python 3, `name = value` on 2.7. */
|
|
44
|
+
export function assign(style , name , type , value ) {
|
|
45
|
+
return style.py27 ? `${name} = ${value}` : `${name}: ${type} = ${value}`;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
/** The lines after the "Generated by" header: an encoding declaration and __future__ imports. */
|
|
49
|
+
export function preamble(writer , style , futureAnnotations = true) {
|
|
50
|
+
if (style.py27) return writer.line("# -*- coding: utf-8 -*-").line("from __future__ import absolute_import, division, unicode_literals").line();
|
|
51
|
+
return futureAnnotations ? writer.line("from __future__ import annotations").line() : writer;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
/** `from typing import …`; on 2.7, which has no typing module, only when it happens to be installed. */
|
|
55
|
+
export function typingImport(writer , style , names ) {
|
|
56
|
+
if (!style.py27) return writer.line(`from typing import ${names} # noqa: F401`);
|
|
57
|
+
return writer
|
|
58
|
+
.line("try: # annotations only; Python 2.7 has no typing module")
|
|
59
|
+
.indent((body) => body.line(`from typing import ${names} # noqa: F401`))
|
|
60
|
+
.line("except ImportError: # pragma: no cover")
|
|
61
|
+
.indent((body) => body.line("pass"));
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
/** A class statement: new-style on 2.7. */
|
|
65
|
+
export function classLine(style , name , base ) {
|
|
66
|
+
return `class ${name}${base !== undefined ? `(${base})` : style.py27 ? "(object)" : ""}:`;
|
|
67
|
+
}
|