Skip to main content

A simple plugin event system

Project description

Event System

A simple plugin event system designed to handle events with plugins dynamically loaded from a specified directory.

Features

  • Load plugins from a directory.
  • Emit events that plugins can handle.
  • Support for both synchronous and asynchronous event handling.

Installation

To install the package, use:

pip install event-plugin-system

Usage

from event_plugin_system import EventPluginSystem

# Specify the directory containing your plugins
plugin_dir = "path/to/your/plugins"

# Initialize the event system
event_system = EventPluginSystem(plugin_dir=plugin_dir)

# Emit an event
event_system.emit("start")

# Emit an event with arguments
results = {}
event_system.emit("arg_pass2", arg="test", results=results)

# Check results
# storage/location for results from event handlers need to provided to the handler
# Appropriate safe objects should be used for results
# Consider async queue for async, threadsafe queue when using threads
# Destination URI's are good choices as well like redis, etc..
# if threadsafe/simple flow dictionary can be used with plugin_name as the key
print(results)

Function Syntax

__call__ is aliased to emit(). The instance can be used like a function.

event_system = EventPluginSystem(plugin_dir=plugin_dir)
event_system("event", arg1=arg)

Plugin Structure

Each plugin should be a Python file in the specified plugin directory and contain a class named Plugin. The class should define methods with names following the pattern handle_<event_name>.

Example Plugin (simple_echo.py):

class Plugin:
    def handle_start(self, **kwargs):
        print("Start event received.")

    def handle_arg(self, arg, **kwargs):
        print(f"Arg received: {arg}")

Asynchronous Support

For asynchronous event handling, use EventPluginSystemAsync:

import asyncio
from event_plugin_system import EventPluginSystemAsync

es = EventPluginSystemAsync(plugin_dir=plugin_dir)

async def example():
    await es.emit("start")

if __name__ == '__main__':
    asyncio.run(example())

Testing

pytest

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

event_plugin_system-0.0.8.tar.gz (4.0 kB view details)

Uploaded Source

Built Distribution

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

event_plugin_system-0.0.8-py3-none-any.whl (4.8 kB view details)

Uploaded Python 3

File details

Details for the file event_plugin_system-0.0.8.tar.gz.

File metadata

  • Download URL: event_plugin_system-0.0.8.tar.gz
  • Upload date:
  • Size: 4.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.12.4

File hashes

Hashes for event_plugin_system-0.0.8.tar.gz
Algorithm Hash digest
SHA256 cd751679b61b60ae2ae8740fe4e86a213999c5c4f00e60c8074662d752b0a00c
MD5 3be8f72b75d141e0d60f28fe622399ec
BLAKE2b-256 1001de5a0f8e138fd76fc2c570022df6c794e278f8c7dd05c09dde204a7f78f1

See more details on using hashes here.

File details

Details for the file event_plugin_system-0.0.8-py3-none-any.whl.

File metadata

File hashes

Hashes for event_plugin_system-0.0.8-py3-none-any.whl
Algorithm Hash digest
SHA256 36fc60738946868bde112463b308d2308209ef7ceda9329460f2c20372447097
MD5 3bdcf7fc6a98b0c3db5852b4a2595661
BLAKE2b-256 5229b5f93ec11fda9b0921a6e5f2a398644bd7063b7fc561ac26ac5ca64599d3

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