diff --git a/src/calibre/db/legacy.py b/src/calibre/db/legacy.py index 2ad5da61b8..809ec816a8 100644 --- a/src/calibre/db/legacy.py +++ b/src/calibre/db/legacy.py @@ -8,6 +8,7 @@ __copyright__ = '2013, Kovid Goyal ' import os, traceback from functools import partial +from future_builtins import zip from calibre.db import _get_next_series_num_for_list, _get_series_values from calibre.db.backend import DB @@ -150,7 +151,7 @@ class LibraryDatabase(object): def path(self, index, index_is_id=False): 'Return the relative path to the directory containing this books files as a unicode string.' book_id = index if index_is_id else self.data.index_to_id(index) - return self.data.cache.field_for('path', book_id).replace('/', os.sep) + return self.new_api.field_for('path', book_id).replace('/', os.sep) def abspath(self, index, index_is_id=False, create_dirs=True): 'Return the absolute path to the directory containing this books files as a unicode string.' @@ -159,6 +160,23 @@ class LibraryDatabase(object): os.makedirs(path) return path + def create_book_entry(self, mi, cover=None, add_duplicates=True, force_id=None): + return self.new_api.create_book_entry(mi, cover=cover, add_duplicates=add_duplicates, force_id=force_id) + + def add_books(self, paths, formats, metadata, add_duplicates=True, return_ids=False): + books = [(mi, {fmt:path}) for mi, path, fmt in zip(metadata, paths, formats)] + book_ids, duplicates = self.new_api.add_books(books, add_duplicates=add_duplicates, dbapi=self) + if duplicates: + paths, formats, metadata = [], [], [] + for mi, format_map in duplicates: + metadata.append(mi) + for fmt, path in format_map.iteritems(): + formats.append(fmt) + paths.append(path) + duplicates = (paths, formats, metadata) + ids = book_ids if return_ids else len(book_ids) + return duplicates or None, ids + # Private interface {{{ def __iter__(self): diff --git a/src/calibre/db/tests/legacy.py b/src/calibre/db/tests/legacy.py index ae99d8190f..5735d77ff4 100644 --- a/src/calibre/db/tests/legacy.py +++ b/src/calibre/db/tests/legacy.py @@ -7,8 +7,27 @@ __license__ = 'GPL v3' __copyright__ = '2013, Kovid Goyal ' import inspect +from repr import repr +from functools import partial +from tempfile import NamedTemporaryFile + from calibre.db.tests.base import BaseTest +class ET(object): + + def __init__(self, func_name, args, kwargs={}, old=None, legacy=None): + self.func_name = func_name + self.args, self.kwargs = args, kwargs + self.old, self.legacy = old, legacy + + def __call__(self, test): + old = self.old or test.init_old(test.cloned_library) + legacy = self.legacy or test.init_legacy(test.cloned_library) + oldres = getattr(old, self.func_name)(*self.args, **self.kwargs) + newres = getattr(legacy, self.func_name)(*self.args, **self.kwargs) + test.assertEqual(oldres, newres, 'Equivalence test for %s with args: %s and kwargs: %s failed' % ( + self.func_name, repr(self.args), repr(self.kwargs))) + class LegacyTest(BaseTest): ''' Test the emulation of the legacy interface. ''' @@ -119,6 +138,26 @@ class LegacyTest(BaseTest): db.close() # }}} + def test_legacy_adding_books(self): # {{{ + 'Test various adding books methods' + from calibre.ebooks.metadata.book.base import Metadata + legacy, old = self.init_legacy(self.cloned_library), self.init_old(self.cloned_library) + mi = Metadata('Added Book', authors=('Added Author',)) + with NamedTemporaryFile(suffix='.aff') as f: + f.write(b'xxx') + f.flush() + T = partial(ET, 'add_books', ([f.name], ['AFF'], [mi]), old=old, legacy=legacy) + T()(self) + T(kwargs={'return_ids':True})(self) + T(kwargs={'add_duplicates':False})(self) + + mi.title = 'Added Book2' + T = partial(ET, 'create_book_entry', (mi,), old=old, legacy=legacy) + T() + T({'add_duplicates':False}) + T({'force_id':1000}) + # }}} + def test_legacy_coverage(self): # {{{ ' Check that the emulation of the legacy interface is (almost) total ' cl = self.cloned_library