2008-07-08 00:55:18 +05:30
|
|
|
#
|
|
|
|
# Gramps - a GTK+/GNOME based genealogy program
|
|
|
|
#
|
|
|
|
# Copyright (C) 2008 Gary Burton
|
|
|
|
#
|
|
|
|
# This program is free software; you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU General Public License as published by
|
|
|
|
# the Free Software Foundation; either version 2 of the License, or
|
|
|
|
# (at your option) any later version.
|
|
|
|
#
|
|
|
|
# This program is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU General Public License
|
|
|
|
# along with this program; if not, write to the Free Software
|
|
|
|
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
|
|
|
#
|
|
|
|
|
|
|
|
# $Id$
|
|
|
|
|
|
|
|
"""
|
|
|
|
Proxy class for the GRAMPS databases. Returns objects which are referenced
|
|
|
|
by at least one other object.
|
|
|
|
"""
|
|
|
|
|
|
|
|
#-------------------------------------------------------------------------
|
|
|
|
#
|
|
|
|
# GRAMPS libraries
|
|
|
|
#
|
|
|
|
#-------------------------------------------------------------------------
|
|
|
|
from proxybase import ProxyDbBase
|
|
|
|
|
|
|
|
class ReferencedProxyDb(ProxyDbBase):
|
|
|
|
"""
|
|
|
|
A proxy to a Gramps database. This proxy will act like a Gramps database,
|
|
|
|
but returning all objects which are referenced by at least one other object.
|
|
|
|
"""
|
|
|
|
|
|
|
|
def __init__(self, dbase):
|
|
|
|
"""
|
|
|
|
Create a new ReferencedProxyDb instance.
|
|
|
|
"""
|
|
|
|
ProxyDbBase.__init__(self, dbase)
|
2009-09-11 00:19:13 +05:30
|
|
|
self.unreferenced_events = set()
|
|
|
|
self.unreferenced_places = set()
|
|
|
|
self.unreferenced_sources = set()
|
|
|
|
self.unreferenced_repositories = set()
|
|
|
|
self.unreferenced_media_objects = set()
|
|
|
|
self.unreferenced_notes = set()
|
2008-07-08 00:55:18 +05:30
|
|
|
|
|
|
|
# Build lists of unreferenced objects
|
|
|
|
self.__find_unreferenced_objects()
|
|
|
|
|
2009-09-01 00:13:05 +05:30
|
|
|
def include_place(self, handle):
|
2009-07-11 21:05:36 +05:30
|
|
|
"""
|
|
|
|
Filter for places
|
|
|
|
"""
|
2009-07-11 00:54:43 +05:30
|
|
|
return handle not in self.unreferenced_places
|
|
|
|
|
2009-09-03 01:40:45 +05:30
|
|
|
def include_media_object(self, handle):
|
2009-07-11 21:05:36 +05:30
|
|
|
"""
|
|
|
|
Filter for media objects
|
|
|
|
"""
|
2009-07-11 00:54:43 +05:30
|
|
|
return handle not in self.unreferenced_media_objects
|
|
|
|
|
2009-09-01 00:13:05 +05:30
|
|
|
def include_event(self, handle):
|
2009-07-11 21:05:36 +05:30
|
|
|
"""
|
|
|
|
Filter for events
|
|
|
|
"""
|
2009-07-11 00:54:43 +05:30
|
|
|
return handle not in self.unreferenced_events
|
2009-07-11 21:05:36 +05:30
|
|
|
|
2009-09-01 00:13:05 +05:30
|
|
|
def include_source(self, handle):
|
2009-07-11 21:05:36 +05:30
|
|
|
"""
|
|
|
|
Filter for sources
|
|
|
|
"""
|
|
|
|
return handle not in self.unreferenced_sources
|
2009-07-11 00:54:43 +05:30
|
|
|
|
2009-09-01 00:13:05 +05:30
|
|
|
def include_repository(self, handle):
|
2009-07-11 21:05:36 +05:30
|
|
|
"""
|
|
|
|
Filter for repositories
|
|
|
|
"""
|
2009-07-11 00:54:43 +05:30
|
|
|
return handle not in self.unreferenced_repositories
|
|
|
|
|
2009-09-01 00:13:05 +05:30
|
|
|
def include_note(self, handle):
|
2009-07-11 21:05:36 +05:30
|
|
|
"""
|
|
|
|
Filter for notes
|
|
|
|
"""
|
2009-07-11 00:54:43 +05:30
|
|
|
return handle not in self.unreferenced_notes
|
2008-07-08 00:55:18 +05:30
|
|
|
|
|
|
|
def find_backlink_handles(self, handle, include_classes=None):
|
|
|
|
"""
|
|
|
|
Find all objects that hold a reference to the object handle.
|
2009-08-10 04:13:43 +05:30
|
|
|
Returns an iterator over a list of (class_name, handle) tuples.
|
2008-07-08 00:55:18 +05:30
|
|
|
|
|
|
|
@param handle: handle of the object to search for.
|
|
|
|
@type handle: database handle
|
|
|
|
@param include_classes: list of class names to include in the results.
|
|
|
|
Default: None means include all classes.
|
|
|
|
@type include_classes: list of class names
|
|
|
|
|
2009-08-10 04:13:43 +05:30
|
|
|
This default implementation does a sequential scan through all
|
2008-07-08 00:55:18 +05:30
|
|
|
the primary object databases and is very slow. Backends can
|
|
|
|
override this method to provide much faster implementations that
|
|
|
|
make use of additional capabilities of the backend.
|
|
|
|
|
|
|
|
Note that this is a generator function, it returns a iterator for
|
|
|
|
use in loops. If you want a list of the results use:
|
|
|
|
|
2009-07-04 01:53:41 +05:30
|
|
|
> result_list = list(find_backlink_handles(handle))
|
2008-07-08 00:55:18 +05:30
|
|
|
"""
|
|
|
|
handle_itr = self.db.find_backlink_handles(handle, include_classes)
|
|
|
|
for (class_name, handle) in handle_itr:
|
2009-09-01 00:13:05 +05:30
|
|
|
if class_name == 'Person':
|
2008-07-21 00:00:23 +05:30
|
|
|
if not self.get_person_from_handle(handle):
|
2008-07-08 00:55:18 +05:30
|
|
|
continue
|
|
|
|
elif class_name == 'Family':
|
2008-07-21 00:00:23 +05:30
|
|
|
if not self.get_family_from_handle(handle):
|
2008-07-08 00:55:18 +05:30
|
|
|
continue
|
|
|
|
yield (class_name, handle)
|
|
|
|
return
|
|
|
|
|
|
|
|
def __find_unreferenced_objects(self):
|
|
|
|
"""
|
|
|
|
Builds lists of all objects that are not referenced by another object.
|
|
|
|
These may be objects that are really unreferenced or because they
|
|
|
|
are referenced by something or someone that has already been filtered
|
|
|
|
by one of the other proxy decorators.
|
|
|
|
By adding an object to a list, other referenced objects could
|
|
|
|
effectively become unreferenced, so the action is performed in a loop
|
|
|
|
until there are no more objects to unreference.
|
|
|
|
"""
|
|
|
|
object_types = {
|
|
|
|
'Event': {'unref_list': self.unreferenced_events,
|
|
|
|
'handle_list': self.get_event_handles},
|
|
|
|
'Place': {'unref_list': self.unreferenced_places,
|
|
|
|
'handle_list': self.get_place_handles},
|
|
|
|
'Source': {'unref_list': self.unreferenced_sources,
|
|
|
|
'handle_list': self.get_source_handles},
|
|
|
|
'Repositories': {'unref_list': self.unreferenced_repositories,
|
|
|
|
'handle_list': self.get_repository_handles},
|
|
|
|
'MediaObjects': {'unref_list': self.unreferenced_media_objects,
|
|
|
|
'handle_list': self.get_media_object_handles},
|
|
|
|
'Notes': {'unref_list': self.unreferenced_notes,
|
|
|
|
'handle_list': self.get_note_handles}
|
|
|
|
}
|
|
|
|
|
|
|
|
last_count = 0
|
2009-07-04 01:53:41 +05:30
|
|
|
while True:
|
2008-07-08 00:55:18 +05:30
|
|
|
current_count = 0
|
2009-05-27 02:18:09 +05:30
|
|
|
for object_type, object_dict in object_types.iteritems():
|
|
|
|
unref_list = object_dict['unref_list']
|
|
|
|
handle_list = object_dict['handle_list']()
|
2008-07-08 00:55:18 +05:30
|
|
|
for handle in handle_list:
|
2009-09-01 00:13:05 +05:30
|
|
|
if (handle not in unref_list and
|
|
|
|
not any(self.find_backlink_handles(handle)) ):
|
2009-09-11 00:19:13 +05:30
|
|
|
unref_list.add(handle)
|
2008-07-08 00:55:18 +05:30
|
|
|
current_count += len(unref_list)
|
|
|
|
|
|
|
|
if current_count == last_count:
|
2009-07-04 01:53:41 +05:30
|
|
|
break
|
|
|
|
last_count = current_count
|