This commit is contained in:
Kovid Goyal 2012-08-27 11:59:01 +05:30
parent 9aa30a8b71
commit f9dc2cd6d7
4 changed files with 30 additions and 44 deletions

View File

@ -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:

View File

@ -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

View File

@ -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,7 +371,7 @@ 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);
if (PyList_Append(ans, entry) != 0) { ok = 0; }
Py_DECREF(entry);
}

View File

@ -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))) {