From 1e81d9d11fc10c1b3c75cc8d7943eefe5a948a7f Mon Sep 17 00:00:00 2001 From: Sam Manzi Date: Fri, 13 May 2016 09:58:20 +1000 Subject: [PATCH] Convert a few more opens to use the with CM --- gramps/plugins/database/bsddb_support/write.py | 11 +++++------ gramps/plugins/importer/importxml.py | 10 ++++------ 2 files changed, 9 insertions(+), 12 deletions(-) diff --git a/gramps/plugins/database/bsddb_support/write.py b/gramps/plugins/database/bsddb_support/write.py index 89aa27bcd..90375984a 100644 --- a/gramps/plugins/database/bsddb_support/write.py +++ b/gramps/plugins/database/bsddb_support/write.py @@ -555,7 +555,7 @@ class DbBsddb(DbBsddbRead, DbWriteBase, UpdateCallback): def __make_zip_backup(self, dirname): import zipfile - # In Windows resrved characters is "<>:"/\|?*" + # In Windows reserved characters is "<>:"/\|?*" reserved_char = r':,<>"/\|?* ' replace_char = "-__________" title = self.get_dbname() @@ -569,11 +569,10 @@ class DbBsddb(DbBsddbRead, DbWriteBase, UpdateCallback): dotgramps_path = os.path.dirname(grampsdb_path) zipname = title + time.strftime("_%Y-%m-%d_%H-%M-%S") + ".zip" zippath = os.path.join(dotgramps_path, zipname) - myzip = zipfile.ZipFile(zippath, 'w') - for filename in os.listdir(dirname): - pathname = os.path.join(dirname, filename) - myzip.write(pathname, os.path.join(db_code, filename)) - myzip.close() + with zipfile.ZipFile(zippath, 'w') as myzip: + for filename in os.listdir(dirname): + pathname = os.path.join(dirname, filename) + myzip.write(pathname, os.path.join(db_code, filename)) _LOG.warning("If upgrade and loading the Family Tree works, you can " "delete the zip file at %s" % zippath) diff --git a/gramps/plugins/importer/importxml.py b/gramps/plugins/importer/importxml.py index 65856caae..01fa162d4 100644 --- a/gramps/plugins/importer/importxml.py +++ b/gramps/plugins/importer/importxml.py @@ -339,9 +339,8 @@ class LineParser: if GZIP_OK: use_gzip = 1 try: - f = gzip.open(filename, "r") - f.read(1) - f.close() + with gzip.open(filename, "r") as f: + f.read(1) except IOError as msg: use_gzip = 0 except ValueError as msg: @@ -419,9 +418,8 @@ class ImportOpenFileContextManager: if GZIP_OK: use_gzip = True try: - ofile = gzip.open(filename, "r") - ofile.read(1) - ofile.close() + with gzip.open(filename, "r") as ofile: + ofile.read(1) except IOError as msg: use_gzip = False except ValueError as msg: