Convert a few more opens to use the with CM

This commit is contained in:
Sam Manzi 2016-05-13 09:58:20 +10:00
parent 05dcde4c8e
commit 1e81d9d11f
No known key found for this signature in database
GPG Key ID: F4A16068AE36B402
2 changed files with 9 additions and 12 deletions

View File

@ -555,7 +555,7 @@ class DbBsddb(DbBsddbRead, DbWriteBase, UpdateCallback):
def __make_zip_backup(self, dirname): def __make_zip_backup(self, dirname):
import zipfile import zipfile
# In Windows resrved characters is "<>:"/\|?*" # In Windows reserved characters is "<>:"/\|?*"
reserved_char = r':,<>"/\|?* ' reserved_char = r':,<>"/\|?* '
replace_char = "-__________" replace_char = "-__________"
title = self.get_dbname() title = self.get_dbname()
@ -569,11 +569,10 @@ class DbBsddb(DbBsddbRead, DbWriteBase, UpdateCallback):
dotgramps_path = os.path.dirname(grampsdb_path) dotgramps_path = os.path.dirname(grampsdb_path)
zipname = title + time.strftime("_%Y-%m-%d_%H-%M-%S") + ".zip" zipname = title + time.strftime("_%Y-%m-%d_%H-%M-%S") + ".zip"
zippath = os.path.join(dotgramps_path, zipname) zippath = os.path.join(dotgramps_path, zipname)
myzip = zipfile.ZipFile(zippath, 'w') with zipfile.ZipFile(zippath, 'w') as myzip:
for filename in os.listdir(dirname): for filename in os.listdir(dirname):
pathname = os.path.join(dirname, filename) pathname = os.path.join(dirname, filename)
myzip.write(pathname, os.path.join(db_code, filename)) myzip.write(pathname, os.path.join(db_code, filename))
myzip.close()
_LOG.warning("If upgrade and loading the Family Tree works, you can " _LOG.warning("If upgrade and loading the Family Tree works, you can "
"delete the zip file at %s" % "delete the zip file at %s" %
zippath) zippath)

View File

@ -339,9 +339,8 @@ class LineParser:
if GZIP_OK: if GZIP_OK:
use_gzip = 1 use_gzip = 1
try: try:
f = gzip.open(filename, "r") with gzip.open(filename, "r") as f:
f.read(1) f.read(1)
f.close()
except IOError as msg: except IOError as msg:
use_gzip = 0 use_gzip = 0
except ValueError as msg: except ValueError as msg:
@ -419,9 +418,8 @@ class ImportOpenFileContextManager:
if GZIP_OK: if GZIP_OK:
use_gzip = True use_gzip = True
try: try:
ofile = gzip.open(filename, "r") with gzip.open(filename, "r") as ofile:
ofile.read(1) ofile.read(1)
ofile.close()
except IOError as msg: except IOError as msg:
use_gzip = False use_gzip = False
except ValueError as msg: except ValueError as msg: