Add code to ignore folders when scanning device to the libmtp driver

This commit is contained in:
Kovid Goyal 2012-12-18 12:24:14 +05:30
parent fe70ea45b2
commit 29f4807ae3
2 changed files with 19 additions and 11 deletions

View File

@ -212,8 +212,13 @@ class MTP_DEVICE(MTPDeviceBase):
ans += pprint.pformat(storage) ans += pprint.pformat(storage)
return ans return ans
def _filesystem_callback(self, entry): def _filesystem_callback(self, entry, level):
self.filesystem_callback(_('Found object: %s')%entry.get('name', '')) name = entry.get('name', '')
self.filesystem_callback(_('Found object: %s')%name)
if (level == 0 and
self.is_folder_ignored(self._currently_getting_sid, name)):
return False
return True
@property @property
def filesystem_cache(self): def filesystem_cache(self):
@ -234,6 +239,7 @@ class MTP_DEVICE(MTPDeviceBase):
storage.append({'id':sid, 'size':capacity, storage.append({'id':sid, 'size':capacity,
'is_folder':True, 'name':name, 'can_delete':False, 'is_folder':True, 'name':name, 'can_delete':False,
'is_system':True}) 'is_system':True})
self._currently_getting_sid = unicode(sid)
items, errs = self.dev.get_filesystem(sid, items, errs = self.dev.get_filesystem(sid,
self._filesystem_callback) self._filesystem_callback)
all_items.extend(items), all_errs.extend(errs) all_items.extend(items), all_errs.extend(errs)

View File

@ -122,7 +122,7 @@ static PyObject* build_file_metadata(LIBMTP_file_t *nf, uint32_t storage_id) {
PyObject *ans = NULL; PyObject *ans = NULL;
ans = Py_BuildValue("{s:s, s:k, s:k, s:k, s:K, s:L, s:O}", ans = Py_BuildValue("{s:s, s:k, s:k, s:k, s:K, s:L, s:O}",
"name", (unsigned long)nf->filename, "name", nf->filename,
"id", (unsigned long)nf->item_id, "id", (unsigned long)nf->item_id,
"parent_id", (unsigned long)nf->parent_id, "parent_id", (unsigned long)nf->parent_id,
"storage_id", (unsigned long)storage_id, "storage_id", (unsigned long)storage_id,
@ -357,10 +357,10 @@ Device_storage_info(Device *self, void *closure) {
// Device.get_filesystem {{{ // Device.get_filesystem {{{
static int recursive_get_files(LIBMTP_mtpdevice_t *dev, uint32_t storage_id, uint32_t parent_id, PyObject *ans, PyObject *errs, PyObject *callback) { static int recursive_get_files(LIBMTP_mtpdevice_t *dev, uint32_t storage_id, uint32_t parent_id, PyObject *ans, PyObject *errs, PyObject *callback, unsigned int level) {
LIBMTP_file_t *f, *files; LIBMTP_file_t *f, *files;
PyObject *entry; PyObject *entry, *r;
int ok = 1; int ok = 1, recurse;
Py_BEGIN_ALLOW_THREADS; Py_BEGIN_ALLOW_THREADS;
files = LIBMTP_Get_Files_And_Folders(dev, storage_id, parent_id); files = LIBMTP_Get_Files_And_Folders(dev, storage_id, parent_id);
@ -372,13 +372,15 @@ static int recursive_get_files(LIBMTP_mtpdevice_t *dev, uint32_t storage_id, uin
entry = build_file_metadata(f, storage_id); entry = build_file_metadata(f, storage_id);
if (entry == NULL) { ok = 0; } if (entry == NULL) { ok = 0; }
else { else {
Py_XDECREF(PyObject_CallFunctionObjArgs(callback, entry, NULL)); r = PyObject_CallFunction(callback, "OI", entry, level);
recurse = (r != NULL && PyObject_IsTrue(r)) ? 1 : 0;
Py_XDECREF(r);
if (PyList_Append(ans, entry) != 0) { ok = 0; } if (PyList_Append(ans, entry) != 0) { ok = 0; }
Py_DECREF(entry); Py_DECREF(entry);
} }
if (ok && f->filetype == LIBMTP_FILETYPE_FOLDER) { if (ok && recurse && f->filetype == LIBMTP_FILETYPE_FOLDER) {
if (!recursive_get_files(dev, storage_id, f->item_id, ans, errs, callback)) { if (!recursive_get_files(dev, storage_id, f->item_id, ans, errs, callback, level+1)) {
ok = 0; ok = 0;
} }
} }
@ -408,7 +410,7 @@ Device_get_filesystem(Device *self, PyObject *args) {
if (errs == NULL || ans == NULL) { PyErr_NoMemory(); return NULL; } if (errs == NULL || ans == NULL) { PyErr_NoMemory(); return NULL; }
LIBMTP_Clear_Errorstack(self->device); LIBMTP_Clear_Errorstack(self->device);
ok = recursive_get_files(self->device, (uint32_t)storage_id, 0, ans, errs, callback); ok = recursive_get_files(self->device, (uint32_t)storage_id, 0xFFFFFFFF, ans, errs, callback, 0);
dump_errorstack(self->device, errs); dump_errorstack(self->device, errs);
if (!ok) { if (!ok) {
Py_DECREF(ans); Py_DECREF(ans);
@ -537,7 +539,7 @@ static PyMethodDef Device_methods[] = {
}, },
{"get_filesystem", (PyCFunction)Device_get_filesystem, METH_VARARGS, {"get_filesystem", (PyCFunction)Device_get_filesystem, METH_VARARGS,
"get_filesystem(storage_id, callback) -> Get the list of files and folders on the device in storage_id. Returns files, errors. callback must be a callable that accepts a single argument. It is called with every found object." "get_filesystem(storage_id, callback) -> Get the list of files and folders on the device in storage_id. Returns files, errors. callback must be a callable that is called as with (entry, level). It is called with every found object. If callback returns False and the object is a folder, it is not recursed into."
}, },
{"get_file", (PyCFunction)Device_get_file, METH_VARARGS, {"get_file", (PyCFunction)Device_get_file, METH_VARARGS,