[postprocessor:metadata] write to stdout by setting filename to "-"

(#2624)
This commit is contained in:
Mike Fährmann
2022-05-30 21:15:16 +02:00
parent 61fa9b535a
commit 5b43faffed
3 changed files with 22 additions and 3 deletions

View File

@@ -3218,6 +3218,8 @@ Description
A `format string`_ to build the filenames for metadata files with.
(see `extractor.filename <extractor.*.filename_>`__)
Using ``"-"`` as filename will write all output to ``stdout``.
If this option is set, `metadata.extension`_ and
`metadata.extension-format`_ will be ignored.

View File

@@ -10,6 +10,7 @@
from .common import PostProcessor
from .. import util, formatter
import sys
import os
@@ -44,8 +45,11 @@ class MetadataPP(PostProcessor):
filename = options.get("filename")
extfmt = options.get("extension-format")
if filename:
self._filename = self._filename_custom
self._filename_fmt = formatter.parse(filename).format_map
if filename == "-":
self.run = self._run_stdout
else:
self._filename = self._filename_custom
self._filename_fmt = formatter.parse(filename).format_map
elif extfmt:
self._filename = self._filename_extfmt
self._extension_fmt = formatter.parse(extfmt).format_map
@@ -107,6 +111,9 @@ class MetadataPP(PostProcessor):
if mtime:
util.set_mtime(path, mtime)
def _run_stdout(self, pathfmt):
self.write(sys.stdout, pathfmt.kwdict)
def _directory(self, pathfmt):
return pathfmt.realdirectory

View File

@@ -329,12 +329,22 @@ class MetadataTest(BasePostprocessorTest):
path = self.pathfmt.realdirectory + "test_file__meta_.data"
m.assert_called_once_with(path, "w", encoding="utf-8")
def test_metadata_stdout(self):
self._create({"filename": "-", "indent": None})
with patch("sys.stdout", Mock()) as m:
self._trigger()
self.assertEqual(self._output(m), """\
{"category": "test", "extension": "ext", "filename": "file"}
""")
@staticmethod
def _output(mock):
return "".join(
call[1][0]
for call in mock.mock_calls
if call[0] == "().write"
if call[0].endswith("write")
)