Skip to main content

High-performance Python bindings for loco-rs code generation

Project description

Loco Bindings - Python Bindings for Loco-rs

Python bindings for the Loco-rs code generator. This provides a thin wrapper around the Loco generator, allowing you to generate models, controllers, and scaffolds from Python.

Features

  • 🚀 Direct Integration - Uses loco-gen directly for 100% compatibility
  • 🎯 Simple API - Clean Python interface to Loco's generator
  • High Performance - Native Rust implementation with Python bindings
  • 🔄 Auto-sync - Always up-to-date with loco-gen improvements

Installation

# Development installation
cd loco-bindings
maturin develop

Usage

Generate a Model

import loco_bindings

result = loco_bindings.generate_model(
    project_path="/path/to/loco/project",
    name="user",
    fields={
        "name": "string",
        "email": "string^",  # unique
        "age": "int",
    },
    with_timestamps=True
)

print(result["messages"])

Generate a Scaffold

Generate a complete scaffold with model, controller, and views:

result = loco_bindings.generate_scaffold(
    project_path="/path/to/loco/project",
    name="post",
    fields={
        "title": "string",
        "content": "text",
        "published": "bool",
    },
    kind="api",  # Options: "api", "html", "htmx"
    with_timestamps=True
)

Generate a Controller

result = loco_bindings.generate_controller_view(
    project_path="/path/to/loco/project",
    name="users",
    actions=["index", "show", "create", "update", "delete"],
    kind="api"  # Options: "api", "html", "htmx"
)

Field Types

The fields dictionary uses Loco's field type syntax:

Type Example Description
string "name": "string" String field
string^ "email": "string^" Unique string
string! "title": "string!" Non-null string
text "content": "text" Text field
int "age": "int" Integer
int^ "code": "int^" Unique integer
float "price": "float" Float
bool "active": "bool" Boolean
ts "published_at": "ts" Timestamp
uuid "id": "uuid" UUID
json "data": "json" JSON field
array:string "tags": "array:string" Array of strings

See Loco field types documentation for more.

Error Handling

from loco_bindings import ValidationError, FileOperationError, ProjectError

try:
    result = loco_bindings.generate_model(
        project_path="/invalid/path",
        name="user",
        fields={"name": "string"},
        with_timestamps=True
    )
except FileOperationError as e:
    print(f"File error: {e}")
except ValidationError as e:
    print(f"Validation error: {e}")
except ProjectError as e:
    print(f"Project error: {e}")

Architecture

This package is a thin binding layer (~250 lines) that directly exposes loco-gen's functionality:

Python Application
    ↓
loco_bindings (thin wrapper)
    ↓
loco-gen (Rust core)
    ↓
File system

All generation logic, template processing, and validations are handled by loco-gen itself, ensuring complete compatibility with the Loco CLI.

Development

Build

maturin develop

Build for release

maturin build --release

License

MIT OR Apache-2.0

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

loco_bindings-0.1.0.tar.gz (77.7 kB view details)

Uploaded Source

Built Distribution

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

loco_bindings-0.1.0-cp312-cp312-macosx_11_0_arm64.whl (2.2 MB view details)

Uploaded CPython 3.12macOS 11.0+ ARM64

File details

Details for the file loco_bindings-0.1.0.tar.gz.

File metadata

  • Download URL: loco_bindings-0.1.0.tar.gz
  • Upload date:
  • Size: 77.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.7.8

File hashes

Hashes for loco_bindings-0.1.0.tar.gz
Algorithm Hash digest
SHA256 c5d013a58af666f7a1062daba0c45caec183598160e5c6da932e14440aa8fb65
MD5 1b438433e66d0cfaa54773bff66b2ad5
BLAKE2b-256 08d053b40ec4e558d6316a62cc83725bbc60dd5dc3be3a53b816f28d2d0f210e

See more details on using hashes here.

File details

Details for the file loco_bindings-0.1.0-cp312-cp312-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for loco_bindings-0.1.0-cp312-cp312-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 7128ffbb959683c5a409867bc45973208477310f5734c4d8c4e4a7ed85e299e8
MD5 3f226d130bf0f41e5c4f7a5d3db89cdf
BLAKE2b-256 4c1331005b9fd13f32fb7950a55fba736e82fb218607cb87cfe5044300ca881c

See more details on using hashes here.

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