diff options
| author | 2022-06-28 19:54:18 -0400 | |
|---|---|---|
| committer | 2022-06-28 19:54:18 -0400 | |
| commit | ce35450b5308adab049c5bd99095986d4c607027 (patch) | |
| tree | f0c2b600f8ef720941bdf615164b942c6c4a5d07 /gallery_dl/formatter.py | |
| parent | 25442ea49f031d4d2df3353dd7e9ad2080e332da (diff) | |
New upstream version 1.22.3.upstream/1.22.3
Diffstat (limited to 'gallery_dl/formatter.py')
| -rw-r--r-- | gallery_dl/formatter.py | 32 |
1 files changed, 25 insertions, 7 deletions
diff --git a/gallery_dl/formatter.py b/gallery_dl/formatter.py index d1b3a8a..107c8ed 100644 --- a/gallery_dl/formatter.py +++ b/gallery_dl/formatter.py @@ -14,6 +14,7 @@ import string import _string import datetime import operator +import functools from . import text, util _CACHE = {} @@ -231,12 +232,7 @@ def parse_field_name(field_name): func = operator.itemgetter try: if ":" in key: - start, _, stop = key.partition(":") - stop, _, step = stop.partition(":") - start = int(start) if start else None - stop = int(stop) if stop else None - step = int(step) if step else None - key = slice(start, stop, step) + key = _slice(key) except TypeError: pass # key is an integer @@ -245,6 +241,16 @@ def parse_field_name(field_name): return first, funcs +def _slice(indices): + start, _, stop = indices.partition(":") + stop, _, step = stop.partition(":") + return slice( + int(start) if start else None, + int(stop) if stop else None, + int(step) if step else None, + ) + + def parse_format_spec(format_spec, conversion): fmt = build_format_func(format_spec) if not conversion: @@ -257,7 +263,7 @@ def parse_format_spec(format_spec, conversion): "u": str.upper, "c": str.capitalize, "C": string.capwords, - "j": json.dumps, + "j": functools.partial(json.dumps, default=str), "t": str.strip, "T": util.datetime_to_timestamp_string, "d": text.parse_timestamp, @@ -282,6 +288,8 @@ def build_format_func(format_spec): fmt = format_spec[0] if fmt == "?": return _parse_optional(format_spec) + if fmt == "[": + return _parse_slice(format_spec) if fmt == "L": return _parse_maxlen(format_spec) if fmt == "J": @@ -304,6 +312,16 @@ def _parse_optional(format_spec): return optional +def _parse_slice(format_spec): + indices, _, format_spec = format_spec.partition("]") + slice = _slice(indices[1:]) + fmt = build_format_func(format_spec) + + def apply_slice(obj): + return fmt(obj[slice]) + return apply_slice + + def _parse_maxlen(format_spec): maxlen, replacement, format_spec = format_spec.split("/", 2) maxlen = text.parse_int(maxlen[1:]) |
