Skip to content

Commit

Permalink
Allow config setting specs as module variables, too.
Browse files Browse the repository at this point in the history
Support reading config setting specs from module (#237)

* Support reading config setting specs from module

* Add additional documentation (review suggestion)

* Amend changelog entry
  • Loading branch information
Flix6x committed Nov 13, 2021
1 parent 65a94b2 commit 1da49a9
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 4 deletions.
2 changes: 1 addition & 1 deletion documentation/changelog.rst
Expand Up @@ -16,7 +16,7 @@ Infrastructure / Support
----------------------
* Account-based authorization, incl. new decorators for endpoints [see `PR #210 <http://www.github.com/SeitaBV/flexmeasures/pull/210>`_]
* Improve data specification for forecasting models using timely-beliefs data [see `PR #154 <http://www.github.com/SeitaBV/flexmeasures/pull/154>`_]
* Allow plugins to register their custom config settings, so that FlexMeasures can check whether they are set up correctly [see `PR #230 <http://www.github.com/SeitaBV/flexmeasures/pull/230>`_]
* Allow plugins to register their custom config settings, so that FlexMeasures can check whether they are set up correctly [see `PR #230 <http://www.github.com/SeitaBV/flexmeasures/pull/230>`_ and `PR #237 <http://www.github.com/SeitaBV/flexmeasures/pull/237>`_]
* Added sensor method to obtain just its latest state (excl. forecasts) [see `PR #235 <http://www.github.com/SeitaBV/flexmeasures/pull/235>`_]


Expand Down
27 changes: 24 additions & 3 deletions documentation/dev/plugins.rst
Expand Up @@ -159,7 +159,7 @@ Config settings can be registered by setting the (optional) ``__settings__`` att

.. code-block:: python
__settings__ = [
__settings__ = {
"MY_PLUGIN_URL": {
"description": "URL used by my plugin for x.",
"level": "error",
Expand All @@ -174,9 +174,30 @@ Config settings can be registered by setting the (optional) ``__settings__`` att
"description": "Color used to override the default plugin color.",
"level": "info",
},
]
}
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:
Alternatively, use ``from my_plugin import __settings__`` in your plugin module, and create ``__settings__.py`` with:

.. code-block:: python
MY_PLUGIN_URL = {
"description": "URL used by my plugin for x.",
"level": "error",
}
MY_PLUGIN_TOKEN = {
"description": "Token used by my plugin for y.",
"level": "warning",
"message_if_missing": "Without this token, my plugin will not do y.",
"parse_as": str,
}
MY_PLUGIN_COLOR = {
"description": "Color used to override the default plugin color.",
"level": "info",
}
Finally, 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. An example:

.. code-block:: python
Expand Down
9 changes: 9 additions & 0 deletions flexmeasures/utils/plugin_utils.py
Expand Up @@ -2,6 +2,7 @@
import os
import sys
from importlib.abc import Loader
from types import ModuleType
from typing import Dict

import sentry_sdk
Expand Down Expand Up @@ -123,6 +124,14 @@ def check_config_settings(app, settings: Dict[str, dict]):
}
"""

# Check config settings are in dict form, after possibly converting them from module variables
if isinstance(settings, ModuleType):
settings = {
setting: settings.__dict__[setting]
for setting in dir(settings)
if not setting.startswith("__")
}
assert isinstance(settings, dict), f"{type(settings)} should be a dict"
for setting_name, setting_fields in settings.items():
assert isinstance(setting_fields, dict), f"{setting_name} should be a dict"
Expand Down

0 comments on commit 1da49a9

Please sign in to comment.