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

implement 'globals' option

This commit is contained in:
Mike Fährmann 2023-02-28 18:18:55 +01:00
parent b14f8d5817
commit d788e6c60c
No known key found for this signature in database
GPG Key ID: 5680CA389D365A88
4 changed files with 101 additions and 3 deletions

View File

@ -4694,6 +4694,29 @@ Description
or by `extractor.modules`_.
globals
-------
Type
* |Path|_
* ``string``
Example
* ``"~/.local/share/gdl-globals.py"``
* ``"gdl-globals"``
Default
The ``GLOBALS`` dict in
`util.py <../gallery_dl/util.py>`__
Description
Path to or name of an
`importable <https://docs.python.org/3/reference/import.html>`__
Python module whose namespace gets used as an alternative
|globals parameter|__
for compiled Python expressions.
.. |globals parameter| replace:: ``globals`` parameter
.. __: https://docs.python.org/3/library/functions.html#eval
cache.file
----------
Type

View File

@ -110,6 +110,11 @@ def main():
from . import formatter
formatter._SEPARATOR = separator
# eval globals
path = config.get((), "globals")
if path:
util.GLOBALS = util.import_file(path).__dict__
# loglevels
output.configure_logging(args.loglevel)
if args.loglevel >= logging.ERROR:

View File

@ -619,9 +619,28 @@ GLOBALS = {
}
def compile_expression(expr, name="<expr>", globals=GLOBALS):
def compile_expression(expr, name="<expr>", globals=None):
code_object = compile(expr, name, "eval")
return functools.partial(eval, code_object, globals)
return functools.partial(eval, code_object, globals or GLOBALS)
def import_file(path):
"""Import a Python module from a filesystem path"""
path, name = os.path.split(path)
name, sep, ext = name.rpartition(".")
if not sep:
name = ext
if path:
path = expand_path(path)
sys.path.insert(0, path)
try:
return __import__(name)
finally:
del sys.path[0]
else:
return __import__(name)
def build_duration_func(duration, min=0.0):

View File

@ -1,7 +1,7 @@
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# Copyright 2015-2022 Mike Fährmann
# Copyright 2015-2023 Mike Fährmann
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License version 2 as
@ -15,6 +15,7 @@ import io
import random
import string
import datetime
import tempfile
import itertools
import http.cookiejar
@ -458,6 +459,56 @@ class TestOther(unittest.TestCase):
with self.assertRaises(exception.StopExtraction):
expr()
def test_import_file(self):
module = util.import_file("datetime")
self.assertIs(module, datetime)
with tempfile.TemporaryDirectory() as path:
file = path + "/module_test.py"
with open(file, "w") as fp:
fp.write("""
import datetime
key = "foobar"
value = 123
""")
module = util.import_file(file)
self.assertEqual(module.__name__, "module_test")
self.assertEqual(module.key, "foobar")
self.assertEqual(module.value, 123)
self.assertIs(module.datetime, datetime)
def test_custom_globals(self):
value = {"v": "foobar"}
result = "8843d7f92416211de9ebb963ff4ce28125932878"
expr = util.compile_expression("hash_sha1(v)")
self.assertEqual(expr(value), result)
expr = util.compile_expression("hs(v)", globals={"hs": util.sha1})
self.assertEqual(expr(value), result)
with tempfile.TemporaryDirectory() as path:
file = path + "/module_sha1.py"
with open(file, "w") as fp:
fp.write("""
import hashlib
def hash(value):
return hashlib.sha1(value.encode()).hexdigest()
""")
module = util.import_file(file)
expr = util.compile_expression("hash(v)", globals=module.__dict__)
self.assertEqual(expr(value), result)
GLOBALS_ORIG = util.GLOBALS
try:
util.GLOBALS = module.__dict__
expr = util.compile_expression("hash(v)")
finally:
util.GLOBALS = GLOBALS_ORIG
self.assertEqual(expr(value), result)
def test_build_duration_func(self, f=util.build_duration_func):
def test_single(df, v):