diff options
Diffstat (limited to 'gallery_dl/util.py')
| -rw-r--r-- | gallery_dl/util.py | 35 |
1 files changed, 31 insertions, 4 deletions
diff --git a/gallery_dl/util.py b/gallery_dl/util.py index 6cdd994..3cbe510 100644 --- a/gallery_dl/util.py +++ b/gallery_dl/util.py @@ -21,6 +21,7 @@ import datetime import functools import itertools import subprocess +import collections import urllib.parse from http.cookiejar import Cookie from email.utils import mktime_tz, parsedate_tz @@ -702,6 +703,28 @@ def compile_expression_raw(expr, name="<expr>", globals=None): return functools.partial(eval, code_object, globals or GLOBALS) +def compile_expression_defaultdict(expr, name="<expr>", globals=None): + global GLOBALS_DEFAULT + + if isinstance(__builtins__, dict): + # cpython + GLOBALS_DEFAULT = collections.defaultdict(lambda n=NONE: n, GLOBALS) + else: + # pypy3 - insert __builtins__ symbols into globals dict + GLOBALS_DEFAULT = collections.defaultdict( + lambda n=NONE: n, __builtins__.__dict__) + GLOBALS_DEFAULT.update(GLOBALS) + + global compile_expression_defaultdict + compile_expression_defaultdict = compile_expression_defaultdict_impl + return compile_expression_defaultdict_impl(expr, name, globals) + + +def compile_expression_defaultdict_impl(expr, name="<expr>", globals=None): + code_object = compile(expr, name, "eval") + return functools.partial(eval, code_object, globals or GLOBALS_DEFAULT) + + def compile_expression_tryexcept(expr, name="<expr>", globals=None): code_object = compile(expr, name, "eval") @@ -711,7 +734,7 @@ def compile_expression_tryexcept(expr, name="<expr>", globals=None): except exception.GalleryDLException: raise except Exception: - return False + return NONE return _eval @@ -719,6 +742,12 @@ def compile_expression_tryexcept(expr, name="<expr>", globals=None): compile_expression = compile_expression_tryexcept +def compile_filter(expr, name="<filter>", globals=None): + if not isinstance(expr, str): + expr = "(" + ") and (".join(expr) + ")" + return compile_expression(expr, name, globals) + + def import_file(path): """Import a Python module from a filesystem path""" path, name = os.path.split(path) @@ -949,10 +978,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) + self.expr = compile_filter(expr, name) def __call__(self, _, kwdict): try: |
