From ca8f29db0bd79392a51a59ef7e57598b54b161c3 Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Fri, 12 Jul 2013 14:43:08 +0530 Subject: [PATCH] get_*_with_ids() API --- src/calibre/db/cache.py | 2 +- src/calibre/db/legacy.py | 6 ++++++ src/calibre/db/tests/legacy.py | 29 ++++++++++++++++++----------- 3 files changed, 25 insertions(+), 12 deletions(-) diff --git a/src/calibre/db/cache.py b/src/calibre/db/cache.py index 2c74a31438..eca5a2a395 100644 --- a/src/calibre/db/cache.py +++ b/src/calibre/db/cache.py @@ -389,7 +389,7 @@ class Cache(object): raise ValueError('%s is not a many-one or many-many field' % field) @read_api - def author_data(self, author_ids): + def author_data(self, author_ids=None): ''' Return author data as a dictionary with keys: name, sort, link diff --git a/src/calibre/db/legacy.py b/src/calibre/db/legacy.py index 398d1b1a86..7f375756e7 100644 --- a/src/calibre/db/legacy.py +++ b/src/calibre/db/legacy.py @@ -73,6 +73,12 @@ class LibraryDatabase(object): for func, field in {'all_authors':'authors', 'all_titles':'title', 'all_tags2':'tags', 'all_series':'series', 'all_publishers':'publisher'}.iteritems(): setattr(self, func, partial(self.field_id_map, field)) self.all_tags = lambda : list(self.all_tag_names()) + self.get_authors_with_ids = lambda : [[aid, adata['name'], adata['sort'], adata['link']] for aid, adata in self.new_api.author_data().iteritems()] + self.get_tags_with_ids = lambda : [[tid, tag] for tid, tag in self.new_api.get_id_map('tags').iteritems()] + self.get_series_with_ids = lambda : [[tid, tag] for tid, tag in self.new_api.get_id_map('series').iteritems()] + self.get_publishers_with_ids = lambda : [[tid, tag] for tid, tag in self.new_api.get_id_map('publisher').iteritems()] + self.get_ratings_with_ids = lambda : [[tid, tag] for tid, tag in self.new_api.get_id_map('rating').iteritems()] + self.get_languages_with_ids = lambda : [[tid, tag] for tid, tag in self.new_api.get_id_map('languages').iteritems()] for func in ( 'standard_field_keys', 'custom_field_keys', 'all_field_keys', diff --git a/src/calibre/db/tests/legacy.py b/src/calibre/db/tests/legacy.py index b28a5cb93e..b731dcc819 100644 --- a/src/calibre/db/tests/legacy.py +++ b/src/calibre/db/tests/legacy.py @@ -143,12 +143,12 @@ class LegacyTest(BaseTest): 'all_tag_names':[()], 'all_series_names':[()], 'all_publisher_names':[()], - 'all_authors':[()], - 'all_tags2':[()], - 'all_tags':[()], - 'all_publishers':[()], - 'all_titles':[()], - 'all_series':[()], + '!all_authors':[()], + '!all_tags2':[()], + '@all_tags':[()], + '!all_publishers':[()], + '!all_titles':[()], + '!all_series':[()], 'standard_field_keys':[()], 'all_field_keys':[()], 'searchable_fields':[()], @@ -156,16 +156,23 @@ class LegacyTest(BaseTest): 'metadata_for_field':[('title',), ('tags',)], 'sortable_field_keys':[()], 'custom_field_keys':[(True,), (False,)], - 'get_usage_count_by_id':[('authors',), ('tags',), ('series',), ('publisher',), ('#tags',), ('languages',)], + '!get_usage_count_by_id':[('authors',), ('tags',), ('series',), ('publisher',), ('#tags',), ('languages',)], 'get_field':[(1, 'title'), (2, 'tags'), (0, 'rating'), (1, 'authors'), (2, 'series'), (1, '#tags')], 'all_formats':[()], + 'get_authors_with_ids':[()], + '!get_tags_with_ids':[()], + '!get_series_with_ids':[()], + '!get_publishers_with_ids':[()], + '!get_ratings_with_ids':[()], + '!get_languages_with_ids':[()], }.iteritems(): for a in args: fmt = lambda x: x - if meth in {'get_usage_count_by_id', 'all_series', 'all_authors', 'all_tags2', 'all_publishers', 'all_titles'}: - fmt = dict - elif meth in {'all_tags'}: - fmt = frozenset + if meth[0] in {'!', '@'}: + fmt = {'!':dict, '@':frozenset}[meth[0]] + meth = meth[1:] + elif meth == 'get_authors_with_ids': + fmt = lambda val:{x[0]:tuple(x[1:]) for x in val} self.assertEqual(fmt(getattr(db, meth)(*a)), fmt(getattr(ndb, meth)(*a)), 'The method: %s() returned different results for argument %s' % (meth, a)) db.close()