flexmeasures.utils.plugin_utils
Utils for registering FlexMeasures plugins
Functions
- flexmeasures.utils.plugin_utils.check_config_settings(app, settings: dict[str, dict])
Make sure expected config settings exist.
For example:
- settings = {
- “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”: “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”,
},
}
- flexmeasures.utils.plugin_utils.find_importable_module(pkg_name: str) ModuleSpec | None
Find the spec of an importable module, if there is one.
A single-file module counts, just like a package:
register_pluginsimports either by name. Namespace packages do not. A folder without an__init__.pyis importable, but accepting it here would shadow the clearer error that the file path branch ofregister_pluginsreports for such a folder.
- flexmeasures.utils.plugin_utils.is_written_as_path(plugin: str) bool
Whether this FLEXMEASURES_PLUGINS entry is spelled out as a file path.
A bare name like
my_pluginis not: it may well name an installed package.
- flexmeasures.utils.plugin_utils.log_missing_config_setting(app, setting_name: str, setting_fields: dict)
Log a message for this missing config setting.
The logging level is taken from the ‘level’ key. If missing, we default to error. If present, we also log the ‘description’ and the ‘message_if_missing’ keys.
- flexmeasures.utils.plugin_utils.log_wrong_type_for_config_setting(app, setting_name: str, setting_fields: dict, setting_type: type)
Log a message for this config setting that has the wrong type.
- flexmeasures.utils.plugin_utils.register_plugins(app: Flask)
Register FlexMeasures plugins as Blueprints. This is configured by the config setting FLEXMEASURES_PLUGINS.
Assumptions: - a setting EITHER points to a plugin folder containing an __init__.py file
OR it is the name of an installed module, which can be imported.
each plugin defines at least one Blueprint object. These will be registered with the Flask app, so their functionality (e.g. routes) becomes available.
If you load a plugin via a file path, we’ll refer to the plugin with the name of your plugin folder (last part of the path).
An entry that is not spelled out as a file path is imported by name, so that normal import resolution along
sys.pathapplies, rather than loaded from the folder of that name in the working directory. An installed plugin therefore wins from such a folder, unless the working directory itself comes first onsys.path. To load a folder on purpose, spell out its path (e.g../my_plugin).