Skip to main content

A lightweigh Python plugin system

Project description

PlugyPy

plugypy

Installation

You can install the plugin system from PyPi with:

pip install PlugyPy

Or locally by cloning the repository:

git clone https://github.com/not-so-cool-anymore/plugypy.git

and then running the setup file in the main directory with:

pip install .

Writing a configuration file

A PlugyPy configuration (or config) file is a JSON file that contains a deserialized representation of a PlugyPy Configuration object. The Configuration object consists of a boolean named will_load_all and a list named plugins. plugins is a list of PlugyPy Plugin objects will_load_all determines whether all of the plugins in the plugins directory will be loaded or only the enabled ones.

An example of a config file is:

{
    "will_load_all": true,
    "plugins": [
        {
            "name": "example_plugin_name_0",
            "is_enabled": true
        },
        {
            "name": "example_plugin_name_1",
            "is_enabled": false
        },
        {
            "name": "example_plugin_name_2",
            "is_enabled": true
        }
    ] 
}

Where name is the name of the plugin file without the .py file extension, and enabled is the boolean variable that indicates whether a plugin will be loaded (when true) or not (when false).

Usage

Importing the plugin system:

import plugypy

Deserializing JSON configuration file into a Configuration object.

configuration_deserializer = ConfigurationDeserializer('/path/to/configuration/file')
configuration = configuration_deserializer.deserialize_config()

Creating a plugin manager object:

plugin_manager = plugypy.PluginManager('/path/to/plugins/directory', configuration)

The plugin manager object has one extra feature - plugin ownership verification. This feature ensures that the plugin that is being executed belongs to the current user (or if sudo is used to run the program - the sudo caller). This feature can be activated via parsing one extra argument - will_verify_ownership=True, which is set to False by default when not passed.

Discovering plugins:

discovered_plugins = plugin_manager.discover_plugins()

discover_plugins is a list of all the plugins in a given plugins directory.

Importing plugins:

plugins_list = plugin_manager.import_plugins(discovered_plugins)

plugins_list is a list of map objects that has a name key and an object value of the imported executable Python module (plugin executable).

Importing a singe plugin by file name:

single_plugin = plugin_manager.import_plugin('PLUGIN_FILENAME_WITHOUT_PY_EXTENSION')

In this case, the plugin will be imported no matter if a configuration for it exists. This importing method is developed for edge cases in which the imported plugin will be executed only once.

Getting a plugin's information:

plugin_name = plugins_list[n]['name']
plugin_executable_object = plugins_list[n]['object']

n is an index of a plugin.

Executing a plugin's function with no parameters:

plugin = plugins_list[n]
plugin_result = plugin_manager.execute_plugin_function(plugin, 'function_name')

if plugin_result == None:
    print('The plugin returned no result')
else:
    print('The plugin returned: {}'.format(result))

n is an index of a plugin.

Executing a plugin's function with parameters:

plugin = plugins_list[n]

arguments_tuple = ('arg1', 'arg2', 'arg3')
plugin_result = plugin_manager.execute_plugin_function(plugin, 'function_name', args=arguments_tuple)

if plugin_result == None:
    print('The plugin returned no result')
else:
    print('The plugin returned: {}'.format(result))

n is an index of a plugin.

args must be a tuple of argument/s.

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

PlugyPy-1.2.2.tar.gz (4.2 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

PlugyPy-1.2.2-py3-none-any.whl (18.0 kB view details)

Uploaded Python 3

File details

Details for the file PlugyPy-1.2.2.tar.gz.

File metadata

  • Download URL: PlugyPy-1.2.2.tar.gz
  • Upload date:
  • Size: 4.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.25.1 setuptools/50.3.0 requests-toolbelt/0.9.1 tqdm/4.46.1 CPython/3.6.9

File hashes

Hashes for PlugyPy-1.2.2.tar.gz
Algorithm Hash digest
SHA256 dc17c8f6cf2e2eee3b32cd607430957440649caf243e2c99daf3fe00c651ac2a
MD5 6e20e08e54febb4fffbd853c169243b7
BLAKE2b-256 147c3bd286d7ae392f0df84dfda987252b103a392cbbd1412317318f0722954a

See more details on using hashes here.

File details

Details for the file PlugyPy-1.2.2-py3-none-any.whl.

File metadata

  • Download URL: PlugyPy-1.2.2-py3-none-any.whl
  • Upload date:
  • Size: 18.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.25.1 setuptools/50.3.0 requests-toolbelt/0.9.1 tqdm/4.46.1 CPython/3.6.9

File hashes

Hashes for PlugyPy-1.2.2-py3-none-any.whl
Algorithm Hash digest
SHA256 a89b6d25097f89218a6db24506137571e2ab62499247f4e64d279ceda75cc791
MD5 4a2f07fe9fdd98067368a139bef6da83
BLAKE2b-256 d037a2c6e12603de0ed88862cf3ec060b5a19f329ce68a54c85f94a8fe64c7af

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