Plugin Management

FlaskBB provides a couple of helpers for helping with plugin management.

Plugin Registry

The plugin registry holds all available plugins. It shows the plugin’s status whether it is enabled or disabled, installable or installed. The registry also holds a reference to the plugin’s instance, provides an interface to access the plugins metadata and stores its settings.

You can query it like any SQLAlchemy Model:

plugin = PluginRegistry.get_by(name="portal")
class flaskbb.plugins.models.PluginRegistry(name)[source]
property info

Returns some information about the plugin.

property is_installable: bool

A plugin is installable if it registered a SettingGroup with at least one setting to configure.

property is_installed: bool

A plugin is installed once every one of its settings has a row in the settings table.

property is_updatable: bool

A plugin is installed once every one of its settings has a row in the settings table.

property has_stored_settings: bool

Unlike is_installed this doesn’t need the plugin’s SettingGroup, so it also works for disabled plugins.

property settings: dict[str, Any]

This plugin’s current setting values, prefixed with this plugin’s group_key (e.g. {“PORTAL_FORUM_IDS”: […]})

classmethod get_installed_plugins()[source]

Plugins that have registered settings and are enabled.

get_settings_form()[source]

Generates a settings form based on this plugin’s SettingGroup - same build_form() used for core settings groups.

get_setting_diff() → SettingsDiff | None[source]

Compares this plugin’s currently registered settings against what’s actually stored in the DB - read-only, makes no changes.

Returns None if the plugin has no registered SettingGroup at all (nothing to compare). Use this to show an admin what a settings upgrade would actually change before applying it.

needs_setting_upgrade() → bool[source]

True if this plugin’s stored settings differ from what its currently loaded code registers - i.e. a new version added or removed a setting since the DB was last synced.

upgrade_settings() → None[source]

Syncs this plugin’s settings rows to match its currently registered SettingGroup: adds rows for settings that are new, removes rows for settings that no longer exist.

update_settings(form_data: dict[str, Any]) → None[source]

Updates the given settings of the plugin.

add_settings(force: bool = False) → None[source]

Adds the settings for this plugin.

Parameters:

force – if True, existing settings for this plugin’s settings are deleted and re-installed from their defaults first. Defaults to False, which only installs settings that don’t exist yet and leaves any existing values untouched.

remove_settings() → None[source]

Deletes all of this plugin’s settings from the DB - called on plugin uninstall. Delegates straight to Setting.remove_group() rather than fetching self._group first, since uninstalling should succeed even if the plugin’s SettingGroup is no longer registered (e.g. the plugin package itself was already removed).

remove_permissions() → None[source]

Deletes every grant of this plugin’s permissions - called on plugin uninstall. Matches the stored keys by their prefix, so it works after the plugin package is gone and its definitions are no longer registered.

Plugin Manager

FlaskBB overrides the PluginManager from pluggy to provide some additional functionality like accessing the information stored in a pyproject.toml file. The plugin manager will only list the currently enabled plugins and can be used to directly access the plugins instance by its name.

Accessing a plugins instance is as easy as:

plugin_instance = current_app.pluggy.get_plugin(name)
class flaskbb.plugins.manager.FlaskBBPluginManager(project_name: str)[source]

Overwrites pluggy.PluginManager to add FlaskBB specific stuff.

register(plugin: _Plugin, name: str | None = None, internal: bool = False)[source]

Register a plugin and return its canonical name or None if the name is blocked from registering. Raise a ValueError if the plugin is already registered.

unregister(plugin: _Plugin | None = None, name: str | None = None)[source]

Unregister a plugin object and all its contained hook implementations from internal data structures.

set_blocked(name: str)[source]

Block registrations of the given name, unregister if already registered.

is_blocked(name: str)[source]

Return True if the name blockss registering plugins of that name.

get_plugin(name: str)[source]

Return a plugin or None for the given name.

get_name(plugin: _Plugin)[source]

Return name for registered plugin or None if not registered.

load_setuptools_entrypoints(group: str, name: str | None = None) → int[source]

Load modules from querying the specified setuptools entrypoint name. Return the number of loaded plugins.

get_metadata(name: str)[source]

Returns the metadata for a given name.

