In method cleanup_empty_objects, replace dispatch dictionary with tuple

svn: r16598
This commit is contained in:
Gerald Britton 2011-02-10 17:46:21 +00:00
parent fd81aef58b
commit 6bfc9224d8

View File

@ -243,15 +243,7 @@ class CheckIntegrity(object):
self.invalid_note_references = [] self.invalid_note_references = []
self.invalid_dates = [] self.invalid_dates = []
self.removed_name_format = [] self.removed_name_format = []
self.empty_objects = {'persons' : [], self.empty_objects = defaultdict(list)
'families': [],
'events' : [],
'sources' : [],
'media' : [],
'places' : [],
'repos' : [],
'notes' : [],
}
self.progress = ProgressMeter(_('Checking Database'),'') self.progress = ProgressMeter(_('Checking Database'),'')
def family_errors(self): def family_errors(self):
@ -623,84 +615,107 @@ class CheckIntegrity(object):
empty_repos_data = gen.lib.Repository().serialize() empty_repos_data = gen.lib.Repository().serialize()
empty_note_data = gen.lib.Note().serialize() empty_note_data = gen.lib.Note().serialize()
tables = { _db = self.db
'persons' : {'get_func': self.db.get_person_from_handle, def _empty(empty, flag):
'cursor_func': self.db.get_person_cursor, ''' Closure for dispatch table, below '''
'total_func' : self.db.get_number_of_people, def _f(value):
'progress' : _('Looking for empty people records'), return self._check_empty(value, empty, flag)
'check_func' : lambda x : self._check_empty(x, return _f
empty_person_data,
CHANGE_PERSON), '''
'remove' : self.db.remove_person}, Dispatch table for cleaning up empty objects. Each entry is
'families': {'get_func': self.db.get_family_from_handle, a tuple containing:
'cursor_func': self.db.get_family_cursor, 0. Type of object being cleaned up
'total_func' : self.db.get_number_of_families, 1. function to read the object from the database
'progress' : _('Looking for empty family records'), 2. function returning cursor over the object type
'check_func' : lambda x : self._check_empty(x, 3. function returning number of objects of this type
empty_family_data, 4. text identifying the object being cleaned up
CHANGE_FAMILY), 5. function to check if the data is empty
'remove' : self.db.remove_family}, 6. function to remove the object, if empty
'events' : {'get_func': self.db.get_event_from_handle, '''
'cursor_func': self.db.get_event_cursor,
'total_func' : self.db.get_number_of_events, table = (
'progress' : _('Looking for empty event records'), ('persons',
'check_func' : lambda x : self._check_empty(x, _db.get_person_from_handle,
empty_event_data, _db.get_person_cursor,
CHANGE_EVENT), _db.get_number_of_people,
'remove' : self.db.remove_event}, _('Looking for empty people records'),
'sources' : {'get_func': self.db.get_source_from_handle, _empty(empty_person_data, CHANGE_PERSON),
'cursor_func': self.db.get_source_cursor, _db.remove_person,
'total_func' : self.db.get_number_of_sources, ),
'progress' : _('Looking for empty source records'), ('families',
'check_func' : lambda x : self._check_empty(x, _db.get_family_from_handle,
empty_source_data, _db.get_family_cursor,
CHANGE_SOURCE), _db.get_number_of_families,
'remove' : self.db.remove_source}, _('Looking for empty family records'),
'places' : {'get_func': self.db.get_place_from_handle, _empty(empty_family_data, CHANGE_FAMILY),
'cursor_func': self.db.get_place_cursor, _db.remove_family,
'total_func' : self.db.get_number_of_places, ),
'progress' : _('Looking for empty place records'), ('events',
'check_func' : lambda x : self._check_empty(x, _db.get_event_from_handle,
empty_place_data, _db.get_event_cursor,
CHANGE_PLACE), _db.get_number_of_events,
'remove' : self.db.remove_place}, _('Looking for empty event records'),
'media' : {'get_func': self.db.get_object_from_handle, _empty(empty_event_data, CHANGE_EVENT),
'cursor_func': self.db.get_media_cursor, _db.remove_event,
'progress' : _('Looking for empty media records'), ),
'total_func' : self.db.get_number_of_media_objects, ('sources',
'check_func' : lambda x : self._check_empty(x, _db.get_source_from_handle,
empty_media_data, _db.get_source_cursor,
CHANGE_MEDIA), _db.get_number_of_sources,
'remove' : self.db.remove_object}, _('Looking for empty source records'),
'repos' : {'get_func': self.db.get_repository_from_handle, _empty(empty_source_data, CHANGE_SOURCE),
'cursor_func': self.db.get_repository_cursor, _db.remove_source,
'total_func' : self.db.get_number_of_repositories, ),
'progress' : _('Looking for empty repository records'), ('places',
'check_func' : lambda x : self._check_empty(x, _db.get_place_from_handle,
empty_repos_data, _db.get_place_cursor,
CHANGE_REPOS), _db.get_number_of_places,
'remove' : self.db.remove_repository}, _('Looking for empty place records'),
'notes' : {'get_func': self.db.get_note_from_handle, _empty(empty_place_data, CHANGE_PLACE),
'cursor_func': self.db.get_note_cursor, _db.remove_place,
'total_func' : self.db.get_number_of_notes, ),
'progress' : _('Looking for empty note records'), ('media',
'check_func' : lambda x : self._check_empty(x, _db.get_object_from_handle,
empty_note_data, _db.get_media_cursor,
CHANGE_NOTE), _db.get_number_of_media_objects,
'remove' : self.db.remove_note}, _('Looking for empty media records'),
} _empty(empty_media_data, CHANGE_MEDIA),
for the_type, the_func in tables.iteritems(): _db.remove_object,
with the_func['cursor_func']() as cursor: ),
total = the_func['total_func']() ('repos',
check = the_func['check_func'] _db.get_repository_from_handle,
remove_func = the_func['remove'] _db.get_repository_cursor,
_db.get_number_of_repositories,
_('Looking for empty repository records'),
_empty(empty_repos_data, CHANGE_REPOS),
_db.remove_repository,
),
('notes',
_db.get_note_from_handle,
_db.get_note_cursor,
_db.get_number_of_notes,
_('Looking for empty note records'),
_empty(empty_note_data, CHANGE_NOTE),
_db.remove_note,
),
)
# Now, iterate over the table, dispatching the functions
for (the_type, get_func, cursor_func, total_func,
text, check_func, remove_func) in table:
with cursor_func() as cursor:
total = total_func()
self.progress.set_pass(text, total)
self.progress.set_pass(the_func['progress'],total)
for handle, data in cursor: for handle, data in cursor:
self.progress.step() self.progress.step()
if check(data): if check_func(data):
# we cannot remove here as that would destroy cursor
# so save the handles for later removal
self.empty_objects[the_type].append(handle) self.empty_objects[the_type].append(handle)
#we cannot remove here as that would destroy cursor
#now remove #now remove
for handle in self.empty_objects[the_type]: for handle in self.empty_objects[the_type]:
@ -1326,16 +1341,7 @@ class CheckIntegrity(object):
media_references = len(self.invalid_media_references) media_references = len(self.invalid_media_references)
note_references = len(self.invalid_note_references) note_references = len(self.invalid_note_references)
name_format = len(self.removed_name_format) name_format = len(self.removed_name_format)
empty_objs = ( empty_objs = sum(len(obj) for obj in self.empty_objects.itervalues())
len(self.empty_objects['persons']) +
len(self.empty_objects['families']) +
len(self.empty_objects['events']) +
len(self.empty_objects['sources']) +
len(self.empty_objects['media']) +
len(self.empty_objects['places']) +
len(self.empty_objects['repos']) +
len(self.empty_objects['notes'])
)
errors = (photos + efam + blink + plink + slink + rel + errors = (photos + efam + blink + plink + slink + rel +
event_invalid + person + event_invalid + person +