A generic, type-safe hook and signal system for Python projects with Pydantic validation.
Project description
Python Hookups
A generic, type-safe hook and signal system for Python projects. It supports automatic discovery of extension points (hooks) and event-driven signals with both synchronous and asynchronous support.
Features
- Type-safe Hooks: Register functions with specific names and execution orders.
- Professional Signals: Standalone signal implementation with
syncandasyncsending. - Automatic Discovery: Automatically finds and imports
hookupssubmodules in your packages. - CLI Support: Inspect registered hooks and signals from the command line.
- Modern Python: Built with Python 3.12+, Pydantic validation, and
uvpackage management.
Installation
# Using uv (recommended)
uv add python-hookups
# Using pip
pip install python-hookups
Usage
1. Hook System
Hooks are used when you want to allow other parts of your application (or plugins) to provide values or execute logic at specific points.
Registration
from hookups import register_hookup
@register_hookup("UI_MENU_HOOKUP", order=10)
def my_menu_item():
return {"label": "My Plugin", "url": "/plugin"}
Discovery and Execution
from hookups import set_scan_paths, discover_hookups, get_hookups
# 1. Configure where to look for 'hookups' submodules
set_scan_paths(["my_app.plugins", "my_app.extensions"])
# 2. Discover and load all hookups
discover_hookups()
# 3. Get and execute hooks
hooks = get_hookups("UI_MENU_HOOKUP")
for hook in hooks:
result = hook()
print(result)
2. Signal System
Signals are used for "fire-and-forget" events. They support both synchronous and asynchronous receivers.
Definition
from hookups import Signal
user_registered = Signal(name="user_registered")
Connection
from hookups import receiver
@receiver(user_registered)
async def send_welcome_email(sender, username, **kwargs):
print(f"Sending welcome email to {username}...")
@receiver(user_registered)
def log_registration(sender, username, **kwargs):
print(f"Audit log: {username} joined.")
Sending
# Asynchronous sending (handles both sync and async receivers)
await user_registered.send_async(sender=None, username="Alice")
# Synchronous sending (for sync-only contexts)
user_registered.send(sender=None, username="Alice")
# Robust sending (continues even if receivers raise exceptions)
await user_registered.send_async_robust(sender=None, username="Alice")
CLI Usage
You can list all registered hooks and signals using the built-in CLI.
# List all registered hooks
python main.py --list hooks
# List all registered signals and their receivers
python main.py --list signals
Example Project
Check out the example/ directory for a complete working demonstration of how to structure a project with plugins using python-hookups.
uv run python example/app.py
Development
# Run tests
uv run pytest
# Run tests with coverage
uv run pytest --cov=hookups
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 python_hookups-0.1.1.tar.gz.
File metadata
- Download URL: python_hookups-0.1.1.tar.gz
- Upload date:
- Size: 34.7 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.11.17 {"installer":{"name":"uv","version":"0.11.17","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":null,"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
f9fc3418f674c2dff21432ccb542ec6bd401ff1b84f2ee0c424300d4b47eb770
|
|
| MD5 |
e3e6792780871e975cd8bb82ff983ad6
|
|
| BLAKE2b-256 |
b1d4255c73b648304edcff1a78467249059a62f9e4daf6c28a96593c999820aa
|
File details
Details for the file python_hookups-0.1.1-py3-none-any.whl.
File metadata
- Download URL: python_hookups-0.1.1-py3-none-any.whl
- Upload date:
- Size: 7.6 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.11.17 {"installer":{"name":"uv","version":"0.11.17","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":null,"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
7df63a933d04021b8fe5a0d9dd51e22f7427b48f7272610992e448d044f89562
|
|
| MD5 |
4c168e1f16dd42039feb8c212d2a5e51
|
|
| BLAKE2b-256 |
a6ae7c0982b2857fb63403f1b7ad4de34fe02437c07f4c0950bb009a3d2e9a7a
|