def test_register_duplicate_provider(self): manager = ProviderManager() manager.register(EnvironmentProvider("foo")) self.assertIn("environment", manager.ordering) self.assertTrue(hasattr(manager, "environment")) with self.assertLogs(level="ERROR"): manager.register(EnvironmentProvider("foo")) self.assertEqual(len(manager.ordering), 1)
def __init__( self, name: str, defaults: Dict = None, providers: Optional[List[ConfigProviderBase]] = None, ) -> None: """ The configuration registry holds configuration data from different sources and reconciles it for retrieval. If the defaults dict is provided, it will be used to seed the default configuration values for the registry, equivalent to calling set_default() for each configuration key in defaults. :param name: namespace for configuration registry :param defaults: default configuration values :param providers: providers to add to the registry """ if not defaults: defaults = {} self.name = name self.values = {"super": {}, "defaults": defaults} if providers is not None: self.providers = ProviderManager(providers=providers) else: self.providers = ProviderManager() path = [os.path.join("/etc", name)] if home := os.getenv("HOME"): path.insert(0, os.path.join(home, ".local", "etc", name)) self.providers.register(FilesystemProvider(path, name)) self.providers.register(EnvironmentProvider(name))
def test_providers_env_extra(self): cf = ConfigurationRegistry("cfitall") cf.providers.register( EnvironmentProvider("extra", provider_name="env_extra")) os.environ["CFITALL__STUFF"] = "cfitall_stuff" os.environ["EXTRA__STUFF"] = "extra_stuff" self.assertEqual(cf.get("stuff"), "extra_stuff")
def test_deregister_provider(self): manager = ProviderManager() manager.register(EnvironmentProvider("foo")) self.assertIn("environment", manager.ordering) self.assertTrue(hasattr(manager, "environment")) manager.deregister("environment") self.assertNotIn("environment", manager.ordering) self.assertFalse(hasattr(manager, "environment"))
def test_providers_env_only(self): cf = ConfigurationRegistry("cfitall", providers=[EnvironmentProvider("cfitall")]) with self.assertRaises(KeyError): _ = cf.values["filesystem"] with self.assertRaises(AttributeError): _ = cf.providers.filesystem self.assertEqual(cf.providers.ordering, ["environment"]) self.assertEqual(cf.get("global.name"), "cfitall") self.assertIsNone(cf.get("foo.bar"))
def test_init_with_providers(self): providers = [ FilesystemProvider(["/etc/foo"], "foo"), EnvironmentProvider("foo"), ] manager = ProviderManager(providers=providers) self.assertEqual(len(manager.ordering), 2) self.assertEqual("filesystem", manager.ordering[0]) self.assertEqual("environment", manager.ordering[1]) self.assertTrue(hasattr(manager, "environment")) self.assertTrue(hasattr(manager, "filesystem"))
def test_dict(self): provider = EnvironmentProvider("cfitall") self.assertEqual( { "global": { "name": "cfitall", "path": ["/Users/wryfi", "/Users/wryfi/tmp"], }, "foo": { "bang": "WHAMMY!" }, "magic": { "enable": True }, "bool_list": [True, False, False, True], "space_list": ["one two \t\t three \t four five six"], }, provider.dict, )
def test_list_trailing_comma(self): os.environ["CFITALL__EXTRA_COMMAS"] = "[,onething,,twothing,]" provider = EnvironmentProvider("cfitall") self.assertEqual(provider.dict["extra_commas"], ["onething", "twothing"])
def test_cast_bool_disabled(self): provider = EnvironmentProvider("cfitall", cast_bool=False) self.assertEqual("true", provider.dict["magic"]["enable"])
def test_cast_bool(self): provider = EnvironmentProvider("cfitall") self.assertTrue(provider.dict["magic"]["enable"])
def test_cast_bool_disabled_list(self): provider = EnvironmentProvider("cfitall", cast_bool=False) self.assertEqual(["true", "false", "false", "true"], provider.dict["bool_list"])
def test_cast_bool_list(self): provider = EnvironmentProvider("cfitall") self.assertEqual([True, False, False, True], provider.dict["bool_list"])
def test_get(self): envprovider = EnvironmentProvider("foo") manager = ProviderManager(providers=[envprovider]) self.assertEqual(manager.get("environment"), envprovider)
def test_list_whitespace(self): provider = EnvironmentProvider("cfitall", value_separator=r"\s+") self.assertEqual(["one", "two", "three", "four", "five", "six"], provider.dict["space_list"])
def test_update(self): provider = EnvironmentProvider("cfitall") self.assertTrue(provider.update())
def test_read_variable(self): provider = EnvironmentProvider("cfitall") self.assertEqual(provider.dict["global"]["name"], "cfitall") self.assertEqual(provider.dict["global"]["path"], ["/Users/wryfi", "/Users/wryfi/tmp"]) self.assertEqual(provider.dict["foo"]["bang"], "WHAMMY!")
def test_single_element_list(self): os.environ["CFITALL__ONELIST"] = "[onething]" provider = EnvironmentProvider("cfitall") self.assertEqual(provider.dict["onelist"], ["onething"])