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.2.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.2-py3-none-any.whl (7.6 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: python_hookups-0.1.2.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.2.tar.gz
Algorithm Hash digest
SHA256 18c2b5c736453cfcdb151b65a0f119e10cdbd073a35de64eb33ba6b9361c5f1a
MD5 ea11f3734fccd0fe97a47a6954ae1a2d
BLAKE2b-256 c86fe2ba10c023f59c816d2beae2df2aa8d6631d4bf3306d048393cce850916e

See more details on using hashes here.

File details

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

File metadata

  • Download URL: python_hookups-0.1.2-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.2-py3-none-any.whl
Algorithm Hash digest
SHA256 3b8e9b85284534e4733762689f1b56086441027f4d0e78e02325ee49ce0e5b05
MD5 3cb97590894985c20b90b6ee7ce2a464
BLAKE2b-256 f1051e44b7f31dcea031c39606693896e9a99cc57127675014a7d41b5f7c75b0

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