summaryrefslogtreecommitdiffstats
path: root/gallery_dl/formatter.py
diff options
context:
space:
mode:
Diffstat (limited to 'gallery_dl/formatter.py')
-rw-r--r--gallery_dl/formatter.py30
1 files changed, 26 insertions, 4 deletions
diff --git a/gallery_dl/formatter.py b/gallery_dl/formatter.py
index fc36fa2..2ff48c3 100644
--- a/gallery_dl/formatter.py
+++ b/gallery_dl/formatter.py
@@ -9,6 +9,7 @@
"""String formatters"""
import os
+import sys
import time
import string
import _string
@@ -255,7 +256,11 @@ def parse_field_name(field_name):
func = operator.itemgetter
try:
if ":" in key:
- key = _slice(key)
+ if key[0] == "b":
+ func = _bytesgetter
+ key = _slice(key[1:])
+ else:
+ key = _slice(key)
else:
key = key.strip("\"'")
except TypeError:
@@ -276,6 +281,14 @@ def _slice(indices):
)
+def _bytesgetter(slice, encoding=sys.getfilesystemencoding()):
+
+ def apply_slice_bytes(obj):
+ return obj.encode(encoding)[slice].decode(encoding, "ignore")
+
+ return apply_slice_bytes
+
+
def _build_format_func(format_spec, default):
if format_spec:
return _FORMAT_SPECIFIERS.get(
@@ -295,11 +308,20 @@ def _parse_optional(format_spec, default):
def _parse_slice(format_spec, default):
indices, _, format_spec = format_spec.partition("]")
- slice = _slice(indices[1:])
fmt = _build_format_func(format_spec, default)
- def apply_slice(obj):
- return fmt(obj[slice])
+ if indices[1] == "b":
+ slice_bytes = _bytesgetter(_slice(indices[2:]))
+
+ def apply_slice(obj):
+ return fmt(slice_bytes(obj))
+
+ else:
+ slice = _slice(indices[1:])
+
+ def apply_slice(obj):
+ return fmt(obj[slice])
+
return apply_slice