You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The **cdd-python-client** tool acts as a dedicated compiler and transpiler. Its fundamental architecture follows standard compiler design principles, divided into three distinct phases: **Frontend (Parsing)**, **Intermediate Representation (IR)**, and **Backend (Emitting)**.
11
10
12
-
This decoupled design ensures that any format capable of being parsed into the IR can subsequently be emitted into any supported output format, whether that is a server-side route, a client-side SDK, a database ORM, or an OpenAPI specification. As a `Client-only` focussed project, its emitters are heavily optimized for generating Python API SDKs and Client Wrappers using LibCST and Pydantic.
11
+
This decoupled design ensures that any format capable of being parsed into the IR can subsequently be emitted into any supported output format, whether that is a server-side route, a client-side SDK, a database ORM, or an OpenAPI specification.
The Frontend's responsibility is to read an input source and translate it into the universal CDD Intermediate Representation (IR).
58
59
59
-
***Static Analysis (AST-Driven)**: For `Python` source code, the tool **does not** use dynamic reflection or execute the code. Instead, it reads the source files using LibCST, generates an Abstract Syntax Tree (AST), and navigates the tree to extract classes, structs, functions, type signatures, API client definitions, server routes, and docstrings.
60
+
***Static Analysis (AST-Driven)**: For `Python` source code, the tool **does not** use dynamic reflection or execute the code. Instead, it reads the source files, generates an Abstract Syntax Tree (AST) via `libcst`, and navigates the tree to extract classes, structs, functions, type signatures, API client definitions, server routes, and docstrings.
60
61
***OpenAPI Parsing**: For OpenAPI and JSON Schema inputs, the parser normalizes the structure, resolving internal `$ref`s and extracting properties, endpoints (client or server perspectives), and metadata into the IR.
61
62
62
63
### 2. Intermediate Representation (IR)
@@ -72,9 +73,10 @@ By standardizing on a single IR (heavily inspired by OpenAPI / JSON Schema primi
72
73
73
74
The Backend's responsibility is to take the universal IR and generate valid target output. Emitters can be written to support various environments (e.g., Client vs Server, Web vs CLI).
74
75
75
-
***Code Generation**: Emitters iterate over the IR and generate idiomatic `Python` source code.
76
-
* A **Server Emitter** creates routing controllers and request-validation logic (such as mocks).
77
-
* A **Client Emitter** creates API wrappers, fetch functions, and response-parsing logic tailored for Python clients.
76
+
***Code Generation**: Emitters iterate over the IR and generate idiomatic `Python` source code.
77
+
* A **Server Emitter** creates routing controllers and request-validation logic (like mock servers using FastAPI).
78
+
* A **Client Emitter** creates API wrappers, fetch functions, and response-parsing logic using `urllib3` or `requests`.
79
+
***Database & CLI Generation**: Emitters can also target ORM models or command-line parsers by mapping IR properties to database columns or CLI arguments.
78
80
***Specification Generation**: Emitters translating back to OpenAPI serialize the IR into standard OpenAPI 3.x JSON or YAML, rigorously formatting descriptions, type constraints, and endpoint schemas based on what was parsed from the source code.
79
81
80
82
## 🔄 Extensibility
@@ -88,4 +90,4 @@ Because of the IR-centric design, adding support for a new `Python` framework (e
88
90
1.**A Single Source of Truth**: Developers should be able to maintain their definitions in whichever format is most ergonomic for their team (OpenAPI files, Native Code, Client libraries, ORM models) and generate the rest.
89
91
2.**Zero-Execution Parsing**: Ensure security and resilience by strictly statically analyzing inputs. The compiler must never need to run the target code to understand its structure.
90
92
3.**Lossless Conversion**: Maximize the retention of metadata (e.g., type annotations, docstrings, default values, validators) during the transition `Source -> IR -> Target`.
91
-
4.**Symmetric Operations**: An Endpoint in the IR holds all the information necessary to generate both the Server-side controller that fulfills it, and the Client-side SDK method that calls it.
93
+
4.**Symmetric Operations**: An Endpoint in the IR holds all the information necessary to generate both the Server-side controller that fulfills it, and the Client-side SDK method that calls it.
The goal of this project is to enable rapid application development without tradeoffs. Tradeoffs of Protocol Buffers / Thrift etc. are an untouchable "generated" directory and package, compile-time and/or runtime overhead. Tradeoffs of Java or JavaScript for everything are: overhead in hardware access, offline mode, ML inefficiency, and more. And neither of these alterantive approaches are truly integrated into your target system, test frameworks, and bigger abstractions you build in your app. Tradeoffs in CDD are code duplication (but CDD handles the synchronisation for you).
21
21
@@ -31,29 +31,29 @@ The `cdd-python-client` compiler leverages a unified architecture to support var
31
31
32
32
## 📦 Installation
33
33
34
-
Requires Python 3.9+.
34
+
Requires Python 3.9+. Install directly from the repository using `uv` or `pip`:
The `cdd-python-client` leverages LibCST for parsing and generating Python Abstract Syntax Trees (AST). LibCST is chosen over Python's built-in `ast` module because it preserves whitespace, comments, and structure, which is crucial for a bidirectional compiler that doesn't mess up your code formatting when syncing changes. Pydantic is used for models generation to provide idiomatic and robust validation for the client interfaces.
75
+
The project leverages `libcst` to guarantee that code parsing and emission are completely whitespace and comment sensitive. By utilizing a lossless Abstract Syntax Tree (AST), `cdd-python` allows for symmetric conversion back and forth between OpenAPI specifications and rich Python source code without clobbering manual developer interventions like inline comments or non-API-related logic.
73
76
74
77
## 🏗 Supported Conversions for Python
75
78
76
79
*(The boxes below reflect the features supported by this specific `cdd-python-client` implementation)*
77
80
78
81
| Concept | Parse (From) | Emit (To) |
79
82
|---------|--------------|-----------|
80
-
| OpenAPI (JSON/YAML) |[✅]|[✅]|
81
-
|`Python` Models / Structs / Types |[✅]|[✅]|
82
-
|`Python` Server Routes / Endpoints |[✅]|[✅]|
83
-
|`Python` API Clients / SDKs |[✅]|[✅]|
83
+
| OpenAPI (JSON/YAML) |✅|✅|
84
+
|`Python` Models / Structs / Types |✅|✅|
85
+
|`Python` Server Routes / Endpoints |✅|✅|
86
+
|`Python` API Clients / SDKs |✅|✅|
84
87
|`Python` ORM / DB Schemas |[]|[]|
85
88
|`Python` CLI Argument Parsers |[]|[]|
86
-
|`Python` Docstrings / Comments |[✅]|[✅]|
89
+
|`Python` Docstrings / Comments |✅|✅|
87
90
88
91
---
89
92
@@ -100,4 +103,4 @@ at your option.
100
103
101
104
Unless you explicitly state otherwise, any contribution intentionally submitted
102
105
for inclusion in the work by you, as defined in the Apache-2.0 license, shall be
103
-
dual licensed as above, without any additional terms or conditions.
106
+
dual licensed as above, without any additional terms or conditions.
0 commit comments