From e5b664c162551c708708ac81e2196361c1c9c120 Mon Sep 17 00:00:00 2001 From: Charles Haley Date: Sat, 17 Dec 2022 17:44:54 +0000 Subject: [PATCH 1/4] Clean up the new composite splitter API to ignore case and ignore dups. Make Quickview use the new API. --- src/calibre/db/cache.py | 8 +++++++- src/calibre/gui2/dialogs/quickview.py | 7 +++---- 2 files changed, 10 insertions(+), 5 deletions(-) diff --git a/src/calibre/db/cache.py b/src/calibre/db/cache.py index 80b925e770..82ed243a14 100644 --- a/src/calibre/db/cache.py +++ b/src/calibre/db/cache.py @@ -2351,9 +2351,15 @@ class Cache: @read_api def split_if_is_multiple_composite(self, f, v): + ''' + If f is a composite column lookup key and the column is is_multiple then + split comma-separated v into unique non-empty values. The uniqueness + comparison is case-insensitive. If values are case-insensitive equals + then the last is returned. + ''' fm = self.field_metadata.get(f, None) if fm and fm['datatype'] == 'composite' and fm['is_multiple']: - return [v.strip() for v in v.split(',') if v.strip()] + return list({v.strip().lower() : v.strip() for v in v.split(',') if v.strip()}.values()) return v @read_api diff --git a/src/calibre/gui2/dialogs/quickview.py b/src/calibre/gui2/dialogs/quickview.py index 2a17db7c2d..3fcaa0e7c6 100644 --- a/src/calibre/gui2/dialogs/quickview.py +++ b/src/calibre/gui2/dialogs/quickview.py @@ -542,10 +542,9 @@ class Quickview(QDialog, Ui_Quickview): self.books_table.setRowCount(0) mi = self.db.new_api.get_proxy_metadata(book_id) - vals = mi.get(key, None) - if self.fm[key]['datatype'] == 'composite' and self.fm[key]['is_multiple']: - sep = self.fm[key]['is_multiple'].get('cache_to_list', ',') - vals = [v.strip() for v in vals.split(sep) if v.strip()] + print('aaa', key, mi.get(key, None)) + vals = self.db.new_api.split_if_is_multiple_composite(key, mi.get(key, None)) + print('bbb', vals) try: # Check if we are in the GridView and there are no values for the # selected column. In this case switch the column to 'authors' From 28b48b4cd234fabc797a82c103425233b1e6f8b1 Mon Sep 17 00:00:00 2001 From: Charles Haley Date: Sun, 18 Dec 2022 11:29:00 +0000 Subject: [PATCH 2/4] Remove print statements --- src/calibre/gui2/dialogs/quickview.py | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/calibre/gui2/dialogs/quickview.py b/src/calibre/gui2/dialogs/quickview.py index 3fcaa0e7c6..3c77b86a12 100644 --- a/src/calibre/gui2/dialogs/quickview.py +++ b/src/calibre/gui2/dialogs/quickview.py @@ -542,9 +542,7 @@ class Quickview(QDialog, Ui_Quickview): self.books_table.setRowCount(0) mi = self.db.new_api.get_proxy_metadata(book_id) - print('aaa', key, mi.get(key, None)) vals = self.db.new_api.split_if_is_multiple_composite(key, mi.get(key, None)) - print('bbb', vals) try: # Check if we are in the GridView and there are no values for the # selected column. In this case switch the column to 'authors' From 45275a48242a530d4720a433bb25fb20d5ca5ea0 Mon Sep 17 00:00:00 2001 From: Charles Haley Date: Sun, 18 Dec 2022 11:29:13 +0000 Subject: [PATCH 3/4] Fix the docstring for the composite delegate. --- src/calibre/gui2/library/delegates.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/calibre/gui2/library/delegates.py b/src/calibre/gui2/library/delegates.py index 4dcfc988b3..eb9ac48bf1 100644 --- a/src/calibre/gui2/library/delegates.py +++ b/src/calibre/gui2/library/delegates.py @@ -773,7 +773,7 @@ class CcTemplateDelegate(QStyledItemDelegate): # {{{ def __init__(self, parent): ''' - Delegate for custom_column bool data. + Delegate for composite custom_columns. ''' QStyledItemDelegate.__init__(self, parent) self.disallow_edit = gprefs['edit_metadata_templates_only_F2_on_booklist'] From f1f597a06b45596ccc9f3ca3707129a1b06c1de5 Mon Sep 17 00:00:00 2001 From: Charles Haley Date: Sun, 18 Dec 2022 11:32:32 +0000 Subject: [PATCH 4/4] Change the implementation to not do case insensitive comparison, because no other part of calibre does that with composites. For correctness get the split value from field metadata, even though at the moment it can only be a comma. Return a list for compatibility with other field getters. There are a fair number of places that do isinstance(v, list). --- src/calibre/db/cache.py | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/src/calibre/db/cache.py b/src/calibre/db/cache.py index 82ed243a14..d3164b5ae1 100644 --- a/src/calibre/db/cache.py +++ b/src/calibre/db/cache.py @@ -2350,17 +2350,18 @@ class Cache: return self._books_for_field(f.name, int(item_id_or_composite_value)) @read_api - def split_if_is_multiple_composite(self, f, v): + def split_if_is_multiple_composite(self, f, val): ''' If f is a composite column lookup key and the column is is_multiple then - split comma-separated v into unique non-empty values. The uniqueness - comparison is case-insensitive. If values are case-insensitive equals - then the last is returned. + split v into unique non-empty values. The comparison is case sensitive. + Order is not preserved. Return a list() for compatibility with proxy + metadata field getters, for example tags. ''' fm = self.field_metadata.get(f, None) if fm and fm['datatype'] == 'composite' and fm['is_multiple']: - return list({v.strip().lower() : v.strip() for v in v.split(',') if v.strip()}.values()) - return v + sep = fm['is_multiple'].get('cache_to_list', ',') + return (list(set(v.strip() for v in val.split(sep) if v.strip()))) + return val @read_api def data_for_find_identical_books(self):