add_hookcall_monitoring(before: Callable[[str, Sequence[HookImpl], Mapping[str, Any]], None], after: Callable[[Result[Any], str, Sequence[HookImpl], Mapping[str, Any]], None]) → Callable[[], None]

Add before/after tracing functions for all hooks.

Returns an undo function which, when called, removes the added tracers.

before(hook_name, hook_impls, kwargs) will be called ahead of all hook calls and receive a hookcaller instance, a list of HookImpl instances and the keyword arguments for the hook call.

after(outcome, hook_name, hook_impls, kwargs) receives the same arguments as before but also a Result object which represents the result of the overall hook call.

add_hookspecs(module_or_class: ModuleType | type) → None

Add new hook specifications defined in the given module_or_class.

Functions are recognized as hook specifications if they have been decorated with a matching HookspecMarker.

check_pending() → None

Verify that all hooks which have not been verified against a hook specification are optional, otherwise raise PluginValidationError.

enable_tracing() → Callable[[], None]

Enable tracing of hook calls.

Returns an undo function which, when called, removes the added tracing.

get_canonical_name(plugin: object) → str

Return a canonical name for a plugin object.

Note that a plugin may be registered under a different name specified by the caller of register(plugin, name). To obtain the name of a registered plugin use get_name(plugin) instead.

get_hookcallers(plugin: object) → list[HookCaller] | None

Get all hook callers for the specified plugin.

Returns:

The hook callers, or None if plugin is not registered in this plugin manager.

get_plugins() → set[Any]

Return a set of all registered plugin objects.

has_plugin(name: str) → bool

Return whether a plugin with the given name is registered.

is_registered(plugin: object) → bool

Return whether the plugin is already registered.

list_name()[source]

Returns only the enabled plugin names.

list_name_plugin() → list[tuple[str, object]]

Return a list of (name, plugin) pairs for all registered plugins.

list_plugin_distinfo() → list[tuple[object, DistFacade]]

Return a list of (plugin, distinfo) pairs for all setuptools-registered plugins.

Note

The distinfo objects are wrapped with DistFacade for backward compatibility with the legacy pkg_resources API. Prefer list_plugin_distributions() for raw importlib.metadata.Distribution objects.

list_plugin_distributions() → list[tuple[_Plugin, importlib.metadata.Distribution]]

Return a list of (plugin, distribution) pairs for all plugins loaded via entry points.

Added in version 1.7.

parse_hookimpl_opts(plugin: object, name: str) → HookimplOpts | None

Try to obtain a hook implementation from an item with the given name in the given plugin which is being searched for hook impls.

Returns:

The parsed hookimpl options, or None to skip the given item.

This method can be overridden by PluginManager subclasses to customize how hook implementation are picked up. By default, returns the options for items decorated with HookimplMarker.

Discovery uses inspect.getattr_static() so properties and other descriptors are not executed. Only functions, classmethods, staticmethods, and bound methods are considered.

parse_hookspec_opts(module_or_class: ModuleType | type, name: str) → HookspecOpts | None

Try to obtain a hook specification from an item with the given name in the given module or class which is being searched for hook specs.

Returns:

The parsed hookspec options for defining a hook, or None to skip the given item.

This method can be overridden by PluginManager subclasses to customize how hook specifications are picked up. By default, returns the options for items decorated with HookspecMarker.

Discovery uses inspect.getattr_static() so properties and other descriptors are not executed. Only functions, classmethods, staticmethods, and bound methods are considered.

subset_hook_caller(name: str, remove_plugins: Iterable[object]) → HookCaller

Return a proxy HookCaller for the named hook which calls all registered plugins except the ones from remove_plugins.

unblock(name: str) → bool

Unblocks a name.

Returns whether the name was actually blocked.

project_name

The project name.

hook

The “hook relay”, used to call a hook on all registered plugins. See Calling hooks.

trace

The tracing entry point. See Built-in tracing.

list_internal_name_plugin()[source]

Returns a list of internal name/plugin pairs.

list_plugin_metadata()[source]

Returns the metadata for all plugins

list_disabled_plugins()[source]

Returns the entry points of the disabled plugins. They are never imported.

get_disabled_plugins()[source]

Returns a list with disabled plugins.

get_internal_plugins()[source]

Returns a set of registered internal plugins.

get_external_plugins() → set[_Plugin][source]

Returns a set of registered external plugins.