From 84e5059b11657aacc36a30e867c8c31cb096c870 Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Sun, 16 May 2010 22:26:59 -0600 Subject: [PATCH] More usbms.driver cleanups --- src/calibre/devices/prs505/driver.py | 2 +- src/calibre/devices/usbms/books.py | 2 +- src/calibre/devices/usbms/driver.py | 17 +++++++++++------ 3 files changed, 13 insertions(+), 8 deletions(-) diff --git a/src/calibre/devices/prs505/driver.py b/src/calibre/devices/prs505/driver.py index d2823ff4a4..9926e5f61c 100644 --- a/src/calibre/devices/prs505/driver.py +++ b/src/calibre/devices/prs505/driver.py @@ -27,7 +27,7 @@ class PRS505(USBMS): supported_platforms = ['windows', 'osx', 'linux'] path_sep = '/' - booklist_class = PRS_BookList # See USBMS for some explanation of this + booklist_class = PRS_BookList # See usbms.driver for some explanation of this FORMATS = ['epub', 'lrf', 'lrx', 'rtf', 'pdf', 'txt'] diff --git a/src/calibre/devices/usbms/books.py b/src/calibre/devices/usbms/books.py index edd5907713..3ecee3755f 100644 --- a/src/calibre/devices/usbms/books.py +++ b/src/calibre/devices/usbms/books.py @@ -37,7 +37,7 @@ class Book(MetaInformation): else: self.lpath = lpath self.mime = mime_type_ext(path_to_ext(lpath)) - self.size = None # will be set later + self.size = size # will be set later if None self.datetime = time.gmtime() if other: diff --git a/src/calibre/devices/usbms/driver.py b/src/calibre/devices/usbms/driver.py index c7c4e06834..1d5343024c 100644 --- a/src/calibre/devices/usbms/driver.py +++ b/src/calibre/devices/usbms/driver.py @@ -15,6 +15,7 @@ import re import json from itertools import cycle +from calibre import prints from calibre.devices.usbms.cli import CLI from calibre.devices.usbms.device import Device from calibre.devices.usbms.books import BookList, Book @@ -122,7 +123,7 @@ 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, must_sync_cache: %s" % (self.count_found_in_bl, len(bl), need_sync) if self.count_found_in_bl != len(bl) or need_sync: if oncard == 'cardb': self.sync_booklists((None, None, metadata)) @@ -146,7 +147,9 @@ class USBMS(CLI, Device): mdata, fname = metadata.next(), names.next() filepath = self.normalize_path(self.create_upload_path(path, mdata, fname)) paths.append(filepath) - self.put_file(self.normalize_path(infile), filepath, replace_file=True) + if not hasattr(infile, 'read'): + infile = self.normalize_path(infile) + self.put_file(infile, filepath, replace_file=True) try: self.upload_cover(os.path.dirname(filepath), os.path.splitext(os.path.basename(filepath))[0], mdata) @@ -188,7 +191,8 @@ class USBMS(CLI, Device): if not prefix and self._card_b_prefix: prefix = self._card_b_prefix if path.startswith(self._card_b_prefix) else None if prefix is None: - print 'in add_books_to_metadata. Prefix is None!', path, self._main_prefix + prints('in add_books_to_metadata. Prefix is None!', path, + self._main_prefix) continue lpath = path.partition(prefix)[2] if lpath.startswith('/') or lpath.startswith('\\'): @@ -238,7 +242,8 @@ class USBMS(CLI, Device): if prefix is not None and isinstance(booklists[listid], self.booklist_class): if not os.path.exists(prefix): os.makedirs(self.normalize_path(prefix)) - js = [item.to_json() for item in booklists[listid]] + js = [item.to_json() for item in booklists[listid] if + hasattr(item, 'to_json')] with open(self.normalize_path(os.path.join(prefix, self.METADATA_CACHE)), 'wb') as f: json.dump(js, f, indent=2, encoding='utf-8') write_prefix(self._main_prefix, 0) @@ -314,6 +319,6 @@ class USBMS(CLI, Device): if mi is None: mi = MetaInformation(os.path.splitext(os.path.basename(path))[0], [_('Unknown')]) - mi.size = os.stat(cls.normalize_path(os.path.join(prefix, path))).st_size - book = cls.book_class(prefix, path, other=mi) + size = os.stat(cls.normalize_path(os.path.join(prefix, path))).st_size + book = cls.book_class(prefix, path, other=mi, size=size) return book