From 049e7b202b27f9936ee417f3317b877c4d3bcb16 Mon Sep 17 00:00:00 2001 From: John Schember Date: Fri, 9 Jan 2009 17:59:15 -0500 Subject: [PATCH 1/4] Updated Kindle driver --- src/calibre/devices/kindle/driver.py | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/src/calibre/devices/kindle/driver.py b/src/calibre/devices/kindle/driver.py index 06c3b1cf27..f6c32915f2 100755 --- a/src/calibre/devices/kindle/driver.py +++ b/src/calibre/devices/kindle/driver.py @@ -20,7 +20,7 @@ class KINDLE(USBMS): VENDOR_ID = 0x1949 PRODUCT_ID = 0x0001 - BCD = 0x399 + BCD = [0x399] VENDOR_NAME = 'AMAZON' PRODUCT_NAME = 'KINDLE' @@ -30,3 +30,15 @@ class KINDLE(USBMS): EBOOK_DIR = "documents" + def delete_books(self, paths, end_session=True): + for path in paths: + if os.path.exists(path): + os.unlink(path) + + filepath, ext = os.path.splitext(path) + basepath, filename = os.path.split(filepath) + + # Delete the ebook auxiliary file + if os.path.exists(filepath + '.mbp'): + os.unlink(filepath + '.mbp') + From afab0395d3de1099aa42db9c06884378079e267c Mon Sep 17 00:00:00 2001 From: John Schember Date: Sat, 10 Jan 2009 08:06:44 -0500 Subject: [PATCH 2/4] skip osx detection if identifier is not set --- src/calibre/devices/usbms/device.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/calibre/devices/usbms/device.py b/src/calibre/devices/usbms/device.py index f6b0c6a0a8..18645ca9b2 100644 --- a/src/calibre/devices/usbms/device.py +++ b/src/calibre/devices/usbms/device.py @@ -26,8 +26,8 @@ class Device(_Device): VENDOR_NAME = '' PRODUCT_NAME = '' - OSX_NAME_MAIN_MEM = '' - OSX_NAME_CARD_MEM = '' + OSX_NAME_MAIN_MEM = None + OSX_NAME_CARD_MEM = None MAIN_MEMORY_VOLUME_LABEL = '' STORAGE_CARD_VOLUME_LABEL = '' @@ -207,9 +207,9 @@ class Device(_Device): break for i, line in enumerate(lines): - if line.strip().endswith('') and self.OSX_NAME_MAIN_MEM in line: + if self.OSX_NAME_MAIN_MEM is not None and line.strip().endswith('') and self.OSX_NAME_MAIN_MEM in line: get_dev_node(lines[i+1:], 'main') - if line.strip().endswith('') and self.OSX_NAME_CARD_MEM in line: + if self.OSX_NAME_CARD_MEM is not None and line.strip().endswith('') and self.OSX_NAME_CARD_MEM in line: get_dev_node(lines[i+1:], 'card') if len(names.keys()) == 2: break From 3ade4dc4105e8e8d713d5f6cc2df97b092be1f09 Mon Sep 17 00:00:00 2001 From: John Schember Date: Sat, 10 Jan 2009 08:10:19 -0500 Subject: [PATCH 3/4] Can use separate ebook dir on card and main memory --- src/calibre/devices/cybookg3/driver.py | 2 +- src/calibre/devices/kindle/driver.py | 2 +- src/calibre/devices/usbms/driver.py | 12 +++++++----- 3 files changed, 9 insertions(+), 7 deletions(-) diff --git a/src/calibre/devices/cybookg3/driver.py b/src/calibre/devices/cybookg3/driver.py index cf9f2e5a41..b9a16965c4 100644 --- a/src/calibre/devices/cybookg3/driver.py +++ b/src/calibre/devices/cybookg3/driver.py @@ -33,7 +33,7 @@ class CYBOOKG3(USBMS): MAIN_MEMORY_VOLUME_LABEL = 'Cybook Gen 3 Main Memory' STORAGE_CARD_VOLUME_LABEL = 'Cybook Gen 3 Storage Card' - EBOOK_DIR = "eBooks" + EBOOK_DIR_MAIN = "eBooks" def delete_books(self, paths, end_session=True): for path in paths: diff --git a/src/calibre/devices/kindle/driver.py b/src/calibre/devices/kindle/driver.py index f6c32915f2..8457dbc89b 100755 --- a/src/calibre/devices/kindle/driver.py +++ b/src/calibre/devices/kindle/driver.py @@ -28,7 +28,7 @@ class KINDLE(USBMS): MAIN_MEMORY_VOLUME_LABEL = 'Kindle Main Memory' STORAGE_CARD_VOLUME_LABEL = 'Kindle Storage Card' - EBOOK_DIR = "documents" + EBOOK_DIR_MAIN = "documents" def delete_books(self, paths, end_session=True): for path in paths: diff --git a/src/calibre/devices/usbms/driver.py b/src/calibre/devices/usbms/driver.py index 733ce76ae7..ba89db29c9 100644 --- a/src/calibre/devices/usbms/driver.py +++ b/src/calibre/devices/usbms/driver.py @@ -14,7 +14,8 @@ from calibre.devices.usbms.books import BookList, Book from calibre.devices.errors import FreeSpaceError class USBMS(Device): - EBOOK_DIR = '' + EBOOK_DIR_MAIN = '' + EBOOK_DIR_CARD = '' MIME_MAP = {} FORMATS = [] @@ -35,9 +36,10 @@ class USBMS(Device): return bl prefix = self._card_prefix if oncard else self._main_prefix + ebook_dir = self.EBOOK_DIR_CARD if oncard else self.EBOOK_DIR_MAIN - # Get all books in all directories under the root EBOOK_DIR directory - for path, dirs, files in os.walk(os.path.join(prefix, self.EBOOK_DIR)): + # Get all books in all directories under the root ebook_dir directory + for path, dirs, files in os.walk(os.path.join(prefix, ebook_dir)): # Filter out anything that isn't in the list of supported ebook types for book_type in self.MIME_MAP.keys(): for filename in fnmatch.filter(files, '*.%s' % (book_type)): @@ -51,9 +53,9 @@ class USBMS(Device): raise ValueError(_('The reader has no storage card connected.')) if not on_card: - path = os.path.join(self._main_prefix, self.EBOOK_DIR) + path = os.path.join(self._main_prefix, self.EBOOK_DIR_MAIN) else: - path = os.path.join(self._card_prefix, self.EBOOK_DIR) + path = os.path.join(self._card_prefix, self.EBOOK_DIR_CARD) sizes = map(os.path.getsize, files) size = sum(sizes) From 701e8cfc917fee03db53a9ba907439ca5ad98b07 Mon Sep 17 00:00:00 2001 From: John Schember Date: Sat, 10 Jan 2009 11:13:17 -0500 Subject: [PATCH 4/4] Windows drive detection based on given id. Fixes drive order bug on Cybook. --- src/calibre/devices/cybookg3/driver.py | 7 +-- src/calibre/devices/kindle/driver.py | 2 +- src/calibre/devices/usbms/device.py | 74 ++++++++++++++------------ 3 files changed, 44 insertions(+), 39 deletions(-) diff --git a/src/calibre/devices/cybookg3/driver.py b/src/calibre/devices/cybookg3/driver.py index b9a16965c4..7ab9a909a0 100644 --- a/src/calibre/devices/cybookg3/driver.py +++ b/src/calibre/devices/cybookg3/driver.py @@ -25,10 +25,11 @@ class CYBOOKG3(USBMS): BCD = [0x110, 0x132] VENDOR_NAME = 'BOOKEEN' - PRODUCT_NAME = 'CYBOOK_GEN3' + WINDOWS_MAIN_MEM = 'CYBOOK_GEN3__-FD' + WINDOWS_CARD_MEM = 'CYBOOK_GEN3__-SD' - OSX_NAME_MAIN_MEM = 'Bookeen Cybook Gen3 -FD Media' - OSX_NAME_CARD_MEM = 'Bookeen Cybook Gen3 -SD Media' + OSX_MAIN_MEM = 'Bookeen Cybook Gen3 -FD Media' + OSX_CARD_MEM = 'Bookeen Cybook Gen3 -SD Media' MAIN_MEMORY_VOLUME_LABEL = 'Cybook Gen 3 Main Memory' STORAGE_CARD_VOLUME_LABEL = 'Cybook Gen 3 Storage Card' diff --git a/src/calibre/devices/kindle/driver.py b/src/calibre/devices/kindle/driver.py index 8457dbc89b..4313f24847 100755 --- a/src/calibre/devices/kindle/driver.py +++ b/src/calibre/devices/kindle/driver.py @@ -23,7 +23,7 @@ class KINDLE(USBMS): BCD = [0x399] VENDOR_NAME = 'AMAZON' - PRODUCT_NAME = 'KINDLE' + WINDOWS_MAIN_MEM = 'KINDLE' MAIN_MEMORY_VOLUME_LABEL = 'Kindle Main Memory' STORAGE_CARD_VOLUME_LABEL = 'Kindle Storage Card' diff --git a/src/calibre/devices/usbms/device.py b/src/calibre/devices/usbms/device.py index 18645ca9b2..128bc58ec5 100644 --- a/src/calibre/devices/usbms/device.py +++ b/src/calibre/devices/usbms/device.py @@ -23,11 +23,12 @@ class Device(_Device): PRODUCT_ID = 0x0 BCD = None - VENDOR_NAME = '' - PRODUCT_NAME = '' + VENDOR_NAME = None + WINDOWS_MAIN_MEM = None + WINDOWS_CARD_MEM = None - OSX_NAME_MAIN_MEM = None - OSX_NAME_CARD_MEM = None + OSX_MAIN_MEM = None + OSX_CARD_MEM = None MAIN_MEMORY_VOLUME_LABEL = '' STORAGE_CARD_VOLUME_LABEL = '' @@ -148,43 +149,46 @@ class Device(_Device): return (msz, 0, csz) - @classmethod - def windows_match_device(cls, device_id): - device_id = device_id.upper() - if 'VEN_'+cls.VENDOR_NAME in device_id and \ - 'PROD_'+cls.PRODUCT_NAME in device_id: - return True - vid, pid = hex(cls.VENDOR_ID)[2:], hex(cls.PRODUCT_ID)[2:] - while len(vid) < 4: vid = '0' + vid - while len(pid) < 4: pid = '0' + pid - if 'VID_'+vid in device_id and 'PID_'+pid in device_id: - return True + def windows_match_device(self, pnp_id, device_id): + if device_id and pnp_id is not None: + pnp_id = pnp_id.upper() + device_id = device_id.upper() + + if 'VEN_' + self.VENDOR_NAME in pnp_id and 'PROD_' + device_id in pnp_id: + return True + return False - # This only supports Windows >= 2000 + def windows_get_drive_prefix(self, drive): + prefix = None + + try: + partition = drive.associators("Win32_DiskDriveToDiskPartition")[0] + logical_disk = partition.associators('Win32_LogicalDiskToPartition')[0] + prefix = logical_disk.DeviceID + os.sep + except IndexError: + pass + + return prefix + def open_windows(self): - drives = [] + drives = {} wmi = __import__('wmi', globals(), locals(), [], -1) c = wmi.WMI() for drive in c.Win32_DiskDrive(): - if self.__class__.windows_match_device(str(drive.PNPDeviceID)): - if drive.Partitions == 0: - continue - try: - partition = drive.associators("Win32_DiskDriveToDiskPartition")[0] - logical_disk = partition.associators('Win32_LogicalDiskToPartition')[0] - prefix = logical_disk.DeviceID+os.sep - drives.append((drive.Index, prefix)) - except IndexError: - continue + if self.windows_match_device(str(drive.PNPDeviceID), WINDOWS_MAIN_MEM): + drives['main'] = self.windows_get_drive_prefix(drive) + else if self.windows_match_device(str(drive.PNPDeviceID), WINDOWS_CARD_MEM): + drives['card'] = self.windows_get_drive_prefix(drive) + + if 'main' and 'card' in drives.keys(): + break if not drives: - raise DeviceError(_('Unable to detect the %s disk drive. Try rebooting.')%self.__class__.__name__) - - drives.sort(cmp=lambda a, b: cmp(a[0], b[0])) - self._main_prefix = drives[0][1] - if len(drives) > 1: - self._card_prefix = drives[1][1] + raise DeviceError(_('Unable to detect the %s disk drive. Try rebooting.') % self.__class__.__name__) + + self._main_prefix = drives['main'] if 'main' in names.keys() else None + self._card_prefix = drives['card'] if 'card' in names.keys() else None @classmethod def get_osx_mountpoints(self, raw=None): @@ -207,9 +211,9 @@ class Device(_Device): break for i, line in enumerate(lines): - if self.OSX_NAME_MAIN_MEM is not None and line.strip().endswith('') and self.OSX_NAME_MAIN_MEM in line: + if self.OSX_MAIN_MEM is not None and line.strip().endswith('') and self.OSX_MAIN_MEM in line: get_dev_node(lines[i+1:], 'main') - if self.OSX_NAME_CARD_MEM is not None and line.strip().endswith('') and self.OSX_NAME_CARD_MEM in line: + if self.OSX_CARD_MEM is not None and line.strip().endswith('') and self.OSX_CARD_MEM in line: get_dev_node(lines[i+1:], 'card') if len(names.keys()) == 2: break