Use FileWrapper instead of StringIO for bug 2112 fix.

This commit is contained in:
John Schember 2009-04-17 22:29:00 -04:00
parent a2064499e8
commit 70e1336a90
2 changed files with 58 additions and 41 deletions

View File

@ -246,6 +246,23 @@ class CurrentDir(object):
os.chdir(self.cwd)
class FileWrapper(object):
'''
Used primarily with pyPdf to ensure the stream is properly closed.
'''
def __init__(self, stream):
for x in ('read', 'seek', 'tell'):
setattr(self, x, getattr(stream, x))
def __exit__(self, *args):
for x in ('read', 'seek', 'tell'):
setattr(self, x, None)
def __enter__(self):
return self
def detect_ncpus():
"""Detects the number of effective CPUs in the system"""
try:

View File

@ -4,8 +4,9 @@ __license__ = 'GPL v3'
__copyright__ = '2008, Kovid Goyal <kovid at kovidgoyal.net>'
'''Read meta information from PDF files'''
import sys, os, StringIO
import sys, os, cStringIO
from calibre import FileWrapper
from calibre.ebooks.metadata import MetaInformation, authors_to_string
from calibre.ptempfile import TemporaryDirectory
from pyPdf import PdfFileReader, PdfFileWriter
@ -21,7 +22,6 @@ def get_metadata(stream, extract_cover=True):
""" Return metadata as a L{MetaInfo} object """
mi = MetaInformation(_('Unknown'), [_('Unknown')])
stream.seek(0)
stream = StringIO.StringIO(stream.read())
if extract_cover and _imagemagick_loaded:
try:
@ -33,6 +33,7 @@ def get_metadata(stream, extract_cover=True):
traceback.print_exc()
try:
with FileWrapper(stream) as stream:
info = PdfFileReader(stream).getDocumentInfo()
if info.title:
mi.title = info.title
@ -52,17 +53,17 @@ def get_metadata(stream, extract_cover=True):
def set_metadata(stream, mi):
stream.seek(0)
# Use a StringIO object for the pdf because we will want to over
# Use a cStringIO object for the pdf because we will want to over
# write it later and if we are working on the stream directly it
# could cause some issues.
raw = StringIO.StringIO(stream.read())
raw = cStringIO.StringIO(stream.read())
orig_pdf = PdfFileReader(raw)
title = mi.title if mi.title else orig_pdf.documentInfo.title
author = authors_to_string(mi.authors) if mi.authors else orig_pdf.documentInfo.author
out_pdf = PdfFileWriter(title=title, author=author)
for page in orig_pdf.pages:
out_pdf.addPage(page)
out_str = StringIO.StringIO()
out_str = cStringIO.StringIO()
out_pdf.write(out_str)
stream.seek(0)
stream.truncate()
@ -72,12 +73,11 @@ def set_metadata(stream, mi):
def get_cover(stream):
stream.seek(0)
if not isinstance(stream, StringIO.StringIO):
stream = StringIO.StringIO(stream.read())
data = StringIO.StringIO()
data = cStringIO.StringIO()
try:
with FileWrapper(stream) as stream:
pdf = PdfFileReader(stream)
output = PdfFileWriter()