event-plugin-system


Nameevent-plugin-system JSON
Version 0.0.8 PyPI version JSON
download
home_pagehttps://github.com/chalonga/event-plugin-system
SummaryA simple plugin event system
upload_time2024-06-30 19:39:40
maintainerNone
docs_urlNone
authorCarlos
requires_python>=3.6
licenseNone
keywords
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            # 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:

```bash
pip install event-plugin-system
```

## Usage

```python

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.
```python
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):
```python
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:

```python
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
```

            

Raw data

            {
    "_id": null,
    "home_page": "https://github.com/chalonga/event-plugin-system",
    "name": "event-plugin-system",
    "maintainer": null,
    "docs_url": null,
    "requires_python": ">=3.6",
    "maintainer_email": null,
    "keywords": null,
    "author": "Carlos",
    "author_email": "chalonga@gmail.com",
    "download_url": "https://files.pythonhosted.org/packages/10/01/de5a0f8e138fd76fc2c570022df6c794e278f8c7dd05c09dde204a7f78f1/event_plugin_system-0.0.8.tar.gz",
    "platform": null,
    "description": "# Event System\n\nA simple plugin event system designed to handle events with plugins dynamically loaded from a specified directory.\n\n## Features\n\n- Load plugins from a directory.\n- Emit events that plugins can handle.\n- Support for both synchronous and asynchronous event handling.\n\n## Installation\n\nTo install the package, use:\n\n```bash\npip install event-plugin-system\n```\n\n## Usage\n\n```python\n\nfrom event_plugin_system import EventPluginSystem\n\n# Specify the directory containing your plugins\nplugin_dir = \"path/to/your/plugins\"\n\n# Initialize the event system\nevent_system = EventPluginSystem(plugin_dir=plugin_dir)\n\n# Emit an event\nevent_system.emit(\"start\")\n\n# Emit an event with arguments\nresults = {}\nevent_system.emit(\"arg_pass2\", arg=\"test\", results=results)\n\n# Check results\n# storage/location for results from event handlers need to provided to the handler\n# Appropriate safe objects should be used for results\n# Consider async queue for async, threadsafe queue when using threads\n# Destination URI's are good choices as well like redis, etc..\n# if threadsafe/simple flow dictionary can be used with plugin_name as the key\nprint(results)\n```\n\n### Function Syntax\n`__call__` is aliased to `emit()`. The instance can be used like a function.\n```python\nevent_system = EventPluginSystem(plugin_dir=plugin_dir)\nevent_system(\"event\", arg1=arg)\n```\n\n## Plugin Structure\n\nEach 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>.\n\n### Example Plugin (simple_echo.py):\n```python\nclass Plugin:\n    def handle_start(self, **kwargs):\n        print(\"Start event received.\")\n\n    def handle_arg(self, arg, **kwargs):\n        print(f\"Arg received: {arg}\")\n```\n\n## Asynchronous Support\n\n### For asynchronous event handling, use EventPluginSystemAsync:\n\n```python\nimport asyncio\nfrom event_plugin_system import EventPluginSystemAsync\n\nes = EventPluginSystemAsync(plugin_dir=plugin_dir)\n\nasync def example():\n    await es.emit(\"start\")\n\nif __name__ == '__main__':\n    asyncio.run(example())\n\n```\n\n## Testing\n\n```\npytest\n```\n",
    "bugtrack_url": null,
    "license": null,
    "summary": "A simple plugin event system",
    "version": "0.0.8",
    "project_urls": {
        "Homepage": "https://github.com/chalonga/event-plugin-system"
    },
    "split_keywords": [],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "5229b5f93ec11fda9b0921a6e5f2a398644bd7063b7fc561ac26ac5ca64599d3",
                "md5": "3bdcf7fc6a98b0c3db5852b4a2595661",
                "sha256": "36fc60738946868bde112463b308d2308209ef7ceda9329460f2c20372447097"
            },
            "downloads": -1,
            "filename": "event_plugin_system-0.0.8-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "3bdcf7fc6a98b0c3db5852b4a2595661",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": ">=3.6",
            "size": 4800,
            "upload_time": "2024-06-30T19:39:38",
            "upload_time_iso_8601": "2024-06-30T19:39:38.755266Z",
            "url": "https://files.pythonhosted.org/packages/52/29/b5f93ec11fda9b0921a6e5f2a398644bd7063b7fc561ac26ac5ca64599d3/event_plugin_system-0.0.8-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "1001de5a0f8e138fd76fc2c570022df6c794e278f8c7dd05c09dde204a7f78f1",
                "md5": "3be8f72b75d141e0d60f28fe622399ec",
                "sha256": "cd751679b61b60ae2ae8740fe4e86a213999c5c4f00e60c8074662d752b0a00c"
            },
            "downloads": -1,
            "filename": "event_plugin_system-0.0.8.tar.gz",
            "has_sig": false,
            "md5_digest": "3be8f72b75d141e0d60f28fe622399ec",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": ">=3.6",
            "size": 3981,
            "upload_time": "2024-06-30T19:39:40",
            "upload_time_iso_8601": "2024-06-30T19:39:40.045954Z",
            "url": "https://files.pythonhosted.org/packages/10/01/de5a0f8e138fd76fc2c570022df6c794e278f8c7dd05c09dde204a7f78f1/event_plugin_system-0.0.8.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2024-06-30 19:39:40",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "chalonga",
    "github_project": "event-plugin-system",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": true,
    "lcname": "event-plugin-system"
}
        
Elapsed time: 0.54917s