implement a 'path-strip' option

pull/1853/head
Mike Fährmann 3 years ago
parent 72c0cd30c7
commit d3eab417ed
No known key found for this signature in database
GPG Key ID: 5680CA389D365A88

@ -245,6 +245,24 @@ Description
escaped with backslashes, e.g. ``"\\[\\]"`` 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() <https://docs.python.org/3/library/stdtypes.html#str.rstrip>`_
Special values:
* ``"auto"``: Use characters from ``"unix"`` or ``"windows"``
depending on the local operating system
* ``"unix"``: ``""``
* ``"windows"``: ``". "``
extractor.*.extension-map extractor.*.extension-map
------------------------- -------------------------
Type Type

@ -23,6 +23,7 @@
"path-restrict": "auto", "path-restrict": "auto",
"path-replace": "_", "path-replace": "_",
"path-remove": "\\u0000-\\u001f\\u007f", "path-remove": "\\u0000-\\u001f\\u007f",
"path-strip": "auto",
"extension-map": { "extension-map": {
"jpeg": "jpg", "jpeg": "jpg",
"jpe" : "jpg", "jpe" : "jpg",

@ -849,6 +849,15 @@ class PathFormat():
remove = config("path-remove", "\x00-\x1f\x7f") remove = config("path-remove", "\x00-\x1f\x7f")
self.clean_path = self._build_cleanfunc(remove, "") 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 basedir = extractor._parentdir
if not basedir: if not basedir:
basedir = config("base-directory") basedir = config("base-directory")
@ -982,13 +991,14 @@ class PathFormat():
"""Apply 'kwdict' to directory format strings""" """Apply 'kwdict' to directory format strings"""
segments = [] segments = []
append = segments.append append = segments.append
strip = self.strip
try: try:
for formatter in self.directory_formatters: for formatter in self.directory_formatters:
segment = formatter(kwdict).strip() segment = formatter(kwdict).strip()
if WINDOWS: if strip:
# remove trailing dots and spaces (#647) # remove trailing dots and spaces (#647)
segment = segment.rstrip(". ") segment = segment.rstrip(strip)
if segment: if segment:
append(self.clean_segment(segment)) append(self.clean_segment(segment))
return segments return segments
@ -998,6 +1008,7 @@ class PathFormat():
def build_directory_conditional(self, kwdict): def build_directory_conditional(self, kwdict):
segments = [] segments = []
append = segments.append append = segments.append
strip = self.strip
try: try:
for condition, formatters in self.directory_conditions: for condition, formatters in self.directory_conditions:
@ -1007,8 +1018,8 @@ class PathFormat():
formatters = self.directory_formatters formatters = self.directory_formatters
for formatter in formatters: for formatter in formatters:
segment = formatter(kwdict).strip() segment = formatter(kwdict).strip()
if WINDOWS: if strip:
segment = segment.rstrip(". ") segment = segment.rstrip(strip)
if segment: if segment:
append(self.clean_segment(segment)) append(self.clean_segment(segment))
return segments return segments

Loading…
Cancel
Save