Mercurial > libervia-backend
diff sat/tools/config.py @ 3634:3c7a64d6f49f
bridge: bridge can now be set using environment variable:
The `LIBERVIA_BRIDGE_NAME` environment variable can now be used to specify the bridge to
use. If set and different from empty string, the environment has precedence over config
file value.
For `pb` bridge, the following environment variable can also be used:
- LIBERVIA_BRIDGE_PB_CONNECTION_TYPE
- LIBERVIA_BRIDGE_PB_HOST
- LIBERVIA_BRIDGE_PB_PORT
author | Goffi <goffi@goffi.org> |
---|---|
date | Sat, 28 Aug 2021 15:26:02 +0200 |
parents | acb28399480f |
children | 524856bd7b19 |
line wrap: on
line diff
--- a/sat/tools/config.py Fri Aug 27 14:59:47 2021 +0200 +++ b/sat/tools/config.py Sat Aug 28 15:26:02 2021 +0200 @@ -23,6 +23,7 @@ import os import csv import json +from typing import Any from configparser import ConfigParser, DEFAULTSECT, NoOptionError, NoSectionError from xdg import BaseDirectory from sat.core.log import getLogger @@ -142,3 +143,29 @@ except ValueError as e: raise exceptions.ParsingError("Error while parsing data: {}".format(e)) return value + + +def getConf( + conf: ConfigParser, + prefix: str, + section: str, + name: str, + default: Any +) -> Any: + """Get configuration value from environment or config file + + @param str: prefix to use for the varilable name (see `name` below) + @param section: config section to use + @param name: unsuffixed name. + For environment variable, `LIBERVIA_<prefix>_` will be prefixed (and name + will be set to uppercase). + For config file, `<prefix>_` will be prefixed (and DEFAULT section will be + used). + Environment variable has priority over config values. If Environment variable + is set but empty string, config value will be used. + @param default: default value to use if varilable is set neither in environment, + nor in config + """ + # XXX: This is a temporary method until parameters are refactored + value = os.getenv(f"LIBERVIA_{prefix}_{name}".upper()) + return value or getConfig(conf, section, f"{prefix}_{name}", default)