1
0
mirror of https://github.com/mikf/gallery-dl.git synced 2024-11-25 04:02:32 +01:00

implement config.setdefault

This commit is contained in:
Mike Fährmann 2015-10-07 00:58:43 +02:00
parent c8e0efe1ff
commit bea33ae9cb
2 changed files with 18 additions and 0 deletions

View File

@ -68,6 +68,18 @@ def set(keys, value):
conf = temp
conf[keys[-1]] = value
def setdefault(keys, value):
"""Set the value of property 'key' if it doesn't exist"""
conf = _config
for k in keys[:-1]:
try:
conf = conf[k]
except KeyError:
temp = {}
conf[k] = temp
conf = temp
return conf.setdefault(keys[-1], value)
# --------------------------------------------------------------------
# internals

View File

@ -36,6 +36,12 @@ class TestConfig(unittest.TestCase):
self.assertEqual(config.get(["b", "c"]), [1, 2, 3])
self.assertEqual(config.get(["e", "f", "g"]), 234)
def test_setdefault(self):
config.setdefault(["b", "c"], [1, 2, 3])
config.setdefault(["e", "f", "g"], value=234)
self.assertEqual(config.get(["b", "c"]), "text")
self.assertEqual(config.get(["e", "f", "g"]), 234)
def test_interpolate(self):
self.assertEqual(config.interpolate(["a"]), "1")
self.assertEqual(config.interpolate(["b", "a"]), "1")