Fixes for Missed 'open' statements

This commit is contained in:
Eno 2016-04-28 08:23:21 +10:00 committed by Sam Manzi
parent af9baca35e
commit 59793536ed
10 changed files with 26 additions and 38 deletions

View File

@ -265,9 +265,8 @@ class CLIManager(object):
# Attempt to figure out the database title
path = os.path.join(filename, "name.txt")
try:
ifile = open(path)
title = ifile.readline().strip()
ifile.close()
with open(path) as ifile:
title = ifile.readline().strip()
except:
title = filename

View File

@ -2026,9 +2026,8 @@ class DbGeneric(DbWriteBase, DbReadBase, UpdateCallback, Callback):
"""
filepath = os.path.join(self._directory, "name.txt")
try:
name_file = open(filepath, "r")
name = name_file.readline().strip()
name_file.close()
with open(filepath, "r") as name_file:
name = name_file.readline().strip()
except (OSError, IOError) as msg:
LOG.error(str(msg))
name = None
@ -2129,9 +2128,8 @@ class DbGeneric(DbWriteBase, DbReadBase, UpdateCallback, Callback):
if self._directory:
filepath = os.path.join(self._directory, "bdbversion.txt")
try:
name_file = open(filepath, "r", encoding='utf-8')
version = name_file.readline().strip()
name_file.close()
with open(filepath, "r", encoding='utf-8') as name_file:
version = name_file.readline().strip()
except (OSError, IOError) as msg:
self.__log_error()
version = "(0, 0, 0)"

View File

@ -527,9 +527,8 @@ class BookList(object):
try:
p = make_parser()
p.setContentHandler(BookParser(self, self.dbase))
the_file = open(self.file)
p.parse(the_file)
the_file.close()
with open(self.file) as the_file:
p.parse(the_file)
except (IOError, OSError, ValueError, SAXParseException, KeyError,
AttributeError):
pass

View File

@ -83,9 +83,8 @@ class PageSizeParser(handler.ContentHandler):
try:
parser = make_parser()
parser.setContentHandler(PageSizeParser(paper_sizes))
the_file = open(PAPERSIZE)
parser.parse(the_file)
the_file.close()
with open(PAPERSIZE) as the_file:
parser.parse(the_file)
paper_sizes.append(PaperSize("Custom Size", -1, -1)) # always in English
except (IOError, OSError, SAXParseException):
paper_sizes = [

View File

@ -96,9 +96,8 @@ class GrampsAboutDialog(Gtk.AboutDialog):
self.set_artists(artists.split('\n'))
try:
ifile = open(LICENSE_FILE, "r")
self.set_license(ifile.read().replace('\x0c', ''))
ifile.close()
with open(LICENSE_FILE, "r") as ifile:
self.set_license(ifile.read().replace('\x0c', ''))
except IOError:
self.set_license("License file is missing")
@ -214,9 +213,8 @@ def _get_authors():
parser = make_parser()
parser.setContentHandler(AuthorParser(authors, contributors))
authors_file = open(AUTHORS_FILE, encoding='utf-8')
parser.parse(authors_file)
authors_file.close()
with open(AUTHORS_FILE, encoding='utf-8') as authors_file:
parser.parse(authors_file)
authors_text = [authors, contributors]

View File

@ -663,9 +663,8 @@ class DbManager(CLIDbManager):
node = self.model.get_iter(path)
filename = self.model.get_value(node, FILE_COL)
try:
name_file = open(filename, "r")
file_name_to_delete=name_file.read()
name_file.close()
with open(filename, "r") as name_file:
file_name_to_delete=name_file.read()
remove_filename(file_name_to_delete)
directory = self.data_to_delete[1]
for (top, dirs, files) in os.walk(directory):

View File

@ -2040,9 +2040,8 @@ class DbBsddbRead(DbReadBase, Callback):
"""
filepath = os.path.join(self.path, "name.txt")
try:
name_file = open(filepath, "r", encoding='utf-8')
name = name_file.readline().strip()
name_file.close()
with open(filepath, "r", encoding='utf-8') as name_file:
name = name_file.readline().strip()
except (OSError, IOError) as msg:
self.__log_error()
name = None
@ -2051,9 +2050,8 @@ class DbBsddbRead(DbReadBase, Callback):
def get_version(self):
filepath = os.path.join(self.path, "bdbversion.txt")
try:
name_file = open(filepath, "r", encoding='utf-8')
version = name_file.readline().strip()
name_file.close()
with open(filepath, "r", encoding='utf-8') as name_file:
version = name_file.readline().strip()
except (OSError, IOError) as msg:
self.__log_error()
version = "(0, 0, 0)"

View File

@ -16,8 +16,8 @@ def get_dbdir_summary(dirpath, name):
bdbversion_file = os.path.join(dirpath, BDBVERSFN)
if os.path.isfile(bdbversion_file):
vers_file = open(bdbversion_file)
bsddb_version = vers_file.readline().strip()
with open(bdbversion_file) as vers_file:
bsddb_version = vers_file.readline().strip()
else:
return "Unknown", "Unknown", "Unknown"

View File

@ -1214,9 +1214,8 @@ class ODFDoc(BaseDoc, TextDoc, DrawDoc):
for image in self.media_list:
try:
ifile = open(image[0], mode='rb')
self._add_zip(zfile, "Pictures/%s" % image[1], ifile.read(), t)
ifile.close()
with open(image[0], mode='rb') as ifile:
self._add_zip(zfile, "Pictures/%s" % image[1], ifile.read(), t)
except:
errmsg = "%s\n%s" % (_("Could not open %s") % image[0],
msg)

View File

@ -572,9 +572,8 @@ class VerifyResults(ManagedWindow):
def _save_ignored(self,filename):
try:
f = open(filename,'wb')
pickle.dump(self.ignores, f, 1)
f.close()
with open(filename,'wb') as f:
pickle.dump(self.ignores, f, 1)
return True
except IOError:
return False