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:
parent
c8e0efe1ff
commit
bea33ae9cb
@ -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
|
||||
|
@ -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")
|
||||
|
Loading…
Reference in New Issue
Block a user