Skip to main content

A simple namespaced plugin facility

Project description

Full Documentation: http://readthedocs.org/docs/straightplugin/
Mailing List: https://groups.google.com/forum/#!forum/straight.plugin

# straight.plugin

straight.plugin is a Python plugin loader inspired by twisted.plugin with two
important distinctions:

- Fewer dependencies
- Python 3 compatible

The system is used to allow multiple Python packages to provide plugins within
a namespace package, where other packages will locate and utilize. The plugins
themselves are modules in a namespace package where the namespace identifies
the plugins in it for some particular purpose or intent.

For example, if I was building a log parser, I might tell users that he parser
will look for plugins in the `logfilter` namespace. Someone else could then
provide a module named `logfilter.normalizedates` and my parser would find this
plugin, load it, and use it to filter the log entries.

It would be up to me to document what the plugin actually looks like, based on
how I need to use it in my project.

## Using plugins

from straight.plugin import load

class Skip(Exception):
pass

plugins = load('logfilter')

def filter_entry(log_entry):
for plugin in plugins:
try:
log_entry = plugin.filter(log_entry)
except Skip:
pass
return log_entry

## Writing plugins

# logfilter/__init__.py
from pkgutil import extend_path
__path__ = extend_path(__path__, __name__)


# logfilter/hide_extra.py

from logfilter import Skip

def filter(log_entry):
level = log_entry.split(':', 1)[0]
if level != 'EXTRA':
return log_entry
else:
raise Skip()

## Module and Class Plugins

straight.plugin is able to load both modules and classes, depending on your
needs. When you call `load()` with a namespace, you'll get all the modules
found under that namespace. When you call it with the optional `subclasses`
parameter, all the classes which are subclases of the given type will be
given.

# Example

from straight.plugins import load

plugins = load("ircclient", subclasses=IrcClientCommand)


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

straight.plugin-1.4.1.tar.gz (2.7 kB view details)

Uploaded Source

File details

Details for the file straight.plugin-1.4.1.tar.gz.

File metadata

File hashes

Hashes for straight.plugin-1.4.1.tar.gz
Algorithm Hash digest
SHA256 c2c817a09efdbfe8cff9fcddd5b890e9b8ed1f0484b1fde62477a04128953719
MD5 75313cd5496a64bc87917e17584e96ca
BLAKE2b-256 cb147bc73cafaef40c2470f071e671a0c7c6cb89eff3fb473aaa0ff4c9782a0e

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