Fixes for Missed 'open' statements

This commit is contained in:
Sam Manzi 2016-04-28 10:34:22 +10:00
parent 59793536ed
commit eb4b4d083f
No known key found for this signature in database
GPG Key ID: F4A16068AE36B402
8 changed files with 191 additions and 203 deletions

View File

@ -1130,14 +1130,13 @@ class PluginRegister(object):
lenpd = len(self.__plugindata) lenpd = len(self.__plugindata)
full_filename = os.path.join(dir, filename) full_filename = os.path.join(dir, filename)
try: try:
fd = open(full_filename, "r", encoding='utf-8') with open(full_filename, "r", encoding='utf-8') as fd:
stream = fd.read()
except Exception as msg: except Exception as msg:
print(_('ERROR: Failed reading plugin registration %(filename)s') % \ print(_('ERROR: Failed reading plugin registration %(filename)s') % \
{'filename' : filename}) {'filename' : filename})
print(msg) print(msg)
continue continue
stream = fd.read()
fd.close()
if os.path.exists(os.path.join(os.path.dirname(full_filename), if os.path.exists(os.path.join(os.path.dirname(full_filename),
'locale')): 'locale')):
try: try:

View File

@ -333,35 +333,33 @@ class ConfigManager(object):
if exp.errno != errno.EEXIST: if exp.errno != errno.EEXIST:
raise raise
try: try:
key_file = open(filename, "w", encoding="utf-8") with open(filename, "w", encoding="utf-8") as key_file:
key_file.write(";; Gramps key file\n")
key_file.write((";; Automatically created at %s" %
time.strftime("%Y/%m/%d %H:%M:%S")) + "\n\n")
sections = sorted(self.data)
for section in sections:
key_file.write(("[%s]\n") % section)
keys = sorted(self.data[section])
for key in keys:
value = self.data[section][key]
# If it has a default:
if self.has_default("%s.%s" % (section, key)):
if value == self.get_default("%s.%s" % (section, key)):
default = ";;"
else:
default = ""
if isinstance(value, int):
value = int(value)
key_file.write(("%s%s=%s\n")% (default,
key,
repr(value)))
key_file.write("\n")
# else, no filename given; nothing to save so do nothing quietly
except IOError as err: except IOError as err:
logging.warn("Failed to open %s because %s", logging.warn("Failed to open %s because %s",
filename, str(err)) filename, str(err))
return; return
key_file.write(";; Gramps key file\n")
key_file.write((";; Automatically created at %s" %
time.strftime("%Y/%m/%d %H:%M:%S")) + "\n\n")
sections = sorted(self.data)
for section in sections:
key_file.write(("[%s]\n") % section)
keys = sorted(self.data[section])
for key in keys:
value = self.data[section][key]
# If it has a default:
if self.has_default("%s.%s" % (section, key)):
if value == self.get_default("%s.%s" % (section, key)):
default = ";;"
else:
default = ""
if isinstance(value, int):
value = int(value)
key_file.write(("%s%s=%s\n")% (default,
key,
repr(value)))
key_file.write("\n")
key_file.close()
# else, no filename given; nothing to save so do nothing quietly
def get(self, key): def get(self, key):
""" """

View File

