From d3fed5963fe943b245f8c8ba918d95edf833b36b Mon Sep 17 00:00:00 2001 From: Nick Hall Date: Sun, 10 Jun 2012 00:10:28 +0000 Subject: [PATCH] GEPS008: Create new module for referent functions svn: r19814 --- po/POTFILES.in | 1 + po/POTFILES.skip | 1 + src/Utils.py | 124 ------------------ .../person/_hastextmatchingsubstringof.py | 2 +- src/gen/utils/Makefile.am | 3 +- src/gui/views/treemodels/citationtreemodel.py | 2 +- src/plugins/view/citationlistview.py | 4 +- src/plugins/view/citationtreeview.py | 7 +- src/plugins/view/mediaview.py | 3 +- src/plugins/view/noteview.py | 4 +- src/plugins/view/sourceview.py | 5 +- src/plugins/webreport/NarrativeWeb.py | 3 +- 12 files changed, 20 insertions(+), 139 deletions(-) diff --git a/po/POTFILES.in b/po/POTFILES.in index 326966b00..b8a8418b0 100644 --- a/po/POTFILES.in +++ b/po/POTFILES.in @@ -324,6 +324,7 @@ src/gen/simple/_simpleaccess.py src/gen/simple/_simpletable.py # gen.utils +src/gen/utils/alive.py src/gen/utils/place.py # gui - GUI code diff --git a/po/POTFILES.skip b/po/POTFILES.skip index 5c3d67f38..61dd37897 100644 --- a/po/POTFILES.skip +++ b/po/POTFILES.skip @@ -217,6 +217,7 @@ src/gen/simple/_simpledoc.py src/gen/utils/__init__.py src/gen/utils/callback.py src/gen/utils/callman.py +src/gen/utils/referent.py # gui - GUI code src/gui/__init__.py diff --git a/src/Utils.py b/src/Utils.py index 1ccf2a33f..1fa325ce4 100644 --- a/src/Utils.py +++ b/src/Utils.py @@ -490,130 +490,6 @@ def create_uid(self, handle=None): uid = uuid.uuid4() return uid.hex.upper() -#------------------------------------------------------------------------- -# -# Other util functions -# -#------------------------------------------------------------------------- -def get_referents(handle, db, primary_objects): - """ Find objects that refer to an object. - - This function is the base for other get__referents functions. - - """ - # Use one pass through the reference map to grab all the references - object_list = list(db.find_backlink_handles(handle)) - - # Then form the object-specific lists - the_lists = () - - for primary in primary_objects: - primary_list = [item[1] for item in object_list if item[0] == primary] - the_lists = the_lists + (primary_list, ) - - return the_lists - -def get_source_referents(source_handle, db): - """ Find objects that refer the source. - - This function finds all primary objects that refer (directly or through - secondary child-objects) to a given source handle in a given database. - - Only Citations can refer to sources, so that is all we need to check - """ - _primaries = ('Citation',) - - return (get_referents(source_handle, db, _primaries)) - -def get_citation_referents(citation_handle, db): - """ Find objects that refer the citation. - - This function finds all primary objects that refer (directly or through - secondary child-objects) to a given citation handle in a given database. - - """ - _primaries = ('Person', 'Family', 'Event', 'Place', - 'Source', 'MediaObject', 'Repository') - - return (get_referents(citation_handle, db, _primaries)) - -def get_source_and_citation_referents(source_handle, db): - """ - Find all citations that refer to the sources, and recursively, all objects - that refer to the sources. - - This function finds all primary objects that refer (directly or through - secondary child-objects) to a given source handle in a given database. - - Objects -> Citations -> Source - e.g. - Media object M1 -> Citation C1 -> Source S1 - Media object M2 -> Citation C1 -> Source S1 - Person object P1 -> Citation C2 -> Source S1 - - The returned structure is rather ugly, but provides all the information in - a way that is consistent with the other Util functions. - ( - tuple of objects that refer to the source - only first element is present - ([C1, C2],), - list of citations with objects that refer to them - [ - (C1, - tuple of reference lists - P, F, E, Pl, S, M, R - ([], [], [], [], [], [M1, M2]. []) - ) - (C2, - tuple of reference lists - P, F, E, Pl, S, M, R - ([P1], [], [], [], [], []. []) - ) - ] - ) -#47738: DEBUG: citationtreeview.py: line 428: source referents [(['bfe59e90dbb555d0d87'],)] -#47743: DEBUG: citationtreeview.py: line 432: citation bfe59e90dbb555d0d87 -#47825: DEBUG: citationtreeview.py: line 435: citation_referents_list [[('bfe59e90dbb555d0d87', ([], [], ['ba77932bf0b2d59eccb'], [], [], [], []))]] -#47827: DEBUG: citationtreeview.py: line 440: the_lists [((['bfe59e90dbb555d0d87'],), [('bfe59e90dbb555d0d87', ([], [], ['ba77932bf0b2d59eccb'], [], [], [], []))])] - - """ - the_lists = get_source_referents(source_handle, db) - LOG.debug('source referents %s' % [the_lists]) - # now, for each citation, get the objects that refer to that citation - citation_referents_list = [] - for citation in the_lists[0]: - LOG.debug('citation %s' % citation) - refs = get_citation_referents(citation, db) - citation_referents_list += [(citation, refs)] - LOG.debug('citation_referents_list %s' % [citation_referents_list]) - - (citation_list) = the_lists - the_lists = (citation_list, citation_referents_list) - - LOG.debug('the_lists %s' % [the_lists]) - return the_lists - -def get_media_referents(media_handle, db): - """ Find objects that refer the media object. - - This function finds all primary objects that refer - to a given media handle in a given database. - - """ - _primaries = ('Person', 'Family', 'Event', 'Place', 'Source', 'Citation') - - return (get_referents(media_handle, db, _primaries)) - -def get_note_referents(note_handle, db): - """ Find objects that refer a note object. - - This function finds all primary objects that refer - to a given note handle in a given database. - - """ - _primaries = ('Person', 'Family', 'Event', 'Place', - 'Source', 'Citation', 'MediaObject', 'Repository') - - return (get_referents(note_handle, db, _primaries)) #------------------------------------------------------------------------- # diff --git a/src/gen/filters/rules/person/_hastextmatchingsubstringof.py b/src/gen/filters/rules/person/_hastextmatchingsubstringof.py index 1bb924aac..d02483511 100644 --- a/src/gen/filters/rules/person/_hastextmatchingsubstringof.py +++ b/src/gen/filters/rules/person/_hastextmatchingsubstringof.py @@ -35,7 +35,7 @@ LOG = logging.getLogger(".citationfilter") # GRAMPS modules # #------------------------------------------------------------------------- -from Utils import get_source_and_citation_referents +from gen.utils.referent import get_source_and_citation_referents from gen.filters.rules import Rule #------------------------------------------------------------------------- diff --git a/src/gen/utils/Makefile.am b/src/gen/utils/Makefile.am index 61fe573c7..0c02a176e 100644 --- a/src/gen/utils/Makefile.am +++ b/src/gen/utils/Makefile.am @@ -13,7 +13,8 @@ pkgpython_PYTHON = \ callman.py \ configmanager.py \ fallback.py \ - place.py + place.py \ + referent.py pkgpyexecdir = @pkgpyexecdir@/gen/utils diff --git a/src/gui/views/treemodels/citationtreemodel.py b/src/gui/views/treemodels/citationtreemodel.py index aba31f959..c8c28a4ba 100644 --- a/src/gui/views/treemodels/citationtreemodel.py +++ b/src/gui/views/treemodels/citationtreemodel.py @@ -52,7 +52,7 @@ import gtk # GRAMPS modules # #------------------------------------------------------------------------- -from Utils import get_source_referents +from gen.utils.referent import get_source_referents from gui.views.treemodels.treebasemodel import TreeBaseModel from gui.views.treemodels.citationbasemodel import CitationBaseModel diff --git a/src/plugins/view/citationlistview.py b/src/plugins/view/citationlistview.py index 8ad091645..82bca1e06 100644 --- a/src/plugins/view/citationlistview.py +++ b/src/plugins/view/citationlistview.py @@ -49,7 +49,7 @@ from gui.views.treemodels.citationlistmodel import CitationListModel from gen.plug import CATEGORY_QR_CITATION import gen.lib from gui.views.listview import ListView -import Utils +from gen.utils.referent import get_citation_referents from gui.views.bookmarks import CitationBookmarks from gen.errors import WindowActiveError from gui.ddtargets import DdTargets @@ -284,7 +284,7 @@ class CitationListView(ListView): self.remove_selected_objects() def remove_object_from_handle(self, handle): - the_lists = Utils.get_citation_referents(handle, self.dbstate.db) + the_lists = get_citation_referents(handle, self.dbstate.db) object = self.dbstate.db.get_citation_from_handle(handle) query = DeleteCitationQuery(self.dbstate, self.uistate, object, the_lists) diff --git a/src/plugins/view/citationtreeview.py b/src/plugins/view/citationtreeview.py index 51d894e64..971157894 100644 --- a/src/plugins/view/citationtreeview.py +++ b/src/plugins/view/citationtreeview.py @@ -50,7 +50,8 @@ from gui.views.treemodels.citationtreemodel import CitationTreeModel from gen.plug import CATEGORY_QR_SOURCE_OR_CITATION import gen.lib from gui.views.listview import ListView -import Utils +from gen.utils.referent import (get_source_and_citation_referents, + get_citation_referents) from gui.views.bookmarks import CitationBookmarks from gen.errors import WindowActiveError from gui.ddtargets import DdTargets @@ -458,14 +459,14 @@ class CitationTreeView(ListView): if (not source and not citation) or (source and citation): raise ValueError("selection must be either source or citation") if citation: - the_lists = Utils.get_citation_referents(handle, self.dbstate.db) + the_lists = get_citation_referents(handle, self.dbstate.db) object = self.dbstate.db.get_citation_from_handle(handle) query = DeleteCitationQuery(self.dbstate, self.uistate, object, the_lists) is_used = any(the_lists) return (query, is_used, object) else: - the_lists = Utils.get_source_and_citation_referents(handle, + the_lists = get_source_and_citation_referents(handle, self.dbstate.db) LOG.debug('the_lists %s' % [the_lists]) diff --git a/src/plugins/view/mediaview.py b/src/plugins/view/mediaview.py index 29dde2ecc..343a4e95e 100644 --- a/src/plugins/view/mediaview.py +++ b/src/plugins/view/mediaview.py @@ -56,6 +56,7 @@ import const from gen.constfunc import win from gen.config import config import Utils +from gen.utils.referent import get_media_referents from gui.views.bookmarks import MediaBookmarks import gen.mime import gen.lib @@ -356,7 +357,7 @@ class MediaView(ListView): Remove the selected objects from the database after getting user verification. """ - the_lists = Utils.get_media_referents(handle, self.dbstate.db) + the_lists = get_media_referents(handle, self.dbstate.db) object = self.dbstate.db.get_object_from_handle(handle) query = DeleteMediaQuery(self.dbstate, self.uistate, handle, the_lists) is_used = any(the_lists) diff --git a/src/plugins/view/noteview.py b/src/plugins/view/noteview.py index 92c237a56..6085a5723 100644 --- a/src/plugins/view/noteview.py +++ b/src/plugins/view/noteview.py @@ -47,7 +47,7 @@ import gtk #------------------------------------------------------------------------- from gui.views.listview import ListView from gui.views.treemodels import NoteModel -import Utils +from gen.utils.referent import get_note_referents from gen.errors import WindowActiveError from gui.views.bookmarks import NoteBookmarks from gen.config import config @@ -246,7 +246,7 @@ class NoteView(ListView): self.remove_selected_objects() def remove_object_from_handle(self, handle): - the_lists = Utils.get_note_referents(handle, self.dbstate.db) + the_lists = get_note_referents(handle, self.dbstate.db) object = self.dbstate.db.get_note_from_handle(handle) query = DeleteNoteQuery(self.dbstate, self.uistate, object, the_lists) is_used = any(the_lists) diff --git a/src/plugins/view/sourceview.py b/src/plugins/view/sourceview.py index 2935100f1..4889fc8c5 100644 --- a/src/plugins/view/sourceview.py +++ b/src/plugins/view/sourceview.py @@ -43,7 +43,7 @@ import gen.lib from gen.config import config from gui.views.listview import ListView from gui.views.treemodels import SourceModel -import Utils +from gen.utils.referent import get_source_and_citation_referents from gui.views.bookmarks import SourceBookmarks from gen.errors import WindowActiveError from gui.ddtargets import DdTargets @@ -214,8 +214,7 @@ class SourceView(ListView): self.remove_selected_objects() def remove_object_from_handle(self, handle): - the_lists = Utils.get_source_and_citation_referents(handle, - self.dbstate.db) + the_lists = get_source_and_citation_referents(handle, self.dbstate.db) LOG.debug('the_lists %s' % [the_lists]) object = self.dbstate.db.get_source_from_handle(handle) diff --git a/src/plugins/webreport/NarrativeWeb.py b/src/plugins/webreport/NarrativeWeb.py index 8d09861e0..07ed5fa90 100644 --- a/src/plugins/webreport/NarrativeWeb.py +++ b/src/plugins/webreport/NarrativeWeb.py @@ -89,6 +89,7 @@ from gen.plug.report import utils as ReportUtils from gen.plug.report import MenuReportOptions import Utils +from gen.utils.referent import get_source_and_citation_referents from gen.constfunc import win from gui.thumbnails import get_thumbnail_path, run_thumbnailer import ImgManip @@ -4514,7 +4515,7 @@ class SourcePage(BasePage): sourcedetail += repo_list # get the Source and its Citation Referents... - the_lists = Utils.get_source_and_citation_referents(src_handle, self.dbase_) + the_lists = get_source_and_citation_referents(src_handle, self.dbase_) if the_lists: (citation_list, citation_referents_list) = the_lists if citation_referents_list: