Skip to main content

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 sync and async sending.
  • Automatic Discovery: Automatically finds and imports hookups submodules in your packages.
  • CLI Support: Inspect registered hooks and signals from the command line.
  • Modern Python: Built with Python 3.12+, Pydantic validation, and uv package 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


Download files

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

Source Distribution

python_hookups-0.1.0.tar.gz (34.7 kB view details)

Uploaded Source

Built Distribution

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

python_hookups-0.1.0-py3-none-any.whl (7.6 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: python_hookups-0.1.0.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

Hashes for python_hookups-0.1.0.tar.gz
Algorithm Hash digest
SHA256 14064526dbd26c4f34a1452ba817fdb69f23d42960b1347895d5aaf9cb727c44
MD5 e0f3e4d97bdcf2fa83f6c785c9334302
BLAKE2b-256 e55d2a714e335a1144ecc42028631152c58488699fd52e71ff4d1d92cdb017cc

See more details on using hashes here.

File details

Details for the file python_hookups-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: python_hookups-0.1.0-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

Hashes for python_hookups-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 20126b05eb07a9d4cbb755ffb82eb4c3ca56169181dc19a50c6c73db7296cac2
MD5 4dd78dd6d43864c380cad658bee2157d
BLAKE2b-256 cbed0ac7312fee65b454341b1b960dfd392a6e5e463b9fc86669d0f4a1eef591

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