Cleanup implementation of thumbnail caching in catalog geenrator

This commit is contained in:
Kovid Goyal 2011-06-19 14:04:16 -06:00
parent ac93c19c1f
commit 1994faa7b2

View File

@ -3,7 +3,7 @@
__license__ = 'GPL v3' __license__ = 'GPL v3'
__copyright__ = '2010, Greg Riker' __copyright__ = '2010, Greg Riker'
import codecs, datetime, htmlentitydefs, os, re, shutil, time, zlib import codecs, datetime, htmlentitydefs, os, re, shutil, zlib
from collections import namedtuple from collections import namedtuple
from copy import deepcopy from copy import deepcopy
from xml.sax.saxutils import escape from xml.sax.saxutils import escape
@ -25,7 +25,7 @@ from calibre.utils.html2text import html2text
from calibre.utils.icu import capitalize from calibre.utils.icu import capitalize
from calibre.utils.logging import default_log as log from calibre.utils.logging import default_log as log
from calibre.utils.magick.draw import thumbnail from calibre.utils.magick.draw import thumbnail
from calibre.utils.zipfile import ZipFile, ZipInfo from calibre.utils.zipfile import ZipFile
FIELDS = ['all', 'title', 'title_sort', 'author_sort', 'authors', 'comments', FIELDS = ['all', 'title', 'title_sort', 'author_sort', 'authors', 'comments',
'cover', 'formats','id', 'isbn', 'ondevice', 'pubdate', 'publisher', 'cover', 'formats','id', 'isbn', 'ondevice', 'pubdate', 'publisher',
@ -4704,24 +4704,33 @@ Author '{0}':
to be replaced. to be replaced.
''' '''
def open_archive(mode='r'):
try:
return ZipFile(self.__archive_path, mode=mode)
except:
# Happens on windows if the file is opened by another
# process
pass
# Generate crc for current cover # Generate crc for current cover
#self.opts.log.info(" generateThumbnail():") #self.opts.log.info(" generateThumbnail():")
data = open(title['cover'], 'rb').read() with open(title['cover'], 'rb') as f:
data = f.read()
cover_crc = hex(zlib.crc32(data)) cover_crc = hex(zlib.crc32(data))
# Test cache for uuid # Test cache for uuid
with ZipFile(self.__archive_path, mode='r') as zfr: zf = open_archive()
try: if zf is not None:
t_info = zfr.getinfo(title['uuid']) with zf:
except: try:
pass zf.getinfo(title['uuid']+cover_crc)
else: except:
if t_info.comment == cover_crc: pass
else:
# uuid found in cache with matching crc # uuid found in cache with matching crc
thumb_data = zfr.read(title['uuid']) thumb_data = zf.read(title['uuid'])
zfr.extract(title['uuid'],image_dir) with open(os.path.join(image_dir, thumb_file), 'wb') as f:
os.rename(os.path.join(image_dir,title['uuid']), f.write(thumb_data)
os.path.join(image_dir,thumb_file))
return return
@ -4732,10 +4741,13 @@ Author '{0}':
f.write(thumb_data) f.write(thumb_data)
# Save thumb to archive # Save thumb to archive
t_info = ZipInfo(title['uuid'],time.localtime()[0:6]) if zf is not None: # Ensure that the read succeeded
t_info.comment = cover_crc # If we failed to open the zip file for reading,
with ZipFile(self.__archive_path, mode='a') as zfw: # we dont know if it contained the thumb or not
zfw.writestr(t_info, thumb_data) zf = open_archive('a')
if zf is not None:
with zf:
zf.writestr(title['uuid']+cover_crc, thumb_data)
def getFriendlyGenreTag(self, genre): def getFriendlyGenreTag(self, genre):
# Find the first instance of friendly_tag matching genre # Find the first instance of friendly_tag matching genre