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

allow removing items or sources from config #132

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
31 changes: 31 additions & 0 deletions confuse/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,11 @@ def add(self, value):
"""
raise NotImplementedError

def remove(self, obj):
"""Remove source or key from config
"""
raise NotImplementedError

def set(self, value):
"""*Override* the value for this configuration view. The
specified value is added as the highest-priority configuration
Expand Down Expand Up @@ -136,6 +141,11 @@ def __setitem__(self, key, value):
"""
self.set({key: value})

def __delitem__(self, key):
"""Remove value from config by key.
"""
self.remove(key)

def __contains__(self, key):
return self[key].exists()

Expand Down Expand Up @@ -439,6 +449,22 @@ def __init__(self, sources):
def add(self, obj):
self.sources.append(ConfigSource.of(obj))

def remove(self, obj):
"""Remove source or key from configuration
"""
if isinstance(obj, ConfigSource):
if obj.default:
raise ConfigError(u'Cannot remove default source')
self.sources.remove(obj)
elif isinstance(obj, util.STRING):
for source in self.sources:
if isinstance(source, ConfigSource) and obj in source:
del source[obj]
else:
raise ConfigError(u'Unrecognized obj {0}'.format(
obj
))

def set(self, value):
self.sources.insert(0, ConfigSource.of(value))

Expand Down Expand Up @@ -514,6 +540,11 @@ def set(self, value):
def add(self, value):
self.parent.add({self.key: value})

def remove(self, key):
value = self.parent[self.key].get()
del value[key]
self.set(value)

def root(self):
return self.parent.root()

Expand Down
24 changes: 24 additions & 0 deletions test/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,30 @@ def test_override_list_index(self):
self.assertEqual(config['foo'][1].get(), 'bar')


class DeleteTest(unittest.TestCase):

def test_remove(self):
config = _root({'foo': 'bar', 'qux': 'baz'})
config.remove('foo')
with self.assertRaises(confuse.NotFoundError):
config['foo'].get()
self.assertEqual(set(config.keys()), set(['qux']))

def test_del(self):
config = _root({'foo': 'bar', 'qux': 'baz'})
del config['foo']
with self.assertRaises(confuse.NotFoundError):
config['foo'].get()
self.assertEqual(set(config.keys()), set(['qux']))

def test_nested_remove(self):
config = _root({'foo': {'bar': {'x': 'y', 'a': 'b'}, 'qux': 'baz'}})
del config['foo']['bar']['x']
self.assertEqual(config['foo']['bar'].get(), {'a': 'b'})
config['foo']['bar'].remove('a')
self.assertEqual(config['foo']['bar'].get(), {})


class BuildNamespaceDictTests(unittest.TestCase):
def test_pure_dicts(self):
config = {'foo': {'bar': 1}}
Expand Down