def _config_sources(): return [ EnvironmentConfigSource(), IniFileConfigSource("mwdb.ini", must_exist=False), IniFileConfigSource(os.path.expanduser("~/.mwdb-core/mwdb.ini"), must_exist=False), IniFileConfigSource("/etc/mwdb-core/mwdb.ini", must_exist=False), ]
def read(self): if self.dict_config is not None: self.add_source(DictConfigSource(self.dict_config)) self.add_source(EnvironmentConfigSource(prefix=self.prefix)) if self.config_path is not None: self.add_source(IniFileConfigSource(self.config_path)) return super().read()
def test_ini_file_config_source(encoding): with tempfile.TemporaryDirectory() as td: file_name = os.path.join(td, 'config.cfg') with open(file_name, 'w', encoding=encoding) as f: f.write(""" [s] a = 1 B = 2 """) source = IniFileConfigSource(file_name, encoding=encoding) do_assertions(source, f"<IniFileConfigSource(filename='{file_name}')>")
def test_ini_file_config_source_no_file_must_exist(): with pytest.raises(FileNotFoundError): IniFileConfigSource('config-this-file-definitely-does-not-exist.cfg', must_exist=True)
def test_ini_file_config_source_no_file_existence_optional(): source = IniFileConfigSource( 'config-this-file-definitely-does-not-exist.cfg', must_exist=False) v = source.get_config_value('s', 'a') assert v is None