Is a simple plugin manager that will dynamically load plugins from a directory given a config or env variable with dynamic kwargs to pass for plugin loading.
Project description
plugo
Is a simple plugin manager that will dynamically load plugins from a directory given a config or env variable with dynamic kwargs to pass for plugin loading. Example uses Flask
Quickstart
Install
pip install plugo
Create a new plugin
These will be created relative to the path you run them from
Base Plugin
plugo new-base-plugin
Flask HTML Plugin
plugo new-ui-plugin
Flask RESTX API Plugin
plugo new-api-plugin
Optional Parameters
--name: Name of the Plugin. This will default the Cookiecutter answer--output-dir: Relative path for output directory for the new plugin. Defaults to './api/plugins'.
Example Creation with Optional Parameters
plugo new-base-plugin --name="Example Plugin" --output-dir="plugins"
Example Plugin
Plugin Structure
All plugins have the following files:
metadata.json(Required)plugin.py(Required)requirements.txt(Optional)
└── 📁sample_plugin
└── __init__.py
└── metadata.json
└── plugin.py
└── requirements.txt
plugin.py Example
The plugin.py must have a init_plugin function defined in it with any optional named kwargs
# plugin.py
from flask import Blueprint
plugin_blueprint = Blueprint('sample_plugin', __name__, template_folder='templates', static_folder='static')
@plugin_blueprint.route('/sample_plugin')
def plugin_route():
return "Hello from sample_plugin!"
def init_plugin(app):
app.register_blueprint(plugin_blueprint, url_prefix='/plugins')
metadata.json Example
The metadata.json is in place to help define metadata about the plugin. a core consideration is plugin dependencies. This is a list/array of plugins in the same directory that are required to load before this plugin can load.
// metadata.json
{
"name": "sample_plugin",
"version": "1.0.0",
"description": "A sample plugin",
"identifier": "com.example.sample_plugin",
"dependencies": [
"test_env_plugin"
],
"author": "Your Name",
"core_version": ">=1.0.0"
}
Example Project
Project Structure
└── 📁flask_base_plugins
└── 📁plugins
└── 📁sample_plugin
└── __init__.py
└── metadata.json
└── plugin.py
└── requirements.txt
└── 📁test_env_plugin
└── __init__.py
└── metadata.json
└── plugin.py
└── requirements.txt
└── __init__.py
└── __init__.py
└── app.py
└── plugins_config.json
Loading Plugins
Plugins can be loaded from a plugins_config.json file or a comma separated list Environment Variable ENABLED_PLUGINS. The major difference is the level of control. The Environment Variable will assume all plugins in the list are active, while the plugins_config.json file allows you to specify if a plugin is active or not e.g.:
// plugins_config.json
{
"plugins": [
{
"name": "sample_plugin",
"enabled": true
},
{
"name": "another_plugin",
"enabled": false
}
]
}
Using the Plugo plugin manager
You can load your plugins with the load_plugins function by loading it into your project with from plugo.services.plugin_manager import load_plugins. This function takes the following parameters:
plugin_directory(Required):config_path(Required):logger(Optional):**kwargs(Optional): e.g.appis an optional kwarg required for our flask plugins
You can optionally consolidate custom requirements from a plugin using the consolidate_plugin_requirements function by loading it into your project with from plugo.services.consolidate_plugin_requirements import consolidate_plugin_requirements. The intent of this function is to support deployments and allow only what is required to be installed into your deployment environment. This function takes the following parameters:
plugin_directory(Required): The directory where plugins are stored.loaded_plugins(Required): List of plugin names that were loaded (This is the output of theload_pluginsfunction).logger(Optional): Logger instance for logging messages.output_file(Optional): The output file to write the consolidated requirements to. Defaults to 'requirements-plugins.txt'
Example app.py:
# app.py
import os
from flask import Flask
from plugo.services.consolidate_plugin_requirements import (
consolidate_plugin_requirements,
)
from plugo.services.plugin_manager import load_plugins
app = Flask(__name__)
# Initialize your app configurations, database, etc.
# Paths
plugin_directory = os.path.join(app.root_path, "plugins")
plugin_config_path = os.path.join(app.root_path, "plugins_config.json")
os.environ["ENABLED_PLUGINS"] = "SomeOtherPlugin"
# Load plugins based on the configuration
loaded_plugins = load_plugins(
plugin_directory=plugin_directory, config_path=plugin_config_path, app=app
)
consolidate_plugin_requirements(
plugin_directory=plugin_directory, loaded_plugins=loaded_plugins
)
if __name__ == "__main__":
app.run(debug=True)
Development
Test (TBC)
(venv) pytest
(venv) coverage run -m pytest
(venv) coverage report
(venv) coverage report -m
(venv) coverage html
(venv) mypy --html-report mypy_report .
(venv) flake8 . --count --select=E9,F63,F7,F82 --show-source --statistics --format=html --htmldir="flake8_report/basic" --exclude=venv
(venv) flake8 . --count --exit-zero --max-complexity=11 --max-line-length=127 --statistics --format=html --htmldir="flake8_report/complexity" --exclude=venv
Build
poetry build
Publish
poetry publish
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 plugo-0.0.1.tar.gz.
File metadata
- Download URL: plugo-0.0.1.tar.gz
- Upload date:
- Size: 17.3 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.8.4 CPython/3.12.2 Windows/11
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
50958b6826d48a233082c6bc07d05e08883a7c9b33e42c38f86c1704810b46e2
|
|
| MD5 |
a2e02fd758b850cffca621f3c3de86df
|
|
| BLAKE2b-256 |
4e6f94277a992041cbb66cedb2973c714cc7cac450c41fc6af0d35f5dcafdc0c
|
File details
Details for the file plugo-0.0.1-py3-none-any.whl.
File metadata
- Download URL: plugo-0.0.1-py3-none-any.whl
- Upload date:
- Size: 33.1 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.8.4 CPython/3.12.2 Windows/11
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
7382ca91f4df0aee62e3bd44dc898e433498afa0d8656d181b9099dab2c4ea78
|
|
| MD5 |
c30a97099db14e44702d7590618c5198
|
|
| BLAKE2b-256 |
578a62b7d1a6c3573e0d5be3d81b7aa240ce4fd96bffafa0f8c199e9cbf28899
|