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
Metadata
Release files for loco-bindings 0.1.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| loco_bindings-0.1.0.tar.gz | 77.7 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| loco_bindings-0.1.0-cp312-cp312-macosx_11_0_arm64.whl | CPython 3.12 | CPython 3.12 | macOS 11.0+ ARM64 | Details |
Total release size: 2.3 MB
Release files / loco_bindings-0.1.0.tar.gz
| Download URL | loco_bindings-0.1.0.tar.gz |
|---|---|
| Size | 77.7 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
c5d013a58af666f7a1062daba0c45caec183598160e5c6da932e14440aa8fb65
|
|
BLAKE2b-256 checksum How to use checksums |
08d053b40ec4e558d6316a62cc83725bbc60dd5dc3be3a53b816f28d2d0f210e
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
uv/0.7.8
|
Release files / loco_bindings-0.1.0-cp312-cp312-macosx_11_0_arm64.whl
| Download URL | loco_bindings-0.1.0-cp312-cp312-macosx_11_0_arm64.whl |
|---|---|
| Size | 2.2 MB |
| Tags | CPython 3.12 macOS 11.0+ ARM64 |
|
SHA-256 checksum How to use checksums |
7128ffbb959683c5a409867bc45973208477310f5734c4d8c4e4a7ed85e299e8
|
|
BLAKE2b-256 checksum How to use checksums |
4c1331005b9fd13f32fb7950a55fba736e82fb218607cb87cfe5044300ca881c
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
uv/0.7.8
|