where possible, open files using context managers

This commit is contained in:
Eli Schwartz 2019-09-09 17:43:28 -04:00
parent cb29d11996
commit 9320e2fe22
No known key found for this signature in database
GPG Key ID: CEB167EFB5722BD6
2 changed files with 5 additions and 5 deletions

View File

@ -2961,7 +2961,8 @@ class CatalogBuilder(object):
left = max(int((MI_WIDTH - width) / 2.), 0) left = max(int((MI_WIDTH - width) / 2.), 0)
top = max(int((MI_HEIGHT - height) / 2.), 0) top = max(int((MI_HEIGHT - height) / 2.), 0)
draw.text((left, top), text, fill=(0, 0, 0), font=font) draw.text((left, top), text, fill=(0, 0, 0), font=font)
img.save(open(out_path, 'wb'), 'GIF') with open(out_path, 'wb') as f:
img.save(f, 'GIF')
def generate_ncx_header(self): def generate_ncx_header(self):
""" Generate the basic NCX file. """ Generate the basic NCX file.

View File

@ -3517,10 +3517,9 @@ class LibraryDatabase2(LibraryDatabase, SchemaUpgrade, CustomColumns):
self.set_metadata(id, mi, commit=True, ignore_errors=True) self.set_metadata(id, mi, commit=True, ignore_errors=True)
npath = self.run_import_plugins(path, format) npath = self.run_import_plugins(path, format)
format = os.path.splitext(npath)[-1].lower().replace('.', '').upper() format = os.path.splitext(npath)[-1].lower().replace('.', '').upper()
stream = lopen(npath, 'rb') with lopen(npath, 'rb') as stream:
format = check_ebook_format(stream, format) format = check_ebook_format(stream, format)
self.add_format(id, format, stream, index_is_id=True) self.add_format(id, format, stream, index_is_id=True)
stream.close()
postimport.append((id, format)) postimport.append((id, format))
self.conn.commit() self.conn.commit()
self.data.refresh_ids(self, ids) # Needed to update format list and size self.data.refresh_ids(self, ids) # Needed to update format list and size