Skip to main content

A framework for creating and importing plugins

Project description

Packaging Status
Documentation Status Travis-CI Build Status Coverage Status
PyPI Package latest release Supported versions Supported implementations

Overview

Pluginlib makes creating plugins for your project simple.

Step 1: Define plugin parent classes

All plugins are subclasses of parent classes. To create a parent class, use the @Parent_ decorator.

The @Parent_ decorator can take a plugin type for accessing child plugins of the parent. If a plugin type isn’t given, the class name will be used.

The @Parent_ decorator can also take a group keyword which restricts plugins to a specific plugin group. group should be specified if plugins for different projects could be accessed in an single program, such as with libraries and frameworks. For more information, see the Plugin Groups section.

Methods required in child plugins should be labeled as abstract methods. Plugins without these methods or with parameters that don’t match, will not be loaded. For more information, see the Abstract Methods section.

"""
sample.py
"""
import pluginlib

@pluginlib.Parent('parser')
class Parser(object):

    @pluginlib.abstractmethod
    def parse(self, string):
        pass

Step 2: Define plugin classes

To create a plugin, subclass a parent class and include any required methods.

Plugins can be customized through optional class attributes:

_alias_

Changes the name of the plugin which defaults to the class name.

_version_

Sets the version of the plugin. Defaults to the module __version__ or None If multiple plugins with the same type and name are loaded, the plugin with the highest version is used. For more information, see the Versions section.

_skipload_

Specifies the plugin should not be loaded. This is useful when a plugin is a parent class for additional plugins or when a plugin should only be loaded under certain conditions. For more information see the Conditional Loading section.

"""
sample_plugins.py
"""
import json
import sample

class JSON(sample.Parser):

    _alias_ = 'json'

    def parse(self, string):
        return json.loads(string)

Step 3: Load plugins

Plugins are loaded when the module they are in is imported. PluginLoader will load modules from specified locations and provides access to them.

PluginLoader can load plugins from several locations.
  • A program’s standard library

  • Entry points

  • A list of modules

  • A list of filesystem paths

Plugins can also be blacklisted. See the Blacklists section for more information.

Plugins are accessible through the PluginLoader.plugins property, a nested dictionary accessible through dot notation.

import pluginlib
import sample

loader = pluginlib.PluginLoader(modules=['sample_plugins'])
plugins = loader.plugins
parser = plugins.parser.json()
print(parser.parse('{"json": "test"}'))

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

pluginlib-0.5.1.tar.gz (124.2 kB view details)

Uploaded Source

Built Distribution

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

pluginlib-0.5.1-py2.py3-none-any.whl (24.8 kB view details)

Uploaded Python 2Python 3

File details

Details for the file pluginlib-0.5.1.tar.gz.

File metadata

  • Download URL: pluginlib-0.5.1.tar.gz
  • Upload date:
  • Size: 124.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for pluginlib-0.5.1.tar.gz
Algorithm Hash digest
SHA256 a12be1cdf5b555eedda7efae34995327f49599a0f0395caaf63a110aedb6ed6d
MD5 92f7b79e3bb3dea2a5efac60ea47f160
BLAKE2b-256 5b42329ef5ca9b5604614a59720bbaf753b22f59ec4dc18b36d93435645963df

See more details on using hashes here.

File details

Details for the file pluginlib-0.5.1-py2.py3-none-any.whl.

File metadata

File hashes

Hashes for pluginlib-0.5.1-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 7e9d7553fe9c1f6a9353947b2e8fb1b7a9861ce1c743765ecb788ce5c74a0c61
MD5 df8bc1c214223ff9c90cceb86033f2ba
BLAKE2b-256 d7cba64217de22ba99d7b8425e0b58074776455eaa923d4559171c1c24334ad2

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