From e1e0668ca876345dfe0347cadd1615e96a139251 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mike=20F=C3=A4hrmann?= Date: Wed, 21 Feb 2018 23:18:21 +0100 Subject: [PATCH] add option to set default replacement field value Missing or undefined keywords will now be replaced with the value set for 'keywords-default'. The default is Python's 'None', which is equivalent to setting this option to JSON's 'null'. --- docs/configuration.rst | 10 ++++++++++ gallery_dl/util.py | 12 +++++++----- test/test_util.py | 13 ++++++++++--- 3 files changed, 27 insertions(+), 8 deletions(-) diff --git a/docs/configuration.rst b/docs/configuration.rst index 73f7b33a..771fe994 100644 --- a/docs/configuration.rst +++ b/docs/configuration.rst @@ -383,6 +383,16 @@ Description Additional key-value pairs to be added to each metadata dictionary. =========== ===== +extractor.*.keywords-default +---------------------------- +=========== ===== +Type any +Default ``"None"`` +Description Default value used for missing or undefined keyword names in + format strings. +=========== ===== + + extractor.*.archive ------------------- =========== ===== diff --git a/gallery_dl/util.py b/gallery_dl/util.py index 8c7498d7..8302d95a 100644 --- a/gallery_dl/util.py +++ b/gallery_dl/util.py @@ -303,6 +303,9 @@ class Formatter(): "a": ascii, } + def __init__(self, default=None): + self.kwdefault = default + def vformat(self, format_string, kwargs): """Apply 'kwargs' to the initial format_string and return its result""" result = [] @@ -337,13 +340,12 @@ class Formatter(): return before[1:] + format(value, format_spec) + after return format(value, format_spec) - @staticmethod - def get_field(field_name, kwargs): - """Return value called 'field_name' from 'kwargs'""" + def get_field(self, field_name, kwargs): + """Return value with key 'field_name' from 'kwargs'""" first, rest = _string.formatter_field_name_split(field_name) if first not in kwargs: - return None + return self.kwdefault obj = kwargs[first] for is_attr, i in rest: @@ -362,7 +364,7 @@ class PathFormat(): "filename", extractor.filename_fmt) self.directory_fmt = extractor.config( "directory", extractor.directory_fmt) - self.formatter = Formatter() + self.formatter = Formatter(extractor.config("keywords-default")) self.has_extension = False self.keywords = {} diff --git a/test/test_util.py b/test/test_util.py index b728586f..ac18df44 100644 --- a/test/test_util.py +++ b/test/test_util.py @@ -186,10 +186,17 @@ class TestFormatter(unittest.TestCase): self._run_test("{missing}", replacement) self._run_test("{missing.attr}", replacement) self._run_test("{missing[key]}", replacement) - self._run_test("{missing?a/b/}", replacement) + self._run_test("{missing:?a//}", "") - def _run_test(self, format_string, result): - formatter = util.Formatter() + def test_missing_custom_default(self): + replacement = default = "foobar" + self._run_test("{missing}" , replacement, default) + self._run_test("{missing.attr}", replacement, default) + self._run_test("{missing[key]}", replacement, default) + self._run_test("{missing:?a//}", "a" + default, default) + + def _run_test(self, format_string, result, default=None): + formatter = util.Formatter(default) output = formatter.vformat(format_string, self.kwdict) self.assertEqual(output, result, format_string)