1
0
mirror of https://github.com/mikf/gallery-dl.git synced 2024-11-22 02:32:33 +01:00

[formatter] implement slice operator as format specifier

this allows using a slice operator alongside other (special) format
specifiers like J, to first join list elements to a string and then
trimming that with a slice.

{tags:J, /[:50]}
This commit is contained in:
Mike Fährmann 2022-06-25 16:45:34 +02:00
parent 241e82e18d
commit 54525d2e21
No known key found for this signature in database
GPG Key ID: 5680CA389D365A88
3 changed files with 48 additions and 7 deletions

View File

@ -139,6 +139,12 @@ Format specifiers can be used for advanced formatting by using the options provi
<td><code>{empty:?[/]/}</code></td>
<td><code></code></td>
</tr>
<tr>
<td><code>[&lt;start&gt;:&lt;stop&gt;]</code></td>
<td>Applies a <a href="https://python-reference.readthedocs.io/en/latest/docs/brackets/slicing.html">Slicing</a> operation to the current value, similar to <a href="#field-names">Field Names</a></td>
<td><code>{foo:[1:-1]}</code></td>
<td><code>oo&nbsp;Ba</code></td>
</tr>
<tr>
<td rowspan="2"><code>L&lt;maxlen&gt;/&lt;repl&gt;/</code></td>
<td rowspan="2">Replaces the entire output with <code>&lt;repl&gt;</code> if its length exceeds <code>&lt;maxlen&gt;</code></td>

View File

@ -232,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
@ -246,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:
@ -283,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":
@ -305,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:])

View File

@ -1,7 +1,7 @@
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# Copyright 2021 Mike Fährmann
# Copyright 2021-2022 Mike Fährmann
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License version 2 as
@ -135,6 +135,21 @@ class TestFormatter(unittest.TestCase):
self._run_test("{a[:50:2]}", v[:50:2])
self._run_test("{a[::]}" , v)
self._run_test("{a:[1:10]}" , v[1:10])
self._run_test("{a:[-10:-1]}", v[-10:-1])
self._run_test("{a:[5:]}" , v[5:])
self._run_test("{a:[50:]}", v[50:])
self._run_test("{a:[:5]}" , v[:5])
self._run_test("{a:[:50]}", v[:50])
self._run_test("{a:[:]}" , v)
self._run_test("{a:[1:10:2]}" , v[1:10:2])
self._run_test("{a:[-10:-1:2]}", v[-10:-1:2])
self._run_test("{a:[5::2]}" , v[5::2])
self._run_test("{a:[50::2]}", v[50::2])
self._run_test("{a:[:5:2]}" , v[:5:2])
self._run_test("{a:[:50:2]}", v[:50:2])
self._run_test("{a:[::]}" , v)
def test_maxlen(self):
v = self.kwdict["a"]
self._run_test("{a:L5/foo/}" , "foo")
@ -177,6 +192,9 @@ class TestFormatter(unittest.TestCase):
# join-and-replace
self._run_test("{l:J-/Rb/E/}", "a-E-c")
# join and slice
self._run_test("{l:J-/[1:-1]}", "-b-")
# optional-and-maxlen
self._run_test("{d[a]:?</>/L1/too long/}", "<too long>")
self._run_test("{d[c]:?</>/L5/too long/}", "")