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):
|
if not os.path.exists(tdir):
|
||||||
os.makedirs(tdir)
|
os.makedirs(tdir)
|
||||||
try:
|
try:
|
||||||
dump = open(os.path.join(tdir, 'html2lrf-verbose.html'), 'wb')
|
with open(os.path.join(tdir, 'html2lrf-verbose.html'), 'wb') as f:
|
||||||
dump.write(unicode_type(soup).encode('utf-8'))
|
f.write(unicode_type(soup).encode('utf-8'))
|
||||||
self.log.info(_('Written preprocessed HTML to ')+dump.name)
|
self.log.info(_('Written preprocessed HTML to ')+f.name)
|
||||||
dump.close()
|
|
||||||
except:
|
except:
|
||||||
pass
|
pass
|
||||||
|
|
||||||
@ -375,15 +374,14 @@ class HTMLConverter(object):
|
|||||||
|
|
||||||
if not os.path.exists(path):
|
if not os.path.exists(path):
|
||||||
path = path.replace('&', '%26') # convertlit replaces & with %26 in file names
|
path = path.replace('&', '%26') # convertlit replaces & with %26 in file names
|
||||||
f = open(path, 'rb')
|
with open(path, 'rb') as f:
|
||||||
raw = f.read()
|
raw = f.read()
|
||||||
if self.pdftohtml: # Bug in pdftohtml that causes it to output invalid UTF-8 files
|
if self.pdftohtml: # Bug in pdftohtml that causes it to output invalid UTF-8 files
|
||||||
raw = raw.decode('utf-8', 'ignore')
|
raw = raw.decode('utf-8', 'ignore')
|
||||||
elif self.encoding is not None:
|
elif self.encoding is not None:
|
||||||
raw = raw.decode(self.encoding, 'ignore')
|
raw = raw.decode(self.encoding, 'ignore')
|
||||||
else:
|
else:
|
||||||
raw = xml_to_unicode(raw, self.verbose)[0]
|
raw = xml_to_unicode(raw, self.verbose)[0]
|
||||||
f.close()
|
|
||||||
soup = self.preprocess(raw)
|
soup = self.preprocess(raw)
|
||||||
self.log.info(_('\tConverting to BBeB...'))
|
self.log.info(_('\tConverting to BBeB...'))
|
||||||
self.current_style = {}
|
self.current_style = {}
|
||||||
@ -1935,7 +1933,8 @@ def try_opf(path, options, logger):
|
|||||||
|
|
||||||
dirpath = os.path.dirname(os.path.abspath(opf))
|
dirpath = os.path.dirname(os.path.abspath(opf))
|
||||||
from calibre.ebooks.metadata.opf2 import OPF as OPF2
|
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:
|
try:
|
||||||
title = opf.title
|
title = opf.title
|
||||||
if title and not getattr(options, 'title', None):
|
if title and not getattr(options, 'title', None):
|
||||||
|
@ -687,9 +687,8 @@ class LrfWriter(object):
|
|||||||
self.tocObjId = obj.objId
|
self.tocObjId = obj.objId
|
||||||
|
|
||||||
def setThumbnailFile(self, filename, encoding=None):
|
def setThumbnailFile(self, filename, encoding=None):
|
||||||
f = open(filename, "rb")
|
with open(filename, "rb") as f:
|
||||||
self.thumbnailData = f.read()
|
self.thumbnailData = f.read()
|
||||||
f.close()
|
|
||||||
|
|
||||||
if encoding is None:
|
if encoding is None:
|
||||||
encoding = os.path.splitext(filename)[1][1:]
|
encoding = os.path.splitext(filename)[1][1:]
|
||||||
|
@ -2268,9 +2268,8 @@ class ImageStream(LrsObject, LrsContainer):
|
|||||||
self.encoding = encoding
|
self.encoding = encoding
|
||||||
|
|
||||||
def toLrf(self, lrfWriter):
|
def toLrf(self, lrfWriter):
|
||||||
imageFile = open(self.filename, "rb")
|
with open(self.filename, "rb") as f:
|
||||||
imageData = imageFile.read()
|
imageData = f.read()
|
||||||
imageFile.close()
|
|
||||||
|
|
||||||
isObj = LrfObject("ImageStream", self.objId)
|
isObj = LrfObject("ImageStream", self.objId)
|
||||||
if self.comment is not None:
|
if self.comment is not None:
|
||||||
|
@ -64,7 +64,8 @@ def set_metadata(stream, mi):
|
|||||||
raise Exception('no cover')
|
raise Exception('no cover')
|
||||||
except:
|
except:
|
||||||
try:
|
try:
|
||||||
new_cdata = open(mi.cover, 'rb').read()
|
with open(mi.cover, 'rb') as f:
|
||||||
|
new_cdata = f.read()
|
||||||
except:
|
except:
|
||||||
pass
|
pass
|
||||||
if new_cdata:
|
if new_cdata:
|
||||||
|
@ -215,7 +215,8 @@ def opf_metadata(opfpath):
|
|||||||
cpath = os.path.join(os.path.dirname(opfpath), opf.cover)
|
cpath = os.path.join(os.path.dirname(opfpath), opf.cover)
|
||||||
if os.access(cpath, os.R_OK):
|
if os.access(cpath, os.R_OK):
|
||||||
fmt = cpath.rpartition('.')[-1]
|
fmt = cpath.rpartition('.')[-1]
|
||||||
data = open(cpath, 'rb').read()
|
with open(cpath, 'rb') as f:
|
||||||
|
data = f.read()
|
||||||
mi.cover_data = (fmt, data)
|
mi.cover_data = (fmt, data)
|
||||||
return mi
|
return mi
|
||||||
except:
|
except:
|
||||||
|
@ -443,7 +443,10 @@ class MetadataUpdater(object):
|
|||||||
|
|
||||||
if mi.cover_data[1] or mi.cover:
|
if mi.cover_data[1] or mi.cover:
|
||||||
try:
|
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:
|
except:
|
||||||
pass
|
pass
|
||||||
else:
|
else:
|
||||||
|
@ -180,8 +180,9 @@ class TOC(list):
|
|||||||
def read_ncx_toc(self, toc, root=None):
|
def read_ncx_toc(self, toc, root=None):
|
||||||
self.base_path = os.path.dirname(toc)
|
self.base_path = os.path.dirname(toc)
|
||||||
if root is None:
|
if root is None:
|
||||||
raw = xml_to_unicode(open(toc, 'rb').read(), assume_utf8=True,
|
with open(toc, 'rb') as f:
|
||||||
strip_encoding_pats=True)[0]
|
raw = xml_to_unicode(f.read(), assume_utf8=True,
|
||||||
|
strip_encoding_pats=True)[0]
|
||||||
root = etree.fromstring(raw, parser=etree.XMLParser(recover=True,
|
root = etree.fromstring(raw, parser=etree.XMLParser(recover=True,
|
||||||
no_network=True))
|
no_network=True))
|
||||||
xpn = {'re': 'http://exslt.org/regular-expressions'}
|
xpn = {'re': 'http://exslt.org/regular-expressions'}
|
||||||
@ -234,7 +235,9 @@ class TOC(list):
|
|||||||
|
|
||||||
def read_html_toc(self, toc):
|
def read_html_toc(self, toc):
|
||||||
self.base_path = os.path.dirname(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
|
add = True
|
||||||
for i in self.flat():
|
for i in self.flat():
|
||||||
if i.href == href and i.fragment == fragment:
|
if i.href == href and i.fragment == fragment:
|
||||||
|
Loading…
x
Reference in New Issue
Block a user