Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add persistent config (storing dynamic changes in file) #131

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions confuse/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -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!
24 changes: 24 additions & 0 deletions test/test_persistent_config.py
Original file line number Diff line number Diff line change
@@ -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'