diff --git a/gramps/cli/clidbman.py b/gramps/cli/clidbman.py index f32641b3b..3b0a778f5 100644 --- a/gramps/cli/clidbman.py +++ b/gramps/cli/clidbman.py @@ -234,9 +234,8 @@ class CLIDbManager(object): except: version = (0, 0, 0) if os.path.isfile(path_name): - file = open(path_name, 'r', encoding='utf8') - name = file.readline().strip() - file.close() + with open(path_name, 'r', encoding='utf8') as file: + name = file.readline().strip() (tval, last) = time_val(dirpath) (enable, stock_id) = self.icon_values(dirpath, self.active, @@ -293,9 +292,8 @@ class CLIDbManager(object): name_list = [ name[0] for name in self.current_names ] title = find_next_db_name(name_list) - name_file = open(path_name, "w", encoding='utf8') - name_file.write(title) - name_file.close() + with open(path_name, "w", encoding='utf8') as name_file: + name_file.write(title) if create_db: # write the version number into metadata @@ -409,9 +407,8 @@ class CLIDbManager(object): dirpath = os.path.join(dbdir, dpath) path_name = os.path.join(dirpath, NAME_FILE) if os.path.isfile(path_name): - file = open(path_name, 'r', encoding='utf8') - name = file.readline().strip() - file.close() + with open(path_name, 'r', encoding='utf8') as file: + name = file.readline().strip() if re.match("^" + dbname + "$", name): match_list.append((name, dirpath)) if len(match_list) == 0: @@ -438,12 +435,10 @@ class CLIDbManager(object): Returns old_name, new_name if success, None, None if no success """ try: - name_file = open(filepath, "r", encoding='utf8') - old_text=name_file.read() - name_file.close() - name_file = open(filepath, "w", encoding='utf8') - name_file.write(new_text) - name_file.close() + with open(filepath, "r", encoding='utf8') as name_file: + old_text=name_file.read() + with open(filepath, "w", encoding='utf8') as name_file: + name_file.write(new_text) except (OSError, IOError) as msg: CLIDbManager.ERROR(_("Could not rename Family Tree"), str(msg)) @@ -543,11 +538,10 @@ def find_locker_name(dirpath): """ try: fname = os.path.join(dirpath, "lock") - ifile = open(fname, 'r', encoding='utf8') - username = ifile.read().strip() - # feature request 2356: avoid genitive form - last = _("Locked by %s") % username - ifile.close() + with open(fname, 'r', encoding='utf8') as ifile: + username = ifile.read().strip() + # feature request 2356: avoid genitive form + last = _("Locked by %s") % username except (OSError, IOError, UnicodeDecodeError): last = _("Unknown") return last diff --git a/gramps/gen/dbstate.py b/gramps/gen/dbstate.py index 1d7097d2d..0856e899c 100644 --- a/gramps/gen/dbstate.py +++ b/gramps/gen/dbstate.py @@ -203,25 +203,22 @@ class DbState(Callback): dirpath = os.path.join(dbdir, dpath) path_name = os.path.join(dirpath, "name.txt") if os.path.isfile(path_name): - file = open(path_name, 'r', encoding='utf8') - name = file.readline().strip() - file.close() + with open(path_name, 'r', encoding='utf8') as file: + name = file.readline().strip() if dbname == name: locked = False locked_by = None backend = None fname = os.path.join(dirpath, "database.txt") if os.path.isfile(fname): - ifile = open(fname, 'r', encoding='utf8') - backend = ifile.read().strip() - ifile.close() + with open(fname, 'r', encoding='utf8') as ifile: + backend = ifile.read().strip() else: backend = "bsddb" try: fname = os.path.join(dirpath, "lock") - ifile = open(fname, 'r', encoding='utf8') - locked_by = ifile.read().strip() - locked = True + with open(fname, 'r', encoding='utf8') as ifile: + locked_by = ifile.read().strip() ifile.close() except (OSError, IOError): pass diff --git a/gramps/gen/filters/_filterlist.py b/gramps/gen/filters/_filterlist.py index 958016bb0..9e7f8b259 100644 --- a/gramps/gen/filters/_filterlist.py +++ b/gramps/gen/filters/_filterlist.py @@ -103,9 +103,8 @@ class FilterList(object): if os.path.isfile(self.file): parser = make_parser() parser.setContentHandler(FilterParser(self)) - the_file = open(self.file, 'r', encoding='utf8') - parser.parse(the_file) - the_file.close() + with open(self.file, 'r', encoding='utf8') as the_file: + parser.parse(the_file) except (IOError, OSError): print("IO/OSError in _filterlist.py") except SAXParseException: diff --git a/gramps/gen/plug/docgen/graphdoc.py b/gramps/gen/plug/docgen/graphdoc.py index 38baaedc9..5e2ee7275 100644 --- a/gramps/gen/plug/docgen/graphdoc.py +++ b/gramps/gen/plug/docgen/graphdoc.py @@ -603,9 +603,8 @@ class GVDotDoc(GVDocBase): if self._filename[-3:] != ".gv": self._filename += ".gv" - dotfile = open(self._filename, "wb") - dotfile.write(self._dot.getvalue()) - dotfile.close() + with open(self._filename, "wb") as dotfile: + dotfile.write(self._dot.getvalue()) #------------------------------------------------------------------------------- # diff --git a/gramps/gen/plug/docgen/stylesheet.py b/gramps/gen/plug/docgen/stylesheet.py index 90de0d6d2..b072da18b 100644 --- a/gramps/gen/plug/docgen/stylesheet.py +++ b/gramps/gen/plug/docgen/stylesheet.py @@ -146,31 +146,30 @@ class StyleSheetList(object): """ Saves the current StyleSheet definitions to the associated file. """ - xml_file = open(self.__file, "w") - xml_file.write('\n') - xml_file.write('\n') + with open(self.__file, "w") as xml_file: + xml_file.write('\n') + xml_file.write('\n') - for name in sorted(self.map.keys()): # enable diff of archived copies - if name == "default": - continue - sheet = self.map[name] - xml_file.write('\n' % escxml(name)) + for name in sorted(self.map.keys()): # enable diff of archived copies + if name == "default": + continue + sheet = self.map[name] + xml_file.write('\n' % escxml(name)) - for p_name in sheet.get_paragraph_style_names(): - self.write_paragraph_style(xml_file, sheet, p_name) + for p_name in sheet.get_paragraph_style_names(): + self.write_paragraph_style(xml_file, sheet, p_name) - for t_name in sheet.get_table_style_names(): - self.write_table_style(xml_file, sheet, t_name) + for t_name in sheet.get_table_style_names(): + self.write_table_style(xml_file, sheet, t_name) - for c_name in sheet.get_cell_style_names(): - self.write_cell_style(xml_file, sheet, c_name) + for c_name in sheet.get_cell_style_names(): + self.write_cell_style(xml_file, sheet, c_name) - for g_name in sheet.get_draw_style_names(): - self.write_graphics_style(xml_file, sheet, g_name) + for g_name in sheet.get_draw_style_names(): + self.write_graphics_style(xml_file, sheet, g_name) - xml_file.write('\n') - xml_file.write('\n') - xml_file.close() + xml_file.write('\n') + xml_file.write('\n') def write_paragraph_style(self, xml_file, sheet, p_name): @@ -275,9 +274,8 @@ class StyleSheetList(object): if os.path.isfile(self.__file): parser = make_parser() parser.setContentHandler(SheetParser(self)) - the_file = open(self.__file) - parser.parse(the_file) - the_file.close() + with open(self.__file) as the_file: + parser.parse(the_file) except (IOError, OSError, SAXParseException): pass diff --git a/gramps/gen/plug/report/_book.py b/gramps/gen/plug/report/_book.py index b4073d65b..d01548b16 100644 --- a/gramps/gen/plug/report/_book.py +++ b/gramps/gen/plug/report/_book.py @@ -458,68 +458,67 @@ class BookList(object): """ Saves the current BookList to the associated file. """ - f = open(self.file, "w") - f.write("\n") - f.write('\n') - for name in sorted(self.bookmap): # enable a diff of archived copies - book = self.get_book(name) - dbname = book.get_dbname() - f.write(' \n' % (name, dbname) ) - for item in book.get_item_list(): - f.write(' \n' % - (item.get_name(), item.get_translated_name() ) ) - options = item.option_class.handler.options_dict - for option_name in sorted(options.keys()): # enable a diff - option_value = options[option_name] - if isinstance(option_value, (list, tuple)): - f.write(' \n') - else: - option_type = type_name(option_value) - value = escape(str(option_value)) - value = value.replace('"', '"') - f.write('