aboutsummaryrefslogtreecommitdiffstats
path: root/gallery_dl/path.py
diff options
context:
space:
mode:
authorLibravatarUnit 193 <unit193@unit193.net>2025-08-16 07:00:40 -0400
committerLibravatarUnit 193 <unit193@unit193.net>2025-08-16 07:00:40 -0400
commit22e8d9823eb9fb802c926fb03a5fdccbea26f878 (patch)
treed399937a3bf139d386b8f5df2fc646b751c14719 /gallery_dl/path.py
parent0839cde5064bd6000162ee23b8445b99afe10068 (diff)
parent3d18761f620a294ea6c5bff13c5994b93b29f3ed (diff)
Update upstream source from tag 'upstream/1.30.3'
Update to upstream version '1.30.3' with Debian dir cbd3490f51b0ee3f2e172965318cd079b856367d
Diffstat (limited to 'gallery_dl/path.py')
-rw-r--r--gallery_dl/path.py86
1 files changed, 52 insertions, 34 deletions
diff --git a/gallery_dl/path.py b/gallery_dl/path.py
index 795564d..eecbd6c 100644
--- a/gallery_dl/path.py
+++ b/gallery_dl/path.py
@@ -90,6 +90,7 @@ class PathFormat():
restrict = config("path-restrict", "auto")
replace = config("path-replace", "_")
+ conv = config("path-convert")
if restrict == "auto":
restrict = "\\\\|/<>:\"?*" if WINDOWS else "/"
elif restrict == "unix":
@@ -100,10 +101,10 @@ class PathFormat():
restrict = "^0-9A-Za-z_."
elif restrict == "ascii+":
restrict = "^0-9@-[\\]-{ #-)+-.;=!}~"
- self.clean_segment = self._build_cleanfunc(restrict, replace)
+ self.clean_segment = _build_cleanfunc(restrict, replace, conv)
remove = config("path-remove", "\x00-\x1f\x7f")
- self.clean_path = self._build_cleanfunc(remove, "")
+ self.clean_path = _build_cleanfunc(remove, "")
strip = config("path-strip", "auto")
if strip == "auto":
@@ -122,7 +123,7 @@ class PathFormat():
basedir = config("base-directory")
sep = os.sep
if basedir is None:
- basedir = "." + sep + "gallery-dl" + sep
+ basedir = f".{sep}gallery-dl{sep}"
elif basedir:
basedir = util.expand_path(basedir)
altsep = os.altsep
@@ -133,37 +134,6 @@ class PathFormat():
basedir = self.clean_path(basedir)
self.basedirectory = basedir
- def _build_cleanfunc(self, chars, repl):
- if not chars:
- return util.identity
- elif isinstance(chars, dict):
- if 0 not in chars:
- chars = self._process_repl_dict(chars)
- chars[0] = None
-
- def func(x, table=str.maketrans(chars)):
- return x.translate(table)
- elif len(chars) == 1:
- def func(x, c=chars, r=repl):
- return x.replace(c, r)
- else:
- return functools.partial(util.re(f"[{chars}]").sub, repl)
- return func
-
- def _process_repl_dict(self, chars):
- # can't modify 'chars' while *directly* iterating over its keys
- for char in [c for c in chars if len(c) > 1]:
- if len(char) == 3 and char[1] == "-":
- citer = range(ord(char[0]), ord(char[2])+1)
- else:
- citer = char
-
- repl = chars.pop(char)
- for c in citer:
- chars[c] = repl
-
- return chars
-
def open(self, mode="wb"):
"""Open file and return a corresponding file object"""
try:
@@ -382,3 +352,51 @@ class PathFormat():
break
self.set_mtime()
+
+
+def _build_convertfunc(func, conv):
+ if len(conv) <= 1:
+ conv = formatter._CONVERSIONS[conv]
+ return lambda x: conv(func(x))
+
+ def convert_many(x):
+ x = func(x)
+ for conv in convs:
+ x = conv(x)
+ return x
+ convs = [formatter._CONVERSIONS[c] for c in conv]
+ return convert_many
+
+
+def _build_cleanfunc(chars, repl, conv=None):
+ if not chars:
+ func = util.identity
+ elif isinstance(chars, dict):
+ if 0 not in chars:
+ chars = _process_repl_dict(chars)
+ chars[0] = None
+
+ def func(x):
+ return x.translate(table)
+ table = str.maketrans(chars)
+ elif len(chars) == 1:
+ def func(x):
+ return x.replace(chars, repl)
+ else:
+ func = functools.partial(util.re(f"[{chars}]").sub, repl)
+ return _build_convertfunc(func, conv) if conv else func
+
+
+def _process_repl_dict(chars):
+ # can't modify 'chars' while *directly* iterating over its keys
+ for char in [c for c in chars if len(c) > 1]:
+ if len(char) == 3 and char[1] == "-":
+ citer = range(ord(char[0]), ord(char[2])+1)
+ else:
+ citer = char
+
+ repl = chars.pop(char)
+ for c in citer:
+ chars[c] = repl
+
+ return chars