diff --git a/src/calibre/devices/mtp/unix/driver.py b/src/calibre/devices/mtp/unix/driver.py index 0e4461df7d..864daef046 100644 --- a/src/calibre/devices/mtp/unix/driver.py +++ b/src/calibre/devices/mtp/unix/driver.py @@ -52,7 +52,8 @@ class MTP_DEVICE(MTPDeviceBase): def is_device_mtp(self, d, debug=None): ''' Returns True iff the _is_device_mtp check returns True and libmtp is able to probe the device successfully. ''' - if self._is_device_mtp is None: return False + if self._is_device_mtp is None: + return False return (self._is_device_mtp(d, debug=debug) and self.libmtp.is_mtp_device(d.busnum, d.devnum)) @@ -61,7 +62,8 @@ class MTP_DEVICE(MTPDeviceBase): @synchronous def detect_managed_devices(self, devices_on_system, force_refresh=False): - if self.libmtp is None: return None + if self.libmtp is None: + return None # First remove blacklisted devices. devs = set() for d in devices_on_system: @@ -109,13 +111,14 @@ class MTP_DEVICE(MTPDeviceBase): p(err) return False devs = [d for d in devices_on_system if - ( (d.vendor_id, d.product_id) in self.known_devices or + ((d.vendor_id, d.product_id) in self.known_devices or self.is_device_mtp(d, debug=p)) and d.vendor_id != APPLE] if not devs: p('No MTP devices connected to system') return False p('MTP devices connected:') - for d in devs: p(d) + for d in devs: + p(d) for d in devs: p('\nTrying to open:', d) @@ -144,7 +147,8 @@ class MTP_DEVICE(MTPDeviceBase): @synchronous def eject(self): - if self.currently_connected_dev is None: return + if self.currently_connected_dev is None: + return self.ejected_devices.add(self.currently_connected_dev) self.post_yank_cleanup() @@ -244,7 +248,8 @@ class MTP_DEVICE(MTPDeviceBase): storage, all_items, all_errs = [], [], [] for sid, capacity in zip([self._main_id, self._carda_id, self._cardb_id], self.total_space()): - if sid is None: continue + if sid is None: + continue name = _('Unknown') for x in self.dev.storage_info: if x['id'] == sid: @@ -384,7 +389,8 @@ def develop(): dev.startup() try: cd = dev.detect_managed_devices(scanner.devices) - if cd is None: raise RuntimeError('No MTP device found') + if cd is None: + raise RuntimeError('No MTP device found') dev.open(cd, 'develop') pprint.pprint(dev.dev.storage_info) dev.filesystem_cache diff --git a/src/calibre/devices/mtp/windows/driver.py b/src/calibre/devices/mtp/windows/driver.py index 268ac07a41..53f6d1abfa 100644 --- a/src/calibre/devices/mtp/windows/driver.py +++ b/src/calibre/devices/mtp/windows/driver.py @@ -80,7 +80,8 @@ class MTP_DEVICE(MTPDeviceBase): @same_thread def detect_managed_devices(self, devices_on_system, force_refresh=False): - if self.wpd is None: return None + if self.wpd is None: + return None if self.eject_dev_on_next_scan: self.eject_dev_on_next_scan = False if self.currently_connected_pnp_id is not None: @@ -166,7 +167,8 @@ class MTP_DEVICE(MTPDeviceBase): p(traceback.format_exc()) continue protocol = data.get('protocol', '').lower() - if not protocol.startswith('mtp:'): continue + if not protocol.startswith('mtp:'): + continue p('MTP device:', pnp_id) p(pprint.pformat(data)) if not self.is_suitable_wpd_device(data): @@ -195,10 +197,12 @@ class MTP_DEVICE(MTPDeviceBase): def is_suitable_wpd_device(self, devdata): # Check that protocol is MTP protocol = devdata.get('protocol', '').lower() - if not protocol.startswith('mtp:'): return False + if not protocol.startswith('mtp:'): + return False # Check that the device has some read-write storage - if not devdata.get('has_storage', False): return False + if not devdata.get('has_storage', False): + return False has_rw_storage = False for s in devdata.get('storage', []): if s.get('filesystem', None) == 'DCF': @@ -206,11 +210,12 @@ class MTP_DEVICE(MTPDeviceBase): # See https://bugs.launchpad.net/calibre/+bug/1054562 continue if s.get('type', 'unknown_unknown').split('_')[-1] == 'rom': - continue # Read only storage + continue # Read only storage if s.get('rw', False): has_rw_storage = True break - if not has_rw_storage: return False + if not has_rw_storage: + return False return True @@ -234,7 +239,8 @@ class MTP_DEVICE(MTPDeviceBase): items = [] for storage_id, capacity in zip([self._main_id, self._carda_id, self._cardb_id], ts): - if storage_id is None: continue + if storage_id is None: + continue name = _('Unknown') for s in self.dev.data['storage']: if s['id'] == storage_id: @@ -245,7 +251,8 @@ class MTP_DEVICE(MTPDeviceBase): self._currently_getting_sid = unicode(storage_id) id_map = self.dev.get_filesystem(storage_id, self._filesystem_callback) - for x in id_map.itervalues(): x['storage_id'] = storage_id + for x in id_map.itervalues(): + x['storage_id'] = storage_id all_storage.append(storage) items.append(id_map.itervalues()) self._filesystem_cache = FilesystemCache(all_storage, chain(*items)) @@ -255,7 +262,8 @@ class MTP_DEVICE(MTPDeviceBase): @same_thread def do_eject(self): - if self.currently_connected_pnp_id is None: return + if self.currently_connected_pnp_id is None: + return self.ejected_devices.add(self.currently_connected_pnp_id) self.currently_connected_pnp_id = self.current_friendly_name = None self._main_id = self._carda_id = self._cardb_id = None @@ -273,7 +281,8 @@ class MTP_DEVICE(MTPDeviceBase): return self.currently_connected_pnp_id is not None def eject(self): - if self.currently_connected_pnp_id is None: return + if self.currently_connected_pnp_id is None: + return self.eject_dev_on_next_scan = True self.current_serial_num = None