From 68bff76d900f57bcbcf67d7e5a8a46e997aa75c3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mike=20F=C3=A4hrmann?= Date: Thu, 19 Sep 2024 13:50:52 +0200 Subject: [PATCH] [formatter] implement 'L' conversion --- docs/formatting.md | 10 ++++++++-- gallery_dl/formatter.py | 1 + test/test_formatter.py | 3 +++ 3 files changed, 12 insertions(+), 2 deletions(-) diff --git a/docs/formatting.md b/docs/formatting.md index 1e695bf2..97801653 100644 --- a/docs/formatting.md +++ b/docs/formatting.md @@ -76,6 +76,12 @@ Conversion specifiers allow to *convert* the value to a different form or type. {tags!j} ["sun", "tree", "water"] + + L + Return the length of a value + {foo!L} + 7 + t Trim a string, i.e. remove leading and trailing whitespace characters @@ -84,13 +90,13 @@ Conversion specifiers allow to *convert* the value to a different form or type. T - Convert a datetime object to a unix timestamp + Convert a datetime object to a Unix timestamp {date!T} 1262304000 d - Convert a unix timestamp to a datetime object + Convert a Unix timestamp to a datetime object {created!d} 2010-01-01 00:00:00 diff --git a/gallery_dl/formatter.py b/gallery_dl/formatter.py index b50bd643..e662c34e 100644 --- a/gallery_dl/formatter.py +++ b/gallery_dl/formatter.py @@ -485,6 +485,7 @@ _CONVERSIONS = { "C": string.capwords, "j": util.json_dumps, "t": str.strip, + "L": len, "T": util.datetime_to_timestamp_string, "d": text.parse_timestamp, "U": text.unescape, diff --git a/test/test_formatter.py b/test/test_formatter.py index 38c22ab9..f1d752da 100644 --- a/test/test_formatter.py +++ b/test/test_formatter.py @@ -67,6 +67,9 @@ class TestFormatter(unittest.TestCase): self._run_test("{l!j}", '["a", "b", "c"]') self._run_test("{dt!j}", '"2010-01-01 00:00:00"') self._run_test("{a!g}", "hello-world") + self._run_test("{a!L}", 11) + self._run_test("{l!L}", 3) + self._run_test("{d!L}", 3) with self.assertRaises(KeyError): self._run_test("{a!q}", "hello world")