LIT Input:Workaround for LIT files created by ReaderWorks that have quoted ampersands in their filenames. Fixes #1627 (Failed to convert LIT to EPUB)

This commit is contained in:
Kovid Goyal 2009-01-21 12:43:22 -08:00
parent c8a9846b0e
commit f35e9376e1
2 changed files with 34 additions and 6 deletions

View File

@ -16,7 +16,7 @@ from calibre.ebooks.epub import config as common_config, process_encryption
from calibre.ebooks.epub.from_html import convert as html2epub, find_html_index from calibre.ebooks.epub.from_html import convert as html2epub, find_html_index
from calibre.ptempfile import TemporaryDirectory from calibre.ptempfile import TemporaryDirectory
from calibre.ebooks.metadata import MetaInformation from calibre.ebooks.metadata import MetaInformation
from calibre.ebooks.metadata.opf2 import OPFCreator from calibre.ebooks.metadata.opf2 import OPFCreator, OPF
from calibre.utils.zipfile import ZipFile from calibre.utils.zipfile import ZipFile
from calibre.customize.ui import run_plugins_on_preprocess from calibre.customize.ui import run_plugins_on_preprocess
@ -25,9 +25,36 @@ def lit2opf(path, tdir, opts):
print 'Exploding LIT file:', path print 'Exploding LIT file:', path
reader = LitReader(path) reader = LitReader(path)
reader.extract_content(tdir, False) reader.extract_content(tdir, False)
for f in walk(tdir): opf = None
if f.lower().endswith('.opf'): for opf in walk(tdir):
return f if opf.lower().endswith('.opf'):
break
if not opf.endswith('.opf'):
opf = None
if opf is not None: # Check for url-quoted filenames
_opf = OPF(opf, os.path.dirname(opf))
replacements = []
for item in _opf.itermanifest():
href = item.get('href', '')
path = os.path.join(os.path.dirname(opf), *(href.split('/')))
if not os.path.exists(path) and os.path.exists(path.replace('&', '%26')):
npath = path
path = path.replace('&', '%26')
replacements.append((path, npath))
if replacements:
print 'Fixing quoted filenames...'
for path, npath in replacements:
if os.path.exists(path):
os.rename(path, npath)
for f in walk(tdir):
with open(f, 'r+b') as f:
raw = f.read()
for path, npath in replacements:
raw = raw.replace(os.path.basename(path), os.path.basename(npath))
f.seek(0)
f.truncate()
f.write(raw)
return opf
def mobi2opf(path, tdir, opts): def mobi2opf(path, tdir, opts):
from calibre.ebooks.mobi.reader import MobiReader from calibre.ebooks.mobi.reader import MobiReader

View File

@ -435,7 +435,7 @@ class OPF(object):
rating = MetadataField('rating', is_dc=False, formatter=int) rating = MetadataField('rating', is_dc=False, formatter=int)
def __init__(self, stream, basedir=os.getcwdu()): def __init__(self, stream, basedir=os.getcwdu(), unquote_urls=True):
if not hasattr(stream, 'read'): if not hasattr(stream, 'read'):
stream = open(stream, 'rb') stream = open(stream, 'rb')
self.basedir = self.base_dir = basedir self.basedir = self.base_dir = basedir
@ -446,7 +446,8 @@ class OPF(object):
if not self.metadata: if not self.metadata:
raise ValueError('Malformed OPF file: No <metadata> element') raise ValueError('Malformed OPF file: No <metadata> element')
self.metadata = self.metadata[0] self.metadata = self.metadata[0]
self.unquote_urls() if unquote_urls:
self.unquote_urls()
self.manifest = Manifest() self.manifest = Manifest()
m = self.manifest_path(self.root) m = self.manifest_path(self.root)
if m: if m: