From 9d1b02acd235e14b4565b5a31c8c4fb9a2aa1868 Mon Sep 17 00:00:00 2001 From: Charles Haley Date: Thu, 10 Jul 2025 12:41:45 +0100 Subject: [PATCH] Depending on ('' in 'abc') (empty string in string) to be true breaks my brain and IMO not readable. This is an alternate implementation that avoids that. --- src/calibre/utils/formatter_functions.py | 21 +++++++++------------ 1 file changed, 9 insertions(+), 12 deletions(-) diff --git a/src/calibre/utils/formatter_functions.py b/src/calibre/utils/formatter_functions.py index f19a589b3d..01d0ebcfe7 100644 --- a/src/calibre/utils/formatter_functions.py +++ b/src/calibre/utils/formatter_functions.py @@ -3489,19 +3489,16 @@ This can be useful to truncate a value. pat = re.compile(r'\[(.)(:(.*?))?\]') if not largest_unit: + highest_index = 0 for m in pat.finditer(template): - fmt_char = m.group(1) - match fmt_char.lower(): - case 'w' if largest_unit in 'dhms': - largest_unit = 'w' - case 'd' if largest_unit in 'hms': - largest_unit = 'd' - case 'h' if largest_unit in 'ms': - largest_unit = 'h' - case 'm' if largest_unit in 's': - largest_unit = 'm' - case 's' if not largest_unit: - largest_unit = 's' + try: + # We know that m.group(1) is a single character so the only + # exception possible is that the character is not in the string + dex = 'smhdw'.index(m.group(1).lower()) + highest_index = dex if dex > highest_index else highest_index + except Exception: + raise ValueError(_('The {} format specifier is not valid').format(m.group())) + largest_unit = 'smhdw'[highest_index] int_val = remainder = round(float(value)) if value else 0 weeks,remainder = divmod(remainder, 60*60*24*7) if largest_unit == 'w' else (-1,remainder)