Skip to content

Commit

Permalink
implement 'config.accumulate()' (#994)
Browse files Browse the repository at this point in the history
  • Loading branch information
mikf committed Sep 14, 2020
1 parent 3afd362 commit 392d022
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 0 deletions.
19 changes: 19 additions & 0 deletions gallery_dl/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
22 changes: 22 additions & 0 deletions test/test_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,28 @@ def test(path, value, expected=None):
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])
Expand Down

0 comments on commit 392d022

Please sign in to comment.