1
0
mirror of https://github.com/mikf/gallery-dl.git synced 2024-11-25 12:12:34 +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`` ``false``
Description Description
Use an extractor's current target directory as Use an extractor's current target directory as
`base-directory <extractor.*.base-directory_>`__ base-directory_ for any spawned child extractors.
for any spawned child extractors.
extractor.*.parent-metadata extractor.*.parent-metadata
@ -5548,8 +5547,23 @@ Example
* ``"metadata"`` * ``"metadata"``
* ``["..", "metadata", "\fF {id // 500 * 500}"]`` * ``["..", "metadata", "\fF {id // 500 * 500}"]``
Description Description
Directory where metadata files are stored in relative to the Directory where metadata files are stored in
current target location for file downloads. 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 metadata.extension
@ -6561,6 +6575,7 @@ Description
.. |open()| replace:: the built-in ``open()`` function .. |open()| replace:: the built-in ``open()`` function
.. |json.dump()| replace:: ``json.dump()`` .. |json.dump()| replace:: ``json.dump()``
.. _directory: `extractor.*.directory`_
.. _base-directory: `extractor.*.base-directory`_ .. _base-directory: `extractor.*.base-directory`_
.. _date-format: `extractor.*.date-format`_ .. _date-format: `extractor.*.date-format`_
.. _deviantart.metadata: `extractor.deviantart.metadata`_ .. _deviantart.metadata: `extractor.deviantart.metadata`_

View File

@ -55,6 +55,20 @@ class MetadataPP(PostProcessor):
self._json_encode = self._make_encoder(options, 4).encode self._json_encode = self._make_encoder(options, 4).encode
ext = "json" 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") directory = options.get("directory")
if isinstance(directory, list): if isinstance(directory, list):
self._directory = self._directory_format self._directory = self._directory_format
@ -147,11 +161,14 @@ class MetadataPP(PostProcessor):
except Exception: except Exception:
pass pass
def _directory(self, pathfmt): def _base(self, pathfmt):
return pathfmt.realdirectory return pathfmt.realdirectory
def _directory(self, pathfmt):
return self._base(pathfmt)
def _directory_custom(self, 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): def _directory_format(self, pathfmt):
formatters = pathfmt.directory_formatters formatters = pathfmt.directory_formatters
@ -161,7 +178,7 @@ class MetadataPP(PostProcessor):
pathfmt.directory_conditions = () pathfmt.directory_conditions = ()
segments = pathfmt.build_directory(pathfmt.kwdict) segments = pathfmt.build_directory(pathfmt.kwdict)
directory = pathfmt.clean_path(os.sep.join(segments) + os.sep) 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: finally:
pathfmt.directory_conditions = conditions pathfmt.directory_conditions = conditions
pathfmt.directory_formatters = formatters pathfmt.directory_formatters = formatters

View File

@ -452,6 +452,27 @@ class MetadataTest(BasePostprocessorTest):
path = self.pathfmt.realdirectory + "../json/12500/file.ext.json" path = self.pathfmt.realdirectory + "../json/12500/file.ext.json"
m.assert_called_once_with(path, "w", encoding="utf-8") 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): def test_metadata_filename(self):
self._create({ self._create({
"filename" : "{category}_{filename}_/meta/\n\r.data", "filename" : "{category}_{filename}_/meta/\n\r.data",