implement 'config.accumulate()' (#994)

pull/997/head
Mike Fährmann 4 years ago
parent 3afd362e2e
commit 392d022b04
No known key found for this signature in database
GPG Key ID: 5680CA389D365A88

@ -140,6 +140,25 @@ def interpolate_common(common, paths, key, default=None, *, conf=_config):
return default
def accumulate(path, key, *, conf=_config):
"""Accumulate the values of 'key' along 'path'"""
result = []
try:
if key in conf:
value = conf[key]
if value:
result.extend(value)
for p in path:
conf = conf[p]
if key in conf:
value = conf[key]
if value:
result[:0] = value
except Exception:
pass
return result
def set(path, key, value, *, conf=_config):
"""Set the value of property 'key' for this session"""
for p in path:

@ -96,6 +96,28 @@ class TestConfig(unittest.TestCase):
test(("Z1", "Z2", "A1", "A2", "A3"), 999, 8)
test((), 9)
def test_accumulate(self):
self.assertEqual(config.accumulate((), "l"), [])
config.set(() , "l", [5, 6])
config.set(("c",) , "l", [3, 4])
config.set(("c", "c"), "l", [1, 2])
self.assertEqual(
config.accumulate((), "l") , [5, 6])
self.assertEqual(
config.accumulate(("c",), "l") , [3, 4, 5, 6])
self.assertEqual(
config.accumulate(("c", "c"), "l"), [1, 2, 3, 4, 5, 6])
config.set(("c",), "l", None)
config.unset(("c", "c"), "l")
self.assertEqual(
config.accumulate((), "l") , [5, 6])
self.assertEqual(
config.accumulate(("c",), "l") , [5, 6])
self.assertEqual(
config.accumulate(("c", "c"), "l"), [5, 6])
def test_set(self):
config.set(() , "c", [1, 2, 3])
config.set(("b",) , "c", [1, 2, 3])

Loading…
Cancel
Save