@ -218,50 +218,49 @@ class GrampletBar(Gtk.Notebook):
""" """
filename = self.configfile filename = self.configfile
try: try:
fp = open(filename, "w", encoding='utf-8') with open(filename, "w", encoding='utf-8') as fp:
fp.write(";; Gramplet bar configuration file" + NL)
fp.write((";; Automatically created at %s" %
time.strftime("%Y/%m/%d %H:%M:%S")) + NL + NL)
fp.write("[Bar Options]" + NL)
fp.write(("visible=%s" + NL) % self.get_property('visible'))
fp.write(("page=%d" + NL) % self.get_current_page())
fp.write(NL)
if self.empty:
gramplet_list = []
else:
gramplet_list = [self.get_nth_page(page_num)
for page_num in range(self.get_n_pages())]
for page_num, gramplet in enumerate(gramplet_list):
opts = get_gramplet_options_by_name(gramplet.gname)
if opts is not None:
base_opts = opts.copy()
for key in base_opts:
if key in gramplet.__dict__:
base_opts[key] = gramplet.__dict__[key]
fp.write(("[%s]" + NL) % gramplet.gname)
for key in base_opts:
if key in ["content", "title", "tname", "row", "column",
"page", "version", "gramps"]: # don't save
continue
elif key == "data":
if not isinstance(base_opts["data"], (list, tuple)):
fp.write(("data[0]=%s" + NL) % base_opts["data"])
else:
cnt = 0
for item in base_opts["data"]:
fp.write(("data[%d]=%s" + NL) % (cnt, item))
cnt += 1
else:
fp.write(("%s=%s" + NL)% (key, base_opts[key]))
fp.write(("page=%d" + NL) % page_num)
fp.write(NL)
except IOError: except IOError:
LOG.warning("Failed writing '%s'; gramplets not saved" % filename) LOG.warning("Failed writing '%s'; gramplets not saved" % filename)
return return
fp.write(";; Gramplet bar configuration file" + NL)
fp.write((";; Automatically created at %s" %
time.strftime("%Y/%m/%d %H:%M:%S")) + NL + NL)
fp.write("[Bar Options]" + NL)
fp.write(("visible=%s" + NL) % self.get_property('visible'))
fp.write(("page=%d" + NL) % self.get_current_page())
fp.write(NL)
if self.empty:
gramplet_list = []
else:
gramplet_list = [self.get_nth_page(page_num)
for page_num in range(self.get_n_pages())]
for page_num, gramplet in enumerate(gramplet_list):
opts = get_gramplet_options_by_name(gramplet.gname)
if opts is not None:
base_opts = opts.copy()
for key in base_opts:
if key in gramplet.__dict__:
base_opts[key] = gramplet.__dict__[key]
fp.write(("[%s]" + NL) % gramplet.gname)
for key in base_opts:
if key in ["content", "title", "tname", "row", "column",
"page", "version", "gramps"]: # don't save
continue
elif key == "data":
if not isinstance(base_opts["data"], (list, tuple)):
fp.write(("data[0]=%s" + NL) % base_opts["data"])
else:
cnt = 0
for item in base_opts["data"]:
fp.write(("data[%d]=%s" + NL) % (cnt, item))
cnt += 1
else:
fp.write(("%s=%s" + NL)% (key, base_opts[key]))
fp.write(("page=%d" + NL) % page_num)
fp.write(NL)
fp.close()
def set_active(self): def set_active(self):
""" """

View File

