1
0
mirror of https://github.com/mikf/gallery-dl.git synced 2024-11-25 04:02:32 +01:00

add 'abort()' as function to filter expressions

calling 'abort()' in a filter aborts the current extractor run
in a cleaner way than using something like 1/0, which
causes an error message to be printed
This commit is contained in:
Mike Fährmann 2018-04-12 17:07:12 +02:00
parent 6bd857a319
commit 51ea699083
No known key found for this signature in database
GPG Key ID: 5680CA389D365A88
2 changed files with 18 additions and 0 deletions

View File

@ -118,6 +118,13 @@ def advance(iterable, num):
return iterator
def raises(obj):
"""Returns a function that raises 'obj' as exception"""
def wrap():
raise obj
return wrap
def combine_dict(a, b):
"""Recursively combine the contents of b into a"""
for key, value in b.items():
@ -249,6 +256,7 @@ class FilterPredicate():
"safe_int": safe_int,
"urlsplit": urllib.parse.urlsplit,
"datetime": datetime.datetime,
"abort": raises(exception.StopExtraction()),
"re": re,
}
@ -258,6 +266,8 @@ class FilterPredicate():
def __call__(self, url, kwds):
try:
return eval(self.codeobj, self.globalsdict, kwds)
except exception.GalleryDLException:
raise
except Exception as exc:
raise exception.FilterError(exc)

View File

@ -255,6 +255,14 @@ class TestOther(unittest.TestCase):
self.assertCountEqual(
util.advance(util.advance(items, 1), 2), range(3, 5))
def test_raises(self):
self.assertRaises(Exception, util.raises(Exception()))
func = util.raises(ValueError(1))
self.assertRaises(Exception, func)
self.assertRaises(Exception, func)
self.assertRaises(Exception, func)
def test_combine_dict(self):
self.assertEqual(
util.combine_dict({}, {}),