1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23 """
24 SourceBase class for GRAMPS.
25 """
26
27
28
29
30
31
32 from types import InstanceType
33
34
35
36
37
38
39 from gen.lib.srcref import SourceRef
40
41
42
43
44
45
47 """
48 Base class for storing source references.
49 """
50
52 """
53 Create a new SourceBase, copying from source if not None.
54
55 @param source: Object used to initialize the new object
56 @type source: SourceBase
57 """
58 if source:
59 self.source_list = [SourceRef(sref) for sref in source.source_list]
60 else:
61 self.source_list = []
62
64 """
65 Convert the object to a serialized tuple of data.
66 """
67 return [sref.serialize() for sref in self.source_list]
68
70 """
71 Convert a serialized tuple of data to an object.
72 """
73 self.source_list = [InstanceType(SourceRef).unserialize(item)
74 for item in data]
75
77 """
78 Add a source reference to this object.
79
80 @param src_ref: The source reference to be added to the
81 SourceNote's list of source references.
82 @type src_ref: L{SourceRef}
83 """
84 self.source_list.append(src_ref)
85
87 """
88 Return the list of source references associated with the object.
89
90 @return: Returns the list of L{SourceRef} objects assocated with
91 the object.
92 @rtype: list
93 """
94 return self.source_list
95
97 """
98 Return the list of child secondary objects that may refer sources.
99
100 @return: Returns the list of child secondary child objects that may
101 refer sources.
102 @rtype: list
103 """
104 return []
105
107 """
108 Return True if the object or any of it's child objects has reference
109 to this source handle.
110
111 @param src_handle: The source handle to be checked.
112 @type src_handle: str
113 @return: Returns whether the object or any of it's child objects has
114 reference to this source handle.
115 @rtype: bool
116 """
117 for src_ref in self.source_list:
118
119 if src_ref.ref == src_handle:
120 return True
121
122 for item in self.get_sourcref_child_list():
123 if item.has_source_reference(src_handle):
124 return True
125
126 return False
127
129 """
130 Remove references to all source handles in the list in this object
131 and all child objects.
132
133 @param src_handle_list: The list of source handles to be removed.
134 @type src_handle_list: list
135 """
136 new_source_list = [ src_ref for src_ref in self.source_list \
137 if src_ref.ref not in src_handle_list ]
138 self.source_list = new_source_list
139
140 for item in self.get_sourcref_child_list():
141 item.remove_source_references(src_handle_list)
142
144 """
145 Replace references to source handles in the list in this object and
146 all child objects.
147
148 @param old_handle: The source handle to be replaced.
149 @type old_handle: str
150 @param new_handle: The source handle to replace the old one with.
151 @type new_handle: str
152 """
153 refs_list = [ src_ref.ref for src_ref in self.source_list ]
154 n_replace = refs_list.count(old_handle)
155 for ix_replace in xrange(n_replace):
156 ix = refs_list.index(old_handle)
157 self.source_list[ix].ref = new_handle
158 refs_list[ix] = new_handle
159
160 for item in self.get_sourcref_child_list():
161 item.replace_source_references(old_handle, new_handle)
162
164 """
165 Assign the passed list to the object's list of source references.
166
167 @param src_ref_list: List of source references to ba associated
168 with the object
169 @type src_ref_list: list of L{SourceRef} instances
170 """
171 self.source_list = src_ref_list
172