A bidirectional Python code generator that converts between AsyncAPI 3.0 specifications and Python code (pure Python or FastAPI implementations).
Project description
Zen Generator 🚀
A bidirectional Python code generator that converts between AsyncAPI 3.0 specifications and Python code (pure Python or FastAPI implementations).
Features ✨
- 🔄 Bidirectional conversion between AsyncAPI 3.0 and Python code
- 🐍 Generate Python code from AsyncAPI 3.0 specifications:
- 🐍 Pure Python implementations with type hints
- ⚡ FastAPI endpoints with Pydantic models
- 📄 Generate AsyncAPI 3.0 specifications from Python code
- 🧠 Automatic type inference and mapping
- ⚡ Support for both async and sync functions
Installation 📦
with uv:
uv tool install zen-generator
with pipx:
pipx install zen-generator
with uvx:
uvx zen-generator
[!IMPORTANT] Currently, only model and function definitions in the
componentsblock of the AsyncAPI file are supported. Inline definitions are not supported.
[!NOTE] This code snippet includes a custom definition for declaring required parameters in model/function definitions. Specifically, the
requiredkeyword is used to specify mandatory fields, as shown below:
required:
- user_id
This ensures that the
user_idparameter is always provided when the model or function is utilized.
Quick Start 🏃
Convert between AsyncAPI 3.0 specifications and Python code:
# Generate FastAPI implementation from AsyncAPI spec
uvx zen-generator fastapi
# Generate pure Python implementation from AsyncAPI spec
uvx zen-generator pure-python
# Generate AsyncAPI spec from Python code
uvx zen-generator asyncapi-documentation
Command Line Interface
The CLI is built with Typer and provides three main commands:
Usage:
$ [OPTIONS] COMMAND [ARGS]...
Options:
--install-completion: Install completion for the current shell.--show-completion: Show completion for the current shell, to copy it or customize the installation.--help: Show this message and exit.
Commands:
asyncapi-documentationpure-pythonfastapi
asyncapi-documentation
Usage:
$ asyncapi-documentation [OPTIONS]
Options:
--models-file PATH: [default: models.py]--functions-file PATH: [default: functions.py]--output-file PATH: [default: asyncapi.yaml]--application-name TEXT: [default: Zen]--help: Show this message and exit.
pure-python
Usage:
$ pure-python [OPTIONS]
Options:
--asyncapi-file PATH: [default: asyncapi.yaml]--models-file PATH: [default: models.py]--functions-file PATH: [default: functions.py]--application-name TEXT: [default: Zen]--is-async / --no-is-async: [default: no-is-async]--help: Show this message and exit.
fastapi
Usage:
$ fastapi [OPTIONS]
Options:
--asyncapi-file PATH: [default: asyncapi.yaml]--models-file PATH: [default: models.py]--functions-file PATH: [default: functions.py]--application-name TEXT: [default: Zen]--is-async / --no-is-async: [default: no-is-async]--help: Show this message and exit.
Generated Code Examples 📝
Pure Python Implementation (models.py)
from __future__ import annotations
from typing import TypedDict
class UserModel(TypedDict):
id: int
name: str
email: str | None = None
Pure Python Implementation (functions.py)
from __future__ import annotations
from .models import UserModel
def get_user(user_id: int) -> UserModel:
...
FastAPI Implementation (models.py)
from __future__annotations
from pydantic import BaseModel
class UserModel(BaseModel):
id: int
name: str
email: str | None = None
FastAPI Implementation (functions.py)
from __future__annotations
from fastapi import FastAPI
from .models import UserModel
app = FastAPI()
@app.get("/users/{user_id}")
async def get_user(user_id: int) -> UserModel:
...
Asyncapi documentation (asyncapi.yaml)
asyncapi: 3.0.0
info:
title: Zen
version: 0.0.1
description: ''
channels:
get_user:
$ref: '#/components/channels/get_user'
operations:
get_user:
$ref: '#/components/operations/get_user'
components:
channels:
get_user:
messages:
request:
$ref: '#/components/messages/get_user_request'
response:
$ref: '#/components/messages/get_user_response'
operations:
get_user:
action: receive
description: ''
channel:
$ref: '#/channels/get_user'
messages:
- $ref: '#/channels/get_user/messages/request'
reply:
channel:
$ref: '#/channels/get_user'
messages:
- $ref: '#/channels/get_user/messages/response'
messages:
get_user_request:
title: Request params for get_user
summary: ''
description: ''
payload:
type: object
required:
- user_id
properties:
user_id:
type: integer
description: ''
get_user_response:
title: Response params for get_user
summary: ''
description: ''
payload:
$ref: '#/components/schemas/UserModel'
format: required
schemas:
UserModel:
type: object
base_class: BaseModel
required:
- id
- name
properties:
id:
type: integer
name:
type: string
email:
type: string
Development Setup 🛠️
Requirements:
- Python 3.10+
- uv (Python packaging toolchain)
# Install uv if not already installed
# see https://docs.astral.sh/uv/getting-started/installation/
# Clone repository
git clone https://github.com/WaYdotNET/zen-generator.git
cd zen-generator
# Install dependencies with uv
uv sync
# Run tests
uv run pytest
Best Practices 💡
-
AsyncAPI Specification
- Follow AsyncAPI 3.0 guidelines
- Define clear schema types
- Include comprehensive examples
- Use semantic versioning
-
Code Generation
- Review generated code for correctness
- Implement business logic in function stubs
- Keep generated files synchronized
- Use type hints consistently
-
Project Organization
- Maintain clear separation between models and functions
- Follow standard Python package structure
- Implement proper error handling
Contributing 🤝
Contributions are welcome! Please:
- Fork the repository
- Create a feature branch
- Commit your changes
- Push to the branch
- Submit a pull request
License 📄
MIT License - see LICENSE file for details
Support 💬
- GitHub Issues: Report bugs or suggest features
Made with ❤️ by WaYdotNET
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file zen_generator-0.1.7.tar.gz.
File metadata
- Download URL: zen_generator-0.1.7.tar.gz
- Upload date:
- Size: 22.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.12.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
25b608fd386b72cdb6b1891c107f23c1da395e9526b61c7a2c60816e5bd8cdc5
|
|
| MD5 |
e18a0658af001e369afbf0b8bfd7b74f
|
|
| BLAKE2b-256 |
00598811d637ee7991543c1f64d7bd6bdd6f7caafdcc7db267096ef9cddc8fb5
|
Provenance
The following attestation bundles were made for zen_generator-0.1.7.tar.gz:
Publisher:
python-publish.yml on WaYdotNET/zen-generator
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
zen_generator-0.1.7.tar.gz -
Subject digest:
25b608fd386b72cdb6b1891c107f23c1da395e9526b61c7a2c60816e5bd8cdc5 - Sigstore transparency entry: 181824952
- Sigstore integration time:
-
Permalink:
WaYdotNET/zen-generator@b3dcf470de656accec3eceac529417cb6609116d -
Branch / Tag:
refs/heads/main - Owner: https://github.com/WaYdotNET
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
python-publish.yml@b3dcf470de656accec3eceac529417cb6609116d -
Trigger Event:
push
-
Statement type:
File details
Details for the file zen_generator-0.1.7-py3-none-any.whl.
File metadata
- Download URL: zen_generator-0.1.7-py3-none-any.whl
- Upload date:
- Size: 20.0 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.12.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
11cf63a0eb36f75a0cb118d55abb4eac1345ed3863dc2728b93c9b5dfd3639d6
|
|
| MD5 |
9dd932523252590a589b11c88307e1c1
|
|
| BLAKE2b-256 |
d824329e0e595454d80f23132326e62ff00a3e075628f9e713b504a7ef9509fa
|
Provenance
The following attestation bundles were made for zen_generator-0.1.7-py3-none-any.whl:
Publisher:
python-publish.yml on WaYdotNET/zen-generator
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
zen_generator-0.1.7-py3-none-any.whl -
Subject digest:
11cf63a0eb36f75a0cb118d55abb4eac1345ed3863dc2728b93c9b5dfd3639d6 - Sigstore transparency entry: 181824954
- Sigstore integration time:
-
Permalink:
WaYdotNET/zen-generator@b3dcf470de656accec3eceac529417cb6609116d -
Branch / Tag:
refs/heads/main - Owner: https://github.com/WaYdotNET
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
python-publish.yml@b3dcf470de656accec3eceac529417cb6609116d -
Trigger Event:
push
-
Statement type: