Fix #6539 (epub-fix fails on ePub check if it contains tag mismatch)

This commit is contained in:
Kovid Goyal 2010-08-18 17:08:57 -06:00
parent e999d1d97b
commit 00b8a303b9
3 changed files with 21 additions and 3 deletions

View File

@ -11,6 +11,14 @@ from calibre.customize import Plugin
class InvalidEpub(ValueError): class InvalidEpub(ValueError):
pass pass
class ParseError(ValueError):
def __init__(self, name, desc):
self.name = name
self.desc = desc
ValueError.__init__(self,
_('Failed to parse: %s with error: %s')%(name, desc))
class ePubFixer(Plugin): class ePubFixer(Plugin):
supported_platforms = ['windows', 'osx', 'linux'] supported_platforms = ['windows', 'osx', 'linux']

View File

@ -8,8 +8,9 @@ __docformat__ = 'restructuredtext en'
import os, posixpath, urllib, sys, re import os, posixpath, urllib, sys, re
from lxml import etree from lxml import etree
from lxml.etree import XMLSyntaxError
from calibre.ebooks.epub.fix import InvalidEpub from calibre.ebooks.epub.fix import InvalidEpub, ParseError
from calibre import guess_type, prepare_string_for_xml from calibre import guess_type, prepare_string_for_xml
from calibre.ebooks.chardet import xml_to_unicode from calibre.ebooks.chardet import xml_to_unicode
from calibre.constants import iswindows from calibre.constants import iswindows
@ -148,7 +149,10 @@ class Container(object):
return self.cache[name] return self.cache[name]
raw = self.get_raw(name) raw = self.get_raw(name)
if name in self.mime_map: if name in self.mime_map:
raw = self._parse(raw, self.mime_map[name]) try:
raw = self._parse(raw, self.mime_map[name])
except XMLSyntaxError, err:
raise ParseError(name, unicode(err))
self.cache[name] = raw self.cache[name] = raw
return raw return raw

View File

@ -14,6 +14,8 @@ from calibre.utils.zipfile import ZipFile
from calibre.utils.logging import default_log from calibre.utils.logging import default_log
from calibre.customize.ui import epub_fixers from calibre.customize.ui import epub_fixers
from calibre.ebooks.epub.fix.container import Container from calibre.ebooks.epub.fix.container import Container
from calibre.ebooks.epub.fix import ParseError
def option_parser(): def option_parser():
parser = OptionParser(usage=_( parser = OptionParser(usage=_(
@ -50,7 +52,11 @@ def main(args=sys.argv):
default_log.error(_('You must specify an epub file')) default_log.error(_('You must specify an epub file'))
return return
epub = os.path.abspath(args[1]) epub = os.path.abspath(args[1])
run(epub, opts, default_log) try:
run(epub, opts, default_log)
except ParseError, err:
default_log.error(unicode(err))
raise SystemExit(1)
if __name__ == '__main__': if __name__ == '__main__':
main() main()