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'.
This commit is contained in:
@@ -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
|
||||
-------------------
|
||||
=========== =====
|
||||
|
||||
@@ -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 = {}
|
||||
|
||||
@@ -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)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user