From 0a5d6a3bf442c48d12126a596b952e4939bad7ce Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Sat, 2 Apr 2016 16:01:23 +0530 Subject: [PATCH] Catalog generation: Do not crash is a book in the library has no uuid. Fixes #1565242 [Generate catalog fails](https://bugs.launchpad.net/calibre/+bug/1565242) --- .../library/catalogs/epub_mobi_builder.py | 56 ++++++++++--------- 1 file changed, 29 insertions(+), 27 deletions(-) diff --git a/src/calibre/library/catalogs/epub_mobi_builder.py b/src/calibre/library/catalogs/epub_mobi_builder.py index e091a26d73..5c856d316a 100644 --- a/src/calibre/library/catalogs/epub_mobi_builder.py +++ b/src/calibre/library/catalogs/epub_mobi_builder.py @@ -4341,35 +4341,37 @@ class CatalogBuilder(object): cover_crc = hex(zlib.crc32(data)) # Test cache for uuid - zf = _open_archive() - if zf is not None: - with zf: - try: - zf.getinfo(title['uuid'] + cover_crc) - except: - pass - else: - # uuid found in cache with matching crc - thumb_data = zf.read(title['uuid'] + cover_crc) - with open(os.path.join(image_dir, thumb_file), 'wb') as f: - f.write(thumb_data) - return - - # Save thumb for catalog. If invalid data, error returns to generate_thumbnails() - thumb_data = thumbnail(data, - width=self.thumb_width, height=self.thumb_height)[-1] - with open(os.path.join(image_dir, thumb_file), 'wb') as f: - f.write(thumb_data) - - # Save thumb to archive - if zf is not None: - # Ensure that the read succeeded - # If we failed to open the zip file for reading, - # we dont know if it contained the thumb or not - zf = _open_archive('a') + uuid = title.get('uuid') + if uuid: + zf = _open_archive() if zf is not None: with zf: - zf.writestr(title['uuid'] + cover_crc, thumb_data) + try: + zf.getinfo(uuid + cover_crc) + except: + pass + else: + # uuid found in cache with matching crc + thumb_data = zf.read(uuid + cover_crc) + with open(os.path.join(image_dir, thumb_file), 'wb') as f: + f.write(thumb_data) + return + + # Save thumb for catalog. If invalid data, error returns to generate_thumbnails() + thumb_data = thumbnail(data, + width=self.thumb_width, height=self.thumb_height)[-1] + with open(os.path.join(image_dir, thumb_file), 'wb') as f: + f.write(thumb_data) + + # Save thumb to archive + if zf is not None: + # Ensure that the read succeeded + # If we failed to open the zip file for reading, + # we dont know if it contained the thumb or not + zf = _open_archive('a') + if zf is not None: + with zf: + zf.writestr(uuid + cover_crc, thumb_data) def generate_thumbnails(self): """ Generate a thumbnail cover for each book.