merge branche "@"

This commit is contained in:
Goffi 2021-08-28 15:32:58 +02:00
commit fe502fc0fc

View file

@ -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 @@ def getConfig(config, section, name, default=None):
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)