Skip to main content

A simple entrypoint-free plugin system for python

Project description

simplug

A simple entrypoint-free plugin system for python with async hooks supported

Installation

pip install -U simplug

Features

  • Entrypoint-free, meaning no unwanted modules imported beforehand.
  • Loading priority definition while implementing a plugin
  • Required hooks (hooks required to be implemented in plugins)
  • Different ways to fetch results from hooks
  • Async hooks supported

Examples

A toy example

from simplug import Simplug

simplug = Simplug('project')

class MySpec:
    """A hook specification namespace."""

    @simplug.spec
    def myhook(self, arg1, arg2):
        """My special little hook that you can customize."""

class Plugin_1:
    """A hook implementation namespace."""

    @simplug.impl
    def myhook(self, arg1, arg2):
        print("inside Plugin_1.myhook()")
        return arg1 + arg2

class Plugin_2:
    """A 2nd hook implementation namespace."""

    @simplug.impl
    def myhook(self, arg1, arg2):
        print("inside Plugin_2.myhook()")
        return arg1 - arg2

simplug.register(Plugin_1, Plugin_2)
results = simplug.hooks.myhook(arg1=1, arg2=2)
print(results)
inside Plugin_1.myhook()
inside Plugin_2.myhook()
[3, -1]

Note that the hooks are executed in the order the plugins are registered. This is different from pluggy.

A complete example

See examples/complete/.

Running python -m examples/complete/ gets us:

Your food. Enjoy some egg, egg, egg, salt, pepper, egg, egg, lovely spam, wonderous spam
Some condiments? We have pickled walnuts, mushy peas, mint sauce, spam sauce
Now this is what I call a condiments tray!

Usage

Definition of hooks

Hooks are specified and implemented by decorating the functions with simplug.spec and simplug.impl respectively.

simplug is initialized by:

simplug = Simplug('project')

The 'project' is a unique name to mark the project, which makes sure Simplug('project') get the same instance each time.

Note that if simplug is initialized without project, then a name is generated automatically as such project-0, project-1, etc.

Hook specification is marked by simplug.spec:

simplug = Simplug('project')

@simplug.spec
def setup(args):
    ...

simplug.spec can take two keyword-arguments:

  • required: Whether this hook is required to be implemented in plugins
  • result: An enumerator to specify the way to collec the results.
    • SimplugResult.ALL: Get all the results from the hook, as a list including NONEs
    • SimplugResult.ALL_BUT_NONE: Get all the results from the hook, as a list, not including NONEs
    • SimplugResult.FIRST: Get the none-None result from the first plugin only (ordered by priority)
    • SimplugResult.LAST: Get the none-None result from the last plugin only

Hook implementation is marked by simplug.impl, which takes no additional arguments.

The name of the function has to match the name of the function by simplug.spec. And the signatures of the specification function and the implementation function have to be the same in terms of names. This means you can specify default values in the specification function, but you don't have to write the default values in the implementation function.

Note that default values in implementation functions will be ignored.

Also note if a hook specification is under a namespace, it can take self as argument. However, this argument will be ignored while the hook is being called (self will be None, and you still have to specify it in the function definition).

The plugin registry

The plugins are registered by simplug.register(*plugins). Each plugin of plugins can be either a python object or a str denoting a module that can be imported by importlib.import_module.

The python object must have an attribute name, __name__ or __class.__name__ for simplug to determine the name of the plugin. If the plugin name is determined from __name__ or __class__.__name__, it will be lowercased.

You can enable or disable a plugin temporarily after registration by:

simplug.disable('plugin_name')
simplug.enable('plugin_name')

You can use following methods to inspect the plugin registry:

  • simplug.get_plugin: Get the plugin by name
  • simplug.get_all_plugins: Get a dictionary of name-plugin mappings of all plugins
  • simplug.get_all_plugin_names: Get the names of all plugins, in the order it will be executed.

Calling hooks

Hooks are call by simplug.hooks.<hook_name>(<arguments>) and results are collected based on the result argument passed in simplug.spec when defining hooks.

Async hooks

It makes no big difference to define an async hook:

@simplug.spec
async def async_hook(arg):
    ...

# to supress warnings for sync implementation
@simplug.spec(warn_sync_impl_on_async=False)
async def async_hook(arg):
    ...

One can implement this hook in either an async or a sync way. However, when implementing it in a sync way, a warning will be raised. To suppress the warning, one can pass a False value of argument warn_sync_impl_on_async to simplug.spec.

To call the async hooks (simplug.hooks.async_hook(arg)), you will just need to call it like any other async functions (using asyncio.run, for example)

API

https://pwwang.github.io/simplug/

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

simplug-0.0.2.tar.gz (8.7 kB view details)

Uploaded Source

Built Distribution

simplug-0.0.2-py3-none-any.whl (8.3 kB view details)

Uploaded Python 3

File details

Details for the file simplug-0.0.2.tar.gz.

File metadata

  • Download URL: simplug-0.0.2.tar.gz
  • Upload date:
  • Size: 8.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.1.3 CPython/3.9.0 Linux/5.4.0-1026-azure

File hashes

Hashes for simplug-0.0.2.tar.gz
Algorithm Hash digest
SHA256 d46fdf269aca9f6be128c56d677c60c44d3d547ca764275b4e8d426aef740a26
MD5 a13f3dac017baa9f1a86f240c306fa5c
BLAKE2b-256 51ff857dcd0c5de52c0fd5b29dfcb101a24c8a8b4aa2a9d800ca3092edd304b3

See more details on using hashes here.

File details

Details for the file simplug-0.0.2-py3-none-any.whl.

File metadata

  • Download URL: simplug-0.0.2-py3-none-any.whl
  • Upload date:
  • Size: 8.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.1.3 CPython/3.9.0 Linux/5.4.0-1026-azure

File hashes

Hashes for simplug-0.0.2-py3-none-any.whl
Algorithm Hash digest
SHA256 32e70524d169167aa2496c8ddaf0a61c5796be688cbabd87da8909804c347127
MD5 9e2bb5fd026a515e96e619dcddbc7d4d
BLAKE2b-256 2d9421fb2b359d315dc4b681b3622b3942601bb4bf485161196f86edfa79d8f0

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page