ZMK keyboard layout manipulation library with fluent API
Project description
ZMK Layout Library
A standalone Python library for manipulating ZMK keyboard layouts with a modern fluent API. Extracted from the glovebox project to enable ecosystem growth while maintaining full backward compatibility.
Project Status
Work in progress
Features
- Fluent API: Intuitive, chainable interface for layout operations
- Type Safety: Comprehensive type hints with mypy strict mode support
- Provider Pattern: Clean abstraction allows integration with any keyboard framework
- Optional Dependencies: Core functionality with optional templating, display, and parsing features
- High Performance: <1 second for all operations, minimal memory footprint
- Comprehensive: Full ZMK layout support including behaviors, combos, macros, and more
- Round-trip Support: Parse → Modify → Save with perfect fidelity
Quick Start
from zmk_layout import Layout
# Load and modify a layout
layout = Layout("keyboard.keymap")
layout.layers.add("gaming")
layout.layers.get("gaming").set(0, "&kp W").set(1, "&kp A").set(2, "&kp S").set(3, "&kp D")
layout.save("modified.keymap")
# Fluent interface for complex operations
(layout.layers
.add("nav")
.get("nav")
.set_range(0, 4, ["&kp UP", "&kp DOWN", "&kp LEFT", "&kp RIGHT"])
.copy_from("default"))
# Add behaviors
layout.behaviors.add_hold_tap("ht_balanced", tap="&kp A", hold="&kp LCTRL")
layout.behaviors.add_combo("copy", keys=[0, 1], binding="&kp LC(C)")
layout.behaviors.add_macro("hello", sequence=["&kp H", "&kp E", "&kp L", "&kp L", "&kp O"])
Installation
From PyPI (Coming Soon)
# Core library
pip install zmk-layout
# With all optional features
pip install zmk-layout[full]
# Specific feature sets
pip install zmk-layout[templating] # Jinja2 template support
pip install zmk-layout[display] # Rich display formatting
pip install zmk-layout[parsing] # Lark parser for devicetree files
Development Installation
# Clone the repository
git clone https://github.com/CaddyGlow/zmk-layout.git
cd zmk-layout
# Install with development dependencies using uv
uv sync --all-extras --dev
# Or use make
make install
# Run all checks (ruff, mypy, tests)
make check
# Auto-fix and format code
make fix
# Run tests only
make test
# Build packages
make build
# Clean build artifacts
make clean
API Examples
Basic Layout Operations
from zmk_layout import Layout
# Create empty layout
layout = Layout.create_empty(size=36)
# Load from file
layout = Layout.from_keymap("keyboard.keymap")
layout = Layout.from_json("layout.json")
# Layer management
layout.layers.add("symbols", position=1)
layout.layers.remove("gaming")
layout.layers.move("nav", position=0)
layout.layers.reorder(["default", "nav", "symbols"])
# Query layers
gaming_layers = layout.find_layers(lambda name: "gaming" in name)
all_layers = layout.layers.list()
Advanced Features
# Context manager with auto-save
with Layout("keyboard.keymap") as layout:
layout.layers.add("test")
# Automatically saves on exit
# Batch operations
layout.batch_operation([
lambda l: l.layers.add("layer1"),
lambda l: l.layers.add("layer2"),
lambda l: l.behaviors.add_macro("test", ["&kp T"])
])
# Statistics and analysis
stats = layout.get_statistics()
print(f"Layers: {stats['layer_count']}")
print(f"Behaviors: {stats['behavior_count']}")
# Validation
layout.validate() # Raises exception if invalid
Provider Pattern
from zmk_layout import Layout
from zmk_layout.providers import LayoutProviders
# Custom provider implementation
class MyConfigProvider:
def get_behavior_definitions(self):
return [...]
def get_validation_rules(self):
return {...}
# Use custom providers
providers = LayoutProviders(
configuration=MyConfigProvider(),
template=MyTemplateProvider(),
logger=MyLogger(),
file=MyFileAdapter()
)
layout = Layout("keyboard.keymap", providers=providers)
Architecture
The library uses a clean, modular architecture:
zmk_layout/
├── core/ # Core layout classes and fluent API
├── models/ # Pydantic models for layout data
├── parsers/ # Keymap and devicetree parsing
├── generators/ # ZMK code generation
├── providers/ # Provider interfaces for external dependencies
└── utils/ # Utilities and helpers
Key Design Patterns
- Provider Pattern: Abstracts external dependencies for maximum flexibility
- Fluent Interface: Chainable methods for intuitive API usage
- Strategy Pattern: Pluggable parsing and generation strategies
- Factory Pattern: Consistent object creation throughout
Testing
Comprehensive test coverage with 200+ tests:
# Run all tests with coverage
make test
# Run all checks (ruff, mypy, tests)
make check
# Run specific test categories
pytest tests/core/ -v # Core functionality tests
pytest tests/fluent/ -v # Fluent API tests
pytest tests/parsers/ -v # Parser tests
pytest tests/models/ -v # Model tests
Performance
All operations complete in <1 second with minimal memory footprint:
- Layout loading: ~50ms
- Layer operations: <1ms each
- Full compilation: ~100ms
- Memory usage: <10MB for typical layouts
Contributing
We welcome contributions! Please see our Contributing Guidelines for details.
Development Workflow
- Fork the repository
- Create a feature branch (
git checkout -b feature/amazing-feature) - Make your changes with tests
- Ensure quality gates pass:
make fix # Auto-fix and format code make check # Run all checks (must pass)
- Commit your changes (
git commit -m 'feat: add amazing feature') - Push to your fork (
git push origin feature/amazing-feature) - Open a Pull Request
Code Style
- We use
rufffor formatting and linting - Type hints are required (mypy strict mode)
- Tests are required for new features
- Documentation updates for API changes
License
MIT License - see LICENSE file for details.
Acknowledgments
- Extracted from the glovebox project
- Built for the ZMK firmware ecosystem
- Inspired by modern Python library design patterns
Links
- GitHub Repository
- Documentation (Coming Soon)
- PyPI Package (Coming Soon)
- Glovebox Integration Plan
Note: This library is in active development.
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 zmk_layout-0.0.2.tar.gz.
File metadata
- Download URL: zmk_layout-0.0.2.tar.gz
- Upload date:
- Size: 299.7 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.12.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
bde4b56ba7c1ab4a9dbeb8a509eb1908178ba85488dabc267872ebcbf9d3f880
|
|
| MD5 |
a1afd6196ca1729d5154e3273be935f7
|
|
| BLAKE2b-256 |
6da4c924ad2a630730d8ddf19078b89d0a71e539e3c861189a526856c8b0dda5
|
Provenance
The following attestation bundles were made for zmk_layout-0.0.2.tar.gz:
Publisher:
publish.yml on CaddyGlow/zmk-layout
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
zmk_layout-0.0.2.tar.gz -
Subject digest:
bde4b56ba7c1ab4a9dbeb8a509eb1908178ba85488dabc267872ebcbf9d3f880 - Sigstore transparency entry: 358884904
- Sigstore integration time:
-
Permalink:
CaddyGlow/zmk-layout@144e6cb04484bd448cacb9f04428c2c72cbfc8af -
Branch / Tag:
refs/tags/v0.0.2 - Owner: https://github.com/CaddyGlow
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@144e6cb04484bd448cacb9f04428c2c72cbfc8af -
Trigger Event:
release
-
Statement type:
File details
Details for the file zmk_layout-0.0.2-py3-none-any.whl.
File metadata
- Download URL: zmk_layout-0.0.2-py3-none-any.whl
- Upload date:
- Size: 153.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 |
1ab3199483c03c5c10e17e2403bd03c2b343d73f6eb19bdb102a94c06415fc5a
|
|
| MD5 |
8be76f418cc2e940401f7ccdaa146918
|
|
| BLAKE2b-256 |
4bcf39eeffa5dccb2f1ad247df8ee51f3dde7872c0825d1a8b1fb51223e28b02
|
Provenance
The following attestation bundles were made for zmk_layout-0.0.2-py3-none-any.whl:
Publisher:
publish.yml on CaddyGlow/zmk-layout
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
zmk_layout-0.0.2-py3-none-any.whl -
Subject digest:
1ab3199483c03c5c10e17e2403bd03c2b343d73f6eb19bdb102a94c06415fc5a - Sigstore transparency entry: 358884933
- Sigstore integration time:
-
Permalink:
CaddyGlow/zmk-layout@144e6cb04484bd448cacb9f04428c2c72cbfc8af -
Branch / Tag:
refs/tags/v0.0.2 - Owner: https://github.com/CaddyGlow
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@144e6cb04484bd448cacb9f04428c2c72cbfc8af -
Trigger Event:
release
-
Statement type: