From: Andrew Schoen Date: Tue, 2 Dec 2014 21:27:09 +0000 (-0600) Subject: modified __getattr__ for FakeNamespace so that it raises AttributeError on a missing... X-Git-Tag: 1.1.0~1067^2~22^2~3 X-Git-Url: http://git-server-git.apps.pok.os.sepia.ceph.com/?a=commitdiff_plain;h=0cf22e08496327830f978c940495547bafabbea8;p=teuthology.git modified __getattr__ for FakeNamespace so that it raises AttributeError on a missing attr. This allows a FakeNamespace instance to work correctly with getattr(). Also removed .get() from YamlConfig as we decided it was unnecessary Signed-off-by: Andrew Schoen --- diff --git a/teuthology/config.py b/teuthology/config.py index 3091b4b7..9623cd38 100644 --- a/teuthology/config.py +++ b/teuthology/config.py @@ -80,19 +80,6 @@ class YamlConfig(collections.MutableMapping): """ return str(self) - def get(self, key, default=None): - """ - A function that acts like dict.get(). Will first check self._conf, then - _defaults for the given key. - - :param key: The key to fetch - :returns: The value for the given key, or the default kwarg if not found - """ - result = self.__getattr__(key) - if not result: - return default - return result - def __str__(self): return yaml.safe_dump(self._conf, default_flow_style=False).strip() @@ -198,6 +185,16 @@ class FakeNamespace(YamlConfig): return result + def __getattr__(self, name): + """ + We need to modify this for FakeNamespace so that getattr() will + work correctly on a FakeNamespace instance. + """ + result = self._conf.get(name, self._defaults.get(name)) + if result is None: + raise AttributeError + return self._conf.get(name, self._defaults.get(name)) + def _get_config_path(): system_config_path = '/etc/teuthology.yaml' diff --git a/teuthology/test/test_config.py b/teuthology/test/test_config.py index 8bb8c149..a1761799 100644 --- a/teuthology/test/test_config.py +++ b/teuthology/test/test_config.py @@ -1,3 +1,5 @@ +import pytest + from .. import config @@ -56,12 +58,6 @@ class TestYamlConfig(object): del conf_obj.foo assert conf_obj.foo is None - def test_get(self): - conf_obj = self.test_class() - conf_obj.foo = "bar" - assert conf_obj.get("foo") == "bar" - assert conf_obj.get("not_there", "default") == "default" - def test_assignment(self): conf_obj = self.test_class() conf_obj["foo"] = "bar" @@ -134,3 +130,18 @@ class TestFakeNamespace(TestYamlConfig): assert not conf_obj.teuthology_config.automated_scheduling assert conf_obj.teuthology_config.ceph_git_base_url == 'https://github.com/ceph/' assert conf_obj.teuthology_config["ceph_git_base_url"] == 'https://github.com/ceph/' + + def test_getattr(self): + conf_obj = self.test_class.from_dict({"foo": "bar"}) + result = getattr(conf_obj, "not_there", "default") + assert result == "default" + result = getattr(conf_obj, "foo") + assert result == "bar" + + def test_delattr(self): + conf_obj = self.test_class() + conf_obj.foo = 'bar' + assert conf_obj.foo == 'bar' + del conf_obj.foo + with pytest.raises(AttributeError): + conf_obj.foo