Skip to content

Commit

Permalink
[pp:metadata] implement 'base-directory' option (#5262, #5728)
Browse files Browse the repository at this point in the history
  • Loading branch information
mikf committed Aug 7, 2024
1 parent 34433f5 commit 605f6fb
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 7 deletions.
23 changes: 19 additions & 4 deletions docs/configuration.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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`_
Expand Down
23 changes: 20 additions & 3 deletions gallery_dl/postprocessor/metadata.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,20 @@ def __init__(self, job, options):
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
Expand Down Expand Up @@ -147,11 +161,14 @@ def _run_delete(self, pathfmt):
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
Expand All @@ -161,7 +178,7 @@ def _directory_format(self, pathfmt):
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
Expand Down
21 changes: 21 additions & 0 deletions test/test_postprocessor.py
Original file line number Diff line number Diff line change
Expand Up @@ -452,6 +452,27 @@ def test_metadata_directory_format(self):
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",
Expand Down

0 comments on commit 605f6fb

Please sign in to comment.