- Allow users to set config values through environment variables.

- Fixes: https://github.com/Tautulli/Tautulli/issues/2309
This commit is contained in:
komuw 2025-04-29 19:16:37 +03:00
commit 27961e5282

View file

@ -487,6 +487,11 @@ class Config(object):
""" Cast any value in the config to the right type or use the default """ """ Cast any value in the config to the right type or use the default """
key, definition_type, section, ini_key, default = self._define(key) key, definition_type, section, ini_key, default = self._define(key)
self.check_section(section) self.check_section(section)
my_val = self._from_env(key, definition_type)
if my_val:
self._config[section][ini_key] = my_val
return my_val
try: try:
my_val = definition_type(self._config[section][ini_key]) my_val = definition_type(self._config[section][ini_key])
except Exception: except Exception:
@ -494,6 +499,15 @@ class Config(object):
self._config[section][ini_key] = my_val self._config[section][ini_key] = my_val
return my_val return my_val
def _from_env(self, key, definition_type):
""" Get key from environment variables, if it exists """
val = os.environ.get(key)
try:
if val:
return definition_type(val)
except Exception:
return None
def write(self): def write(self):
""" Make a copy of the stored config and write it to the configured file """ """ Make a copy of the stored config and write it to the configured file """
new_config = ConfigObj(encoding="UTF-8") new_config = ConfigObj(encoding="UTF-8")