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.4.tar.gz (3.9 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.4-py3-none-any.whl (4.5 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: event_plugin_system-0.0.4.tar.gz
  • Upload date:
  • Size: 3.9 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.4.tar.gz
Algorithm Hash digest
SHA256 d9d2b272b886c2aa3f13f630169cc05cd9b5154300e9c4d5e986add38e7ab9c2
MD5 ad59573e690a8d8ce3ab8ef9293755e2
BLAKE2b-256 d9edf3f46f6d352e3fd5fc784b1d1f15c11f9f726ef546fa292c547005dc2728

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for event_plugin_system-0.0.4-py3-none-any.whl
Algorithm Hash digest
SHA256 2b1a85c212b4a42c92e35769396640e45688a6c5e3596e3823c5c3bbed55084a
MD5 92a2615e5671ca181988f7326f26ccf0
BLAKE2b-256 bd6132412b2f00846de433502e75b4493ec3eefcd98a5613fc9409b0e4e678ea

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