Missed 'open' statements

From: Dale Athanasias <dalegrind@hotmail.com>
Mon, 25 Apr 2016 13:41:18 +1000
Subject: Missed 'open' statements

Hi Sam,
Here's a few files with missed 'open' statements:
gramps/gen/filters/_filterlist.py
gramps/plugins/export/exportftree.py
gramps/plugins/database/bsddb_support/write.py

And some older files which you probably left alone for a reason?
windows/nonAIO/builder/build_GrampsWin32.py
windows/nonAIO/check_gtk_install.py
windows/nonAIO/builder/make_launcher.py
windows/nonAIO/builder/check_gtk_install.py
windows/nonAIO/nsis/gcheck.py

Regards
-
Dale

Re: Prefer with to open files
https://github.com/gramps-project/gramps/pull/113
This commit is contained in:
Dale Athanasias 2016-04-26 14:29:40 +10:00 committed by Sam Manzi
parent 344f953c0b
commit a9685a64ff
3 changed files with 102 additions and 106 deletions

View File

@ -118,28 +118,27 @@ class FilterList(object):
return l.replace('"', '&quot;')
def save(self):
f = open(self.file, 'w', encoding='utf8')
f.write("<?xml version=\"1.0\" encoding=\"utf-8\"?>\n")
f.write('<filters>\n')
for namespace in self.filter_namespaces:
f.write('<object type="%s">\n' % namespace)
filter_list = self.filter_namespaces[namespace]
for the_filter in filter_list:
f.write(' <filter name="%s"' %self.fix(the_filter.get_name()))
f.write(' function="%s"' % the_filter.get_logical_op())
if the_filter.invert:
f.write(' invert="1"')
comment = the_filter.get_comment()
if comment:
f.write(' comment="%s"' % self.fix(comment))
f.write('>\n')
for rule in the_filter.get_rules():
f.write(' <rule class="%s" use_regex="%s">\n'
% (rule.__class__.__name__, rule.use_regex))
for value in list(rule.values()):
f.write(' <arg value="%s"/>\n' % self.fix(value))
f.write(' </rule>\n')
f.write(' </filter>\n')
f.write('</object>\n')
f.write('</filters>\n')
f.close()
with open(self.file, 'w', encoding='utf8') as f:
f.write("<?xml version=\"1.0\" encoding=\"utf-8\"?>\n")
f.write('<filters>\n')
for namespace in self.filter_namespaces:
f.write('<object type="%s">\n' % namespace)
filter_list = self.filter_namespaces[namespace]
for the_filter in filter_list:
f.write(' <filter name="%s"' %self.fix(the_filter.get_name()))
f.write(' function="%s"' % the_filter.get_logical_op())
if the_filter.invert:
f.write(' invert="1"')
comment = the_filter.get_comment()
if comment:
f.write(' comment="%s"' % self.fix(comment))
f.write('>\n')
for rule in the_filter.get_rules():
f.write(' <rule class="%s" use_regex="%s">\n'
% (rule.__class__.__name__, rule.use_regex))
for value in list(rule.values()):
f.write(' <arg value="%s"/>\n' % self.fix(value))
f.write(' </rule>\n')
f.write(' </filter>\n')
f.write('</object>\n')
f.write('</filters>\n')

View File

