diff --git a/src/DisplayModels/_PeopleModel.py b/src/DisplayModels/_PeopleModel.py index a405312ad..1769a0d0f 100644 --- a/src/DisplayModels/_PeopleModel.py +++ b/src/DisplayModels/_PeopleModel.py @@ -29,6 +29,7 @@ TreeModel for the GRAMPS Person tree. # Standard python modules # #------------------------------------------------------------------------- +from __future__ import with_statement from gettext import gettext as _ import time import cgi @@ -308,61 +309,48 @@ class PeopleModel(gtk.GenericTreeModel): self.current_filter = data_filter def _build_search_sub(self,dfilter, skip): - ngn = name_displayer.name_grouping_data nsn = name_displayer.raw_sorted_name self.mapper.clear_sort_names() - cursor = self.db.get_person_cursor() - node = cursor.first() - self.total = 0 self.displayed = 0 - - while node: - self.total += 1 - handle, d = node - if not (handle in skip or (dfilter and not dfilter.match(handle,self.db))): - name_data = d[PeopleModel._NAME_COL] - - group_name = ngn(self.db, name_data) - sorted_name = nsn(name_data) - self.displayed += 1 - self.mapper.assign_sort_name(handle, sorted_name, group_name) - node = cursor.next() - cursor.close() + with self.db.get_person_cursor() as cursor: + for handle, d in cursor: + self.total += 1 + if not (handle in skip or (dfilter and not dfilter.match(handle,self.db))): + name_data = d[PeopleModel._NAME_COL] + group_name = ngn(self.db, name_data) + sorted_name = nsn(name_data) + self.displayed += 1 + self.mapper.assign_sort_name(handle, sorted_name, group_name) def _build_filter_sub(self,dfilter, skip): - ngn = name_displayer.name_grouping_data nsn = name_displayer.raw_sorted_name - + handle_list = self.db.iter_person_handles() + if dfilter: - handle_list = dfilter.apply(self.db, self.db.get_person_handles()) + handle_list = dfilter.apply(self.db, handle_list) + self.displayed = len(handle_list) else: - handle_list = self.db.get_person_handles() - - self.displayed = len(handle_list) + self.displayed = self.db.get_number_of_people() self.mapper.clear_sort_names() - status = LongOpStatus(msg="Loading People", - total_steps=len(handle_list), - interval=len(handle_list)/10) + total_steps=self.displayed, + interval=self.displayed//10) self.db.emit('long-op-start', (status,)) - for handle in handle_list: status.heartbeat() d = self.db.get_raw_person_data(handle) if not handle in skip: name_data = d[PeopleModel._NAME_COL] - group_name = ngn(self.db, name_data) sorted_name = nsn(name_data) self.mapper.assign_sort_name(handle, sorted_name, group_name) - status.end() def calculate_data(self, dfilter=None, skip=[]): @@ -387,7 +375,6 @@ class PeopleModel(gtk.GenericTreeModel): return self._build_data(dfilter, skip) - self.mapper.build_toplevel() self.in_build = False diff --git a/src/Filters/_GenericFilter.py b/src/Filters/_GenericFilter.py index f993e3feb..8bcaff6c9 100644 --- a/src/Filters/_GenericFilter.py +++ b/src/Filters/_GenericFilter.py @@ -23,7 +23,7 @@ """ Package providing filtering framework for GRAMPS. """ - +from __future__ import with_statement import gen.lib #------------------------------------------------------------------------- # @@ -116,17 +116,14 @@ class GenericFilter(object): final_list = [] if id_list is None: - cursor = self.get_cursor(db) - data = cursor.first() - while data: - person = self.make_obj() - person.unserialize(data[1]) - if progress: - progress.step() - if task(db, person) != self.invert: - final_list.append(data[0]) - data = cursor.next() - cursor.close() + with self.get_cursor(db) as cursor: + for handle, data in cursor: + person = self.make_obj() + person.unserialize(data) + if progress: + progress.step() + if task(db, person) != self.invert: + final_list.append(handle) else: for handle in id_list: person = self.find_from_handle(db, handle) @@ -139,33 +136,23 @@ class GenericFilter(object): def check_and(self, db, id_list, progress=None): final_list = [] flist = self.flist + if id_list is None: - cursor = self.get_cursor(db) - data = cursor.first() - while data: - person = self.make_obj() - person.unserialize(data[1]) - if progress: - progress.step() - val = True - for rule in flist: - if not rule.apply(db, person): - val = False - break - if val != self.invert: - final_list.append(data[0]) - data = cursor.next() - cursor.close() + with self.get_cursor(db) as cursor: + for handle, data in cursor: + person = self.make_obj() + person.unserialize(data) + if progress: + progress.step() + val = all(rule.apply(db, person) for rule in flist) + if val != self.invert: + final_list.append(handle) else: for handle in id_list: person = self.find_from_handle(db, handle) if progress: progress.step() - val = True - for rule in flist: - if not rule.apply(db, person): - val = False - break + val = all(rule.apply(db, person) for rule in flist) if val != self.invert: final_list.append(handle) return final_list diff --git a/src/Simple/_SimpleAccess.py b/src/Simple/_SimpleAccess.py index 391c474d3..799205f7e 100644 --- a/src/Simple/_SimpleAccess.py +++ b/src/Simple/_SimpleAccess.py @@ -21,7 +21,7 @@ """ Provide a simplified database access interface to the GRAMPS database. """ - +from __future__ import with_statement from types import NoneType import gen.lib @@ -686,16 +686,10 @@ class SimpleAccess(object): @return: list of objects of a particular type in the database @rtype: list """ - slist = [] - cursor = gen_cursor() - data = cursor.first() - while data: - slist.append(data[0]) - data = cursor.next() - cursor.close() - for info in slist: - obj = get_object(info) - yield obj + + with gen_cursor() as cursor: + for key, data in cursor: + yield get_object(key) def all_people(self): """ @@ -708,14 +702,10 @@ class SimpleAccess(object): @return: list of people in the database @rtype: list """ - slist = [] - cursor = self.dbase.get_person_cursor() - data = cursor.first() - while data: - slist.append((data[1][3][3], data[0])) - data = cursor.next() - cursor.close() - slist.sort() + + with self.dbase.get_person_cursor() as cursor: + slist = sorted((data[3][3], key) for key, data in cursor) + for info in slist: obj = self.dbase.get_person_from_handle(info[1]) yield obj diff --git a/src/gen/db/base.py b/src/gen/db/base.py index 6f2516114..4ce2083a5 100644 --- a/src/gen/db/base.py +++ b/src/gen/db/base.py @@ -1293,13 +1293,18 @@ class GrampsDbBase(Callback): Needs to be overridden in the derived class. """ raise NotImplementedError + + @staticmethod + def get_number_of_records(table): + return table.stat(db.DB_FAST_STAT)['nkeys'] def get_number_of_people(self): """ Return the number of people currently in the database. """ if self.db_is_open: - return len(self.person_map) + return self.get_number_of_records(self.person_map) + #return len(self.person_map) else: return 0 @@ -1307,25 +1312,25 @@ class GrampsDbBase(Callback): """ Return the number of families currently in the database. """ - return len(self.family_map) + return self.get_number_of_records(self.family_map) def get_number_of_events(self): """ Return the number of events currently in the database. """ - return len(self.event_map) + return self.get_number_of_records(self.event_map) def get_number_of_places(self): """ Return the number of places currently in the database. """ - return len(self.place_map) + return self.get_number_of_records(self.place_map) def get_number_of_sources(self): """ Return the number of sources currently in the database. """ - return len(self.source_map) + return self.get_number_of_records(self.source_map) def get_number_of_media_objects(self): """ @@ -1337,13 +1342,13 @@ class GrampsDbBase(Callback): """ Return the number of source repositories currently in the database. """ - return len(self.repository_map) + return self.get_number_of_records(self.repository_map) def get_number_of_notes(self): """ Return the number of notes currently in the database. """ - return len(self.note_map) + return self.get_number_of_records(self.note_map) def all_handles(self, table): return table.keys() @@ -1358,7 +1363,7 @@ class GrampsDbBase(Callback): if self.db_is_open: if sort_handles: with self.get_person_cursor() as cursor: - slist = sorted((data[1][3][3], data[0]) for data in cursor) + slist = sorted((data[3][3], key) for key, data in cursor) return [x[1] for x in slist] else: return self.all_handles(self.person_map) @@ -1379,11 +1384,10 @@ class GrampsDbBase(Callback): If sort_handles is True, the list is sorted by Place title. """ - print "base.py: get_place_handles" if self.db_is_open: if sort_handles: with self.get_place_cursor() as cursor: - slist = sorted(((data[1][2], data[0])) for data in cursor) + slist = sorted((data[2], key) for key, data in cursor) return [x[1] for x in slist] else: return self.all_handles(self.place_map) @@ -1404,7 +1408,6 @@ class GrampsDbBase(Callback): If sort_handles is True, the list is sorted by Source title. """ - print "base.py: get_source_handles" if self.db_is_open: handle_list = self.all_handles(self.source_map) if sort_handles: @@ -1427,7 +1430,6 @@ class GrampsDbBase(Callback): If sort_handles is True, the list is sorted by title. """ - print "base.py: get_media_object_handles" if self.db_is_open: handle_list = self.all_handles(self.media_map) if sort_handles: @@ -2565,29 +2567,21 @@ class GrampsDbBase(Callback): # Now we use the functions and classes defined above to loop through # each of the existing primary object tables - for primary_table_name in the_tables: - cursor = primary_tables[primary_table_name]['cursor_func']() - data = cursor.first() + for primary_table_name, funcs in the_tables.iteritems(): + with funcs['cursor_func']() as cursor: # Grab the real object class here so that the lookup does # not happen inside the main loop. - class_func = primary_tables[primary_table_name]['class_func'] - - while data: - found_handle, val = data - obj = class_func() - obj.unserialize(val) - - # Now we need to loop over all object types - # that have been requests in the include_classes list - for classname in primary_tables.keys(): - if obj.has_handle_reference(classname, handle): - yield (primary_table_name, found_handle) - - data = cursor.next() - - cursor.close() + class_func = funcs['class_func'] + for found_handle, val in cursor: + obj = class_func() + obj.unserialize(val) + # Now we need to loop over all object types + # that have been requests in the include_classes list + for classname in primary_tables: + if obj.has_handle_reference(classname, handle): + yield (primary_table_name, found_handle) return def report_bm_change(self): diff --git a/src/gen/db/dbdir.py b/src/gen/db/dbdir.py index 3cb167afa..25fa10613 100644 --- a/src/gen/db/dbdir.py +++ b/src/gen/db/dbdir.py @@ -30,6 +30,7 @@ This is used since GRAMPS version 3.0 # Standard python modules # #------------------------------------------------------------------------- +from __future__ import with_statement import cPickle as pickle import os import sys @@ -1052,21 +1053,16 @@ class GrampsDBDir(GrampsDbBase, UpdateCallback): # to loop through each of the primary object tables. for cursor_func, class_func in primary_table: - cursor = cursor_func() - data = cursor.first() - while data: - found_handle, val = data - obj = class_func() - obj.unserialize(val) + with cursor_func() as cursor: + for found_handle, val in cursor: + obj = class_func() + obj.unserialize(val) - the_txn = self.env.txn_begin() - self.update_reference_map(obj, transaction, the_txn) - if the_txn: - the_txn.commit() - - data = cursor.next() + the_txn = self.env.txn_begin() + self.update_reference_map(obj, transaction, the_txn) + if the_txn: + the_txn.commit() - cursor.close() callback(5) self.transaction_commit(transaction, _("Rebuild reference map")) @@ -2072,12 +2068,9 @@ if __name__ == "__main__": d = GrampsDBDir() d.load(sys.argv[1], lambda x: x) - c = d.get_person_cursor() - data = c.first() - while data: - person = Person(data[1]) - print data[0], person.get_primary_name().get_name(), - data = c.next() - c.close() + with d.get_person_cursor() as c: + for key, data in c: + person = Person(data) + print key, person.get_primary_name().get_name(), print d.surnames.keys() diff --git a/src/gen/utils/callback.py b/src/gen/utils/callback.py index c01fe1bc7..3164f6303 100644 --- a/src/gen/utils/callback.py +++ b/src/gen/utils/callback.py @@ -399,7 +399,7 @@ class Callback(object): try: if isinstance(fn, types.FunctionType) or \ isinstance(fn, types.MethodType): # call func - fn(*args) + fn(*args) else: self._warn("Badly formed entry in callback map.\n") except: diff --git a/src/gen/utils/dbutils.py b/src/gen/utils/dbutils.py index ee5e290c6..c030bcd8b 100644 --- a/src/gen/utils/dbutils.py +++ b/src/gen/utils/dbutils.py @@ -20,6 +20,7 @@ # $Id$ +from __future__ import with_statement from gettext import gettext as _ import copy @@ -222,7 +223,6 @@ def db_copy(from_db,to_db,callback): uc = UpdateCallback(callback) uc.set_total(get_total(from_db)) - tables = { 'Person': {'cursor_func': from_db.get_person_cursor, 'add_func' : to_db.add_person, @@ -253,19 +253,15 @@ def db_copy(from_db,to_db,callback): # Start batch transaction to use async TXN and other tricks trans = to_db.transaction_begin("", batch=True) for table_name, table_dict in tables.iteritems(): - cursor_func = table_dict['cursor_func'] - add_func = table_dict['add_func'] - - cursor = cursor_func() - item = cursor.first() - while item: - (handle,data) = item - exec('obj = gen.lib.%s()' % table_name) - obj.unserialize(data) - add_func(obj,trans) - item = cursor.next() - uc.update() - cursor.close() + with table_dict['cursor_func']() as cursor: + add_func = table_dict['add_func'] + for handle, data in cursor: + exec('obj = gen.lib.%s()' % table_name) + obj_ = getattr(gen.lib, table_name)() + assert obj_ == obj + obj.unserialize(data) + add_func(obj,trans) + uc.update() # Copy name grouping group_map = from_db.get_name_group_keys() diff --git a/src/plugins/import/ImportGrdb.py b/src/plugins/import/ImportGrdb.py index 32920b753..a055db2a7 100644 --- a/src/plugins/import/ImportGrdb.py +++ b/src/plugins/import/ImportGrdb.py @@ -29,6 +29,7 @@ # Standard Python Modules # #------------------------------------------------------------------------- +from __future__ import with_statement import os import shutil import tempfile @@ -1008,32 +1009,26 @@ class GrampsBSDDB(GrampsDbBase, UpdateCallback): # Now we use the functions and classes defined above # to loop through each of the primary object tables. - for primary_table_name in primary_tables.keys(): - - cursor = primary_tables[primary_table_name]['cursor_func']() - data = cursor.first() + for primary_table_name, funcs in primary_tables.iteritems(): + with funcs['cursor_func']() as cursor: # Grab the real object class here so that the lookup does # not happen inside the cursor loop. - class_func = primary_tables[primary_table_name]['class_func'] - while data: - found_handle, val = data - obj = class_func() - obj.unserialize(val) + class_func = funcs['class_func'] + for found_handle, val in cursor: + obj = class_func() + obj.unserialize(val) - if self.UseTXN: - the_txn = self.env.txn_begin() - else: - the_txn = None - self.update_reference_map(obj, transaction, the_txn) - if not self.UseTXN: - self.reference_map.sync() - if the_txn: - the_txn.commit() - - data = cursor.next() + if self.UseTXN: + the_txn = self.env.txn_begin() + else: + the_txn = None + self.update_reference_map(obj, transaction, the_txn) + if not self.UseTXN: + self.reference_map.sync() + if the_txn: + the_txn.commit() - cursor.close() if callback: callback(5) self.transaction_commit(transaction, _("Rebuild reference map")) diff --git a/src/plugins/tool/Check.py b/src/plugins/tool/Check.py index 36e307907..230f26f18 100644 --- a/src/plugins/tool/Check.py +++ b/src/plugins/tool/Check.py @@ -28,6 +28,7 @@ # python modules # #------------------------------------------------------------------------- +from __future__ import with_statement import os import sys import cStringIO @@ -266,7 +267,7 @@ class CheckIntegrity(object): in self.db.name_formats if not act] # remove the invalid references from all Name objects - for person_handle in self.db.iter_person_handles(): + for person_handle in self.db.get_person_handles(): person = self.db.get_person_from_handle(person_handle) p_changed = False @@ -310,7 +311,7 @@ class CheckIntegrity(object): self.progress.set_pass(_('Looking for duplicate spouses'), self.db.get_number_of_people()) - for handle in self.db.iter_person_handles(): + for handle in self.db.person_map.keys(): value = self.db.person_map[handle] p = gen.lib.Person(value) splist = p.get_family_handle_list() @@ -327,8 +328,7 @@ class CheckIntegrity(object): def fix_encoding(self): self.progress.set_pass(_('Looking for character encoding errors'), self.db.get_number_of_media_objects()) - - for handle in self.db.iter_media_handles(): + for handle in self.db.media_map.keys(): data = self.db.media_map[handle] if not isinstance(data[2], unicode) or not isinstance(data[4], unicode): obj = self.db.get_object_from_handle(handle) @@ -351,10 +351,9 @@ class CheckIntegrity(object): def check_for_broken_family_links(self): # Check persons referenced by the family objects - fhandle_list = self.db.iter_family_handles() + fhandle_list = self.db.get_family_handles() self.progress.set_pass(_('Looking for broken family links'), - self.db.get_number_of_families()+ - self.db.get_number_of_people()) + len(fhandle_list) + self.db.get_number_of_people()) for family_handle in fhandle_list: family = self.db.get_family_from_handle(family_handle) @@ -434,7 +433,7 @@ class CheckIntegrity(object): self.progress.step() # Check persons membership in referenced families - for person_handle in self.db.iter_person_handles(): + for person_handle in self.db.get_person_handles(): person = self.db.get_person_from_handle(person_handle) phandle_list = person.get_parent_family_handle_list() @@ -479,38 +478,38 @@ class CheckIntegrity(object): def cleanup_missing_photos(self,cl=0): self.progress.set_pass(_('Looking for unused objects'), - self.db.get_number_of_media_objects()) + len(self.db.get_media_object_handles())) missmedia_action = 0 #------------------------------------------------------------------------- def remove_clicked(): # File is lost => remove all references and the object itself - for handle in self.db.iter_person_handles(): + for handle in self.db.get_person_handles(sort_handles=False): person = self.db.get_person_from_handle(handle) if person.has_media_reference(ObjectId): person.remove_media_references([ObjectId]) self.db.commit_person(person,self.trans) - for handle in self.db.iter_family_handles(): + for handle in self.db.get_family_handles(): family = self.db.get_family_from_handle(handle) if family.has_media_reference(ObjectId): family.remove_media_references([ObjectId]) self.db.commit_family(family,self.trans) - for handle in self.db.iter_event_handles(): + for handle in self.db.get_event_handles(): event = self.db.get_event_from_handle(handle) if event.has_media_reference(ObjectId): event.remove_media_references([ObjectId]) self.db.commit_event(event,self.trans) - for handle in self.db.iter_source_handles(): + for handle in self.db.get_source_handles(): source = self.db.get_source_from_handle(handle) if source.has_media_reference(ObjectId): source.remove_media_references([ObjectId]) self.db.commit_source(source,self.trans) - for handle in self.db.iter_place_handles(): + for handle in self.db.get_place_handles(): place = self.db.get_place_from_handle(handle) if place.has_media_reference(ObjectId): place.remove_media_references([ObjectId]) @@ -550,7 +549,7 @@ class CheckIntegrity(object): #------------------------------------------------------------------------- - for ObjectId in self.db.iter_media_handles(): + for ObjectId in self.db.get_media_object_handles(): obj = self.db.get_object_from_handle(ObjectId) photo_name = Utils.media_path_full(self.db, obj.get_path()) if photo_name is not None and photo_name != "" and not Utils.find_file(photo_name): @@ -663,23 +662,18 @@ class CheckIntegrity(object): 'remove' : self.db.remove_note}, } for the_type, the_func in tables.iteritems(): - cursor = the_func['cursor_func']() - total = the_func['total_func']() - check = the_func['check_func'] - remove_func = the_func['remove'] - - self.progress.set_pass(the_func['progress'],total) - - item = cursor.first() - while item: - self.progress.step() - (handle,data) = item - if check(data): - self.empty_objects[the_type].append(handle) - #we cannot remove here as that would destroy cursor - item = cursor.next() - - cursor.close() + with the_func['cursor_func']() as cursor: + total = the_func['total_func']() + check = the_func['check_func'] + remove_func = the_func['remove'] + + self.progress.set_pass(the_func['progress'],total) + for handle, data in cursor: + self.progress.step() + if check(data): + self.empty_objects[the_type].append(handle) + #we cannot remove here as that would destroy cursor + #now remove for handle in self.empty_objects[the_type]: remove_func(handle, self.trans) @@ -696,10 +690,10 @@ class CheckIntegrity(object): def cleanup_empty_families(self,automatic): - fhandle_list = self.db.iter_family_handles() + fhandle_list = self.db.get_family_handles() self.progress.set_pass(_('Looking for empty families'), - self.db.get_number_of_families()) + len(fhandle_list)) for family_handle in fhandle_list: self.progress.step() @@ -714,7 +708,7 @@ class CheckIntegrity(object): self.delete_empty_family(family_handle) def delete_empty_family(self,family_handle): - for key in self.iter.get_person_handles(): + for key in self.db.get_person_handles(sort_handles=False): child = self.db.get_person_from_handle(key) child.remove_parent_family_handle(family_handle) child.remove_family_handle(family_handle) @@ -724,9 +718,9 @@ class CheckIntegrity(object): """Repair father=female or mother=male in hetero families """ - fhandle_list = self.db.iter_family_handles() + fhandle_list = self.db.get_family_handles() self.progress.set_pass(_('Looking for broken parent relationships'), - self.db.get_number_of_families()) + len(fhandle_list)) for family_handle in fhandle_list: self.progress.step() @@ -760,7 +754,7 @@ class CheckIntegrity(object): self.db.get_number_of_people() +self.db.get_number_of_families()) - for key in self.db.iter_person_handles(): + for key in self.db.get_person_handles(sort_handles=False): self.progress.step() person = self.db.get_person_from_handle(key) @@ -814,7 +808,7 @@ class CheckIntegrity(object): self.db.commit_person(person,self.trans) self.invalid_events.append(key) - for key in self.db.iter_family_handles(): + for key in self.db.get_family_handles(): self.progress.step() family = self.db.get_family_from_handle(key) if family.get_event_ref_list(): @@ -837,10 +831,10 @@ class CheckIntegrity(object): self.invalid_events.append(key) def check_person_references(self): - plist = self.db.iter_person_handles() + plist = self.db.get_person_handles() self.progress.set_pass(_('Looking for person reference problems'), - self.db.get_number_of_people()) + len(plist)) for key in plist: person = self.db.get_person_from_handle(key) @@ -857,9 +851,9 @@ class CheckIntegrity(object): Fix issues in 3.1.0 upgrade: missed some dates on associated people source dates. """ - plist = self.db.iter_person_handles() + plist = self.db.get_person_handles() self.progress.set_pass(_('Checking people for proper date formats'), - self.db.get_number_of_people()) + len(plist)) # First, decode all of a person: for handle in plist: need_to_fix = False @@ -948,9 +942,9 @@ class CheckIntegrity(object): self.invalid_dates.append(handle) self.progress.step() - flist = self.db.iter_family_handles() + flist = self.db.get_family_handles() self.progress.set_pass(_('Checking families for proper date formats'), - self.db.get_number_of_families()) + len(flist)) # First, decode all of a person: for handle in flist: need_to_fix = False @@ -1004,10 +998,10 @@ class CheckIntegrity(object): self.progress.step() def check_repo_references(self): - slist = self.db.iter_source_handles() + slist = self.db.get_source_handles() self.progress.set_pass(_('Looking for repository reference problems'), - self.db.get_number_of_sources) + len(slist)) for key in slist: source = self.db.get_source_from_handle(key) @@ -1020,13 +1014,11 @@ class CheckIntegrity(object): self.invalid_repo_references.append(key) def check_place_references(self): - plist = self.db.iter_person_handles() - flist = self.db.iter_family_handles() - elist = self.db.iter_event_handles() + plist = self.db.get_person_handles() + flist = self.db.get_family_handles() + elist = self.db.get_event_handles() self.progress.set_pass(_('Looking for place reference problems'), - self.db.get_number_of_people()+ - self.db.get_number_of_events()+ - self.db.get_number_of_families()) + len(elist)+len(plist)+len(flist)) # check persons -> the LdsOrd references a place for key in plist: person = self.db.get_person_from_handle(key) @@ -1079,7 +1071,7 @@ class CheckIntegrity(object): self.progress.set_pass(_('Looking for source reference problems'), total) - for handle in self.db.iter_person_handles(): + for handle in self.db.person_map.keys(): self.progress.step() info = self.db.person_map[handle] person = gen.lib.Person() @@ -1095,7 +1087,7 @@ class CheckIntegrity(object): not in self.invalid_source_references] self.invalid_source_references += new_bad_handles - for handle in self.db.iter_family_handles(): + for handle in self.db.family_map.keys(): self.progress.step() info = self.db.family_map[handle] family = gen.lib.Family() @@ -1111,7 +1103,7 @@ class CheckIntegrity(object): not in self.invalid_source_references] self.invalid_source_references += new_bad_handles - for handle in self.db.iter_place_handles(): + for handle in self.db.place_map.keys(): self.progress.step() info = self.db.place_map[handle] place = gen.lib.Place() @@ -1127,7 +1119,7 @@ class CheckIntegrity(object): not in self.invalid_source_references] self.invalid_source_references += new_bad_handles - for handle in self.db.iter_repository_handles(): + for handle in self.db.repository_map.keys(): self.progress.step() info = self.db.repository_map[handle] repo = gen.lib.Repository() @@ -1160,7 +1152,7 @@ class CheckIntegrity(object): not in self.invalid_source_references] self.invalid_source_references += new_bad_handles - for handle in self.db.iter_media_handles(): + for handle in self.db.media_map.keys(): self.progress.step() info = self.db.media_map[handle] obj = gen.lib.MediaObject() @@ -1176,7 +1168,7 @@ class CheckIntegrity(object): not in self.invalid_source_references] self.invalid_source_references += new_bad_handles - for handle in self.db.iter_event_handles(): + for handle in self.db.event_map.keys(): self.progress.step() info = self.db.event_map[handle] event = gen.lib.Event() @@ -1206,7 +1198,7 @@ class CheckIntegrity(object): self.progress.set_pass(_('Looking for media object reference problems'), total) - for handle in self.db.iter_person_handles(): + for handle in self.db.person_map.keys(): self.progress.step() info = self.db.person_map[handle] person = gen.lib.Person() @@ -1222,7 +1214,7 @@ class CheckIntegrity(object): not in self.invalid_media_references] self.invalid_media_references += new_bad_handles - for handle in self.db.iter_family_handles(): + for handle in self.db.family_map.keys(): self.progress.step() info = self.db.family_map[handle] family = gen.lib.Family() @@ -1238,7 +1230,7 @@ class CheckIntegrity(object): not in self.invalid_media_references] self.invalid_media_references += new_bad_handles - for handle in self.db.iter_place_handles(): + for handle in self.db.place_map.keys(): self.progress.step() info = self.db.place_map[handle] place = gen.lib.Place() @@ -1254,7 +1246,7 @@ class CheckIntegrity(object): not in self.invalid_media_references] self.invalid_media_references += new_bad_handles - for handle in self.db.iter_event_handles(): + for handle in self.db.event_map.keys(): self.progress.step() info = self.db.event_map[handle] event = gen.lib.Event() @@ -1270,7 +1262,7 @@ class CheckIntegrity(object): not in self.invalid_media_references] self.invalid_media_references += new_bad_handles - for handle in self.db.iter_source_handles(): + for handle in self.db.source_map.keys(): self.progress.step() info = self.db.source_map[handle] source = gen.lib.Source() @@ -1302,7 +1294,7 @@ class CheckIntegrity(object): self.progress.set_pass(_('Looking for note reference problems'), total) - for handle in self.db.iter_person_handles(): + for handle in self.db.person_map.keys(): self.progress.step() info = self.db.person_map[handle] person = gen.lib.Person() @@ -1319,7 +1311,7 @@ class CheckIntegrity(object): not in self.invalid_note_references] self.invalid_note_references += new_bad_handles - for handle in self.db.iter_family_handles(): + for handle in self.db.family_map.keys(): self.progress.step() info = self.db.family_map[handle] family = gen.lib.Family() @@ -1336,7 +1328,7 @@ class CheckIntegrity(object): not in self.invalid_note_references] self.invalid_note_references += new_bad_handles - for handle in self.db.iter_place_handles(): + for handle in self.db.place_map.keys(): self.progress.step() info = self.db.place_map[handle] place = gen.lib.Place() @@ -1353,7 +1345,7 @@ class CheckIntegrity(object): not in self.invalid_note_references] self.invalid_note_references += new_bad_handles - for handle in self.db.iter_source_handles(): + for handle in self.db.source_map.keys(): self.progress.step() info = self.db.source_map[handle] source = gen.lib.Source() @@ -1370,7 +1362,7 @@ class CheckIntegrity(object): not in self.invalid_note_references] self.invalid_note_references += new_bad_handles - for handle in self.db.iter_media_handles(): + for handle in self.db.media_map.keys(): self.progress.step() info = self.db.media_map[handle] obj = gen.lib.MediaObject() @@ -1387,7 +1379,7 @@ class CheckIntegrity(object): not in self.invalid_note_references] self.invalid_note_references += new_bad_handles - for handle in self.db.iter_event_handles(): + for handle in self.db.event_map.keys(): self.progress.step() info = self.db.event_map[handle] event = gen.lib.Event() @@ -1404,7 +1396,7 @@ class CheckIntegrity(object): not in self.invalid_note_references] self.invalid_note_references += new_bad_handles - for handle in self.db.iter_repository_handles(): + for handle in self.db.repository_map.keys(): self.progress.step() info = self.db.repository_map[handle] repo = gen.lib.Repository() diff --git a/src/plugins/tool/MediaManager.py b/src/plugins/tool/MediaManager.py index 83afccd71..e724fc9e5 100644 --- a/src/plugins/tool/MediaManager.py +++ b/src/plugins/tool/MediaManager.py @@ -31,6 +31,7 @@ # standard python modules # #------------------------------------------------------------------------ +from __future__ import with_statement import os #------------------------------------------------------------------------ @@ -460,19 +461,15 @@ class PathChange(BatchOp): def _prepare(self): from_text = unicode(self.from_entry.get_text()) - cursor = self.db.get_media_cursor() self.set_total(self.db.get_number_of_media_objects()) - item = cursor.first() - while item: - (handle,data) = item - obj = MediaObject() - obj.unserialize(data) - if obj.get_path().find(from_text) != -1: - self.handle_list.append(handle) - self.path_list.append(obj.path) - item = cursor.next() - self.update() - cursor.close() + with self.db.get_media_cursor() as cursor: + for handle, data in cursor: + obj = MediaObject() + obj.unserialize(data) + if obj.get_path().find(from_text) != -1: + self.handle_list.append(handle) + self.path_list.append(obj.path) + self.update() self.reset() self.prepared = True @@ -501,19 +498,15 @@ class Convert2Abs(BatchOp): "that is not set, it prepends user's directory.") def _prepare(self): - cursor = self.db.get_media_cursor() self.set_total(self.db.get_number_of_media_objects()) - item = cursor.first() - while item: - (handle,data) = item - obj = MediaObject() - obj.unserialize(data) - if not os.path.isabs(obj.path): - self.handle_list.append(handle) - self.path_list.append(obj.path) - item = cursor.next() - self.update() - cursor.close() + with self.db.get_media_cursor() as cursor: + for handle, data in cursor: + obj = MediaObject() + obj.unserialize(data) + if not os.path.isabs(obj.path): + self.handle_list.append(handle) + self.path_list.append(obj.path) + self.update() self.reset() def _run(self): @@ -541,19 +534,15 @@ class Convert2Rel(BatchOp): "a base path that can change to your needs.") def _prepare(self): - cursor = self.db.get_media_cursor() self.set_total(self.db.get_number_of_media_objects()) - item = cursor.first() - while item: - (handle,data) = item - obj = MediaObject() - obj.unserialize(data) - if os.path.isabs(obj.path): - self.handle_list.append(handle) - self.path_list.append(obj.path) - item = cursor.next() - self.update() - cursor.close() + with self.db.get_media_cursor() as cursor: + for handle, data in cursor: + obj = MediaObject() + obj.unserialize(data) + if os.path.isabs(obj.path): + self.handle_list.append(handle) + self.path_list.append(obj.path) + self.update() self.reset() def _run(self): diff --git a/src/plugins/tool/RemoveUnused.py b/src/plugins/tool/RemoveUnused.py index 1ecfbdd8a..67acbbfbc 100644 --- a/src/plugins/tool/RemoveUnused.py +++ b/src/plugins/tool/RemoveUnused.py @@ -29,6 +29,7 @@ # python modules # #------------------------------------------------------------------------- +from __future__ import with_statement from gettext import gettext as _ #------------------------------------------------------------------------ @@ -277,19 +278,14 @@ class RemoveUnused(Tool.Tool,ManagedWindow.ManagedWindow,UpdateCallback): # This table was not requested. Skip it. continue - cursor = the_func['cursor_func']() - total = the_func['total_func']() - self.set_total(total) - item = cursor.first() - while item: - (handle,data) = item - - hlist = [x for x in self.db.find_backlink_handles(handle)] - if len(hlist) == 0: - self.add_results((the_type, handle, data)) - item = cursor.next() - self.update() - cursor.close() + with the_func['cursor_func']() as cursor: + total = the_func['total_func']() + self.set_total(total) + fbh = self.db.find_backlink_handles + for handle, data in cursor: + if not any(h for h in fbh(handle)): + self.add_results((the_type, handle, data)) + self.update() self.reset() def do_remove(self, obj):