Skip to main content

WordPress-style Hooks for Python

Project description

WPHooks: WordPress-style Hooks for Python

Python Version License

WPHooks is a lightweight Python library that brings the powerful Event-Driven Architecture of WordPress—Actions and Filters—to your Python projects. It allows you to create extensible, modular, and plugin-friendly applications with ease.

Table of Contents

Why WPHooks?

WordPress's hook system is a proven pattern for building flexible software. It allows developers to "hook" into specific points of the application execution to modify data or add custom functionality without altering the core codebase. wphooks implements this exact behavior in Python.

Perfect for Legacy Projects

Refactoring legacy code ("spaghetti code") is often risky and difficult. wphooks offers a strategic way to modernize these projects:

  1. Open/Closed Principle: You can extend the functionality of a legacy function without modifying its internal logic.
  2. Safe Injection: Instead of rewriting a massive function to add a new feature, you can simply trigger an action or apply a filter at the key point.
  3. Decoupling: New features implementation stays in separate files/modules, keeping the old codebase clean and untouched.

Example Scenario: Imagine a 1000-line function process_order() that is critical to your business. You need to add a "Send Slack Notification" feature. Instead of risking a bug by editing process_order(), you just add do_action('order_processed', order_id) at the end. Your new Slack logic lives in a completely new file, hooked to order_processed.


Types of Hooks

There are two main types of hooks available in this library:

Actions

Actions are "do something" events that allow you to execute custom code at specific points in your execution flow. They trigger when specific events occur during your script's execution (e.g., "User registered", "Page loaded"). They do not return a value.

Basic Example

from wphooks import add_action, do_action


# 1. Define your custom function
def send_welcome_email(user_id):
    print(f"Sending welcome email to user {user_id}...")


# 2. Hook your function to an action name
add_action('user_registered', send_welcome_email)

# 3. Trigger the action somewhere in your code
# This will execute all functions hooked to 'user_registered'
do_action('user_registered', 123)
# Output: Sending welcome email to user 123...

See more Action examples and detailed usage

Filters

Filters are used to "modify something". They accept a data value, modify it, and return it (e.g., "Format title", " Calculate total price"). They must always return a value.

Basic Example

from wphooks import add_filter, apply_filters


# 1. Define your filter function
# It receives a value (and optional args), and MUST return a value
def make_title_uppercase(title):
    return title.upper()


# 2. Hook your function to a filter name
add_filter('the_title', make_title_uppercase)

# 3. Apply the filter to your data
title = "hello world"
filtered_title = apply_filters('the_title', title)

print(filtered_title)
# Output: HELLO WORLD

See more Filter examples and detailed usage


Installation

You can install wphooks easily using pip:

pip install wphooks

Or install it directly from the source:

git clone https://github.com/manuelcanga/wphooks.git
cd wphooks
pip install .

Then, you can import it in your project:

from wphooks import add_action, do_action
from wphooks import add_filter, apply_filters

Development & Testing

To run the unit tests for this project, you can use the built-in unittest module.

Run the following command from the root of the project:

python3 -m unittest discover tests

This will automatically discover and run all tests located in the tests/ directory.

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

wphooks-0.1.0.tar.gz (9.3 kB view details)

Uploaded Source

Built Distribution

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

wphooks-0.1.0-py3-none-any.whl (6.8 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: wphooks-0.1.0.tar.gz
  • Upload date:
  • Size: 9.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.3

File hashes

Hashes for wphooks-0.1.0.tar.gz
Algorithm Hash digest
SHA256 4c4bca2fba6f145e4c02cccccc2220aa39eecfba84d8851056e17245a74f1ee5
MD5 ba24057f38370551a10ddb2735fb3635
BLAKE2b-256 93d23fb9ec686b454f6880e0a6734bee0e7b6088185b8cb30fd545af85104360

See more details on using hashes here.

File details

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

File metadata

  • Download URL: wphooks-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 6.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.3

File hashes

Hashes for wphooks-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 a8a98410aa73ce6b5494a1d121030ad450b7f4216f6c3bdc8b6c32b13c85a6ab
MD5 87fecb12cbce8e20b5e39712560baee6
BLAKE2b-256 aaa27bbe0103737bd62101ce31d18c490f47d9e4ae8ad34a3567b6e59f41b290

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