From b9737b96593e8bef40660cf33889825f24cb63fd Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Sat, 1 Sep 2012 21:57:09 +0530 Subject: [PATCH] MTP: Implement get_file() --- src/calibre/devices/mtp/driver.py | 6 ++++++ src/calibre/devices/mtp/filesystem_cache.py | 21 ++++++++++++++++++++- 2 files changed, 26 insertions(+), 1 deletion(-) diff --git a/src/calibre/devices/mtp/driver.py b/src/calibre/devices/mtp/driver.py index 13e8394288..9ac0c3d31a 100644 --- a/src/calibre/devices/mtp/driver.py +++ b/src/calibre/devices/mtp/driver.py @@ -130,6 +130,7 @@ class MTP_DEVICE(BASE): del relpath_cache[relpath] if cached_metadata.size == mtp_file.size: cached_metadata.datetime = mtp_file.last_modified.timetuple() + cached_metadata.path = mtp_file.mtp_id_path debug('Using cached metadata for', '/'.join(mtp_file.full_path)) continue # No need to update metadata @@ -150,6 +151,7 @@ class MTP_DEVICE(BASE): traceback.print_exc() book.size = mtp_file.size book.datetime = mtp_file.last_modified.timetuple() + book.path = mtp_file.mtp_id_path # Remove books in the cache that no longer exist for idx in sorted(relpath_cache.itervalues(), reverse=True): @@ -197,6 +199,10 @@ class MTP_DEVICE(BASE): # }}} + def get_file(self, path, outfile, end_session=True): + f = self.filesystem_cache.resolve_mtp_id_path(path) + self.get_mtp_file(f, outfile) + def create_upload_path(self, path, mdata, fname): from calibre.devices import create_upload_path from calibre.utils.filenames import ascii_filename as sanitize diff --git a/src/calibre/devices/mtp/filesystem_cache.py b/src/calibre/devices/mtp/filesystem_cache.py index 6aab711199..216e06031f 100644 --- a/src/calibre/devices/mtp/filesystem_cache.py +++ b/src/calibre/devices/mtp/filesystem_cache.py @@ -7,7 +7,7 @@ __license__ = 'GPL v3' __copyright__ = '2012, Kovid Goyal ' __docformat__ = 'restructuredtext en' -import weakref, sys +import weakref, sys, json from collections import deque from operator import attrgetter from future_builtins import map @@ -166,6 +166,10 @@ class FileOrFolder(object): def mtp_relpath(self): return tuple(x.lower() for x in self.full_path[1:]) + @property + def mtp_id_path(self): + return 'mtp:::' + json.dumps(self.object_id) + ':::' + '/'.join(self.full_path) + class FilesystemCache(object): def __init__(self, all_storage, entries): @@ -215,4 +219,19 @@ class FilesystemCache(object): if x.storage_id == storage_id and x.is_ebook: yield x + def resolve_mtp_id_path(self, path): + if not path.startswith('mtp:::'): + raise ValueError('%s is not a valid MTP path'%path) + parts = path.split(':::') + if len(parts) < 3: + raise ValueError('%s is not a valid MTP path'%path) + try: + object_id = json.loads(parts[1]) + except: + raise ValueError('%s is not a valid MTP path'%path) + try: + return self.id_map[object_id] + except KeyError: + raise ValueError('No object found with MTP path: %s'%path) +