From 9b43a3677e476e8a15665533131d24282f4dccc7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mike=20F=C3=A4hrmann?= Date: Sun, 27 Apr 2025 22:05:35 +0200 Subject: [PATCH] [pp:directory] add 'directory' post processor (#7432) --- docs/configuration.rst | 15 ++++++++++++++ gallery_dl/postprocessor/__init__.py | 1 + gallery_dl/postprocessor/directory.py | 30 +++++++++++++++++++++++++++ test/test_postprocessor.py | 18 ++++++++++++++++ 4 files changed, 64 insertions(+) create mode 100644 gallery_dl/postprocessor/directory.py diff --git a/docs/configuration.rst b/docs/configuration.rst index 99caba67..6263ca63 100644 --- a/docs/configuration.rst +++ b/docs/configuration.rst @@ -6440,6 +6440,19 @@ Description Only compare file sizes. Do not read and compare their content. +directory.event +--------------- +Type + * ``string`` + * ``list`` of ``strings`` +Default + ``"prepare"`` +Description + The event(s) for which directory_ format strings are (re)evaluated. + + See `metadata.event`_ for a list of available events. + + exec.archive ------------ Type @@ -7738,6 +7751,8 @@ Description ``compare`` | Compare versions of the same file and replace/enumerate them on mismatch | (requires `downloader.*.part`_ = ``true`` and `extractor.*.skip`_ = ``false``) + ``directory`` + Reevaluate directory_ format strings ``exec`` Execute external commands ``hash`` diff --git a/gallery_dl/postprocessor/__init__.py b/gallery_dl/postprocessor/__init__.py index 7837b063..dd44a8a2 100644 --- a/gallery_dl/postprocessor/__init__.py +++ b/gallery_dl/postprocessor/__init__.py @@ -11,6 +11,7 @@ modules = [ "classify", "compare", + "directory", "exec", "hash", "metadata", diff --git a/gallery_dl/postprocessor/directory.py b/gallery_dl/postprocessor/directory.py new file mode 100644 index 00000000..ed8c02e4 --- /dev/null +++ b/gallery_dl/postprocessor/directory.py @@ -0,0 +1,30 @@ +# -*- coding: utf-8 -*- + +# Copyright 2025 Mike Fährmann +# +# This program is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License version 2 as +# published by the Free Software Foundation. + +"""Trigger directory format string evaluation""" + +from .common import PostProcessor + + +class DirectoryPP(PostProcessor): + + def __init__(self, job, options): + PostProcessor.__init__(self, job) + + events = options.get("event") + if events is None: + events = ("prepare",) + elif isinstance(events, str): + events = events.split(",") + job.register_hooks({event: self.run for event in events}, options) + + def run(self, pathfmt): + pathfmt.set_directory(pathfmt.kwdict) + + +__postprocessor__ = DirectoryPP diff --git a/test/test_postprocessor.py b/test/test_postprocessor.py index 8b073b4c..76e728cd 100644 --- a/test/test_postprocessor.py +++ b/test/test_postprocessor.py @@ -173,6 +173,24 @@ class ClassifyTest(BasePostprocessorTest): self.assertEqual(self.pathfmt.realpath, path + "/file.foo") +class DirectoryTest(BasePostprocessorTest): + + def test_default(self): + self._create() + + path = os.path.join(self.dir.name, "test") + self.assertEqual(self.pathfmt.realdirectory, path + "/") + self.assertEqual(self.pathfmt.realpath, path + "/file.ext") + + self.pathfmt.kwdict["category"] = "custom" + self._trigger() + + path = os.path.join(self.dir.name, "custom") + self.assertEqual(self.pathfmt.realdirectory, path + "/") + self.pathfmt.build_path() + self.assertEqual(self.pathfmt.realpath, path + "/file.ext") + + class ExecTest(BasePostprocessorTest): def test_command_string(self):