@ -458,8 +458,8 @@ class DbBsddb(DbBsddbRead, DbWriteBase, UpdateCallback):
def __log_error(self):
mypath = os.path.join(self.get_save_path(),DBRECOVFN)
ofile = open(mypath, "w")
ofile.close()
with open(mypath, "w") as ofile:
pass
try:
clear_lock_file(self.get_save_path())
except:
@ -2591,15 +2591,14 @@ def do_export(database):
try:
for (base, tbl) in build_tbl_map(database):
backup_name = mk_tmp_name(database, base)
backup_table = open(backup_name, 'wb')
with open(backup_name, 'wb') as backup_table:
cursor = tbl.cursor()
data = cursor.first()
while data:
pickle.dump(data, backup_table, 2)
data = cursor.next()
cursor.close()
backup_table.close()
cursor = tbl.cursor()
data = cursor.first()
while data:
pickle.dump(data, backup_table, 2)
data = cursor.next()
cursor.close()
except (IOError,OSError):
return
@ -2678,29 +2677,28 @@ def clear_lock_file(name):
def write_lock_file(name):
if not os.path.isdir(name):
os.mkdir(name)
f = open(os.path.join(name, DBLOCKFN), "w", encoding='utf8')
if win():
user = get_env_var('USERNAME')
host = get_env_var('USERDOMAIN')
if host is None:
host = ""
else:
host = os.uname()[1]
# An ugly workaround for os.getlogin() issue with Konsole
try:
user = os.getlogin()
except:
# not win, so don't need get_env_var.
# under cron getlogin() throws and there is no USER.
user = os.environ.get('USER', 'noUSER')
if host:
text = "%s@%s" % (user, host)
else:
text = user
# Save only the username and host, so the massage can be
# printed with correct locale in DbManager.py when a lock is found
f.write(text)
f.close()
with open(os.path.join(name, DBLOCKFN), "w", encoding='utf8') as f:
if win():
user = get_env_var('USERNAME')
host = get_env_var('USERDOMAIN')
if host is None:
host = ""
else:
host = os.uname()[1]
# An ugly workaround for os.getlogin() issue with Konsole
try:
user = os.getlogin()
except:
# not win, so don't need get_env_var.
# under cron getlogin() throws and there is no USER.
user = os.environ.get('USER', 'noUSER')
if host:
text = "%s@%s" % (user, host)
else:
text = user
# Save only the username and host, so the massage can be
# printed with correct locale in DbManager.py when a lock is found
f.write(text)
def upgrade_researcher(owner_data):
"""

View File

@ -121,61 +121,60 @@ class FtreeWriter(object):
id_map[key] = n
id_name[key] = get_name(pn, sn, count)
f = open(self.filename,"w")
with open(self.filename,"w") as f:
for key in self.plist:
self.update()
p = self.db.get_person_from_handle(key)
name = id_name[key]
father = mother = email = web = ""
for key in self.plist:
self.update()
p = self.db.get_person_from_handle(key)
name = id_name[key]
father = mother = email = web = ""
family_handle = p.get_main_parents_family_handle()
if family_handle:
family = self.db.get_family_from_handle(family_handle)
if family.get_father_handle() and \
family.get_father_handle() in id_map:
father = id_map[family.get_father_handle()]
if family.get_mother_handle() and \
family.get_mother_handle() in id_map:
mother = id_map[family.get_mother_handle()]
family_handle = p.get_main_parents_family_handle()
if family_handle:
family = self.db.get_family_from_handle(family_handle)
if family.get_father_handle() and \
family.get_father_handle() in id_map:
father = id_map[family.get_father_handle()]
if family.get_mother_handle() and \
family.get_mother_handle() in id_map:
mother = id_map[family.get_mother_handle()]
#
# Calculate Date
#
birth_ref = p.get_birth_ref()
death_ref = p.get_death_ref()
if birth_ref:
birth_event = self.db.get_event_from_handle(birth_ref.ref)
birth = birth_event.get_date_object()
else:
birth = None
if death_ref:
death_event = self.db.get_event_from_handle(death_ref.ref)
death = death_event.get_date_object()
else:
death = None
#if self.restrict:
# alive = probably_alive(p, self.db)
#else:
# alive = 0
if birth:
if death:
dates = "%s-%s" % (fdate(birth), fdate(death))
#
# Calculate Date
#
birth_ref = p.get_birth_ref()
death_ref = p.get_death_ref()
if birth_ref:
birth_event = self.db.get_event_from_handle(birth_ref.ref)
birth = birth_event.get_date_object()
else:
dates = fdate(birth)
else:
if death:
dates = fdate(death)
birth = None
if death_ref:
death_event = self.db.get_event_from_handle(death_ref.ref)
death = death_event.get_date_object()
else:
dates = ""
death = None
f.write('%s;%s;%s;%s;%s;%s\n' % (name, father, mother, email, web,
dates))
#if self.restrict:
# alive = probably_alive(p, self.db)
#else:
# alive = 0
f.close()
return True
if birth:
if death:
dates = "%s-%s" % (fdate(birth), fdate(death))
else:
dates = fdate(birth)
else:
if death:
dates = fdate(death)
else:
dates = ""
f.write('%s;%s;%s;%s;%s;%s\n' % (name, father, mother, email, web,
dates))
return True
def fdate(val):
if val.get_year_valid():