From 392d022b0411dc0278fd8cd02b63d72c0b310a46 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mike=20F=C3=A4hrmann?= Date: Mon, 14 Sep 2020 21:13:08 +0200 Subject: [PATCH] implement 'config.accumulate()' (#994) --- gallery_dl/config.py | 19 +++++++++++++++++++ test/test_config.py | 22 ++++++++++++++++++++++ 2 files changed, 41 insertions(+) diff --git a/gallery_dl/config.py b/gallery_dl/config.py index a3c71cde..e0a54591 100644 --- a/gallery_dl/config.py +++ b/gallery_dl/config.py @@ -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: diff --git a/test/test_config.py b/test/test_config.py index a9cefd40..7cbb12b8 100644 --- a/test/test_config.py +++ b/test/test_config.py @@ -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])