@ -1187,82 +1187,81 @@ class GrampletPane(Gtk.ScrolledWindow):
return # something is the matter return # something is the matter
filename = self.configfile filename = self.configfile
try: try:
fp = open(filename, "w", encoding='utf-8') with open(filename, "w", encoding='utf-8') as fp:
fp.write(";; Gramps gramplets file\n")
fp.write(";; Automatically created at %s" %
time.strftime("%Y/%m/%d %H:%M:%S\n\n"))
fp.write("[Gramplet View Options]\n")
fp.write("column_count=%d\n" % self.column_count)
fp.write("pane_position=%d\n" % self.pane_position)
fp.write("pane_orientation=%s\n\n" % self.pane_orientation)
# showing gramplets:
for col in range(self.column_count):
row = 0
for gframe in self.columns[col]:
gramplet = self.frame_map[str(gframe)]
opts = get_gramplet_options_by_name(gramplet.gname)
if opts is not None:
base_opts = opts.copy()
for key in base_opts:
if key in gramplet.__dict__:
base_opts[key] = gramplet.__dict__[key]
fp.write("[%s]\n" % gramplet.gname)
for key in base_opts:
if key == "content": continue
elif key == "title":
if gramplet.title_override:
fp.write("title=%s\n" % base_opts[key])
elif key == "tname": continue
elif key == "column": continue
elif key == "row": continue
elif key == "version": continue # code, don't save
elif key == "gramps": continue # code, don't save
elif key == "data":
if not isinstance(base_opts["data"], (list, tuple)):
fp.write("data[0]=%s\n" % base_opts["data"])
else:
cnt = 0
for item in base_opts["data"]:
fp.write("data[%d]=%s\n" % (cnt, item))
cnt += 1
else:
fp.write("%s=%s\n"% (key, base_opts[key]))
fp.write("column=%d\n" % col)
fp.write("row=%d\n\n" % row)
row += 1
for gramplet in self.detached_gramplets:
opts = get_gramplet_options_by_name(gramplet.gname)
if opts is not None:
base_opts = opts.copy()
for key in base_opts:
if key in gramplet.__dict__:
base_opts[key] = gramplet.__dict__[key]
fp.write("[%s]\n" % gramplet.title)
for key in base_opts:
if key == "content": continue
elif key == "title":
if "title_override" in base_opts:
base_opts["title"] = base_opts["title_override"]
fp.write("title=%s\n" % base_opts[key])
elif key == "tname": continue
elif key == "version": continue # code, don't save
elif key == "gramps": continue # code, don't save
elif key == "data":
if not isinstance(base_opts["data"], (list, tuple)):
fp.write("data[0]=%s\n" % base_opts["data"])
else:
cnt = 0
for item in base_opts["data"]:
fp.write("data[%d]=%s\n" % (cnt, item))
cnt += 1
else:
fp.write("%s=%s\n\n" % (key, base_opts[key]))
except IOError as err: except IOError as err:
LOG.warn("Failed to open %s because $s; gramplets not saved", LOG.warn("Failed to open %s because $s; gramplets not saved",
filename, str(err)) filename, str(err))
return return
fp.write(";; Gramps gramplets file\n")
fp.write(";; Automatically created at %s" %
time.strftime("%Y/%m/%d %H:%M:%S\n\n"))
fp.write("[Gramplet View Options]\n")
fp.write("column_count=%d\n" % self.column_count)
fp.write("pane_position=%d\n" % self.pane_position)
fp.write("pane_orientation=%s\n\n" % self.pane_orientation)
# showing gramplets:
for col in range(self.column_count):
row = 0
for gframe in self.columns[col]:
gramplet = self.frame_map[str(gframe)]
opts = get_gramplet_options_by_name(gramplet.gname)
if opts is not None:
base_opts = opts.copy()
for key in base_opts:
if key in gramplet.__dict__:
base_opts[key] = gramplet.__dict__[key]
fp.write("[%s]\n" % gramplet.gname)
for key in base_opts:
if key == "content": continue
elif key == "title":
if gramplet.title_override:
fp.write("title=%s\n" % base_opts[key])
elif key == "tname": continue
elif key == "column": continue
elif key == "row": continue
elif key == "version": continue # code, don't save
elif key == "gramps": continue # code, don't save
elif key == "data":
if not isinstance(base_opts["data"], (list, tuple)):
fp.write("data[0]=%s\n" % base_opts["data"])
else:
cnt = 0
for item in base_opts["data"]:
fp.write("data[%d]=%s\n" % (cnt, item))
cnt += 1
else:
fp.write("%s=%s\n"% (key, base_opts[key]))
fp.write("column=%d\n" % col)
fp.write("row=%d\n\n" % row)
row += 1
for gramplet in self.detached_gramplets:
opts = get_gramplet_options_by_name(gramplet.gname)
if opts is not None:
base_opts = opts.copy()
for key in base_opts:
if key in gramplet.__dict__:
base_opts[key] = gramplet.__dict__[key]
fp.write("[%s]\n" % gramplet.title)
for key in base_opts:
if key == "content": continue
elif key == "title":
if "title_override" in base_opts:
base_opts["title"] = base_opts["title_override"]
fp.write("title=%s\n" % base_opts[key])
elif key == "tname": continue
elif key == "version": continue # code, don't save
elif key == "gramps": continue # code, don't save
elif key == "data":
if not isinstance(base_opts["data"], (list, tuple)):
fp.write("data[0]=%s\n" % base_opts["data"])
else:
cnt = 0
for item in base_opts["data"]:
fp.write("data[%d]=%s\n" % (cnt, item))
cnt += 1
else:
fp.write("%s=%s\n\n" % (key, base_opts[key]))
fp.close()
def drop_widget(self, source, context, x, y, timedata): def drop_widget(self, source, context, x, y, timedata):
""" """

View File

@ -90,7 +90,20 @@ class GeneWebWriter(object):
self.dirname = os.path.dirname (self.filename) self.dirname = os.path.dirname (self.filename)
try: try:
self.g = open(self.filename, "wb") with open(self.filename, "wb") as self.g:
self.flist = [x for x in self.db.iter_family_handles()]
if len(self.flist) < 1:
self.user.notify_error(_("No families matched by selected filter"))
return False
self.count = 0
self.oldval = 0
self.total = len(self.flist)
for key in self.flist:
self.write_family(key)
self.writeln("")
return True
except IOError as msg: except IOError as msg:
msg2 = _("Could not create %s") % self.filename msg2 = _("Could not create %s") % self.filename
self.user.notify_error(msg2, str(msg)) self.user.notify_error(msg2, str(msg))
@ -99,21 +112,6 @@ class GeneWebWriter(object):
self.user.notify_error(_("Could not create %s") % self.filename) self.user.notify_error(_("Could not create %s") % self.filename)
return False return False
self.flist = [x for x in self.db.iter_family_handles()]
if len(self.flist) < 1:
self.user.notify_error(_("No families matched by selected filter"))
return False
self.count = 0
self.oldval = 0
self.total = len(self.flist)
for key in self.flist:
self.write_family(key)
self.writeln("")
self.g.close()
return True
def write_family(self, family_handle): def write_family(self, family_handle):
family = self.db.get_family_from_handle(family_handle) family = self.db.get_family_from_handle(family_handle)
if family: if family:

