2
# Gramps - a GTK+/GNOME based genealogy program
4
# Copyright (C) 2006 Donald N. Allingham
5
# Copyright (C) 2010 Michiel D. Nauta
7
# This program is free software; you can redistribute it and/or modify
8
# it under the terms of the GNU General Public License as published by
9
# the Free Software Foundation; either version 2 of the License, or
10
# (at your option) any later version.
12
# This program is distributed in the hope that it will be useful,
13
# but WITHOUT ANY WARRANTY; without even the implied warranty of
14
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15
# GNU General Public License for more details.
17
# You should have received a copy of the GNU General Public License
18
# along with this program; if not, write to the Free Software
19
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22
# $Id: srcbase.py 16425 2011-01-21 16:27:54Z gbritton $
25
SourceBase class for GRAMPS.
28
#-------------------------------------------------------------------------
32
#-------------------------------------------------------------------------
33
from gen.lib.srcref import SourceRef
34
from gen.lib.const import IDENTICAL, EQUAL, DIFFERENT
36
#-------------------------------------------------------------------------
40
#-------------------------------------------------------------------------
41
class SourceBase(object):
43
Base class for storing source references.
46
def __init__(self, source=None):
48
Create a new SourceBase, copying from source if not None.
50
:param source: Object used to initialize the new object
51
:type source: SourceBase
54
self.source_list = map(SourceRef, source.source_list) if source else []
58
Convert the object to a serialized tuple of data.
60
return [sref.serialize() for sref in self.source_list]
62
def unserialize(self, data):
64
Convert a serialized tuple of data to an object.
66
self.source_list = [SourceRef().unserialize(item) for item in data]
68
def add_source_reference(self, src_ref) :
70
Add a source reference to this object.
72
:param src_ref: The source reference to be added to the
73
SourceNote's list of source references.
74
:type src_ref: :class:`~gen.lib.srcref.SourceRef`
76
self.source_list.append(src_ref)
78
def get_source_references(self) :
80
Return the list of source references associated with the object.
82
:returns: Returns the list of :class:`~gen.lib.srcref.SourceRef` objects associated with
86
return self.source_list
88
def get_sourcref_child_list(self):
90
Return the list of child secondary objects that may refer sources.
92
:returns: Returns the list of child secondary child objects that may
98
def has_source_reference(self, src_handle) :
100
Return True if the object or any of it's child objects has reference
101
to this source handle.
103
:param src_handle: The source handle to be checked.
104
:type src_handle: str
105
:returns: Returns whether the object or any of it's child objects has
106
reference to this source handle.
109
for src_ref in self.source_list:
110
# Using direct access here, not the getter method -- efficiency!
111
if src_ref.ref == src_handle:
114
for item in self.get_sourcref_child_list():
115
if item.has_source_reference(src_handle):
120
def remove_source_references(self, src_handle_list):
122
Remove references to all source handles in the list in this object
123
and all child objects.
125
:param src_handle_list: The list of source handles to be removed.
126
:type src_handle_list: list
128
new_source_list = [src_ref for src_ref in self.source_list
129
if src_ref.ref not in src_handle_list]
130
self.source_list = new_source_list
132
for item in self.get_sourcref_child_list():
133
item.remove_source_references(src_handle_list)
135
def replace_source_references(self, old_handle, new_handle):
137
Replace references to source handles in the list in this object and
138
all child objects and merge equivalent entries.
140
:param old_handle: The source handle to be replaced.
141
:type old_handle: str
142
:param new_handle: The source handle to replace the old one with.
143
:type new_handle: str
145
refs_list = [ src_ref.ref for src_ref in self.source_list ]
147
if new_handle in refs_list:
148
new_ref = self.source_list[refs_list.index(new_handle)]
149
n_replace = refs_list.count(old_handle)
150
for ix_replace in xrange(n_replace):
151
idx = refs_list.index(old_handle)
152
self.source_list[idx].ref = new_handle
153
refs_list[idx] = new_handle
155
src_ref = self.source_list[idx]
156
equi = new_ref.is_equivalent(src_ref)
157
if equi != DIFFERENT:
159
new_ref.merge(src_ref)
160
self.source_list.pop(idx)
163
for item in self.get_sourcref_child_list():
164
item.replace_source_references(old_handle, new_handle)
166
def set_source_reference_list(self, src_ref_list) :
168
Assign the passed list to the object's list of source references.
170
:param src_ref_list: List of source references to ba associated
172
:type src_ref_list: list of :class:`~gen.lib.srcref.SourceRef` instances
174
self.source_list = src_ref_list
176
def _merge_source_reference_list(self, acquisition):
178
Merge the list of source references from acquisition with our own.
180
:param acquisition: the source references list of this object will be
181
merged with the current source references list.
182
:rtype acquisition: SourceRef
184
srcref_list = self.source_list[:]
185
for addendum in acquisition.get_source_references():
186
for srcref in srcref_list:
187
equi = srcref.is_equivalent(addendum)
188
if equi == IDENTICAL:
191
srcref.merge(addendum)
194
self.source_list.append(addendum)