Skip to content

Enabling and Disabling

The host, and through it the operator, decides which installed plugins run. A plugin can also decline on its own through should_load() (Plugin Classes and Functions); this page is about the host's decision.

From configuration

Pass the names to leave out:

from pluxeo import PluginDisabledError, PluginManager

pm = PluginManager("acme.renderers", disabled={"charts"})

A disabled plugin is still discovered, so it appears in pm.names() and pm.state(name). But load_all() skips it, and pm.load("charts") raises PluginDisabledError.

At runtime

from pluxeo import Plugin, PluginManager, PluginState, hookimpl, hookspec


class Spec:
    @hookspec
    def render_block(self, block): ...


class Charts(Plugin):
    namespace = "acme.renderers"
    name = "charts"

    @hookimpl
    def render_block(self, block):
        return "chart"


pm = PluginManager("acme.renderers", hookspec=Spec, discover=False)
pm.register(Charts)
pm.load_all()

pm.disable("charts")
assert pm.hook.render_block(block={}) == []
assert pm.state("charts") is PluginState.DISABLED

pm.enable("charts")
assert pm.hook.render_block(block={}) == ["chart"]
assert pm.is_enabled("charts")

disable() takes the plugin's hooks out of dispatch immediately. The instance stays alive, and enable() puts the hooks back without running load() again: disabling silences a plugin, it does not destroy it. To run its cleanup, use pm.unload(name) (Cleanup and Dependencies).

Disabling a plugin that is not loaded yet prevents it from loading until it is enabled.

A policy of your own

For anything beyond a fixed list, pass an object with an is_enabled(namespace, name) method:

from pluxeo import PluginManager

ACTIVE = {"acme.renderers": ["quotes"]}


class ConfigEnablement:
    def is_enabled(self, namespace: str, name: str) -> bool:
        return name in ACTIVE.get(namespace, [])


pm = PluginManager("acme.renderers", enablement=ConfigEnablement())

When enablement= is given, disabled= is ignored.

The policy is asked every time a plugin is loaded and every time pm.is_enabled(name) is called, so a changed setting applies to the next load. It does not take an already loaded plugin out of dispatch when its answer changes; call pm.disable(name) for that. A plugin the host disabled with disable() stays disabled whatever the policy says, until enable().

Enablement and the hooks

The rule from Concepts holds throughout: a plugin's hooks are called only while it is loaded and enabled.