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
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file event_plugin_system-0.0.6.tar.gz.
File metadata
- Download URL: event_plugin_system-0.0.6.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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
596f2e79168a66ff303c005bd7d272b220913a514d810f8e9281184a12ed9c7c
|
|
| MD5 |
a8d9b4f2a41336a29d75db0ccdb014ec
|
|
| BLAKE2b-256 |
b9dcedd5fd7282dfbb0f2bbf7139cb91f771fcf76b03cca4f8e7d9102a860180
|
File details
Details for the file event_plugin_system-0.0.6-py3-none-any.whl.
File metadata
- Download URL: event_plugin_system-0.0.6-py3-none-any.whl
- Upload date:
- Size: 4.3 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.12.4
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
5edcf1b8f4c3b20eae968a1fef95f2b7f99ad0fd75dbe5677a2ff48285505af4
|
|
| MD5 |
00829c721fe1df0dc1b26bcea196c9e8
|
|
| BLAKE2b-256 |
0111dfbd62351615a530acdbe4ea571152f60fcf0e0bcf101d870ff3903cb63b
|