From 2d2a894e05aaeece4d7271488aba9554ffcc93eb Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Sat, 16 Feb 2013 21:40:14 +0530 Subject: [PATCH] Polish books: Do not error out on epub files containing an iTunesMetadata.plist file. Fixes #1127308 (polish books error) --- src/calibre/ebooks/oeb/polish/container.py | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) diff --git a/src/calibre/ebooks/oeb/polish/container.py b/src/calibre/ebooks/oeb/polish/container.py index 187669cc13..34ef72a9bf 100644 --- a/src/calibre/ebooks/oeb/polish/container.py +++ b/src/calibre/ebooks/oeb/polish/container.py @@ -14,7 +14,7 @@ from urlparse import urlparse from lxml import etree -from calibre import guess_type, CurrentDir +from calibre import guess_type as _guess_type, CurrentDir from calibre.customize.ui import (plugin_for_input_format, plugin_for_output_format) from calibre.ebooks.chardet import xml_to_unicode @@ -35,7 +35,10 @@ from calibre.utils.zipfile import ZipFile exists, join, relpath = os.path.exists, os.path.join, os.path.relpath -OEB_FONTS = {guess_type('a.ttf')[0], guess_type('b.ttf')[0]} +def guess_type(x): + return _guess_type(x)[0] or 'application/octet-stream' + +OEB_FONTS = {guess_type('a.ttf'), guess_type('b.ttf')} OPF_NAMESPACES = {'opf':OPF2_NS, 'dc':DC11_NS} class Container(object): @@ -77,12 +80,12 @@ class Container(object): path = join(dirpath, f) name = self.abspath_to_name(path) self.name_path_map[name] = path - self.mime_map[name] = guess_type(path)[0] + self.mime_map[name] = guess_type(path) # Special case if we have stumbled onto the opf if path == opfpath: self.opf_name = name self.opf_dir = os.path.dirname(path) - self.mime_map[name] = guess_type('a.opf')[0] + self.mime_map[name] = guess_type('a.opf') if not hasattr(self, 'opf_name'): raise InvalidBook('Book has no OPF file') @@ -206,7 +209,7 @@ class Container(object): def parsed(self, name): ans = self.parsed_cache.get(name, None) if ans is None: - mime = self.mime_map.get(name, guess_type(name)[0]) + mime = self.mime_map.get(name, guess_type(name)) ans = self.parse(self.name_path_map[name], mime) self.parsed_cache[name] = ans return ans @@ -341,7 +344,7 @@ class Container(object): name. Ensures uniqueness of href and id automatically. Returns generated item.''' id_prefix = id_prefix or 'id' - media_type = media_type or guess_type(name)[0] + media_type = media_type or guess_type(name) href = self.name_to_href(name, self.opf_name) base, ext = href.rpartition('.')[0::2] all_ids = {x.get('id') for x in self.opf_xpath('//*[@id]')} @@ -476,7 +479,7 @@ class EpubContainer(Container): self.container = etree.fromstring(open(container_path, 'rb').read()) opf_files = self.container.xpath(( r'child::ocf:rootfiles/ocf:rootfile' - '[@media-type="%s" and @full-path]'%guess_type('a.opf')[0] + '[@media-type="%s" and @full-path]'%guess_type('a.opf') ), namespaces={'ocf':OCF_NS} ) if not opf_files: @@ -555,7 +558,7 @@ class EpubContainer(Container): outpath = self.pathtoepub from calibre.ebooks.tweak import zip_rebuilder with open(join(self.root, 'mimetype'), 'wb') as f: - f.write(guess_type('a.epub')[0]) + f.write(guess_type('a.epub')) zip_rebuilder(self.root, outpath) # }}}