From 9249fd8a3e79ebe8a9dc7049e6fad8878d1f8c2d Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Fri, 12 Jul 2013 14:25:39 +0530 Subject: [PATCH] Refactor the author data api for multiple authors --- src/calibre/db/cache.py | 17 +++++++++-------- src/calibre/db/tests/legacy.py | 2 ++ src/calibre/db/view.py | 6 ++---- 3 files changed, 13 insertions(+), 12 deletions(-) diff --git a/src/calibre/db/cache.py b/src/calibre/db/cache.py index ce921537f2..2c74a31438 100644 --- a/src/calibre/db/cache.py +++ b/src/calibre/db/cache.py @@ -161,7 +161,8 @@ class Cache(object): def _get_metadata(self, book_id, get_user_categories=True): # {{{ mi = Metadata(None, template_cache=self.formatter_template_cache) author_ids = self._field_ids_for('authors', book_id) - aut_list = [self._author_data(i) for i in author_ids] + adata = self._author_data(author_ids) + aut_list = [adata[i] for i in author_ids] aum = [] aus = {} aul = {} @@ -388,17 +389,17 @@ class Cache(object): raise ValueError('%s is not a many-one or many-many field' % field) @read_api - def author_data(self, author_id): + def author_data(self, author_ids): ''' Return author data as a dictionary with keys: name, sort, link - If no author with the specified id is found an empty dictionary is - returned. + If no authors with the specified ids are found an empty dictionary is + returned. If author_ids is None, data for all authors is returned. ''' - try: - return self.fields['authors'].author_data(author_id) - except (KeyError, IndexError): - return {} + af = self.fields['authors'] + if author_ids is None: + author_ids = tuple(af.table.id_map) + return {aid:af.author_data(aid) for aid in author_ids if aid in af.table.id_map} @read_api def format_metadata(self, book_id, fmt, allow_cache=True): diff --git a/src/calibre/db/tests/legacy.py b/src/calibre/db/tests/legacy.py index b816341b5a..b28a5cb93e 100644 --- a/src/calibre/db/tests/legacy.py +++ b/src/calibre/db/tests/legacy.py @@ -243,6 +243,8 @@ class LegacyTest(BaseTest): '_set_title', '_set_custom', '_update_author_in_cache', # Feeds are now stored in the config folder 'get_feeds', 'get_feed', 'update_feed', 'remove_feeds', 'add_feed', 'set_feeds', + # Obsolete/broken methods + 'author_id', # replaced by get_author_id } SKIP_ARGSPEC = { '__init__', 'get_next_series_num_for', 'has_book', 'author_sort_from_authors', 'all_tags', diff --git a/src/calibre/db/view.py b/src/calibre/db/view.py index 3f135860d9..ecd5182232 100644 --- a/src/calibre/db/view.py +++ b/src/calibre/db/view.py @@ -189,10 +189,8 @@ class View(object): id_ = idx if index_is_id else self.index_to_id(idx) with self.cache.read_lock: ids = self.cache._field_ids_for('authors', id_) - ans = [] - for id_ in ids: - data = self.cache._author_data(id_) - ans.append(':::'.join((data['name'], data['sort'], data['link']))) + adata = self.cache._author_data(ids) + ans = [':::'.join((adata[aid]['name'], adata[aid]['sort'], adata[aid]['link'])) for aid in ids if aid in adata] return ':#:'.join(ans) if ans else default_value def multisort(self, fields=[], subsort=False, only_ids=None):