Skip to content

Commit

Permalink
[pp:metadata] implement format strings for 'directory' (#5728)
Browse files Browse the repository at this point in the history
  • Loading branch information
mikf committed Jul 6, 2024
1 parent 8f3f061 commit da9916c
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 3 deletions.
6 changes: 4 additions & 2 deletions docs/configuration.rst
Original file line number Diff line number Diff line change
Expand Up @@ -5442,11 +5442,13 @@ Description
metadata.directory
------------------
Type
``string``
* ``string``
* ``list`` of ``strings``
Default
``"."``
Example
``"metadata"``
* ``"metadata"``
* ``["..", "metadata", "\fF {id // 500 * 500}"]``
Description
Directory where metadata files are stored in relative to the
current target location for file downloads.
Expand Down
18 changes: 17 additions & 1 deletion gallery_dl/postprocessor/metadata.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,13 @@ def __init__(self, job, options):
ext = "json"

directory = options.get("directory")
if directory:
if isinstance(directory, list):
self._directory = self._directory_format
self._directory_formatters = [
formatter.parse(dirfmt, util.NONE).format_map
for dirfmt in directory
]
elif directory:
self._directory = self._directory_custom
sep = os.sep + (os.altsep or "")
self._metadir = util.expand_path(directory).rstrip(sep) + os.sep
Expand Down Expand Up @@ -147,6 +153,16 @@ def _directory(self, pathfmt):
def _directory_custom(self, pathfmt):
return os.path.join(pathfmt.realdirectory, self._metadir)

def _directory_format(self, pathfmt):
formatters = pathfmt.directory_formatters
try:
pathfmt.directory_formatters = self._directory_formatters
segments = pathfmt.build_directory(pathfmt.kwdict)
directory = pathfmt.clean_path(os.sep.join(segments) + os.sep)
return os.path.join(pathfmt.realdirectory, directory)
finally:
pathfmt.directory_formatters = formatters

def _filename(self, pathfmt):
return (pathfmt.filename or "metadata") + "." + self.extension

Expand Down
12 changes: 12 additions & 0 deletions test/test_postprocessor.py
Original file line number Diff line number Diff line change
Expand Up @@ -440,6 +440,18 @@ def test_metadata_directory_2(self):
path = self.pathfmt.realdirectory + "metadata/file.json"
m.assert_called_once_with(path, "w", encoding="utf-8")

def test_metadata_directory_format(self):
self._create(
{"directory": ["..", "json", "\fF {id // 500 * 500 + 500}"]},
{"id": 12345},
)

with patch("builtins.open", mock_open()) as m:
self._trigger()

path = self.pathfmt.realdirectory + "../json/12500/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 da9916c

Please sign in to comment.