py3: Remove another use of cStringIO

This commit is contained in:
Kovid Goyal 2019-03-31 10:05:15 +05:30
parent c33768b35e
commit dc15aff051
No known key found for this signature in database
GPG Key ID: 06BC317B515ACE7C

View File

@ -4,7 +4,7 @@ __copyright__ = '2008, Kovid Goyal <kovid at kovidgoyal.net>'
""" """
Edit metadata in RTF files. Edit metadata in RTF files.
""" """
import re, cStringIO, codecs import re, codecs
from calibre import force_unicode from calibre import force_unicode
from calibre.ebooks.metadata import MetaInformation, string_to_authors from calibre.ebooks.metadata import MetaInformation, string_to_authors
@ -26,38 +26,38 @@ def get_document_info(stream):
""" """
block_size = 4096 block_size = 4096
stream.seek(0) stream.seek(0)
found, block = False, "" found, block = False, b""
while not found: while not found:
prefix = block[-6:] prefix = block[-6:]
block = prefix + stream.read(block_size) block = prefix + stream.read(block_size)
actual_block_size = len(block) - len(prefix) actual_block_size = len(block) - len(prefix)
if len(block) == len(prefix): if len(block) == len(prefix):
break break
idx = block.find(r'{\info') idx = block.find(br'{\info')
if idx >= 0: if idx >= 0:
found = True found = True
pos = stream.tell() - actual_block_size + idx - len(prefix) pos = stream.tell() - actual_block_size + idx - len(prefix)
stream.seek(pos) stream.seek(pos)
else: else:
if block.find(r'\sect') > -1: if block.find(br'\sect') > -1:
break break
if not found: if not found:
return None, 0 return None, 0
data, count, = cStringIO.StringIO(), 0 data, count, = [], 0
pos = stream.tell() pos = stream.tell()
while True: while True:
ch = stream.read(1) ch = stream.read(1)
if ch == '\\': if ch == b'\\':
data.write(ch + stream.read(1)) data.append(ch + stream.read(1))
continue continue
if ch == '{': if ch == b'{':
count += 1 count += 1
elif ch == '}': elif ch == b'}':
count -= 1 count -= 1
data.write(ch) data.append(ch)
if count == 0: if count == 0:
break break
return data.getvalue(), pos return b''.join(data), pos
def detect_codepage(stream): def detect_codepage(stream):