Update handling Tautulli environment variables

This commit is contained in:
JonnyWong16 2025-05-10 18:36:36 -07:00
parent 4610bf3769
commit 5ef9911049
No known key found for this signature in database
GPG key ID: B1F1F9807184697A

View file

@ -479,35 +479,45 @@ class Config(object):
""" Check if INI section exists, if not create it """ """ Check if INI section exists, if not create it """
if section not in self._config: if section not in self._config:
self._config[section] = {} self._config[section] = {}
return True
else:
return False
def check_setting(self, key): def check_setting(self, name):
""" Cast any value in the config to the right type or use the default """ """ Check if INI key exists, if not create it """
key, definition_type, section, ini_key, default = self._define(key) key, definition_type, section, ini_key, default = self._define(name)
self.check_section(section) self.check_section(section)
my_val = self._from_env(key, definition_type) value = self._config[section].get(ini_key, default)
if my_val: self._config[section][ini_key] = self._cast_setting(definition_type, value, default)
self._config[section][ini_key] = my_val
return my_val
try: def get_setting(self, name):
my_val = definition_type(self._config[section][ini_key]) """ Get the value of a setting, either from the config file or environment variable """
except Exception: key, definition_type, section, ini_key, default = self._define(name)
my_val = definition_type(default) # Check if the key is in the environment variables
self._config[section][ini_key] = my_val value = self._from_env(key)
return my_val if not value:
# If not, check if the key is in the config file
value = self._config[section].get(ini_key, default)
return self._cast_setting(definition_type, value, default)
def _from_env(self, key, definition_type): def set_setting(self, name, value):
""" Set the value of a setting in the config file """
key, definition_type, section, ini_key, default = self._define(name)
# Check if the key is in the environment variables
env_value = self._from_env(key)
if not env_value:
# If not, set the value in the config file
self._config[section][ini_key] = self._cast_setting(definition_type, value, default)
return self._config[section][ini_key]
def _from_env(self, key):
""" Get key from environment variables, if it exists """ """ Get key from environment variables, if it exists """
env_key = "TAUTULLI_" + key env_key = f"TAUTULLI_{key}"
val = os.environ.get(env_key) return os.environ.get(env_key)
def _cast_setting(self, definition_type, value, default=None):
""" Cast the value to the correct type, or use the default if it fails """
try: try:
if val: return definition_type(value)
return definition_type(val)
except Exception: except Exception:
return None return definition_type(default)
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 """
@ -548,7 +558,7 @@ class Config(object):
if not re.match(r'[A-Z0-9_]+$', name): if not re.match(r'[A-Z0-9_]+$', name):
return super(Config, self).__getattr__(name) return super(Config, self).__getattr__(name)
else: else:
return self.check_setting(name) return self.get_setting(name)
def __setattr__(self, name, value): def __setattr__(self, name, value):
""" """
@ -559,9 +569,7 @@ class Config(object):
super(Config, self).__setattr__(name, value) super(Config, self).__setattr__(name, value)
return value return value
else: else:
key, definition_type, section, ini_key, default = self._define(name) return self.set_setting(name, value)
self._config[section][ini_key] = definition_type(value)
return self._config[section][ini_key]
def __delattr__(self, name): def __delattr__(self, name):
""" """
@ -578,8 +586,7 @@ class Config(object):
Given a big bunch of key value pairs, apply them to the ini. Given a big bunch of key value pairs, apply them to the ini.
""" """
for name, value in kwargs.items(): for name, value in kwargs.items():
key, definition_type, section, ini_key, default = self._define(name) self.__setattr__(name.upper(), value)
self._config[section][ini_key] = definition_type(value)
def _upgrade(self): def _upgrade(self):
""" """