def test_library_id_construction(self): from calibre.srv.library_broker import library_id_from_path, correct_case_of_last_path_component, db_matches self.ae(library_id_from_path('as'), 'as') self.ae(library_id_from_path('as/'), 'as') self.ae(library_id_from_path('as////'), 'as') self.ae(library_id_from_path('/as/'), 'as') if os.sep == '\\': self.ae(library_id_from_path('as/' + os.sep), 'as') self.ae(library_id_from_path('X:' + os.sep), 'X') class MockDB: def __init__(self, base): self.new_api = self self.server_library_id = 'lid' self.dbpath = os.path.join(base, 'metadata.db') with TemporaryDirectory() as tdir: path = os.path.join(tdir, 'Test') os.mkdir(path) self.ae(correct_case_of_last_path_component(os.path.join(tdir, 'test')), path) self.ae(correct_case_of_last_path_component(os.path.join(tdir, 'Test')), path) db = MockDB(tdir) self.assertTrue(db_matches(db, db.server_library_id, None)) self.assertTrue(db_matches(db, db.server_library_id.upper(), tdir))
def test_library_id_construction(self): from calibre.srv.library_broker import library_id_from_path self.ae(library_id_from_path('as'), 'as') self.ae(library_id_from_path('as/'), 'as') self.ae(library_id_from_path('as////'), 'as') self.ae(library_id_from_path('/as/'), 'as') if os.sep == '\\': self.ae(library_id_from_path('as/' + os.sep), 'as') self.ae(library_id_from_path('X:' + os.sep), 'X')
def get_book_library_details(absolute_path_to_ebook): from calibre.srv.library_broker import correct_case_of_last_path_component, library_id_from_path absolute_path_to_ebook = os.path.abspath( os.path.expanduser(absolute_path_to_ebook)) base = os.path.dirname(absolute_path_to_ebook) m = re.search(r' \((\d+)\)$', os.path.basename(base)) if m is None: return book_id = int(m.group(1)) library_dir = os.path.dirname(os.path.dirname(base)) corrected_path = correct_case_of_last_path_component(library_dir) library_id = library_id_from_path(corrected_path) dbpath = os.path.join(library_dir, 'metadata.db') dbpath = os.environ.get('CALIBRE_OVERRIDE_DATABASE_PATH') or dbpath if not os.path.exists(dbpath): return return { 'dbpath': dbpath, 'book_id': book_id, 'fmt': absolute_path_to_ebook.rpartition('.')[-1].upper(), 'library_id': library_id }