implement 'config.accumulate()' (#994)
This commit is contained in:
@@ -140,6 +140,25 @@ def interpolate_common(common, paths, key, default=None, *, conf=_config):
|
|||||||
return default
|
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):
|
def set(path, key, value, *, conf=_config):
|
||||||
"""Set the value of property 'key' for this session"""
|
"""Set the value of property 'key' for this session"""
|
||||||
for p in path:
|
for p in path:
|
||||||
|
|||||||
@@ -96,6 +96,28 @@ class TestConfig(unittest.TestCase):
|
|||||||
test(("Z1", "Z2", "A1", "A2", "A3"), 999, 8)
|
test(("Z1", "Z2", "A1", "A2", "A3"), 999, 8)
|
||||||
test((), 9)
|
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):
|
def test_set(self):
|
||||||
config.set(() , "c", [1, 2, 3])
|
config.set(() , "c", [1, 2, 3])
|
||||||
config.set(("b",) , "c", [1, 2, 3])
|
config.set(("b",) , "c", [1, 2, 3])
|
||||||
|
|||||||
Reference in New Issue
Block a user