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

change argument format for config-calls

This commit is contained in:
Mike Fährmann 2015-10-05 12:42:42 +02:00
parent 7ac106096f
commit 2026223ed1
2 changed files with 19 additions and 21 deletions

View File

@ -35,20 +35,19 @@ def clear():
"""Reset configuration to en empty state"""
globals()["_config"] = {}
def get(key, default=None):
def get(keys, default=None):
"""Get the value of property 'key' or a default-value if it doenst exist"""
conf = _config
try:
for k in key.split("."):
for k in keys:
conf = conf[k]
return conf
except (KeyError, AttributeError):
return default
def interpolate(key, default=None):
def interpolate(keys, default=None):
"""Interpolate the value of 'key'"""
conf = _config
keys = key.split(".")
try:
for k in keys:
default = conf.get(keys[-1], default)
@ -57,10 +56,9 @@ def interpolate(key, default=None):
except (KeyError, AttributeError):
return default
def set(key, value):
def set(keys, value):
"""Set the value of property 'key' for this session"""
conf = _config
keys = key.split(".")
for k in keys[:-1]:
try:
conf = conf[k]

View File

@ -25,25 +25,25 @@ class TestConfig(unittest.TestCase):
os.remove(self._configfile)
def test_get(self):
self.assertEqual(config.get("a"), "1")
self.assertEqual(config.get("b.c"), "text")
self.assertEqual(config.get("d"), None)
self.assertEqual(config.get("e.f.g", 123), 123)
self.assertEqual(config.get(["a"]), "1")
self.assertEqual(config.get(["b", "c"]), "text")
self.assertEqual(config.get(["d"]), None)
self.assertEqual(config.get(["e", "f", "g"], 123), 123)
def test_set(self):
config.set("b.c", [1, 2, 3])
config.set("e.f.g", 234)
self.assertEqual(config.get("b.c"), [1, 2, 3])
self.assertEqual(config.get("e.f.g"), 234)
config.set(["b", "c"], [1, 2, 3])
config.set(["e", "f", "g"], value=234)
self.assertEqual(config.get(["b", "c"]), [1, 2, 3])
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")
self.assertEqual(config.interpolate("b.c", "2"), "text")
self.assertEqual(config.interpolate("b.d", "2"), "2")
config.set("d", 123)
self.assertEqual(config.interpolate("b.d", "2"), 123)
self.assertEqual(config.interpolate("d.d", "2"), 123)
self.assertEqual(config.interpolate(["a"]), "1")
self.assertEqual(config.interpolate(["b", "a"]), "1")
self.assertEqual(config.interpolate(["b", "c"], "2"), "text")
self.assertEqual(config.interpolate(["b", "d"], "2"), "2")
config.set(["d"], 123)
self.assertEqual(config.interpolate(["b", "d"], "2"), 123)
self.assertEqual(config.interpolate(["d", "d"], "2"), 123)
if __name__ == '__main__':
unittest.main()