mirror of
https://github.com/kovidgoyal/calibre.git
synced 2025-07-08 10:44:09 -04:00
More API
This commit is contained in:
parent
51018ff76f
commit
3e184968f2
@ -1325,6 +1325,32 @@ class Cache(object):
|
||||
ans.add(book_id)
|
||||
return ans
|
||||
|
||||
@write_api
|
||||
def set_sort_for_authors(self, author_id_to_sort_map, update_books=True):
|
||||
self.fields['authors'].table.set_sort_names(author_id_to_sort_map, self.backend)
|
||||
changed_books = set()
|
||||
if update_books:
|
||||
val_map = {}
|
||||
for author_id in author_id_to_sort_map:
|
||||
books = self._books_for_field('authors', author_id)
|
||||
changed_books |= books
|
||||
for book_id in books:
|
||||
authors = self._field_ids_for('authors', book_id)
|
||||
adata = self._author_data(authors)
|
||||
sorts = [adata[x]['sort'] for x in authors]
|
||||
val_map[book_id] = ' & '.join(sorts)
|
||||
if val_map:
|
||||
self._set_field('author_sort', val_map)
|
||||
return changed_books
|
||||
|
||||
@write_api
|
||||
def set_link_for_authors(self, author_id_to_link_map):
|
||||
self.fields['authors'].table.set_links(author_id_to_link_map, self.backend)
|
||||
changed_books = set()
|
||||
for author_id in author_id_to_link_map:
|
||||
changed_books |= self._books_for_field('authors', author_id)
|
||||
return changed_books
|
||||
|
||||
# }}}
|
||||
|
||||
class SortKey(object): # {{{
|
||||
|
@ -296,6 +296,16 @@ class LibraryDatabase(object):
|
||||
adata = self.new_api._author_data(authors)
|
||||
return [(aid, adata[aid]['name'], adata[aid]['sort'], adata[aid]['link']) for aid in authors]
|
||||
|
||||
def set_sort_field_for_author(self, old_id, new_sort, commit=True, notify=False):
|
||||
changed_books = self.new_api.set_sort_for_authors({old_id:new_sort})
|
||||
if notify:
|
||||
self.notify('metadata', list(changed_books))
|
||||
|
||||
def set_link_field_for_author(self, aid, link, commit=True, notify=False):
|
||||
changed_books = self.new_api.set_link_for_authors({aid:link})
|
||||
if notify:
|
||||
self.notify('metadata', list(changed_books))
|
||||
|
||||
def book_on_device(self, book_id):
|
||||
with self.new_api.read_lock:
|
||||
return self.new_api.fields['ondevice'].book_on_device(book_id)
|
||||
@ -476,6 +486,14 @@ class LibraryDatabase(object):
|
||||
for book_id in sorted(self.new_api.tags_older_than(tag, delta=delta, must_have_tag=must_have_tag, must_have_authors=must_have_authors)):
|
||||
yield book_id
|
||||
|
||||
def sizeof_format(self, index, fmt, index_is_id=False):
|
||||
book_id = index if index_is_id else self.id(index)
|
||||
return self.new_api.format_metadata(book_id, fmt).get('size', None)
|
||||
|
||||
def get_metadata(self, index, index_is_id=False, get_cover=False, get_user_categories=True, cover_as_data=False):
|
||||
book_id = index if index_is_id else self.id(index)
|
||||
return self.new_api.get_metadata(book_id, get_cover=get_cover, get_user_categories=get_user_categories, cover_as_data=cover_as_data)
|
||||
|
||||
# Private interface {{{
|
||||
def __iter__(self):
|
||||
for row in self.data.iterall():
|
||||
|
@ -293,10 +293,17 @@ class AuthorsTable(ManyToManyTable):
|
||||
self.alink_map[row[0]] = row[3]
|
||||
|
||||
def set_sort_names(self, aus_map, db):
|
||||
aus_map = {aid:(a or '').strip() for aid, a in aus_map.iteritems()}
|
||||
self.asort_map.update(aus_map)
|
||||
db.conn.executemany('UPDATE authors SET sort=? WHERE id=?',
|
||||
[(v, k) for k, v in aus_map.iteritems()])
|
||||
|
||||
def set_links(self, link_map, db):
|
||||
link_map = {aid:(l or '').strip() for aid, l in link_map.iteritems()}
|
||||
self.alink_map.update(link_map)
|
||||
db.conn.executemany('UPDATE authors SET link=? WHERE id=?',
|
||||
[(v, k) for k, v in link_map.iteritems()])
|
||||
|
||||
def remove_books(self, book_ids, db):
|
||||
clean = ManyToManyTable.remove_books(self, book_ids, db)
|
||||
for item_id in clean:
|
||||
|
@ -168,6 +168,7 @@ class LegacyTest(BaseTest):
|
||||
],
|
||||
'format':[(1, 'FMT1', True), (2, 'FMT1', True), (0, 'xxxxxx')],
|
||||
'has_format':[(1, 'FMT1', True), (2, 'FMT1', True), (0, 'xxxxxx')],
|
||||
'sizeof_format':[(1, 'FMT1', True), (2, 'FMT1', True), (0, 'xxxxxx')],
|
||||
'@format_files':[(0,),(1,),(2,)],
|
||||
'formats':[(0,),(1,),(2,)],
|
||||
'format_hash':[(1, 'FMT1'),(1, 'FMT2'), (2, 'FMT1')],
|
||||
@ -341,7 +342,7 @@ class LegacyTest(BaseTest):
|
||||
# Obsolete/broken methods
|
||||
'author_id', # replaced by get_author_id
|
||||
'books_for_author', # broken
|
||||
'books_in_old_database', # unused
|
||||
'books_in_old_database', 'sizeof_old_database', # unused
|
||||
'migrate_old', # no longer supported
|
||||
|
||||
# Internal API
|
||||
@ -425,6 +426,8 @@ class LegacyTest(BaseTest):
|
||||
from calibre.utils.date import now
|
||||
n = now()
|
||||
ndb = self.init_legacy(self.cloned_library)
|
||||
amap = ndb.new_api.get_id_map('authors')
|
||||
sorts = [(aid, 's%d' % aid) for aid in amap]
|
||||
db = self.init_old(self.cloned_library)
|
||||
run_funcs(self, db, ndb, (
|
||||
('+format_metadata', 1, 'FMT1', itemgetter('size')),
|
||||
@ -448,7 +451,19 @@ class LegacyTest(BaseTest):
|
||||
|
||||
('update_last_modified', (1,), True, n), ('update_last_modified', (3,), True, n),
|
||||
('metadata_last_modified', 1, True), ('metadata_last_modified', 3, True),
|
||||
('set_sort_field_for_author', sorts[0][0], sorts[0][1]),
|
||||
('set_sort_field_for_author', sorts[1][0], sorts[1][1]),
|
||||
('set_sort_field_for_author', sorts[2][0], sorts[2][1]),
|
||||
('set_link_field_for_author', sorts[0][0], sorts[0][1]),
|
||||
('set_link_field_for_author', sorts[1][0], sorts[1][1]),
|
||||
('set_link_field_for_author', sorts[2][0], sorts[2][1]),
|
||||
(db.refresh,),
|
||||
('author_sort', 0), ('author_sort', 1), ('author_sort', 2),
|
||||
))
|
||||
omi = [db.get_metadata(x) for x in (0, 1, 2)]
|
||||
nmi = [ndb.get_metadata(x) for x in (0, 1, 2)]
|
||||
self.assertEqual([x.author_sort_map for x in omi], [x.author_sort_map for x in nmi])
|
||||
self.assertEqual([x.author_link_map for x in omi], [x.author_link_map for x in nmi])
|
||||
|
||||
ndb = self.init_legacy(self.cloned_library)
|
||||
db = self.init_old(self.cloned_library)
|
||||
|
Loading…
x
Reference in New Issue
Block a user