Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Check plugin settings #230

Merged
merged 9 commits into from Nov 10, 2021
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
22 changes: 19 additions & 3 deletions documentation/dev/plugins.rst
Expand Up @@ -150,12 +150,28 @@ This will find `css/styles.css` if you add that folder and file to your Blueprin
.. note:: This styling will only apply to the pages defined in your plugin (to pages based on your own base template). To apply a styling to all other pages which are served by FlexMeasures, consider using the config setting :ref:`extra-css-config`.


Adding config options
Adding config settings
^^^^^^^^^^^^^^^^^^^^^^^^

You might want to override some FlexMEasures configuration settings from within your plugin. Some examples for possible settings are named on this page, e.g. the custom style (see above) or custom logo (see below). There is a `record_once` function on Blueprints which can help with this. Example:
FlexMeasures can be made to check for any custom config settings your plugin is using.
Flix6x marked this conversation as resolved.
Show resolved Hide resolved
Required and optional config options can be registered by setting the ``__settings__`` attribute on your plugin module:

.. code-block: python
.. code-block:: python

__settings__ = {
nhoening marked this conversation as resolved.
Show resolved Hide resolved
"required": (
"MY_PLUGIN_SETTING_A",
"MY_PLUGIN_SETTING_B",
),
"optional": (
"MY_PLUGIN_SETTING_C",
"MY_PLUGIN_SETTING_D",
),
}

You might want to override some FlexMeasures configuration settings from within your plugin. Some examples for possible settings are named on this page, e.g. the custom style (see above) or custom logo (see below). There is a `record_once` function on Blueprints which can help with this. Example:

.. code-block:: python

@our_client_bp.record_once
def record_logo_path(setup_state):
Expand Down
34 changes: 34 additions & 0 deletions flexmeasures/utils/app_utils.py
Expand Up @@ -2,6 +2,7 @@
import os
import sys
import importlib.util
import warnings
from importlib.abc import Loader

import click
Expand All @@ -16,6 +17,14 @@
from flexmeasures.app import create as create_app


class ConfigurationWarning(Warning):
pass


class ConfigurationError(ValueError):
pass


@click.group(cls=FlaskGroup, create_app=create_app)
@with_appcontext
def flexmeasures_cli():
Expand Down Expand Up @@ -246,6 +255,8 @@ def register_plugins(app: Flask):
continue

plugin_version = getattr(module, "__version__", "0.1")
plugin_settings = getattr(module, "__settings__", {})
check_config_settings(app, plugin_settings)

# Look for blueprints in the plugin's main __init__ module and register them
plugin_blueprints = [
Expand All @@ -265,3 +276,26 @@ def register_plugins(app: Flask):
app.config["LOADED_PLUGINS"][plugin_name] = plugin_version
app.logger.info(f"Loaded plugins: {app.config['LOADED_PLUGINS']}")
sentry_sdk.set_context("plugins", app.config.get("LOADED_PLUGINS", {}))


def check_config_settings(app, settings: dict):
"""Make sure expected config settings exist."""
missing_optional_config_settings = []
if "optional" in settings:
for setting in settings["optional"]:
if app.config.get(setting) is None:
missing_optional_config_settings.append(setting)
if missing_optional_config_settings:
warnings.warn(
Flix6x marked this conversation as resolved.
Show resolved Hide resolved
f"Missing optional config setting(s): {missing_optional_config_settings}",
category=ConfigurationWarning,
)
if "required" in settings:
missing_required_config_settings = []
for setting in settings["required"]:
if app.config.get(setting) is None:
missing_required_config_settings.append(setting)
if missing_required_config_settings:
raise ConfigurationError(
f"Missing required config setting(s): {missing_required_config_settings}"
)