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

Uploaded Python 3

File details

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

File metadata

  • Download URL: event_plugin_system-0.0.7.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.7.tar.gz
Algorithm Hash digest
SHA256 ae6904591d250a4d7945dd0838eb55b6a7ba292dd39f0133b8426c86d1fb4a36
MD5 088153f3ed456c5f788e3181cf1e09a0
BLAKE2b-256 4061af148b201d6aa1a314ba0495bb2c4e1cd74e9b7e56015f0220dbec2d191c

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for event_plugin_system-0.0.7-py3-none-any.whl
Algorithm Hash digest
SHA256 2b96337286fec1303d54899c8549309106e3c08df0b80695901122846a656108
MD5 d68572276bd97e9d1a8a2af0d6e56a1f
BLAKE2b-256 d05521d1dbe48040ac254e3a9a53bfec084ba13cb67c5d27c0d88239b5c79ca9

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