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_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_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 test_deregister_unregistered(self): manager = ProviderManager() manager.register(FilesystemProvider(["/etc/foo"], "foo")) manager.deregister("environment") self.assertIn("filesystem", manager.ordering) self.assertEqual(len(manager.ordering), 1) self.assertTrue(hasattr(manager, "filesystem")) self.assertNotIn("environment", manager.ordering) self.assertNotEqual(len(manager.ordering), 2)
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_init_empty(self): manager = ProviderManager() self.assertEqual(manager.ordering, [])
def test_get_none(self): manager = ProviderManager() self.assertIsNone(manager.get("filesystem"))
def test_get(self): envprovider = EnvironmentProvider("foo") manager = ProviderManager(providers=[envprovider]) self.assertEqual(manager.get("environment"), envprovider)