From cbf2bb0c4e58ad787d1a638fc48cc43bb515ac0f Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Wed, 24 Jul 2013 11:35:48 +0530 Subject: [PATCH] Tentative {virtual_libraries} template support This should allow the use of {virtual_libraries} in custom column templates, with good performance, thanks to search caching. --- src/calibre/db/cache.py | 12 ++++++++++++ src/calibre/db/lazy.py | 10 ++++++++++ 2 files changed, 22 insertions(+) diff --git a/src/calibre/db/cache.py b/src/calibre/db/cache.py index f909563fa5..c8f28a1967 100644 --- a/src/calibre/db/cache.py +++ b/src/calibre/db/cache.py @@ -1620,6 +1620,18 @@ class Cache(object): if cover and os.path.exists(cover): self._set_field('cover', {book_id:1}) self.backend.restore_book(book_id, path, formats) + + @read_api + def virtual_libraries_for_books(self, book_ids): + libraries = tuple(self._pref('virtual_libraries', {}).iterkeys()) + ans = {book_id:[] for book_id in book_ids} + for lib in libraries: + books = self._search(lib) # We deliberately dont use book_ids as we want to use the search cache + for book in book_ids: + if book in books: + ans[book].append(lib) + return {k:tuple(sorted(v, key=sort_key)) for k, v in book_ids} + # }}} class SortKey(object): # {{{ diff --git a/src/calibre/db/lazy.py b/src/calibre/db/lazy.py index 4b1740154b..74cf5af94b 100644 --- a/src/calibre/db/lazy.py +++ b/src/calibre/db/lazy.py @@ -231,6 +231,15 @@ def composite_getter(mi, field, metadata, book_id, cache, formatter, template_ca template_cache=template_cache).strip() return ret +def virtual_libraries_getter(dbref, book_id, cache): + try: + return cache[field] + except KeyError: + db = dbref() + vls = db.virtual_libraries_for_books((book_id,))[book_id] + ret = cache[field] = ', '.join(vls) + return ret + getters = { 'title':simple_getter('title', _('Unknown')), 'title_sort':simple_getter('sort', _('Unknown')), @@ -247,6 +256,7 @@ getters = { 'series_index':series_index_getter(), 'application_id':lambda x, book_id, y: book_id, 'id':lambda x, book_id, y: book_id, + 'virtual_libraries':virtual_libraries_getter, } for field in ('comments', 'publisher', 'identifiers', 'series', 'rating'):