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.5.tar.gz (3.7 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.5-py3-none-any.whl (4.3 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: event_plugin_system-0.0.5.tar.gz
  • Upload date:
  • Size: 3.7 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.5.tar.gz
Algorithm Hash digest
SHA256 a3370e06b7f2c3a5833232d7a523856b4b4fa01703472b4023eb68fd3085d4bc
MD5 cdab13654a5bb3e169a1e9fdf3b5447f
BLAKE2b-256 e64b9150c47cf6e101c41e899219a38eb32f5e447b7c765cc8cc159775d911e5

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for event_plugin_system-0.0.5-py3-none-any.whl
Algorithm Hash digest
SHA256 cc6284735bfef5eddb7524463a910b68b35938279d9dcce647d9da680e25e163
MD5 d5b6003244ad88e5807fa5b19f7c6c2f
BLAKE2b-256 39da9d5c72038fdd93d3a2b0495093e599936bd3ad42c04cfdb2d9f61ea7c793

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