From 2e84a2e1a9029ed26a449cdd5e132443b5af5c4c Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Mon, 10 Apr 2017 16:14:07 +0530 Subject: [PATCH] Get copy to library working witht he new library broker --- src/calibre/gui2/actions/copy_to_library.py | 13 +++++++------ src/calibre/srv/library_broker.py | 15 +++++++++++++++ 2 files changed, 22 insertions(+), 6 deletions(-) diff --git a/src/calibre/gui2/actions/copy_to_library.py b/src/calibre/gui2/actions/copy_to_library.py index 40fad7bf9a..1258daecd6 100644 --- a/src/calibre/gui2/actions/copy_to_library.py +++ b/src/calibre/gui2/actions/copy_to_library.py @@ -133,19 +133,20 @@ class Worker(Thread): # {{{ def add_formats(self, id_, paths, newdb, replace=True): for path in paths: fmt = os.path.splitext(path)[-1].replace('.', '').upper() - with open(path, 'rb') as f: + with lopen(path, 'rb') as f: newdb.add_format(id_, fmt, f, index_is_id=True, notify=False, replace=replace) def doit(self): - from calibre.db.legacy import LibraryDatabase - newdb = LibraryDatabase(self.loc, is_second_db=True) - with closing(newdb): + from calibre.gui2.ui import get_gui + library_broker = get_gui().library_broker + newdb = library_broker.get_library(self.loc) + try: if self.check_for_duplicates: self.find_identical_books_data = newdb.new_api.data_for_find_identical_books() self._doit(newdb) - newdb.break_cycles() - del newdb + finally: + library_broker.prune_loaded_dbs() def _doit(self, newdb): for i, x in enumerate(self.ids): diff --git a/src/calibre/srv/library_broker.py b/src/calibre/srv/library_broker.py index 5ce833687f..6550c3e4f8 100644 --- a/src/calibre/srv/library_broker.py +++ b/src/calibre/srv/library_broker.py @@ -127,6 +127,21 @@ class GuiLibraryBroker(LibraryBroker): finally: self.last_used_times[library_id or self.default_library] = monotonic() + def get_library(self, library_path): + library_path = canonicalize_path(library_path) + with self: + for library_id, path in self.lmap.iteritems(): + if samefile(library_path, path): + db = self.loaded_dbs.get(library_id) + if db is None: + db = self.loaded_dbs[library_id] = self.init_library(path, False) + return db + db = self.init_library(library_path, False) + library_id = library_id_from_path(library_path, self.lmap) + self.lmap[library_id] = library_path + self.loaded_dbs[library_id] = db + return db + def prepare_for_gui_library_change(self, newloc): # Must be called with lock held for library_id, path in self.lmap.iteritems():