When adding books, if reading metadata raises an exception, make error reporting more robust

This commit is contained in:
Kovid Goyal 2009-07-08 19:23:16 -06:00
parent a9f3765632
commit 61850df263
3 changed files with 37 additions and 17 deletions

View File

@ -24,21 +24,26 @@ def read_metadata_(task, tdir, notification=lambda x,y:x):
from calibre.ebooks.metadata.meta import metadata_from_formats from calibre.ebooks.metadata.meta import metadata_from_formats
from calibre.ebooks.metadata.opf2 import OPFCreator from calibre.ebooks.metadata.opf2 import OPFCreator
for x in task: for x in task:
id, formats = x try:
if isinstance(formats, basestring): formats = [formats] id, formats = x
mi = metadata_from_formats(formats) if isinstance(formats, basestring): formats = [formats]
mi.cover = None mi = metadata_from_formats(formats)
cdata = None mi.cover = None
if mi.cover_data: cdata = None
cdata = mi.cover_data[-1] if mi.cover_data:
mi.cover_data = None cdata = mi.cover_data[-1]
opf = OPFCreator(tdir, mi) mi.cover_data = None
with open(os.path.join(tdir, '%s.opf'%id), 'wb') as f: opf = OPFCreator(tdir, mi)
opf.render(f) with open(os.path.join(tdir, '%s.opf'%id), 'wb') as f:
if cdata: opf.render(f)
with open(os.path.join(tdir, str(id)), 'wb') as f: if cdata:
f.write(cdata) with open(os.path.join(tdir, str(id)), 'wb') as f:
notification(0.5, id) f.write(cdata)
notification(0.5, id)
except:
import traceback
with open(os.path.join(tdir, '%s.error'%id), 'wb') as f:
f.write(traceback.format_exc())
class Progress(object): class Progress(object):
@ -49,7 +54,10 @@ class Progress(object):
def __call__(self, id): def __call__(self, id):
cover = os.path.join(self.tdir, str(id)) cover = os.path.join(self.tdir, str(id))
if not os.path.exists(cover): cover = None if not os.path.exists(cover): cover = None
self.result_queue.put((id, os.path.join(self.tdir, '%s.opf'%id), cover)) res = os.path.join(self.tdir, '%s.error'%id)
if not os.path.exists(res):
res = res.replace('.error', '.opf')
self.result_queue.put((id, res, cover))
class ReadMetadata(Thread): class ReadMetadata(Thread):

View File

@ -44,6 +44,7 @@ class Adder(QObject):
self.pd = ProgressDialog(_('Adding...'), parent=parent) self.pd = ProgressDialog(_('Adding...'), parent=parent)
self.spare_server = spare_server self.spare_server = spare_server
self.db = db self.db = db
self.critical = {}
self.pd.setModal(True) self.pd.setModal(True)
self.pd.show() self.pd.show()
self._parent = parent self._parent = parent
@ -123,8 +124,12 @@ class Adder(QObject):
return return
self.pd.value += 1 self.pd.value += 1
formats = self.ids.pop(id) formats = self.ids.pop(id)
mi = MetaInformation(OPF(opf))
name = self.nmap.pop(id) name = self.nmap.pop(id)
if opf.endswith('.error'):
mi = MetaInformation('', [_('Unknown')])
self.critical[name] = open(opf, 'rb').read().decode('utf-8', 'replace')
else:
mi = MetaInformation(OPF(opf))
if not mi.title: if not mi.title:
mi.title = os.path.splitext(name)[0] mi.title = os.path.splitext(name)[0]
mi.title = mi.title if isinstance(mi.title, unicode) else \ mi.title = mi.title if isinstance(mi.title, unicode) else \

View File

@ -860,6 +860,13 @@ class Main(MainWindow, Ui_MainWindow, DeviceGUI):
self.library_view.model().books_added(self._adder.number_of_books_added) self.library_view.model().books_added(self._adder.number_of_books_added)
if hasattr(self, 'db_images'): if hasattr(self, 'db_images'):
self.db_images.reset() self.db_images.reset()
if self._adder.critical:
det_msg = []
for name, log in self._adder.critical.items():
det_msg.append(name+'\n'+log)
error_dialog(self, _('Failed to read metadata'),
_('Failed to read metadata from the following')+':',
det_msg='\n\n'.join(det_msg), show=True)
self._adder = None self._adder = None