From 1a4d4a799b65baa525b3f9f3a149fa87dcbd6953 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mike=20F=C3=A4hrmann?= Date: Mon, 20 Mar 2023 22:01:33 +0100 Subject: [PATCH] [formatter] support filesystem paths for \fM --- docs/formatting.md | 7 ++++--- gallery_dl/formatter.py | 2 +- test/test_formatter.py | 10 ++++++++-- 3 files changed, 13 insertions(+), 6 deletions(-) diff --git a/docs/formatting.md b/docs/formatting.md index 5420c69e..7ebff499 100644 --- a/docs/formatting.md +++ b/docs/formatting.md @@ -272,9 +272,10 @@ Starting a format string with '\f ' allows to set a different format strin M - Name of a Python module followed by one of its functions. - This function gets called with the current metadata dict as - argument and should return a string. + Path or name of a Python module + followed by the name of one of its functions. + This function gets called with the current metadata dict as + argument and should return a string. \fM my_module:generate_text diff --git a/gallery_dl/formatter.py b/gallery_dl/formatter.py index 2c5bd116..95085c79 100644 --- a/gallery_dl/formatter.py +++ b/gallery_dl/formatter.py @@ -218,7 +218,7 @@ class ModuleFormatter(): def __init__(self, function_spec, default=NONE, fmt=None): module_name, _, function_name = function_spec.partition(":") - module = __import__(module_name) + module = util.import_file(module_name) self.format_map = getattr(module, function_name) diff --git a/test/test_formatter.py b/test/test_formatter.py index 50e55a6c..ee9b2491 100644 --- a/test/test_formatter.py +++ b/test/test_formatter.py @@ -374,7 +374,7 @@ def noarg(): try: fmt1 = formatter.parse("\fM testmod:gentext") fmt2 = formatter.parse("\fM testmod:lengths") - fmt3 = formatter.parse("\fM testmod:noarg") + fmt0 = formatter.parse("\fM testmod:noarg") with self.assertRaises(AttributeError): formatter.parse("\fM testmod:missing") @@ -383,11 +383,17 @@ def noarg(): finally: sys.path.pop(0) + fmt3 = formatter.parse("\fM " + path + ":gentext") + fmt4 = formatter.parse("\fM " + path + ":lengths") + self.assertEqual(fmt1.format_map(self.kwdict), "'Title' by Name") self.assertEqual(fmt2.format_map(self.kwdict), "89") + self.assertEqual(fmt3.format_map(self.kwdict), "'Title' by Name") + self.assertEqual(fmt4.format_map(self.kwdict), "89") + with self.assertRaises(TypeError): - self.assertEqual(fmt3.format_map(self.kwdict), "") + self.assertEqual(fmt0.format_map(self.kwdict), "") def _run_test(self, format_string, result, default=None, fmt=format): fmt = formatter.parse(format_string, default, fmt)