mirror of
https://github.com/kovidgoyal/calibre.git
synced 2025-08-07 09:01:38 -04:00
More API
This commit is contained in:
parent
67025df8ef
commit
eac44f83b8
@ -65,6 +65,7 @@ class LibraryDatabase(object):
|
|||||||
cache.init()
|
cache.init()
|
||||||
self.data = View(cache)
|
self.data = View(cache)
|
||||||
self.id = self.data.index_to_id
|
self.id = self.data.index_to_id
|
||||||
|
self.count = self.data.count
|
||||||
|
|
||||||
self.get_property = self.data.get_property
|
self.get_property = self.data.get_property
|
||||||
|
|
||||||
@ -84,6 +85,10 @@ class LibraryDatabase(object):
|
|||||||
delattr(self, x)
|
delattr(self, x)
|
||||||
|
|
||||||
# Library wide properties {{{
|
# Library wide properties {{{
|
||||||
|
@property
|
||||||
|
def prefs(self):
|
||||||
|
return self.new_api.backend.prefs
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def field_metadata(self):
|
def field_metadata(self):
|
||||||
return self.backend.field_metadata
|
return self.backend.field_metadata
|
||||||
@ -521,6 +526,18 @@ for prop in ('author_sort', 'authors', 'comment', 'comments', 'publisher',
|
|||||||
return func
|
return func
|
||||||
setattr(LibraryDatabase, prop, MT(getter(prop)))
|
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.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.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))
|
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_all_identifier_types = MT(lambda self: list(self.new_api.fields['identifiers'].table.all_identifier_types()))
|
||||||
LibraryDatabase.get_authors_with_ids = MT(
|
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()])
|
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'):
|
for field in ('tags', 'series', 'publishers', 'ratings', 'languages'):
|
||||||
def getter(field):
|
def getter(field):
|
||||||
|
@ -159,8 +159,13 @@ class LegacyTest(BaseTest):
|
|||||||
ndb = self.init_legacy(self.cloned_library)
|
ndb = self.init_legacy(self.cloned_library)
|
||||||
db = self.init_old()
|
db = self.init_old()
|
||||||
|
|
||||||
|
self.assertEqual(dict(db.prefs), dict(ndb.prefs))
|
||||||
|
|
||||||
for meth, args in {
|
for meth, args in {
|
||||||
'get_next_series_num_for': [('A Series One',)],
|
'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': [
|
'@tags_older_than': [
|
||||||
('News', None), ('Tag One', None), ('xxxx', None), ('Tag One', None, 'News'), ('News', None, 'xxxx'),
|
('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']),
|
('News', None, None, ['xxxxxxx']), ('News', None, 'Tag One', ['Author Two', 'Author One']),
|
||||||
@ -178,6 +183,7 @@ class LegacyTest(BaseTest):
|
|||||||
'id':[(1,), (2,), (0,),],
|
'id':[(1,), (2,), (0,),],
|
||||||
'index':[(1,), (2,), (3,), ],
|
'index':[(1,), (2,), (3,), ],
|
||||||
'is_empty':[()],
|
'is_empty':[()],
|
||||||
|
'count':[()],
|
||||||
'all_author_names':[()],
|
'all_author_names':[()],
|
||||||
'all_tag_names':[()],
|
'all_tag_names':[()],
|
||||||
'all_series_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',
|
'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',
|
'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',
|
'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 = {
|
SKIP_ARGSPEC = {
|
||||||
'__init__',
|
'__init__',
|
||||||
|
@ -120,6 +120,9 @@ class View(object):
|
|||||||
self._map = tuple(sorted(self.cache.all_book_ids()))
|
self._map = tuple(sorted(self.cache.all_book_ids()))
|
||||||
self._map_filtered = tuple(self._map)
|
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):
|
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]
|
book_id = id_or_index if index_is_id else self._map_filtered[id_or_index]
|
||||||
return self._field_getters[loc](book_id)
|
return self._field_getters[loc](book_id)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user