flexmeasures.utils.config_utils

Reading in configuration

Functions

flexmeasures.utils.config_utils.are_required_settings_complete(app) → bool

Check if all settings we expect are not None. Return False if they are not. Printout helpful advice.

flexmeasures.utils.config_utils.check_app_env(env: str | None)
flexmeasures.utils.config_utils.configure_logging()

Configure and register logging

flexmeasures.utils.config_utils.find_flexmeasures_cfg() → str | None

Try to find a flexmeasures.cfg file in the home or instance directories. Return the path if found, else None.

flexmeasures.utils.config_utils.get_config_warnings(app) → tuple[list[str], list[str]]

return missing settings and the warnings for them.

flexmeasures.utils.config_utils.get_configuration_keys(app) → list[str]

Collect all members of DefaultConfig who are not in-built fields or callables.

flexmeasures.utils.config_utils.get_flexmeasures_env(app, cfg_location) → str | None

Determine which flexmeasures_env should be used, trying various ways in decreasing importance.

flexmeasures.utils.config_utils.normalize_security_durations(app: Flask) → None

Hand Flask-Security each duration setting in the form its version reads.

Flask-Security builds a timedelta from “<amount> <unit>”, using the unit as a timedelta keyword, so it reads “7 days”, but not “1 week” or an ISO 8601 duration. As of 5.9, it does so at startup and prefers a timedelta, while 5.8 (which Python 3.10 still gets) only reads a string, so it gets the duration in seconds.

flexmeasures.utils.config_utils.normalize_trusted_hosts(app: Flask) → None

Turn a comma-separated TRUSTED_HOSTS string into a list, and an empty one into None.

Werkzeug matches a plain string as a single host, so “a.example.com,b.example.com” would silently match nothing. It also trusts every host when the list is empty, just as when the setting is unset, so an empty value (e.g. TRUSTED_HOSTS=”” in a container env file) becomes None. Otherwise it would disable host validation while passing as configured, suppressing the warning.

flexmeasures.utils.config_utils.parse_bool_env(value: str) → bool

Parse a boolean setting from a (case-insensitive) environment variable string.

“true”, “1”, “yes” and “on” count as True; anything else is False. Surrounding whitespace is ignored.

flexmeasures.utils.config_utils.parse_duration_setting(setting: str, value: str | timedelta) → timedelta

Parse a duration given in a setting, such as “1 week”, “7 days” or an ISO 8601 duration like “P1W”.

Only units of a fixed length are accepted, so months and years are not.

flexmeasures.utils.config_utils.pin_database_driver(app: Flask) → None

Connect through psycopg 3 if the database URI names no driver.

SQLAlchemy 2.0 connects a plain postgresql:// URI through psycopg2, and SQLAlchemy 2.1 through psycopg 3, so without naming the driver, which one we get would depend on the SQLAlchemy version installed. An explicitly chosen driver (e.g. postgresql+psycopg2://) is left alone.

flexmeasures.utils.config_utils.read_config(app: Flask, custom_path_to_config: str | None)

Read configuration from various expected sources, complain if not setup correctly.

flexmeasures.utils.config_utils.read_custom_config(app: Flask, suggested_path_to_config, path_to_config_home, path_to_config_instance) → str

Read in a custom config file and env vars. For the config, there are two fallback options, tried in a specific order: If no custom path is suggested, we’ll try the path in the home dir first, then in the instance dir.

Return the path to the config file.

flexmeasures.utils.config_utils.read_env_vars(app: Flask)

Read in what we support as environment settings. At the moment, these are: - All required and warnable variables - Logging and debugging settings - access tokens - plugins (handled in plugin utils) - json compactness - two-factor authentication (SECURITY_TOTP_SECRETS is handled in app_utils.set_totp_secrets)

Settings whose default is a boolean are parsed as booleans (env values are strings, and e.g. “False” is truthy).