Skip to content

Plugins

cbabil edited this page Sep 12, 2024 · 1 revision

PyPluginizer organizes plugins into core and user-specific categories, providing flexibility for customization.

Plugin Structure

PyPluginizer/
│
├── src/
│   └── plugins/
│       ├── core/       # Core plugins managed by the framework.
│       │   └── plugin
│       │       └── DEPENDENCY
│       │       └── VERSION
│       │       └── __init__.py
│       │       └── plugin.py
│       ├── users/      # User-created plugins for extending functionality.
│       │   └── plugin
│       │       └── DEPENDENCY
│       │       └── VERSION
│       │       └── __init__.py
│       │       └── plugin.py

Core Plugins

Found in ./src/plugins/core, these are fundamental components of the framework, handling essential functions.

User Plugins

Located in ./src/plugins/users, these allow users to extend the framework's functionality.

How to Write a Plugin

To create a plugin for PyPluginizer, follow these steps:

  1. Create Plugin File: Inside the relevant directory (./src/plugins/core for core plugins or ./src/plugins/users for user plugins), create a new Python file (e.g., my_plugin.py).

  2. Define Plugin Class: Write a class that implements required plugin methods, init(), execute(), and process_results(). This ensures the plugin integrates with PyPluginizer's lifecycle.

  3. Hook Functionality: Add specific logic or functionality in your plugin's process_results() method.

  4. Metadata: Ensure each plugin has metadata to identify its purpose, version, and dependencies.

  5. Register Plugin: PyPluginizer automatically registers plugins based on their directory. Ensure the directory structure matches the framework's expectations.

Example:

# -*- coding: utf-8 -*-
class Test:
    def __init__(self, logger, hooks=None, **kwargs):
        # Assign the logger to an instance variable
        self.logger = logger
        # Assign the hooks to an instance variable
        self.hooks = hooks
        # Assign each value from kwargs to an instance variable
        for key, value in kwargs.items():
            setattr(self, key, value)

    def execute(self):
        self.logger.info("Executing plugin")

    def process_results(self):
        self.logger.info("Processing results for plugin")
        self.hooks["json"].trigger()

Clone this wiki locally