mirror of
https://github.com/kovidgoyal/calibre.git
synced 2025-07-09 03:04:10 -04:00
A better error message when users attempt to open KFX books as MOBI files
This commit is contained in:
parent
4d88929e4e
commit
3caa7f86de
@ -29,6 +29,10 @@ class TopazError(ValueError):
|
|||||||
pass
|
pass
|
||||||
|
|
||||||
|
|
||||||
|
class KFXError(ValueError):
|
||||||
|
pass
|
||||||
|
|
||||||
|
|
||||||
class MobiReader(object):
|
class MobiReader(object):
|
||||||
PAGE_BREAK_PAT = re.compile(
|
PAGE_BREAK_PAT = re.compile(
|
||||||
r'<\s*/{0,1}\s*mbp:pagebreak((?:\s+[^/>]*){0,1})/{0,1}\s*>\s*(?:<\s*/{0,1}\s*mbp:pagebreak\s*/{0,1}\s*>)*',
|
r'<\s*/{0,1}\s*mbp:pagebreak((?:\s+[^/>]*){0,1})/{0,1}\s*>\s*(?:<\s*/{0,1}\s*mbp:pagebreak\s*/{0,1}\s*>)*',
|
||||||
@ -71,6 +75,8 @@ class MobiReader(object):
|
|||||||
raw = stream.read()
|
raw = stream.read()
|
||||||
if raw.startswith('TPZ'):
|
if raw.startswith('TPZ'):
|
||||||
raise TopazError(_('This is an Amazon Topaz book. It cannot be processed.'))
|
raise TopazError(_('This is an Amazon Topaz book. It cannot be processed.'))
|
||||||
|
if raw.startswith(b'\xeaDRMION\xee'):
|
||||||
|
raise KFXError(_('This is an Amazon KFX book. It cannot be processed.'))
|
||||||
|
|
||||||
self.header = raw[0:72]
|
self.header = raw[0:72]
|
||||||
self.name = self.header[:32].replace('\x00', '')
|
self.name = self.header[:32].replace('\x00', '')
|
||||||
@ -890,5 +896,3 @@ def test_mbp_regex():
|
|||||||
ans = MobiReader.PAGE_BREAK_PAT.sub(r'\1', raw)
|
ans = MobiReader.PAGE_BREAK_PAT.sub(r'\1', raw)
|
||||||
if ans != m:
|
if ans != m:
|
||||||
raise Exception('%r != %r for %r'%(ans, m, raw))
|
raise Exception('%r != %r for %r'%(ans, m, raw))
|
||||||
|
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user