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

Support reading config setting specs from module #237

Merged
merged 3 commits into from Nov 13, 2021
Merged
Show file tree
Hide file tree
Changes from all 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
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:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Technically, you can have your settings in a module with any name you want and import them with:

from my_plugin import whatever_settings as __settings__

But a Python programmer should understand that.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

True. I also tried having FlexMeasures look specifically for a __settings__.py in the plugin module, but I couldn't figure out how to do that. That would have saved the import statement, while requiring an explicit filename.


.. 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