From 1800bd7d146f7e7985205fbca032471fcbe17ff3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mike=20F=C3=A4hrmann?= Date: Wed, 21 Dec 2022 20:36:46 +0100 Subject: [PATCH] allow '*-filter' options to be a list of expressions --- docs/configuration.rst | 19 +++++++++++-------- gallery_dl/util.py | 2 ++ test/test_util.py | 8 ++++++++ 3 files changed, 21 insertions(+), 8 deletions(-) diff --git a/docs/configuration.rst b/docs/configuration.rst index 9359715f..cd88eec3 100644 --- a/docs/configuration.rst +++ b/docs/configuration.rst @@ -843,24 +843,27 @@ Description extractor.*.image-filter ------------------------ Type - ``string`` -Example - * ``"width >= 1200 and width/height > 1.2"`` + * ``string`` + * ``list`` of ``strings`` +Examples * ``"re.search(r'foo(bar)+', description)"`` + * ``["width >= 1200", "width/height > 1.2"]`` Description Python expression controlling which files to download. - | Files for which the expression evaluates to ``False`` are ignored. - | Available keys are the filename-specific ones listed by ``-K`` or ``-j``. + A file only gets downloaded when *all* of the given expressions evaluate to ``True``. + + Available values are the filename-specific ones listed by ``-K`` or ``-j``. extractor.*.chapter-filter -------------------------- Type - ``string`` -Example + * ``string`` + * ``list`` of ``strings`` +Examples * ``"lang == 'en'"`` - * ``"language == 'French' and 10 <= chapter < 20"`` + * ``["language == 'French'", "10 <= chapter < 20"]`` Description Like `image-filter `__, but applies to delegated URLs like manga chapters, etc. diff --git a/gallery_dl/util.py b/gallery_dl/util.py index 23d5bc8e..54e9dae7 100644 --- a/gallery_dl/util.py +++ b/gallery_dl/util.py @@ -802,6 +802,8 @@ class FilterPredicate(): """Predicate; True if evaluating the given expression returns True""" def __init__(self, expr, target="image"): + if not isinstance(expr, str): + expr = "(" + ") and (".join(expr) + ")" name = "<{} filter>".format(target) self.expr = compile_expression(expr, name) diff --git a/test/test_util.py b/test/test_util.py index 4b8f9ae4..4de8ce84 100644 --- a/test/test_util.py +++ b/test/test_util.py @@ -116,6 +116,14 @@ class TestPredicate(unittest.TestCase): with self.assertRaises(exception.FilterError): util.FilterPredicate("b > 1")(url, {"a": 2}) + pred = util.FilterPredicate(["a < 3", "b < 4", "c < 5"]) + self.assertTrue(pred(url, {"a": 2, "b": 3, "c": 4})) + self.assertFalse(pred(url, {"a": 3, "b": 3, "c": 4})) + self.assertFalse(pred(url, {"a": 2, "b": 4, "c": 4})) + self.assertFalse(pred(url, {"a": 2, "b": 3, "c": 5})) + with self.assertRaises(exception.FilterError): + pred(url, {"a": 2}) + def test_build_predicate(self): pred = util.build_predicate([]) self.assertIsInstance(pred, type(lambda: True))