From 942bceaa61365a5af2b52a388f2633af68efe265 Mon Sep 17 00:00:00 2001 From: Charles Haley Date: Tue, 9 May 2023 18:29:30 +0100 Subject: [PATCH] Improvements to template function identifier_in_list() --- manual/template_lang.rst | 1 + src/calibre/utils/formatter_functions.py | 48 +++++++++++++++--------- 2 files changed, 31 insertions(+), 18 deletions(-) diff --git a/manual/template_lang.rst b/manual/template_lang.rst index c11df09e88..7a376818f4 100644 --- a/manual/template_lang.rst +++ b/manual/template_lang.rst @@ -558,6 +558,7 @@ In `GPM` the functions described in `Single Function Mode` all require an additi * ``fractional_part(x)`` -- returns the value after the decimal point. For example, ``fractional_part(3.14)`` returns ``0.14``. Throws an exception if ``x`` is not a number. * ``has_cover()`` -- return ``'Yes'`` if the book has a cover, otherwise the empty string. * ``has_extra_files([pattern])`` -- returns the count of extra files, otherwise '' (the empty string). If the optional parameter ``pattern`` (a regular expression) is supplied then the list is filtered to files that match ``pattern`` before the files are counted. The pattern match is case insensitive. See also the functions ``extra_file_names()``, ``extra_file_size()`` and ``extra_file_modtime()``. This function can be used only in the GUI. +* ``identifier_in_list(val, id_name [, found_val, not_found_val])`` -- treat ``val`` as a list of identifiers separated by commas. An identifier has the format ``id_name:value``. The ``id_name`` parameter is the id_name text to search for, either ``id_name`` or ``id_name:regexp``. The first case matches if there is any identifier matching that id_name. The second case matches if id_name matches an identifier and the regexp matches the identifier's value. If ``found_val`` and ``not_found_val`` are provided then if there is a match then return ``found_val``, otherwise return ``not_found_val``. If ``found_val`` and ``not_found_val`` are not provided then if there is a match then return the ``indentfier:value`` pair, otherwise the empty string (``''``). * ``is_marked()`` -- check whether the book is `marked` in calibre. If it is then return the value of the mark, either ``'true'`` (lower case) or a comma-separated list of named marks. Returns ``''`` (the empty string) if the book is not marked. This function works only in the GUI. * ``language_codes(lang_strings)`` -- return the `language codes `_ for the language names passed in `lang_strings`. The strings must be in the language of the current locale. ``Lang_strings`` is a comma-separated list. * ``list_contains(value, separator, [ pattern, found_val, ]* not_found_val)`` -- (Alias of ``in_list``) Interpreting the value as a list of items separated by ``separator``, evaluate the ``pattern`` against each value in the list. If the ``pattern`` matches any value then return ``found_val``, otherwise return ``not_found_val``. The ``pattern`` and ``found_value`` can be repeated as many times as desired, permitting returning different values depending on the search. The patterns are checked in order. The first match is returned. Aliases: ``in_list()``, ``list_contains()`` diff --git a/src/calibre/utils/formatter_functions.py b/src/calibre/utils/formatter_functions.py index 6f82cf7dda..814c314139 100644 --- a/src/calibre/utils/formatter_functions.py +++ b/src/calibre/utils/formatter_functions.py @@ -796,28 +796,40 @@ class BuiltinStrInList(BuiltinFormatterFunction): class BuiltinIdentifierInList(BuiltinFormatterFunction): name = 'identifier_in_list' - arg_count = 4 + arg_count = -1 category = 'List lookup' - __doc__ = doc = _('identifier_in_list(val, id, found_val, not_found_val) -- ' - 'treat val as a list of identifiers separated by commas, ' - 'comparing the string against each value in the list. An identifier ' - 'has the format "identifier:value". The id parameter should be ' - 'either "id" or "id:regexp". The first case matches if there is any ' - 'identifier with that id. The second case matches if the regexp ' - 'matches the identifier\'s value. If there is a match, ' - 'return found_val, otherwise return not_found_val.') + __doc__ = doc = _('identifier_in_list(val, id_name [, found_val, not_found_val]) -- ' + 'treat val as a list of identifiers separated by commas. An identifier ' + 'has the format "id_name:value". The id_name parameter is the id_name ' + 'text to search for, either "id_name" or "id_name:regexp". The first case ' + 'matches if there is any identifier matching that id_name. The second ' + 'case matches if id_name matches an identifier and the regexp ' + 'matches the identifier\'s value. If found_val and not_found_val ' + 'are provided then if there is a match then return found_val, otherwise ' + 'return not_found_val. If found_val and not_found_val are not ' + 'provided then if there is a match then return the indentfier:value ' + 'pair, otherwise the empty string.') + + def evaluate(self, formatter, kwargs, mi, locals, val, ident, *args): + if len(args) == 0: + fv_is_id = True + nfv = '' + elif len(args) == 2: + fv_is_id = False + fv = args[0] + nfv = args[1] + else: + raise ValueError(_("{} requires 2 or 4 arguments").format(self.name)) - def evaluate(self, formatter, kwargs, mi, locals, val, ident, fv, nfv): l = [v.strip() for v in val.split(',') if v.strip()] - (id, _, regexp) = ident.partition(':') - if not id: + (id_, _, regexp) = ident.partition(':') + if not id_: return nfv - id += ':' - if l: - for v in l: - if v.startswith(id): - if not regexp or re.search(regexp, v[len(id):], flags=re.I): - return fv + for candidate in l: + i, _, v = candidate.partition(':') + if v and i == id_: + if not regexp or re.search(regexp, v, flags=re.I): + return candidate if fv_is_id else fv return nfv