use context managers for open()

This commit is contained in:
Eli Schwartz 2019-06-14 03:29:29 -04:00
parent 67f8b23baa
commit 2da800aa6e
No known key found for this signature in database
GPG Key ID: CEB167EFB5722BD6
7 changed files with 25 additions and 20 deletions

View File

@ -352,10 +352,9 @@ class HTMLConverter(object):
if not os.path.exists(tdir):
os.makedirs(tdir)
try:
dump = open(os.path.join(tdir, 'html2lrf-verbose.html'), 'wb')
dump.write(unicode_type(soup).encode('utf-8'))
self.log.info(_('Written preprocessed HTML to ')+dump.name)
dump.close()
with open(os.path.join(tdir, 'html2lrf-verbose.html'), 'wb') as f:
f.write(unicode_type(soup).encode('utf-8'))
self.log.info(_('Written preprocessed HTML to ')+f.name)
except:
pass
@ -375,7 +374,7 @@ class HTMLConverter(object):
if not os.path.exists(path):
path = path.replace('&', '%26') # convertlit replaces & with %26 in file names
f = open(path, 'rb')
with open(path, 'rb') as f:
raw = f.read()
if self.pdftohtml: # Bug in pdftohtml that causes it to output invalid UTF-8 files
raw = raw.decode('utf-8', 'ignore')
@ -383,7 +382,6 @@ class HTMLConverter(object):
raw = raw.decode(self.encoding, 'ignore')
else:
raw = xml_to_unicode(raw, self.verbose)[0]
f.close()
soup = self.preprocess(raw)
self.log.info(_('\tConverting to BBeB...'))
self.current_style = {}
@ -1935,7 +1933,8 @@ def try_opf(path, options, logger):
dirpath = os.path.dirname(os.path.abspath(opf))
from calibre.ebooks.metadata.opf2 import OPF as OPF2
opf = OPF2(open(opf, 'rb'), dirpath)
with open(opf, 'rb') as f:
opf = OPF2(f, dirpath)
try:
title = opf.title
if title and not getattr(options, 'title', None):

View File

@ -687,9 +687,8 @@ class LrfWriter(object):
self.tocObjId = obj.objId
def setThumbnailFile(self, filename, encoding=None):
f = open(filename, "rb")
with open(filename, "rb") as f:
self.thumbnailData = f.read()
f.close()
if encoding is None:
encoding = os.path.splitext(filename)[1][1:]

View File

@ -2268,9 +2268,8 @@ class ImageStream(LrsObject, LrsContainer):
self.encoding = encoding
def toLrf(self, lrfWriter):
imageFile = open(self.filename, "rb")
imageData = imageFile.read()
imageFile.close()
with open(self.filename, "rb") as f:
imageData = f.read()
isObj = LrfObject("ImageStream", self.objId)
if self.comment is not None:

View File

@ -64,7 +64,8 @@ def set_metadata(stream, mi):
raise Exception('no cover')
except:
try:
new_cdata = open(mi.cover, 'rb').read()
with open(mi.cover, 'rb') as f:
new_cdata = f.read()
except:
pass
if new_cdata:

View File

@ -215,7 +215,8 @@ def opf_metadata(opfpath):
cpath = os.path.join(os.path.dirname(opfpath), opf.cover)
if os.access(cpath, os.R_OK):
fmt = cpath.rpartition('.')[-1]
data = open(cpath, 'rb').read()
with open(cpath, 'rb') as f:
data = f.read()
mi.cover_data = (fmt, data)
return mi
except:

View File

@ -443,7 +443,10 @@ class MetadataUpdater(object):
if mi.cover_data[1] or mi.cover:
try:
data = mi.cover_data[1] if mi.cover_data[1] else open(mi.cover, 'rb').read()
data = mi.cover_data[1]
if not data:
with open(mi.cover, 'rb') as f:
data = f.read()
except:
pass
else:

View File

@ -180,7 +180,8 @@ class TOC(list):
def read_ncx_toc(self, toc, root=None):
self.base_path = os.path.dirname(toc)
if root is None:
raw = xml_to_unicode(open(toc, 'rb').read(), assume_utf8=True,
with open(toc, 'rb') as f:
raw = xml_to_unicode(f.read(), assume_utf8=True,
strip_encoding_pats=True)[0]
root = etree.fromstring(raw, parser=etree.XMLParser(recover=True,
no_network=True))
@ -234,7 +235,9 @@ class TOC(list):
def read_html_toc(self, toc):
self.base_path = os.path.dirname(toc)
for href, fragment, txt in parse_html_toc(lopen(toc, 'rb').read()):
with lopen(toc, 'rb') as f:
parsed_toc = parse_html_toc(f.read())
for href, fragment, txt in parsed_toc:
add = True
for i in self.flat():
if i.href == href and i.fragment == fragment: