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_installedthis 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”: […]})
- 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.
- 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).
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.PluginManagerto 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.
- 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.
- 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 asbeforebut also aResultobject 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 useget_name(plugin)instead.
- get_hookcallers(plugin: object) list[HookCaller] | None¶
Get all hook callers for the specified plugin.
- Returns:
The hook callers, or
Noneifpluginis not registered in this plugin manager.
- 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
DistFacadefor backward compatibility with the legacy pkg_resources API. Preferlist_plugin_distributions()for rawimportlib.metadata.Distributionobjects.
- 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
PluginManagersubclasses to customize how hook implementation are picked up. By default, returns the options for items decorated withHookimplMarker.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
PluginManagersubclasses to customize how hook specifications are picked up. By default, returns the options for items decorated withHookspecMarker.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
HookCallerfor the named hook which calls all registered plugins except the ones from remove_plugins.
- 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.