Skip to main content

A simple Python plugin framework

Project description

stoepsel - a simple plug-in system for python

Stoepsel (pronounce ʃtœpsl̩) is an attempt to create a simple (as in minimalistic) but flexible and powerful plugin system for python.

Stoepsel gives you the ability to develop flexible and scalable applications. It doesn't matter if you want to build a UI or console program. You just need to deploy a plugin class to register your application part.

Plugins can use other plugins. Therefore, you can register objects or functions in a model tree where other plugins can find them. To make this as safe as possible, a simple dependency resolving algorithm is implemented. Plugins are registered by their name and a version. They also can define dependencies that have to be resolved. (more on that later)

using stoepsel

installing stoepsel

you can install stoepsel by using pip

python3 -m pip install stoepsel

Or just initialize a virtualenv and run setup.py

python3 -m venv env
source env/bin/activate
python3 setup.py install

running a stoepsel application

A simple stoepsel application can look like this:

import logging
from stoepsel import PluginManager

def main(args):
    logging.basicConfig(level=logging.DEBUG)

    # instanciate PluginManager
    pm = PluginManager()
    # find main and execute it
    pm.get_item(PluginManager.PGM_MAIN)()

    return 0

if __name__ == '__main__':
    import sys
    sys.exit(main(sys.argv))

In this case, stoepsel will look for folder named 'plugins' and read any .py- file into its registry. One file has to register (see below) the term 'main' or simply PluginManager.PGM_MAIN. This is the entry point for our application.

stoepsel configuration

It's possible to configure stoepsel and stoepsel plugins. Therefore, you can give a dictionary to PluginManager constructor which at least consists of 'plugin_path', a string which tells a directory where stoepsel looks for plugins and 'plugin_config' where plugin configuration can be stored.

config = {}
config['plugin_path'] = 'simple_plugins'
config['plugin_config'] = {}

pm = PluginManager(config)

You could also put this into a json based (or other) config file

with open('config.json') as fp:
    config = json.load(fp)
    pm = PluginManager(config)

creating a plugin

a plugin is a class which derives from stoepsel.Plugin. It needs to deploy static information about it's name, version and dependencies. Additionally, you need to define a variable export which points to your plugin class

from stoepsel import Plugin

class MyPlugin(Plugin):
    name = 'simple_plugin'
    version = '0.0.1'
    dependencies = []

    def setup(self):
        self.register(self.PGM_MAIN,self.main)

    def main(self):
        print('Running around...')

export = MyPlugin

registering objects

you can register objects by utilizing the Plugin method 'register'

def setup(self):
    self.register('myapp/plugins/plg1/sayhi', self.sayhi)

def sayhi(self):
    print('hello world')

using registered objects

to use registered objects you fave to find them via get_item method

def setup(self):
    sayhi = self.get_item('myapp/plugins/plg1/sayhi')
    if sayhi is not None:
        sayhi()

configuration

The configuration of the PluginManager is also set into the model tree so you can get it by utilizing get_item

self.get_item('config:plugin_config/myplugin')

this can be useful to create configuration dialogues. To make it easier there is a property config set:

print(self.config['plugin_config/mypath'])

Also a list of plugins can be read by using 'plugins:' as path or just

print(self.plugins)

TODOs

  • pattern matching for dependency version control
  • get rid of the export = in plugn file
  • find plugins recursively

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

stoepsel-0.1.4.tar.gz (7.4 kB view details)

Uploaded Source

Built Distribution

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

stoepsel-0.1.4-py3-none-any.whl (8.9 kB view details)

Uploaded Python 3

File details

Details for the file stoepsel-0.1.4.tar.gz.

File metadata

  • Download URL: stoepsel-0.1.4.tar.gz
  • Upload date:
  • Size: 7.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.6.0 importlib_metadata/4.8.2 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.9.2

File hashes

Hashes for stoepsel-0.1.4.tar.gz
Algorithm Hash digest
SHA256 142e35ed2d019c4b09fb3b3b652137ca57c2ff56e0042ef44ce8c8244bdc0391
MD5 57343bd7437b439e47aca10a168c0be9
BLAKE2b-256 744e02ffd593d92b0703abec8daa861e1470f5c52ee49a4d72139e51786750d9

See more details on using hashes here.

File details

Details for the file stoepsel-0.1.4-py3-none-any.whl.

File metadata

  • Download URL: stoepsel-0.1.4-py3-none-any.whl
  • Upload date:
  • Size: 8.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.6.0 importlib_metadata/4.8.2 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.9.2

File hashes

Hashes for stoepsel-0.1.4-py3-none-any.whl
Algorithm Hash digest
SHA256 08985c9c33bf26c69313305b11b1af1c8214073f29dd9fb9a4fefb31a5d5abfd
MD5 882760b2d6bc7ff6013f9ac70eed7ab1
BLAKE2b-256 641089fc4bf9e5b636fc1193ddb9addcbfc7f8515f6cbe0d4075b3f5df718599

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