View File

@ -96,7 +96,25 @@ class CalendarWriter(object):
self.dirname = os.path.dirname (filename) self.dirname = os.path.dirname (filename)
try: try:
self.g = open(filename,"w") with open(filename,"w") as self.g:
self.writeln("BEGIN:VCALENDAR")
self.writeln("PRODID:-//GNU//Gramps//EN")
self.writeln("VERSION:1.0")
self.total = (len([x for x in self.db.iter_person_handles()]) +
len([x for x in self.db.iter_family_handles()]))
for key in self.db.iter_person_handles():
self.write_person(key)
self.update()
for key in self.db.iter_family_handles():
self.write_family(key)
self.update()
self.writeln("")
self.writeln("END:VCALENDAR")
return True
except IOError as msg: except IOError as msg:
msg2 = _("Could not create %s") % filename msg2 = _("Could not create %s") % filename
self.user.notify_error(msg2, str(msg)) self.user.notify_error(msg2, str(msg))
@ -105,26 +123,6 @@ class CalendarWriter(object):
self.user.notify_error(_("Could not create %s") % filename) self.user.notify_error(_("Could not create %s") % filename)
return False return False
self.writeln("BEGIN:VCALENDAR")
self.writeln("PRODID:-//GNU//Gramps//EN")
self.writeln("VERSION:1.0")
self.total = (len([x for x in self.db.iter_person_handles()]) +
len([x for x in self.db.iter_family_handles()]))
for key in self.db.iter_person_handles():
self.write_person(key)
self.update()
for key in self.db.iter_family_handles():
self.write_family(key)
self.update()
self.writeln("")
self.writeln("END:VCALENDAR")
self.g.close()
return True
def write_family(self, family_handle): def write_family(self, family_handle):
family = self.db.get_family_from_handle(family_handle) family = self.db.get_family_from_handle(family_handle)
if family: if family:

View File

@ -67,28 +67,27 @@ def importData(database, filename, user):
database.__class__.__bases__ database.__class__.__bases__
try: try:
ifile = open(filename, "rb") with open(filename, "rb") as ifile:
ansel = False
gramps = False
for index in range(50):
# Treat the file as though it is UTF-8 since this is the more modern
# option; and anyway it doesn't really matter as we are only trying to
# detect a CHAR or SOUR line which is only 7-bit ASCII anyway, and we
# ignore anything that can't be translated.
line = ifile.readline()
line = line.decode(encoding='utf-8', errors='replace')
line = line.split()
if len(line) == 0:
break
if len(line) > 2 and line[1][0:4] == 'CHAR' and line[2] == "ANSEL":
ansel = True
if len(line) > 2 and line[1][0:4] == 'SOUR' and line[2] == "GRAMPS":
gramps = True
except IOError: except IOError:
return return
ansel = False
gramps = False
for index in range(50):
# Treat the file as though it is UTF-8 since this is the more modern
# option; and anyway it doesn't really matter as we are only trying to
# detect a CHAR or SOUR line which is only 7-bit ASCII anyway, and we
# ignore anything that can't be translated.
line = ifile.readline()
line = line.decode(encoding='utf-8', errors='replace')
line = line.split()
if len(line) == 0:
break
if len(line) > 2 and line[1][0:4] == 'CHAR' and line[2] == "ANSEL":
ansel = True
if len(line) > 2 and line[1][0:4] == 'SOUR' and line[2] == "GRAMPS":
gramps = True
ifile.close()
if not gramps and ansel: if not gramps and ansel:
top = Glade() top = Glade()
code = top.get_object('codeset') code = top.get_object('codeset')

View File

@ -1129,14 +1129,12 @@ class GedcomInfoDB(object):
try: try:
filepath = os.path.join(DATA_DIR, "gedcom.xml") filepath = os.path.join(DATA_DIR, "gedcom.xml")
ged_file = open(filepath, "rb") with open(filepath, "rb") as ged_file:
parser = GedInfoParser(self)
parser.parse(ged_file)
except: except:
return return
parser = GedInfoParser(self)
parser.parse(ged_file)
ged_file.close()
def add_description(self, name, obj): def add_description(self, name, obj):
self.map[name] = obj self.map[name] = obj