comparison 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
comparison
equal deleted inserted replaced
3632:7acf19bdca2f 3634:3c7a64d6f49f
21 """ Configuration related useful methods """ 21 """ Configuration related useful methods """
22 22
23 import os 23 import os
24 import csv 24 import csv
25 import json 25 import json
26 from typing import Any
26 from configparser import ConfigParser, DEFAULTSECT, NoOptionError, NoSectionError 27 from configparser import ConfigParser, DEFAULTSECT, NoOptionError, NoSectionError
27 from xdg import BaseDirectory 28 from xdg import BaseDirectory
28 from sat.core.log import getLogger 29 from sat.core.log import getLogger
29 from sat.core.constants import Const as C 30 from sat.core.constants import Const as C
30 from sat.core.i18n import _ 31 from sat.core.i18n import _
140 try: 141 try:
141 value = json.loads(value) 142 value = json.loads(value)
142 except ValueError as e: 143 except ValueError as e:
143 raise exceptions.ParsingError("Error while parsing data: {}".format(e)) 144 raise exceptions.ParsingError("Error while parsing data: {}".format(e))
144 return value 145 return value
146
147
148 def getConf(
149 conf: ConfigParser,
150 prefix: str,
151 section: str,
152 name: str,
153 default: Any
154 ) -> Any:
155 """Get configuration value from environment or config file
156
157 @param str: prefix to use for the varilable name (see `name` below)
158 @param section: config section to use
159 @param name: unsuffixed name.
160 For environment variable, `LIBERVIA_<prefix>_` will be prefixed (and name
161 will be set to uppercase).
162 For config file, `<prefix>_` will be prefixed (and DEFAULT section will be
163 used).
164 Environment variable has priority over config values. If Environment variable
165 is set but empty string, config value will be used.
166 @param default: default value to use if varilable is set neither in environment,
167 nor in config
168 """
169 # XXX: This is a temporary method until parameters are refactored
170 value = os.getenv(f"LIBERVIA_{prefix}_{name}".upper())
171 return value or getConfig(conf, section, f"{prefix}_{name}", default)