Fix regression in epub2lrf when converting EPUB files that specify a HTML page as a cover

This commit is contained in:
Kovid Goyal 2008-12-31 10:35:33 -08:00
parent dad9ec898f
commit e3b148e84a
3 changed files with 36 additions and 21 deletions

View File

@ -2,14 +2,14 @@ __license__ = 'GPL v3'
__copyright__ = '2008, Kovid Goyal <kovid at kovidgoyal.net>'
import os, sys, shutil, logging
from tempfile import mkdtemp
from calibre.ebooks.lrf import option_parser as lrf_option_parser
from calibre.ebooks import ConversionError, DRMError
from calibre.ebooks.lrf.html.convert_from import process_file as html_process_file
from calibre.ebooks.metadata.opf import OPF
from calibre.ebooks.metadata.epub import OCFDirReader
from calibre.utils.zipfile import ZipFile
from calibre import __appname__, setup_cli_handlers
from calibre import setup_cli_handlers
from calibre.ptempfile import PersistentTemporaryDirectory
def option_parser():
@ -22,17 +22,16 @@ _('''Usage: %prog [options] mybook.epub
def generate_html(pathtoepub, logger):
if not os.access(pathtoepub, os.R_OK):
raise ConversionError, 'Cannot read from ' + pathtoepub
tdir = mkdtemp(prefix=__appname__+'_')
os.rmdir(tdir)
raise ConversionError('Cannot read from ' + pathtoepub)
tdir = PersistentTemporaryDirectory('_epub2lrf')
#os.rmdir(tdir)
try:
ZipFile(pathtoepub).extractall(tdir)
except:
raise ConversionError, '.epub extraction failed'
if os.path.exists(os.path.join(tdir, 'META-INF', 'encryption.xml')):
raise DRMError(os.path.basename(pathtoepub))
except:
if os.path.exists(tdir) and os.path.isdir(tdir):
shutil.rmtree(tdir)
raise ConversionError, '.epub extraction failed'
return tdir
def process_file(path, options, logger=None):

View File

@ -1982,17 +1982,7 @@ def try_opf(path, options, logger):
PILImage.open(cover)
options.cover = cover
except:
for prefix in opf.possible_cover_prefixes():
if options.cover:
break
for suffix in ['.jpg', '.jpeg', '.gif', '.png', '.bmp']:
cpath = os.path.join(os.path.dirname(path), prefix+suffix)
try:
PILImage.open(cpath)
options.cover = cpath
break
except:
continue
pass
if not getattr(options, 'cover', None) and orig_cover is not None:
options.cover = orig_cover
if getattr(opf, 'spine', False):

View File

@ -418,7 +418,8 @@ class OPF(object):
tags_path = XPath('descendant::*[re:match(name(), "subject", "i")]')
isbn_path = XPath('descendant::*[re:match(name(), "identifier", "i") and '+
'(re:match(@scheme, "isbn", "i") or re:match(@opf:scheme, "isbn", "i"))]')
application_id_path= XPath('descendant::*[re:match(name(), "identifier", "i") and '+
identifier_path = XPath('descendant::*[re:match(name(), "identifier", "i")]')
application_id_path = XPath('descendant::*[re:match(name(), "identifier", "i") and '+
'(re:match(@opf:scheme, "calibre|libprs500", "i") or re:match(@scheme, "calibre|libprs500", "i"))]')
manifest_path = XPath('descendant::*[re:match(name(), "manifest", "i")]/*[re:match(name(), "item", "i")]')
manifest_ppath = XPath('descendant::*[re:match(name(), "manifest", "i")]')
@ -719,6 +720,27 @@ class OPF(object):
return property(fget=fget, fset=fset)
def guess_cover(self):
'''
Try to guess a cover. Needed for some old/badly formed OPF files.
'''
if self.base_dir and os.path.exists(self.base_dir):
for item in self.identifier_path(self.metadata):
scheme = None
for key in item.attrib.keys():
if key.endswith('scheme'):
scheme = item.get(key)
break
if scheme is None:
continue
if item.text:
prefix = item.text.replace('-', '')
for suffix in ['.jpg', '.jpeg', '.gif', '.png', '.bmp']:
cpath = os.access(os.path.join(self.base_dir, prefix+suffix), os.R_OK)
if os.access(os.path.join(self.base_dir, prefix+suffix), os.R_OK):
return cpath
@apply
def cover():
@ -728,6 +750,10 @@ class OPF(object):
for item in self.guide:
if item.type.lower() == t:
return item.path
try:
return self.guess_cover()
except:
pass
def fset(self, path):
if self.guide is not None: