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
|
return iterator
|
||||||
|
|
||||||
|
|
||||||
|
def raises(obj):
|
||||||
|
"""Returns a function that raises 'obj' as exception"""
|
||||||
|
def wrap():
|
||||||
|
raise obj
|
||||||
|
return wrap
|
||||||
|
|
||||||
|
|
||||||
def combine_dict(a, b):
|
def combine_dict(a, b):
|
||||||
"""Recursively combine the contents of b into a"""
|
"""Recursively combine the contents of b into a"""
|
||||||
for key, value in b.items():
|
for key, value in b.items():
|
||||||
@ -249,6 +256,7 @@ class FilterPredicate():
|
|||||||
"safe_int": safe_int,
|
"safe_int": safe_int,
|
||||||
"urlsplit": urllib.parse.urlsplit,
|
"urlsplit": urllib.parse.urlsplit,
|
||||||
"datetime": datetime.datetime,
|
"datetime": datetime.datetime,
|
||||||
|
"abort": raises(exception.StopExtraction()),
|
||||||
"re": re,
|
"re": re,
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -258,6 +266,8 @@ class FilterPredicate():
|
|||||||
def __call__(self, url, kwds):
|
def __call__(self, url, kwds):
|
||||||
try:
|
try:
|
||||||
return eval(self.codeobj, self.globalsdict, kwds)
|
return eval(self.codeobj, self.globalsdict, kwds)
|
||||||
|
except exception.GalleryDLException:
|
||||||
|
raise
|
||||||
except Exception as exc:
|
except Exception as exc:
|
||||||
raise exception.FilterError(exc)
|
raise exception.FilterError(exc)
|
||||||
|
|
||||||
|
@ -255,6 +255,14 @@ class TestOther(unittest.TestCase):
|
|||||||
self.assertCountEqual(
|
self.assertCountEqual(
|
||||||
util.advance(util.advance(items, 1), 2), range(3, 5))
|
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):
|
def test_combine_dict(self):
|
||||||
self.assertEqual(
|
self.assertEqual(
|
||||||
util.combine_dict({}, {}),
|
util.combine_dict({}, {}),
|
||||||
|
Loading…
x
Reference in New Issue
Block a user