From f9dc2cd6d7efe90f0b5a2d9b7fc05652682f8003 Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Mon, 27 Aug 2012 11:59:01 +0530 Subject: [PATCH] ... --- src/calibre/devices/mtp/filesystem_cache.py | 6 +-- src/calibre/devices/mtp/unix/driver.py | 19 ++++++-- src/calibre/devices/mtp/unix/libmtp.c | 47 +++++-------------- .../mtp/windows/device_enumeration.cpp | 2 +- 4 files changed, 30 insertions(+), 44 deletions(-) diff --git a/src/calibre/devices/mtp/filesystem_cache.py b/src/calibre/devices/mtp/filesystem_cache.py index 9fc65a61f5..3370967054 100644 --- a/src/calibre/devices/mtp/filesystem_cache.py +++ b/src/calibre/devices/mtp/filesystem_cache.py @@ -31,8 +31,8 @@ class FileOrFolder(object): self.all_storage_ids = fs_cache.all_storage_ids if self.storage_id not in self.all_storage_ids: - raise ValueError('Storage id %s not valid for %s'%(self.storage_id, - entry)) + raise ValueError('Storage id %s not valid for %s, valid values: %s'%(self.storage_id, + entry, self.all_storage_ids)) if self.parent_id == 0: self.parent_id = self.storage_id @@ -138,7 +138,7 @@ class FilesystemCache(object): self.entries.append(e) self.entries.sort(key=attrgetter('object_id')) - all_storage_ids = [x.object_id for x in self.entries] + all_storage_ids = [x.storage_id for x in self.entries] self.all_storage_ids = tuple(all_storage_ids) for entry in entries: diff --git a/src/calibre/devices/mtp/unix/driver.py b/src/calibre/devices/mtp/unix/driver.py index 85460ff818..e179647629 100644 --- a/src/calibre/devices/mtp/unix/driver.py +++ b/src/calibre/devices/mtp/unix/driver.py @@ -320,11 +320,22 @@ class MTP_DEVICE(MTPDeviceBase): (obj.full_path, self.format_errorstack(errs))) parent.remove_child(obj) -if __name__ == '__main__': - class PR: - def report_progress(self, sent, total): - print (sent, total, end=', ') +def develop(): + from calibre.devices.scanner import DeviceScanner + scanner = DeviceScanner() + scanner.scan() + dev = MTP_DEVICE(None) + dev.startup() + try: + cd = dev.detect_managed_devices(scanner.devices) + if cd is None: raise RuntimeError('No MTP device found') + dev.open(cd, 'develop') + pprint.pprint(dev.dev.storage_info) + dev.filesystem_cache + finally: + dev.shutdown() +if __name__ == '__main__': dev = MTP_DEVICE(None) dev.startup() from calibre.devices.scanner import DeviceScanner diff --git a/src/calibre/devices/mtp/unix/libmtp.c b/src/calibre/devices/mtp/unix/libmtp.c index ffbcbe688d..48fd55a2eb 100644 --- a/src/calibre/devices/mtp/unix/libmtp.c +++ b/src/calibre/devices/mtp/unix/libmtp.c @@ -119,43 +119,18 @@ static uint16_t data_from_python(void *params, void *priv, uint32_t wantlen, uns } static PyObject* build_file_metadata(LIBMTP_file_t *nf, uint32_t storage_id) { - PyObject *ans = NULL, *l = NULL; + PyObject *ans = NULL; - ans = Py_BuildValue("{s:s}", "name", nf->filename); - if (ans == NULL) return PyErr_NoMemory(); - - // We explicitly populate the dictionary instead of using Py_BuildValue to - // handle the numeric variables properly. Without this, for some reason the - // dict sometimes has incorrect values - l = PyLong_FromUnsignedLong(nf->item_id); - if (l == NULL) goto error; - if (PyDict_SetItemString(ans, "id", l) != 0) goto error; - Py_DECREF(l); l = NULL; - - l = PyLong_FromUnsignedLong(nf->parent_id); - if (l == NULL) goto error; - if (PyDict_SetItemString(ans, "parent_id", l) != 0) goto error; - Py_DECREF(l); l = NULL; - - l = PyLong_FromUnsignedLong(storage_id); - if (l == NULL) goto error; - if (PyDict_SetItemString(ans, "storage_id", l) != 0) goto error; - Py_DECREF(l); l = NULL; - - l = PyLong_FromUnsignedLongLong(nf->filesize); - if (l == NULL) goto error; - if (PyDict_SetItemString(ans, "size", l) != 0) goto error; - Py_DECREF(l); l = NULL; - - if (PyDict_SetItemString(ans, "is_folder", - (nf->filetype == LIBMTP_FILETYPE_FOLDER) ? Py_True : Py_False) != 0) - goto error; + ans = Py_BuildValue("{s:s, s:k, s:k, s:k, s:K, s:O}", + "name", nf->filename, + "id", nf->item_id, + "parent_id", nf->parent_id, + "storage_id", storage_id, + "size", nf->filesize, + "is_folder", (nf->filetype == LIBMTP_FILETYPE_FOLDER) ? Py_True : Py_False + ); return ans; - -error: - Py_XDECREF(ans); Py_XDECREF(l); - return PyErr_NoMemory(); } static PyObject* file_metadata(LIBMTP_mtpdevice_t *device, PyObject *errs, uint32_t item_id, uint32_t storage_id) { @@ -396,8 +371,8 @@ static int recursive_get_files(LIBMTP_mtpdevice_t *dev, uint32_t storage_id, uin entry = build_file_metadata(f, storage_id); if (entry == NULL) { ok = 0; } else { - PyList_Append(ans, entry); - Py_DECREF(entry); + if (PyList_Append(ans, entry) != 0) { ok = 0; } + Py_DECREF(entry); } if (ok && f->filetype == LIBMTP_FILETYPE_FOLDER) { diff --git a/src/calibre/devices/mtp/windows/device_enumeration.cpp b/src/calibre/devices/mtp/windows/device_enumeration.cpp index 90bc437be1..2c9b48d506 100644 --- a/src/calibre/devices/mtp/windows/device_enumeration.cpp +++ b/src/calibre/devices/mtp/windows/device_enumeration.cpp @@ -149,7 +149,7 @@ PyObject* get_storage_info(IPortableDevice *device) { // {{{ if (SUCCEEDED(values->GetUnsignedIntegerValue(WPD_STORAGE_ACCESS_CAPABILITY, &access)) && access == WPD_STORAGE_ACCESS_CAPABILITY_READWRITE) desc = Py_True; soid = PyUnicode_FromWideChar(object_ids[i], wcslen(object_ids[i])); if (soid == NULL) { PyErr_NoMemory(); goto end; } - so = Py_BuildValue("{s:K,s:K,s:K,s:K,s:O,s:N}", + so = Py_BuildValue("{s:K, s:K, s:K, s:K, s:O, s:N}", "capacity", capacity, "capacity_objects", capacity_objects, "free_space", free_space, "free_objects", free_objects, "rw", desc, "id", soid); if (so == NULL) { PyErr_NoMemory(); goto end; } if (SUCCEEDED(values->GetStringValue(WPD_STORAGE_DESCRIPTION, &storage_desc))) {