config: add default_config_filename parameter

This commit is contained in:
Benjamin Renard 2023-12-14 21:25:00 +01:00
parent 07ab4490d2
commit 73795d27b8
Signed by: bn8
GPG key ID: 3E2E1CE1907115BC

View file

@ -666,6 +666,7 @@ class Config: # pylint: disable=too-many-instance-attributes
encoding=None, encoding=None,
config_file_env_variable=None, config_file_env_variable=None,
default_config_dirpath=None, default_config_dirpath=None,
default_config_filename=None,
): ):
self.appname = appname self.appname = appname
self.shortname = shortname self.shortname = shortname
@ -680,6 +681,7 @@ class Config: # pylint: disable=too-many-instance-attributes
self._filepath = None self._filepath = None
self.config_file_env_variable = config_file_env_variable self.config_file_env_variable = config_file_env_variable
self.default_config_dirpath = default_config_dirpath self.default_config_dirpath = default_config_dirpath
self.default_config_filename = default_config_filename
self._init_config_parser() self._init_config_parser()
def add_section(self, name, loaded_callback=None, **kwargs): def add_section(self, name, loaded_callback=None, **kwargs):
@ -1204,9 +1206,11 @@ class Config: # pylint: disable=too-many-instance-attributes
return self._filepath return self._filepath
if self.config_file_env_variable and os.environ.get(self.config_file_env_variable): if self.config_file_env_variable and os.environ.get(self.config_file_env_variable):
return os.environ.get(self.config_file_env_variable) return os.environ.get(self.config_file_env_variable)
return os.path.join( if self.default_config_filename:
self.config_dir, f"{self.shortname}.ini" if self.shortname else "config.ini" filename = self.default_config_filename
) else:
filename = f"{self.shortname}.ini" if self.shortname else "config.ini"
return os.path.join(self.config_dir, filename)
class ConfigurableObject: class ConfigurableObject: