Fix open files with PDF metadata reader

This commit is contained in:
John Schember 2009-03-01 18:10:14 -05:00
parent 3dc42b5302
commit 8119b9e868
2 changed files with 11 additions and 9 deletions

View File

@ -39,13 +39,13 @@ def metadata_from_formats(formats):
return mi2
for path, ext in zip(formats, extensions):
stream = open(path, 'rb')
try:
mi.smart_update(get_metadata(stream, stream_type=ext, use_libprs_metadata=True))
except:
continue
if getattr(mi, 'application_id', None) is not None:
return mi
with open(path, 'rb') as stream:
try:
mi.smart_update(get_metadata(stream, stream_type=ext, use_libprs_metadata=True))
except:
continue
if getattr(mi, 'application_id', None) is not None:
return mi
if not mi.title:
mi.title = _('Unknown')

View File

@ -67,6 +67,8 @@ def set_metadata(stream, mi):
stream.seek(0)
def get_cover(stream):
data = StringIO.StringIO()
try:
pdf = PdfFileReader(stream)
output = PdfFileWriter()
@ -88,12 +90,12 @@ def get_cover(stream):
img = Image.open('%s.jpg' % cover_path)
data = StringIO.StringIO()
img.save(data, 'JPEG')
return data.getvalue()
except:
import traceback
traceback.print_exc()
return data.getvalue()
def option_parser():
p = get_parser('pdf')