~ubuntu-branches/ubuntu/utopic/gramps/utopic

« back to all changes in this revision

Viewing changes to src/gen/lib/srcbase.py

  • Committer: Package Import Robot
  • Author(s): James A. Treacy
  • Date: 2012-05-22 17:18:36 UTC
  • mfrom: (39.1.4 sid)
  • Revision ID: package-import@ubuntu.com-20120522171836-35fi62lp4w7jnrd7
Tags: 3.4.0-1
* New upstream version
* Updated desktop file. Closes: #667472

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
#
2
 
# Gramps - a GTK+/GNOME based genealogy program
3
 
#
4
 
# Copyright (C) 2006  Donald N. Allingham
5
 
# Copyright (C) 2010  Michiel D. Nauta
6
 
#
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.
11
 
#
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.
16
 
#
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
20
 
#
21
 
 
22
 
# $Id: srcbase.py 16425 2011-01-21 16:27:54Z gbritton $
23
 
 
24
 
"""
25
 
SourceBase class for GRAMPS.
26
 
"""
27
 
 
28
 
#-------------------------------------------------------------------------
29
 
#
30
 
# GRAMPS modules
31
 
#
32
 
#-------------------------------------------------------------------------
33
 
from gen.lib.srcref import SourceRef
34
 
from gen.lib.const import IDENTICAL, EQUAL, DIFFERENT
35
 
 
36
 
#-------------------------------------------------------------------------
37
 
#
38
 
# SourceBase classes
39
 
#
40
 
#-------------------------------------------------------------------------
41
 
class SourceBase(object):
42
 
    """
43
 
    Base class for storing source references.
44
 
    """
45
 
    
46
 
    def __init__(self, source=None):
47
 
        """
48
 
        Create a new SourceBase, copying from source if not None.
49
 
        
50
 
        :param source: Object used to initialize the new object
51
 
        :type source: SourceBase
52
 
        """
53
 
 
54
 
        self.source_list = map(SourceRef, source.source_list) if source else []
55
 
 
56
 
    def serialize(self):
57
 
        """
58
 
        Convert the object to a serialized tuple of data.
59
 
        """
60
 
        return [sref.serialize() for sref in self.source_list]
61
 
 
62
 
    def unserialize(self, data):
63
 
        """
64
 
        Convert a serialized tuple of data to an object.
65
 
        """
66
 
        self.source_list = [SourceRef().unserialize(item) for item in data]
67
 
 
68
 
    def add_source_reference(self, src_ref) :
69
 
        """
70
 
        Add a source reference to this object.
71
 
 
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`
75
 
        """
76
 
        self.source_list.append(src_ref)
77
 
 
78
 
    def get_source_references(self) :
79
 
        """
80
 
        Return the list of source references associated with the object.
81
 
 
82
 
        :returns: Returns the list of :class:`~gen.lib.srcref.SourceRef` objects associated with
83
 
            the object.
84
 
        :rtype: list
85
 
        """
86
 
        return self.source_list
87
 
 
88
 
    def get_sourcref_child_list(self):
89
 
        """
90
 
        Return the list of child secondary objects that may refer sources.
91
 
 
92
 
        :returns: Returns the list of child secondary child objects that may 
93
 
                refer sources.
94
 
        :rtype: list
95
 
        """
96
 
        return []
97
 
 
98
 
    def has_source_reference(self, src_handle) :
99
 
        """
100
 
        Return True if the object or any of it's child objects has reference
101
 
        to this source handle.
102
 
 
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.
107
 
        :rtype: bool
108
 
        """
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:
112
 
                return True
113
 
 
114
 
        for item in self.get_sourcref_child_list():
115
 
            if item.has_source_reference(src_handle):
116
 
                return True
117
 
 
118
 
        return False
119
 
 
120
 
    def remove_source_references(self, src_handle_list):
121
 
        """
122
 
        Remove references to all source handles in the list in this object 
123
 
        and all child objects.
124
 
 
125
 
        :param src_handle_list: The list of source handles to be removed.
126
 
        :type src_handle_list: list
127
 
        """
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
131
 
 
132
 
        for item in self.get_sourcref_child_list():
133
 
            item.remove_source_references(src_handle_list)
134
 
 
135
 
    def replace_source_references(self, old_handle, new_handle):
136
 
        """
137
 
        Replace references to source handles in the list in this object and
138
 
        all child objects and merge equivalent entries.
139
 
 
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
144
 
        """
145
 
        refs_list = [ src_ref.ref for src_ref in self.source_list ]
146
 
        new_ref = None
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
154
 
            if new_ref:
155
 
                src_ref = self.source_list[idx]
156
 
                equi = new_ref.is_equivalent(src_ref)
157
 
                if equi != DIFFERENT:
158
 
                    if equi == EQUAL:
159
 
                        new_ref.merge(src_ref)
160
 
                    self.source_list.pop(idx)
161
 
                    refs_list.pop(idx)
162
 
 
163
 
        for item in self.get_sourcref_child_list():
164
 
            item.replace_source_references(old_handle, new_handle)
165
 
 
166
 
    def set_source_reference_list(self, src_ref_list) :
167
 
        """
168
 
        Assign the passed list to the object's list of source references.
169
 
 
170
 
        :param src_ref_list: List of source references to ba associated
171
 
            with the object
172
 
        :type src_ref_list: list of :class:`~gen.lib.srcref.SourceRef` instances
173
 
        """
174
 
        self.source_list = src_ref_list
175
 
 
176
 
    def _merge_source_reference_list(self, acquisition):
177
 
        """
178
 
        Merge the list of source references from acquisition with our own.
179
 
 
180
 
        :param acquisition: the source references list of this object will be
181
 
            merged with the current source references list.
182
 
        :rtype acquisition: SourceRef
183
 
        """
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:
189
 
                    break
190
 
                elif equi == EQUAL:
191
 
                    srcref.merge(addendum)
192
 
                    break
193
 
            else:
194
 
                self.source_list.append(addendum)