Skip to content

Commit 681920b

Browse files
committed
allow removing items or sources from config
1 parent f0485fa commit 681920b

File tree

2 files changed

+43
-0
lines changed

2 files changed

+43
-0
lines changed

confuse/core.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,11 @@ def add(self, value):
8787
"""
8888
raise NotImplementedError
8989

90+
def remove(self, value):
91+
"""Remove source or value from config
92+
"""
93+
raise NotImplementedError
94+
9095
def set(self, value):
9196
"""*Override* the value for this configuration view. The
9297
specified value is added as the highest-priority configuration
@@ -136,6 +141,11 @@ def __setitem__(self, key, value):
136141
"""
137142
self.set({key: value})
138143

144+
def __delitem__(self, key):
145+
"""Remove value from config by key.
146+
"""
147+
self.remove(key)
148+
139149
def __contains__(self, key):
140150
return self[key].exists()
141151

@@ -439,6 +449,22 @@ def __init__(self, sources):
439449
def add(self, obj):
440450
self.sources.append(ConfigSource.of(obj))
441451

452+
def remove(self, obj):
453+
"""Remove source or item from configuration
454+
"""
455+
if isinstance(obj, ConfigSource):
456+
if obj.default:
457+
raise ConfigError(u'Cannot remove default source')
458+
self.sources.remove(obj)
459+
elif isinstance(obj, util.STRING):
460+
for source in self.sources:
461+
if isinstance(source, ConfigSource) and obj in source:
462+
del source[obj]
463+
else:
464+
raise ConfigError(u'Unrecognized obj {0}'.format(
465+
obj
466+
))
467+
442468
def set(self, value):
443469
self.sources.insert(0, ConfigSource.of(value))
444470

test/test_views.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -241,6 +241,23 @@ def test_override_list_index(self):
241241
self.assertEqual(config['foo'][1].get(), 'bar')
242242

243243

244+
class DeleteTest(unittest.TestCase):
245+
246+
def test_remove(self):
247+
config = _root({'foo': 'bar', 'qux': 'baz'})
248+
config.remove('foo')
249+
with self.assertRaises(confuse.NotFoundError):
250+
config['foo'].get()
251+
self.assertEqual(set(config.keys()), set(['qux']))
252+
253+
def test_del(self):
254+
config = _root({'foo': 'bar', 'qux': 'baz'})
255+
del config['foo']
256+
with self.assertRaises(confuse.NotFoundError):
257+
config['foo'].get()
258+
self.assertEqual(set(config.keys()), set(['qux']))
259+
260+
244261
class BuildNamespaceDictTests(unittest.TestCase):
245262
def test_pure_dicts(self):
246263
config = {'foo': {'bar': 1}}

0 commit comments

Comments
 (0)