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

View File

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

View File

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