Skip to content

Commit

Permalink
[postprocessor:metadata] implement 'extension-format' option
Browse files Browse the repository at this point in the history
closes #477
  • Loading branch information
mikf committed Nov 30, 2019
1 parent 0f1538a commit a412531
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 11 deletions.
17 changes: 15 additions & 2 deletions docs/configuration.rst
Original file line number Diff line number Diff line change
Expand Up @@ -1474,15 +1474,28 @@ metadata.extension
=========== =====
Type ``string``
Default ``"json"`` or ``"txt"``
Description Filename extension for metadata files.
Description Filename extension for metadata files that will be appended to the
original file names.
=========== =====

metadata.extension-format
-------------------------
=========== =====
Type ``string``
Example * ``"{extension}.json"``
* ``"json"``
Description Custom format string to build filename extensions for metadata
files with, which will replace the original filename extensions.

Note: `metadata.extension`_ is ignored if this option is set.
=========== =====

metadata.format
---------------
=========== =====
Type ``string``
Example ``"tags:\n\n{tags:J\n}\n"``
Description Custom format string to build content of metadata files.
Description Custom format string to build the content of metadata files with.

Note: Only applies for ``"mode": "custom"``.
=========== =====
Expand Down
32 changes: 24 additions & 8 deletions gallery_dl/postprocessor/metadata.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,29 +18,45 @@ def __init__(self, pathfmt, options):
PostProcessor.__init__(self)

mode = options.get("mode", "json")
ext = "txt"

if mode == "custom":
self.write = self._write_custom
self.formatter = util.Formatter(options.get("format"))
self.contentfmt = util.Formatter(options.get("format")).format_map
ext = "txt"
elif mode == "tags":
self.write = self._write_tags
ext = "txt"
else:
self.write = self._write_json
self.indent = options.get("indent", 4)
self.ascii = options.get("ascii", False)
ext = "json"

self.extension = options.get("extension", ext)
extfmt = options.get("extension-format")
if extfmt:
self.path = self._path_format
self.extfmt = util.Formatter(extfmt).format_map
else:
self.path = self._path_append
self.extension = options.get("extension", ext)

def run(self, pathfmt):
path = "{}.{}".format(pathfmt.realpath, self.extension)
with open(path, "w", encoding="utf-8") as file:
with open(self.path(pathfmt), "w", encoding="utf-8") as file:
self.write(file, pathfmt.kwdict)

def _path_append(self, pathfmt):
return "{}.{}".format(pathfmt.realpath, self.extension)

def _path_format(self, pathfmt):
kwdict = pathfmt.kwdict
ext = kwdict["extension"]
kwdict["extension"] = pathfmt.extension
kwdict["extension"] = pathfmt.prefix + self.extfmt(kwdict)
path = pathfmt.realdirectory + pathfmt.build_filename()
kwdict["extension"] = ext
return path

def _write_custom(self, file, kwdict):
output = self.formatter.format_map(kwdict)
file.write(output)
file.write(self.contentfmt(kwdict))

def _write_tags(self, file, kwdict):
tags = kwdict.get("tags") or kwdict.get("tag_string")
Expand Down
31 changes: 30 additions & 1 deletion test/test_postprocessor.py
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,7 @@ def test_metadata_json(self):
"_private" : "world",
})

self.assertEqual(pp.path , pp._path_append)
self.assertEqual(pp.write , pp._write_json)
self.assertEqual(pp.ascii , True)
self.assertEqual(pp.indent , 2)
Expand Down Expand Up @@ -228,13 +229,41 @@ def test_metadata_custom(self):
)
self.assertEqual(pp.write, pp._write_custom)
self.assertEqual(pp.extension, "txt")
self.assertTrue(pp.formatter)
self.assertTrue(pp.contentfmt)

with patch("builtins.open", mock_open()) as m:
pp.prepare(self.pathfmt)
pp.run(self.pathfmt)
self.assertEqual(self._output(m), "bar\nNone\n")

def test_metadata_extfmt(self):
pp = self._create({
"extension" : "ignored",
"extension-format": "json",
})

self.assertEqual(pp.path, pp._path_format)

with patch("builtins.open", mock_open()) as m:
pp.prepare(self.pathfmt)
pp.run(self.pathfmt)

path = self.pathfmt.realdirectory + "file.json"
m.assert_called_once_with(path, "w", encoding="utf-8")

def test_metadata_extfmt_2(self):
pp = self._create({
"extension-format": "{extension!u}-data:{category:Res/ES/}",
})

self.pathfmt.prefix = "2."
with patch("builtins.open", mock_open()) as m:
pp.prepare(self.pathfmt)
pp.run(self.pathfmt)

path = self.pathfmt.realdirectory + "file.2.EXT-data:tESt"
m.assert_called_once_with(path, "w", encoding="utf-8")

@staticmethod
def _output(mock):
return "".join(
Expand Down

0 comments on commit a412531

Please sign in to comment.