diff --git a/confuse/core.py b/confuse/core.py index c63b1a9..5f6e3c1 100644 --- a/confuse/core.py +++ b/confuse/core.py @@ -720,4 +720,18 @@ def clear(self): self._lazy_prefix = [] +class PersistentConfig(Configuration): + + def add(self, obj): + super(PersistentConfig, self).add(obj) + + with open(self.user_config_path(), 'w') as f: + f.write(self.dump().strip()) + + def set(self, value): + super(PersistentConfig, self).set(value) + + with open(self.user_config_path(), 'w') as f: + f.write(self.dump().strip()) + # "Validated" configuration views: experimental! diff --git a/test/test_persistent_config.py b/test/test_persistent_config.py new file mode 100644 index 0000000..25d6815 --- /dev/null +++ b/test/test_persistent_config.py @@ -0,0 +1,24 @@ +import confuse +import unittest + + +class PersistentConfigTest(unittest.TestCase): + + def test_add(self): + config = confuse.PersistentConfig('myapp', read=False) + config.add({'foo': False}) + del config + + config_test = confuse.Configuration('myapp', read=True) + yaml_dump = config_test.dump().strip() + assert yaml_dump == 'foo: no' + + def test_set(self): + config = confuse.PersistentConfig('myapp', read=False) + config['s'] = 'string' + config.set({'abc': 6}) + del config + + config_test = confuse.Configuration('myapp', read=True) + yaml_dump = config_test.dump().strip() + assert yaml_dump == 'abc: 6\ns: string'