mirror of
https://github.com/kovidgoyal/calibre.git
synced 2025-07-08 10:44:09 -04:00
use context managers for open()
This commit is contained in:
parent
67f8b23baa
commit
2da800aa6e
@ -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,15 +374,14 @@ class HTMLConverter(object):
|
||||
|
||||
if not os.path.exists(path):
|
||||
path = path.replace('&', '%26') # convertlit replaces & with %26 in file names
|
||||
f = open(path, 'rb')
|
||||
raw = f.read()
|
||||
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')
|
||||
elif self.encoding is not None:
|
||||
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):
|
||||
|
@ -687,9 +687,8 @@ class LrfWriter(object):
|
||||
self.tocObjId = obj.objId
|
||||
|
||||
def setThumbnailFile(self, filename, encoding=None):
|
||||
f = open(filename, "rb")
|
||||
self.thumbnailData = f.read()
|
||||
f.close()
|
||||
with open(filename, "rb") as f:
|
||||
self.thumbnailData = f.read()
|
||||
|
||||
if encoding is None:
|
||||
encoding = os.path.splitext(filename)[1][1:]
|
||||
|
@ -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:
|
||||
|
@ -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:
|
||||
|
@ -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:
|
||||
|
@ -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:
|
||||
|
@ -180,8 +180,9 @@ 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,
|
||||
strip_encoding_pats=True)[0]
|
||||
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))
|
||||
xpn = {'re': 'http://exslt.org/regular-expressions'}
|
||||
@ -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:
|
||||
|
Loading…
x
Reference in New Issue
Block a user