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
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)