1
0
mirror of https://github.com/mikf/gallery-dl.git synced 2024-11-22 02:32:33 +01:00

[pp:metadata] implement 'base-directory' option (#5262, #5728)

This commit is contained in:
Mike Fährmann 2024-08-07 09:43:02 +02:00
parent 34433f5a5d
commit 605f6fba2c
No known key found for this signature in database
GPG Key ID: 5680CA389D365A88
3 changed files with 60 additions and 7 deletions

View File

@ -160,8 +160,7 @@ Default
``false``
Description
Use an extractor's current target directory as
`base-directory <extractor.*.base-directory_>`__
for any spawned child extractors.
base-directory_ for any spawned child extractors.
extractor.*.parent-metadata
@ -5548,8 +5547,23 @@ Example
* ``"metadata"``
* ``["..", "metadata", "\fF {id // 500 * 500}"]``
Description
Directory where metadata files are stored in relative to the
current target location for file downloads.
Directory where metadata files are stored in
relative to `metadata.base-directory`_.
metadata.base-directory
-----------------------
Type
* ``bool``
* |Path|_
Default
``false``
Description
Selects the relative location for metadata files.
* ``false``: current target location for file downloads (base-directory_ + directory_)
* ``true``: current base-directory_ location
* any |Path|_: custom location
metadata.extension
@ -6561,6 +6575,7 @@ Description
.. |open()| replace:: the built-in ``open()`` function
.. |json.dump()| replace:: ``json.dump()``
.. _directory: `extractor.*.directory`_
.. _base-directory: `extractor.*.base-directory`_
.. _date-format: `extractor.*.date-format`_
.. _deviantart.metadata: `extractor.deviantart.metadata`_

View File

@ -55,6 +55,20 @@ class MetadataPP(PostProcessor):
self._json_encode = self._make_encoder(options, 4).encode
ext = "json"
base_directory = options.get("base-directory")
if base_directory:
if base_directory is True:
self._base = lambda p: p.basedirectory
else:
sep = os.sep
altsep = os.altsep
base_directory = util.expand_path(base_directory)
if altsep and altsep in base_directory:
base_directory = base_directory.replace(altsep, sep)
if base_directory[-1] != sep:
base_directory += sep
self._base = lambda p: base_directory
directory = options.get("directory")
if isinstance(directory, list):
self._directory = self._directory_format
@ -147,11 +161,14 @@ class MetadataPP(PostProcessor):
except Exception:
pass
def _directory(self, pathfmt):
def _base(self, pathfmt):
return pathfmt.realdirectory
def _directory(self, pathfmt):
return self._base(pathfmt)
def _directory_custom(self, pathfmt):
return os.path.join(pathfmt.realdirectory, self._metadir)
return os.path.join(self._base(pathfmt), self._metadir)
def _directory_format(self, pathfmt):
formatters = pathfmt.directory_formatters
@ -161,7 +178,7 @@ class MetadataPP(PostProcessor):
pathfmt.directory_conditions = ()
segments = pathfmt.build_directory(pathfmt.kwdict)
directory = pathfmt.clean_path(os.sep.join(segments) + os.sep)
return os.path.join(pathfmt.realdirectory, directory)
return os.path.join(self._base(pathfmt), directory)
finally:
pathfmt.directory_conditions = conditions
pathfmt.directory_formatters = formatters

View File

@ -452,6 +452,27 @@ class MetadataTest(BasePostprocessorTest):
path = self.pathfmt.realdirectory + "../json/12500/file.ext.json"
m.assert_called_once_with(path, "w", encoding="utf-8")
def test_metadata_basedirectory(self):
self._create({"base-directory": True})
with patch("builtins.open", mock_open()) as m:
self._trigger()
path = self.pathfmt.basedirectory + "file.ext.json"
m.assert_called_once_with(path, "w", encoding="utf-8")
def test_metadata_basedirectory_custom(self):
self._create({
"base-directory": "/home/test",
"directory": "meta",
})
with patch("builtins.open", mock_open()) as m:
self._trigger()
path = "/home/test/meta/file.ext.json"
m.assert_called_once_with(path, "w", encoding="utf-8")
def test_metadata_filename(self):
self._create({
"filename" : "{category}_{filename}_/meta/\n\r.data",