From 4201dbeeaca994ebfed1c87fc9a8c2e6f29e1a43 Mon Sep 17 00:00:00 2001 From: Charles Haley <> Date: Mon, 17 May 2010 10:48:47 +0100 Subject: [PATCH] 1) Ensure that folder_device prefix (the path) end in os.sep 2) Fix regression causing sync of metadata cache on every connect 3) Report correct progress when adding books --- src/calibre/devices/folder_device/driver.py | 5 ++++- src/calibre/devices/usbms/books.py | 2 +- src/calibre/devices/usbms/device.py | 3 +-- src/calibre/devices/usbms/driver.py | 19 ++++++++++++++----- 4 files changed, 20 insertions(+), 9 deletions(-) diff --git a/src/calibre/devices/folder_device/driver.py b/src/calibre/devices/folder_device/driver.py index 6cc825dd9b..6b06cdf092 100644 --- a/src/calibre/devices/folder_device/driver.py +++ b/src/calibre/devices/folder_device/driver.py @@ -54,7 +54,10 @@ class FOLDER_DEVICE(USBMS): def __init__(self, path): if not os.path.isdir(path): raise IOError, 'Path is not a folder' - self._main_prefix = path + if path.endswith(os.sep): + self._main_prefix = path + else: + self._main_prefix = path + os.sep self.booklist_class = BookList self.is_connected = True diff --git a/src/calibre/devices/usbms/books.py b/src/calibre/devices/usbms/books.py index 3ecee3755f..59f098d421 100644 --- a/src/calibre/devices/usbms/books.py +++ b/src/calibre/devices/usbms/books.py @@ -117,7 +117,7 @@ class BookList(_BookList): def add_book(self, book, replace_metadata): if book not in self: self.append(book) - return True + return False # subclasses return True if device metadata has changed def remove_book(self, book): self.remove(book) diff --git a/src/calibre/devices/usbms/device.py b/src/calibre/devices/usbms/device.py index 249733b4e3..9b1da24805 100644 --- a/src/calibre/devices/usbms/device.py +++ b/src/calibre/devices/usbms/device.py @@ -113,8 +113,7 @@ class Device(DeviceConfig, DevicePlugin): def _windows_space(cls, prefix): if not prefix: return 0, 0 - if prefix.endswith(os.sep): - prefix = prefix[:-1] + prefix = prefix[:-1] win32file = __import__('win32file', globals(), locals(), [], -1) try: sectors_per_cluster, bytes_per_sector, free_clusters, total_clusters = \ diff --git a/src/calibre/devices/usbms/driver.py b/src/calibre/devices/usbms/driver.py index 1d5343024c..3a30b3c10e 100644 --- a/src/calibre/devices/usbms/driver.py +++ b/src/calibre/devices/usbms/driver.py @@ -90,6 +90,7 @@ class USBMS(CLI, Device): self.count_found_in_bl += 1 else: item = self.book_from_path(prefix, lpath) + changed = True if metadata.add_book(item, replace_metadata=False): changed = True except: # Probably a filename encoding error @@ -106,12 +107,17 @@ class USBMS(CLI, Device): if not os.path.exists(ebook_dir): continue # Get all books in the ebook_dir directory if self.SUPPORTS_SUB_DIRS: + # build a list of files to check, so we can accurately report progress + flist = [] for path, dirs, files in os.walk(ebook_dir): for filename in files: - self.report_progress(0.5, _('Getting list of books on device...')) - changed = update_booklist(filename, path, prefix) - if changed: - need_sync = True + if filename != self.METADATA_CACHE: + flist.append({'filename':filename, 'path': path}) + for i, f in enumerate(flist): + self.report_progress(i/float(len(flist)), _('Getting list of books on device...')) + changed = update_booklist(f['filename'], f['path'], prefix) + if changed: + need_sync = True else: paths = os.listdir(ebook_dir) for i, filename in enumerate(paths): @@ -123,7 +129,10 @@ class USBMS(CLI, Device): # if count != len(bl) then there were items in it that we did not # find on the device. If need_sync is True then there were either items # on the device that were not in bl or some of the items were changed. - #print "count found in cache: %d, count of files in cache: %d, must_sync_cache: %s" % (self.count_found_in_bl, len(bl), need_sync) + + #print "count found in cache: %d, count of files in cache: %d, need_sync: %s, must_sync_cache: %s" % \ + # (self.count_found_in_bl, len(bl), need_sync, + # need_sync or self.count_found_in_bl != len(bl)) if self.count_found_in_bl != len(bl) or need_sync: if oncard == 'cardb': self.sync_booklists((None, None, metadata))