From d3eab417ed643cfcb686e2326e064f3539acede0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mike=20F=C3=A4hrmann?= Date: Tue, 24 Aug 2021 23:23:12 +0200 Subject: [PATCH] implement a 'path-strip' option --- docs/configuration.rst | 18 ++++++++++++++++++ docs/gallery-dl.conf | 1 + gallery_dl/util.py | 19 +++++++++++++++---- 3 files changed, 34 insertions(+), 4 deletions(-) diff --git a/docs/configuration.rst b/docs/configuration.rst index f74bef17..291e3cdb 100644 --- a/docs/configuration.rst +++ b/docs/configuration.rst @@ -245,6 +245,24 @@ Description escaped with backslashes, e.g. ``"\\[\\]"`` +extractor.*.path-strip +---------------------- +Type + ``string`` +Default + ``"auto"`` +Description + Set of characters to remove from the end of generated path segment names + using `str.rstrip() `_ + + Special values: + + * ``"auto"``: Use characters from ``"unix"`` or ``"windows"`` + depending on the local operating system + * ``"unix"``: ``""`` + * ``"windows"``: ``". "`` + + extractor.*.extension-map ------------------------- Type diff --git a/docs/gallery-dl.conf b/docs/gallery-dl.conf index 228b7541..c3f45d65 100644 --- a/docs/gallery-dl.conf +++ b/docs/gallery-dl.conf @@ -23,6 +23,7 @@ "path-restrict": "auto", "path-replace": "_", "path-remove": "\\u0000-\\u001f\\u007f", + "path-strip": "auto", "extension-map": { "jpeg": "jpg", "jpe" : "jpg", diff --git a/gallery_dl/util.py b/gallery_dl/util.py index 2c0fae60..b1ed15ab 100644 --- a/gallery_dl/util.py +++ b/gallery_dl/util.py @@ -849,6 +849,15 @@ class PathFormat(): remove = config("path-remove", "\x00-\x1f\x7f") self.clean_path = self._build_cleanfunc(remove, "") + strip = config("path-strip", "auto") + if strip == "auto": + strip = ". " if WINDOWS else "" + elif strip == "unix": + strip = "" + elif strip == "windows": + strip = ". " + self.strip = strip + basedir = extractor._parentdir if not basedir: basedir = config("base-directory") @@ -982,13 +991,14 @@ class PathFormat(): """Apply 'kwdict' to directory format strings""" segments = [] append = segments.append + strip = self.strip try: for formatter in self.directory_formatters: segment = formatter(kwdict).strip() - if WINDOWS: + if strip: # remove trailing dots and spaces (#647) - segment = segment.rstrip(". ") + segment = segment.rstrip(strip) if segment: append(self.clean_segment(segment)) return segments @@ -998,6 +1008,7 @@ class PathFormat(): def build_directory_conditional(self, kwdict): segments = [] append = segments.append + strip = self.strip try: for condition, formatters in self.directory_conditions: @@ -1007,8 +1018,8 @@ class PathFormat(): formatters = self.directory_formatters for formatter in formatters: segment = formatter(kwdict).strip() - if WINDOWS: - segment = segment.rstrip(". ") + if strip: + segment = segment.rstrip(strip) if segment: append(self.clean_segment(segment)) return segments