Prevent unnecessary filesystem access when adding books to calibre

This commit is contained in:
Kovid Goyal 2014-10-05 09:16:14 +05:30
parent 7dc87c468e
commit 7d5b5309b0
2 changed files with 5 additions and 3 deletions

View File

@ -560,12 +560,13 @@ class OPF(object): # {{{
formatter=json.loads, renderer=dump_dict)
def __init__(self, stream, basedir=os.getcwdu(), unquote_urls=True,
populate_spine=True):
populate_spine=True, try_to_guess_cover=True):
if not hasattr(stream, 'read'):
stream = open(stream, 'rb')
raw = stream.read()
if not raw:
raise ValueError('Empty file: '+getattr(stream, 'name', 'stream'))
self.try_to_guess_cover = try_to_guess_cover
self.basedir = self.base_dir = basedir
self.path_to_html_toc = self.html_toc_fragment = None
raw, self.encoding = xml_to_unicode(raw, strip_encoding_pats=True,
@ -1186,7 +1187,8 @@ class OPF(object): # {{{
if item.type and item.type.lower() == t:
return item.path
try:
return self.guess_cover()
if self.try_to_guess_cover:
return self.guess_cover()
except:
pass

View File

@ -198,7 +198,7 @@ class DBAdder(QObject): # {{{
self.critical[name] = open(opf, 'rb').read().decode('utf-8', 'replace')
else:
try:
mi = OPF(opf).to_book_metadata()
mi = OPF(opf, try_to_guess_cover=False, basedir=os.path.dirname(opf)).to_book_metadata()
except:
import traceback
mi = MetaInformation('', [_('Unknown')])