From eac44f83b8d80e5820d567f44027bd7f6b676e62 Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Mon, 15 Jul 2013 18:53:55 +0530 Subject: [PATCH] More API --- src/calibre/db/legacy.py | 19 +++++++++++++++++++ src/calibre/db/tests/legacy.py | 8 +++++++- src/calibre/db/view.py | 3 +++ 3 files changed, 29 insertions(+), 1 deletion(-) diff --git a/src/calibre/db/legacy.py b/src/calibre/db/legacy.py index fef440dd6d..ec480c3d2d 100644 --- a/src/calibre/db/legacy.py +++ b/src/calibre/db/legacy.py @@ -65,6 +65,7 @@ class LibraryDatabase(object): cache.init() self.data = View(cache) self.id = self.data.index_to_id + self.count = self.data.count self.get_property = self.data.get_property @@ -84,6 +85,10 @@ class LibraryDatabase(object): delattr(self, x) # Library wide properties {{{ + @property + def prefs(self): + return self.new_api.backend.prefs + @property def field_metadata(self): return self.backend.field_metadata @@ -521,6 +526,18 @@ for prop in ('author_sort', 'authors', 'comment', 'comments', 'publisher', return func setattr(LibraryDatabase, prop, MT(getter(prop))) +for prop in ('series', 'publisher'): + def getter(field): + def func(self, index, index_is_id=False): + book_id = index if index_is_id else self.id(index) + ans = self.new_api.field_ids_for(field, book_id) + try: + return ans[0] + except IndexError: + pass + return func + setattr(LibraryDatabase, prop + '_id', MT(getter(prop))) + LibraryDatabase.format_hash = MT(lambda self, book_id, fmt:self.new_api.format_hash(book_id, fmt)) LibraryDatabase.index = MT(lambda self, book_id, cache=False:self.data.id_to_index(book_id)) LibraryDatabase.has_cover = MT(lambda self, book_id:self.new_api.field_for('cover', book_id)) @@ -590,6 +607,8 @@ LibraryDatabase.all_tags = MT(lambda self: list(self.all_tag_names())) LibraryDatabase.get_all_identifier_types = MT(lambda self: list(self.new_api.fields['identifiers'].table.all_identifier_types())) LibraryDatabase.get_authors_with_ids = MT( lambda self: [[aid, adata['name'], adata['sort'], adata['link']] for aid, adata in self.new_api.author_data().iteritems()]) +LibraryDatabase.get_author_id = MT( + lambda self, author: {icu_lower(v):k for k, v in self.new_api.get_id_map('authors').iteritems()}.get(icu_lower(author), None)) for field in ('tags', 'series', 'publishers', 'ratings', 'languages'): def getter(field): diff --git a/src/calibre/db/tests/legacy.py b/src/calibre/db/tests/legacy.py index b609cb478d..5f1aa2ff41 100644 --- a/src/calibre/db/tests/legacy.py +++ b/src/calibre/db/tests/legacy.py @@ -159,8 +159,13 @@ class LegacyTest(BaseTest): ndb = self.init_legacy(self.cloned_library) db = self.init_old() + self.assertEqual(dict(db.prefs), dict(ndb.prefs)) + for meth, args in { 'get_next_series_num_for': [('A Series One',)], + 'get_author_id': [('author one',), ('unknown',), ('xxxxx',)], + 'series_id': [(0,), (1,), (2,)], + 'publisher_id': [(0,), (1,), (2,)], '@tags_older_than': [ ('News', None), ('Tag One', None), ('xxxx', None), ('Tag One', None, 'News'), ('News', None, 'xxxx'), ('News', None, None, ['xxxxxxx']), ('News', None, 'Tag One', ['Author Two', 'Author One']), @@ -178,6 +183,7 @@ class LegacyTest(BaseTest): 'id':[(1,), (2,), (0,),], 'index':[(1,), (2,), (3,), ], 'is_empty':[()], + 'count':[()], 'all_author_names':[()], 'all_tag_names':[()], 'all_series_names':[()], @@ -352,7 +358,7 @@ class LegacyTest(BaseTest): 'run_import_plugins', 'vacuum', 'set_path', 'row', 'row_factory', 'rows', 'rmtree', 'series_index_pat', 'import_old_database', 'dirtied_lock', 'dirtied_cache', 'dirty_queue_length', 'dirty_books_referencing', 'windows_check_if_files_in_use', 'get_metadata_for_dump', 'get_a_dirtied_book', 'dirtied_sequence', - 'format_filename_cache', 'format_metadata_cache', 'filter', 'create_version1', + 'format_filename_cache', 'format_metadata_cache', 'filter', 'create_version1', 'normpath', } SKIP_ARGSPEC = { '__init__', diff --git a/src/calibre/db/view.py b/src/calibre/db/view.py index bc341698e2..bb9131e212 100644 --- a/src/calibre/db/view.py +++ b/src/calibre/db/view.py @@ -120,6 +120,9 @@ class View(object): self._map = tuple(sorted(self.cache.all_book_ids())) self._map_filtered = tuple(self._map) + def count(self): + return len(self._map) + def get_property(self, id_or_index, index_is_id=False, loc=-1): book_id = id_or_index if index_is_id else self._map_filtered[id_or_index] return self._field_getters[loc](book_id)