From 05275a30989102319d1725f985fd8b330b11ea1b Mon Sep 17 00:00:00 2001 From: Timothy Legge Date: Sun, 16 Oct 2011 22:59:55 -0300 Subject: [PATCH] Get the device path for matched books - the kobo stores kepubs in its own directory structure. This lays the groundwork for adding annotations support to the Kobo driver --- src/calibre/devices/kobo/driver.py | 5 +++++ src/calibre/devices/usbms/device.py | 3 +++ src/calibre/gui2/actions/annotate.py | 10 +++++++++- src/calibre/gui2/library/models.py | 9 ++++++--- 4 files changed, 23 insertions(+), 4 deletions(-) diff --git a/src/calibre/devices/kobo/driver.py b/src/calibre/devices/kobo/driver.py index 0e11302ec3..ed22ebd9d5 100644 --- a/src/calibre/devices/kobo/driver.py +++ b/src/calibre/devices/kobo/driver.py @@ -77,6 +77,11 @@ class KOBO(USBMS): self.book_class = Book self.dbversion = 7 + def create_annotations_path(self, mdata, device_path=None): + if device_path: + return device_path + return USBMS.create_annotations_path(self, mdata) + def books(self, oncard=None, end_session=True): from calibre.ebooks.metadata.meta import path_to_ext diff --git a/src/calibre/devices/usbms/device.py b/src/calibre/devices/usbms/device.py index 85ab5905b9..f1b8a9580a 100644 --- a/src/calibre/devices/usbms/device.py +++ b/src/calibre/devices/usbms/device.py @@ -1147,3 +1147,6 @@ class Device(DeviceConfig, DevicePlugin): os.makedirs(filedir) return filepath + + def create_annotations_path(self, mdata, device_path=None): + return self.create_upload_path(os.path.abspath('/'), mdata, 'x.bookmark', create_dirs=False) diff --git a/src/calibre/gui2/actions/annotate.py b/src/calibre/gui2/actions/annotate.py index 1db532bfc4..5ae0860d3b 100644 --- a/src/calibre/gui2/actions/annotate.py +++ b/src/calibre/gui2/actions/annotate.py @@ -41,13 +41,21 @@ class FetchAnnotationsAction(InterfaceAction): fmts.append(format.lower()) return fmts + def get_device_path_from_id(id_): + paths = [] + for x in ('memory', 'card_a', 'card_b'): + x = getattr(self.gui, x+'_view').model() + paths += x.paths_for_db_ids(set([id_]), as_map=True)[id_] + return paths[0].path if paths else None + def generate_annotation_paths(ids, db, device): # Generate path templates # Individual storage mount points scanned/resolved in driver.get_annotations() path_map = {} for id in ids: + path = get_device_path_from_id(id) mi = db.get_metadata(id, index_is_id=True) - a_path = device.create_upload_path(os.path.abspath('/'), mi, 'x.bookmark', create_dirs=False) + a_path = device.create_annotations_path(mi, path) path_map[id] = dict(path=a_path, fmts=get_formats(id)) return path_map diff --git a/src/calibre/gui2/library/models.py b/src/calibre/gui2/library/models.py index a0870b1e8d..9c456ac771 100644 --- a/src/calibre/gui2/library/models.py +++ b/src/calibre/gui2/library/models.py @@ -1239,11 +1239,14 @@ class DeviceBooksModel(BooksModel): # {{{ def paths(self, rows): return [self.db[self.map[r.row()]].path for r in rows ] - def paths_for_db_ids(self, db_ids): - res = [] + def paths_for_db_ids(self, db_ids, as_map=False): + res = defaultdict(list) if as_map else [] for r,b in enumerate(self.db): if b.application_id in db_ids: - res.append((r,b)) + if as_map: + res[b.application_id].append(b) + else: + res.append((r,b)) return res def get_collections_with_ids(self):