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:
parent
6bd857a319
commit
51ea699083
@ -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)
|
||||
|
||||
|
@ -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({}, {}),
|
||||
|
Loading…
Reference in New Issue
Block a user