use contextmanagers for open()

This commit is contained in:
Eli Schwartz 2019-05-27 13:12:09 -04:00
parent 94db9b84cd
commit b5eda37c75
No known key found for this signature in database
GPG Key ID: CEB167EFB5722BD6
2 changed files with 6 additions and 3 deletions

View File

@ -52,7 +52,8 @@ class MOBIInput(InputFormatPlugin):
if raw:
if isinstance(raw, unicode_type):
raw = raw.encode('utf-8')
open(u'debug-raw.html', 'wb').write(raw)
with open(u'debug-raw.html', 'wb') as f:
f.write(raw)
from calibre.ebooks.oeb.base import close_self_closing_tags
for f, root in parse_cache.items():
raw = html.tostring(root, encoding='utf-8', method='xml',

View File

@ -65,7 +65,8 @@ class RecipeInput(InputFormatPlugin):
zf = ZipFile(recipe_or_file, 'r')
zf.extractall()
zf.close()
self.recipe_source = open(u'download.recipe', 'rb').read()
with open('download.recipe', 'rb') as f:
self.recipe_source = f.read()
recipe = compile_recipe(self.recipe_source)
recipe.needs_subscription = False
self.recipe_object = recipe(opts, log, self.report_progress)
@ -87,7 +88,8 @@ class RecipeInput(InputFormatPlugin):
self.recipe_source = self.recipe_source.encode('utf-8')
recipe = compile_recipe(self.recipe_source)
elif os.access(recipe_or_file, os.R_OK):
self.recipe_source = open(recipe_or_file, 'rb').read()
with open(recipe_or_file, 'rb') as f:
self.recipe_source = f.read()
recipe = compile_recipe(self.recipe_source)
log('Using custom recipe')
else: