From c4404f52c821f0ee4b2680bd53b62c748d201951 Mon Sep 17 00:00:00 2001 From: Charles Haley <> Date: Tue, 27 Sep 2011 18:42:11 +0200 Subject: [PATCH] Add the list_equals formatter function. --- src/calibre/manual/template_lang.rst | 1 + src/calibre/utils/formatter_functions.py | 20 +++++++++++++++++++- 2 files changed, 20 insertions(+), 1 deletion(-) diff --git a/src/calibre/manual/template_lang.rst b/src/calibre/manual/template_lang.rst index e7b8594708..8953bda2f7 100644 --- a/src/calibre/manual/template_lang.rst +++ b/src/calibre/manual/template_lang.rst @@ -266,6 +266,7 @@ The following functions are available in addition to those described in single-f * ``has_cover()`` -- return ``Yes`` if the book has a cover, otherwise return the empty string * ``not(value)`` -- returns the string "1" if the value is empty, otherwise returns the empty string. This function works well with test or first_non_empty. You can have as many values as you want. * ``list_difference(list1, list2, separator)`` -- return a list made by removing from `list1` any item found in `list2`, using a case-insensitive compare. The items in `list1` and `list2` are separated by separator, as are the items in the returned list. + * ``list_equals(list1, sep1, list2, sep2, yes_val, no_val) -- return `yes_val` if list1 and list2 contain the same items, otherwise return `no_val`. The items are determined by splitting each list using the appropriate separator character (`sep1` or `sep2`). The order of items in the lists is not relevant. The compare is case insensitive. * ``list_intersection(list1, list2, separator)`` -- return a list made by removing from `list1` any item not found in `list2`, using a case-insensitive compare. The items in `list1` and `list2` are separated by separator, as are the items in the returned list. * ``list_sort(list, direction, separator)`` -- return list sorted using a case-insensitive sort. If `direction` is zero, the list is sorted ascending, otherwise descending. The list items are separated by separator, as are the items in the returned list. * ``list_union(list1, list2, separator)`` -- return a list made by merging the items in list1 and list2, removing duplicate items using a case-insensitive compare. If items differ in case, the one in list1 is used. The items in list1 and list2 are separated by separator, as are the items in the returned list. diff --git a/src/calibre/utils/formatter_functions.py b/src/calibre/utils/formatter_functions.py index b5163faa74..49472d0ca9 100644 --- a/src/calibre/utils/formatter_functions.py +++ b/src/calibre/utils/formatter_functions.py @@ -965,6 +965,24 @@ class BuiltinListSort(BuiltinFormatterFunction): res = [l.strip() for l in list1.split(separator) if l.strip()] return ', '.join(sorted(res, key=sort_key, reverse=direction != "0")) +class BuiltinListEquals(BuiltinFormatterFunction): + name = 'list_equals' + arg_count = 6 + category = 'List Manipulation' + __doc__ = doc = _('list_equals(list1, sep1, list2, sep2, yes_val, no_val) -- ' + 'return yes_val if list1 and list2 contain the same items, ' + 'otherwise return `no_val. The items are determined by splitting ' + 'each list using the appropriate separator character (sep1 or ' + 'sep2). The order of items in the lists is not relevant. ' + 'The compare is case insensitive.') + + def evaluate(self, formatter, kwargs, mi, locals, list1, sep1, list2, sep2, yes_val, no_val): + s1 = set([icu_lower(l.strip()) for l in list1.split(sep1) if l.strip()]) + s2 = set([icu_lower(l.strip()) for l in list2.split(sep2) if l.strip()]) + if s1 == s2: + return yes_val + return no_val + class BuiltinToday(BuiltinFormatterFunction): name = 'today' arg_count = 0 @@ -1045,7 +1063,7 @@ _formatter_builtins = [ BuiltinFormatNumber(), BuiltinFormatsModtimes(), BuiltinFormatsSizes(), BuiltinHasCover(), BuiltinHumanReadable(), BuiltinIdentifierInList(), BuiltinIfempty(), BuiltinLanguageCodes(), BuiltinLanguageStrings(), - BuiltinInList(), BuiltinListDifference(), + BuiltinInList(), BuiltinListDifference(), BuiltinListEquals(), BuiltinListIntersection(), BuiltinListitem(), BuiltinListSort(), BuiltinListUnion(), BuiltinLookup(), BuiltinLowercase(), BuiltinMultiply(), BuiltinNot(),