MTP: Implement get_file()

This commit is contained in:
Kovid Goyal 2012-09-01 21:57:09 +05:30
parent 4785359e6a
commit b9737b9659
2 changed files with 26 additions and 1 deletions

View File

@ -130,6 +130,7 @@ class MTP_DEVICE(BASE):
del relpath_cache[relpath] del relpath_cache[relpath]
if cached_metadata.size == mtp_file.size: if cached_metadata.size == mtp_file.size:
cached_metadata.datetime = mtp_file.last_modified.timetuple() cached_metadata.datetime = mtp_file.last_modified.timetuple()
cached_metadata.path = mtp_file.mtp_id_path
debug('Using cached metadata for', debug('Using cached metadata for',
'/'.join(mtp_file.full_path)) '/'.join(mtp_file.full_path))
continue # No need to update metadata continue # No need to update metadata
@ -150,6 +151,7 @@ class MTP_DEVICE(BASE):
traceback.print_exc() traceback.print_exc()
book.size = mtp_file.size book.size = mtp_file.size
book.datetime = mtp_file.last_modified.timetuple() book.datetime = mtp_file.last_modified.timetuple()
book.path = mtp_file.mtp_id_path
# Remove books in the cache that no longer exist # Remove books in the cache that no longer exist
for idx in sorted(relpath_cache.itervalues(), reverse=True): 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): def create_upload_path(self, path, mdata, fname):
from calibre.devices import create_upload_path from calibre.devices import create_upload_path
from calibre.utils.filenames import ascii_filename as sanitize from calibre.utils.filenames import ascii_filename as sanitize

View File

@ -7,7 +7,7 @@ __license__ = 'GPL v3'
__copyright__ = '2012, Kovid Goyal <kovid at kovidgoyal.net>' __copyright__ = '2012, Kovid Goyal <kovid at kovidgoyal.net>'
__docformat__ = 'restructuredtext en' __docformat__ = 'restructuredtext en'
import weakref, sys import weakref, sys, json
from collections import deque from collections import deque
from operator import attrgetter from operator import attrgetter
from future_builtins import map from future_builtins import map
@ -166,6 +166,10 @@ class FileOrFolder(object):
def mtp_relpath(self): def mtp_relpath(self):
return tuple(x.lower() for x in self.full_path[1:]) 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): class FilesystemCache(object):
def __init__(self, all_storage, entries): def __init__(self, all_storage, entries):
@ -215,4 +219,19 @@ class FilesystemCache(object):
if x.storage_id == storage_id and x.is_ebook: if x.storage_id == storage_id and x.is_ebook:
yield x 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)