Skip to main content

A bidirectional Python code generator that converts between AsyncAPI 3.0 specifications and Python code (pure Python or FastAPI implementations).

Reason this release was yanked:

RecursionError: maximum recursion depth exceeded

Project description

Zen Generator Logo

Zen Generator 🚀

PyPI version License: MIT Python Version Code Style: Ruff

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 📦

pip install zen-generator

[!IMPORTANT] Currently, only model and function definitions in the components block 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 required keyword is used to specify mandatory fields, as shown below:

required:
  - user_id

This ensures that the user_id parameter 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
zen-generator fastapi

# Generate pure Python implementation from AsyncAPI spec  
zen-generator pure-python 

# Generate AsyncAPI spec from Python code
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-documentation
  • pure-python
  • fastapi

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
pip install uv

# Clone repository
git clone https://github.com/WaYdotNET/zen-generator.git
cd zen-generator

# Create and activate virtual environment with uv
uv venv
source .venv/bin/activate  # On macOS/Linux
# or
.venv\Scripts\activate  # On Windows

# Install dependencies with uv
uv sync

# Run tests
python -m pytest

Best Practices 💡

  1. AsyncAPI Specification

    • Follow AsyncAPI 3.0 guidelines
    • Define clear schema types
    • Include comprehensive examples
    • Use semantic versioning
  2. Code Generation

    • Review generated code for correctness
    • Implement business logic in function stubs
    • Keep generated files synchronized
    • Use type hints consistently
  3. Project Organization

    • Maintain clear separation between models and functions
    • Follow standard Python package structure
    • Implement proper error handling

Contributing 🤝

Contributions are welcome! Please:

  1. Fork the repository
  2. Create a feature branch
  3. Commit your changes
  4. Push to the branch
  5. Submit a pull request

License 📄

MIT License - see LICENSE file for details

Support 💬


Made with ❤️ by WaYdotNET

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

zen_generator-0.1.6.tar.gz (22.1 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

zen_generator-0.1.6-py3-none-any.whl (19.9 kB view details)

Uploaded Python 3

File details

Details for the file zen_generator-0.1.6.tar.gz.

File metadata

  • Download URL: zen_generator-0.1.6.tar.gz
  • Upload date:
  • Size: 22.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for zen_generator-0.1.6.tar.gz
Algorithm Hash digest
SHA256 0796f640087593d2314b4125f75840866f62546f0eb859e9e800b837f556a0eb
MD5 5fefef81abf36d02700edd8c4cfd9fb2
BLAKE2b-256 426aecace6abb004efe21a6bf61e46d4e61a82dfe9b7ab0deaffec0867ad423a

See more details on using hashes here.

Provenance

The following attestation bundles were made for zen_generator-0.1.6.tar.gz:

Publisher: python-publish.yml on WaYdotNET/zen-generator

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file zen_generator-0.1.6-py3-none-any.whl.

File metadata

  • Download URL: zen_generator-0.1.6-py3-none-any.whl
  • Upload date:
  • Size: 19.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for zen_generator-0.1.6-py3-none-any.whl
Algorithm Hash digest
SHA256 2fb0ababfe644f8ac8006263f90cce82cd42ce2e25423839272b1767b3b85cb3
MD5 2de23827736b0092a626981979a9fab1
BLAKE2b-256 f522206d0c179e139e8c88a17a8a4625ffdf80a4955e479a3c6dbacf7d9261a4

See more details on using hashes here.

Provenance

The following attestation bundles were made for zen_generator-0.1.6-py3-none-any.whl:

Publisher: python-publish.yml on WaYdotNET/zen-generator

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page