1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23 """
24 Family object for GRAMPS.
25 """
26
27
28
29
30
31
32 from warnings import warn
33 from types import InstanceType
34
35
36
37
38
39
40 from gen.lib.primaryobj import PrimaryObject
41 from gen.lib.srcbase import SourceBase
42 from gen.lib.notebase import NoteBase
43 from gen.lib.mediabase import MediaBase
44 from gen.lib.attrbase import AttributeBase
45 from gen.lib.eventref import EventRef
46 from gen.lib.ldsordbase import LdsOrdBase
47 from gen.lib.childref import ChildRef
48 from gen.lib.familyreltype import FamilyRelType
49 from gen.lib.markertype import MarkerType
50
51
52
53
54
55
56 -class Family(SourceBase, NoteBase, MediaBase, AttributeBase, LdsOrdBase,
57 PrimaryObject):
58 """
59 Introduction
60 ============
61 The Family record is the GRAMPS in-memory representation of the
62 relationships between people. It contains all the information
63 related to the relationship.
64
65 Usage
66 =====
67 Family objects are usually created in one of two ways.
68
69 1. Creating a new Family object, which is then initialized and
70 added to the database.
71 2. Retrieving an object from the database using the records
72 handle.
73
74 Once a Family object has been modified, it must be committed
75 to the database using the database object's commit_family function,
76 or the changes will be lost.
77 """
78
98
100 """
101 Convert the data held in the event to a Python tuple that
102 represents all the data elements.
103
104 This method is used to convert the object into a form that can easily
105 be saved to a database.
106
107 These elements may be primative Python types (string, integers),
108 complex Python types (lists or tuples, or Python objects. If the
109 target database cannot handle complex types (such as objectes or
110 lists), the database is responsible for converting the data into
111 a form that it can use.
112
113 @returns: Returns a python tuple containing the data that should
114 be considered persistent.
115 @rtype: tuple
116 """
117 return (self.handle, self.gramps_id, self.father_handle,
118 self.mother_handle,
119 [cr.serialize() for cr in self.child_ref_list],
120 self.type.serialize(),
121 [er.serialize() for er in self.event_ref_list],
122 MediaBase.serialize(self),
123 AttributeBase.serialize(self),
124 LdsOrdBase.serialize(self),
125 SourceBase.serialize(self),
126 NoteBase.serialize(self),
127 self.change, self.marker.serialize(), self.private)
128
130 """
131 Convert the data held in a tuple created by the serialize method
132 back into the data in a Family structure.
133 """
134 (self.handle, self.gramps_id, self.father_handle, self.mother_handle,
135 child_ref_list, the_type, event_ref_list, media_list,
136 attribute_list, lds_seal_list, source_list, note_list,
137 self.change, marker, self.private) = data
138
139 self.marker = InstanceType(MarkerType)
140 self.marker.unserialize(marker)
141 self.type = InstanceType(FamilyRelType)
142 self.type.unserialize(the_type)
143 self.event_ref_list = [InstanceType(EventRef).unserialize(er)
144 for er in event_ref_list]
145 self.child_ref_list = [InstanceType(ChildRef).unserialize(cr)
146 for cr in child_ref_list]
147 MediaBase.unserialize(self, media_list)
148 AttributeBase.unserialize(self, attribute_list)
149 SourceBase.unserialize(self, source_list)
150 NoteBase.unserialize(self, note_list)
151 LdsOrdBase.unserialize(self, lds_seal_list)
152
154 """
155 Return True if the object has reference to a given handle of given
156 primary object type.
157
158 @param classname: The name of the primary object class.
159 @type classname: str
160 @param handle: The handle to be checked.
161 @type handle: str
162 @return: Returns whether the object has reference to this handle of
163 this object type.
164 @rtype: bool
165 """
166 if classname == 'Event':
167 return handle in [ref.ref for ref in self.event_ref_list]
168 elif classname == 'Person':
169 return handle in ([ref.ref for ref in self.child_ref_list]
170 + [self.father_handle, self.mother_handle])
171 elif classname == 'Place':
172 return handle in [ x.place for x in self.lds_ord_list ]
173 return False
174
176 """
177 Remove all references in this object to object handles in the list.
178
179 @param classname: The name of the primary object class.
180 @type classname: str
181 @param handle_list: The list of handles to be removed.
182 @type handle_list: str
183 """
184 if classname == 'Event':
185 new_list = [ ref for ref in self.event_ref_list \
186 if ref.ref not in handle_list ]
187 self.event_ref_list = new_list
188 elif classname == 'Person':
189 new_list = [ ref for ref in self.child_ref_list \
190 if ref.ref not in handle_list ]
191 self.child_ref_list = new_list
192 if self.father_handle in handle_list:
193 self.father_handle = None
194 if self.mother_handle in handle_list:
195 self.mother_handle = None
196 elif classname == 'Place':
197 for x in self.lds_ord_list:
198 if x.place in handle_list:
199 x.place = None
200
202 """
203 Replace all references to old handle with those to the new handle.
204
205 @param classname: The name of the primary object class.
206 @type classname: str
207 @param old_handle: The handle to be replaced.
208 @type old_handle: str
209 @param new_handle: The handle to replace the old one with.
210 @type new_handle: str
211 """
212 if classname == 'Event':
213 handle_list = [ref.ref for ref in self.event_ref_list]
214 while old_handle in handle_list:
215 ix = handle_list.index(old_handle)
216 self.event_ref_list[ix].ref = new_handle
217 handle_list[ix] = ''
218 elif classname == 'Person':
219 handle_list = [ref.ref for ref in self.child_ref_list]
220 while old_handle in handle_list:
221 ix = handle_list.index(old_handle)
222 self.child_ref_list[ix].ref = new_handle
223 handle_list[ix] = ''
224 if self.father_handle == old_handle:
225 self.father_handle = new_handle
226 if self.mother_handle == old_handle:
227 self.mother_handle = new_handle
228 elif classname == 'Place':
229 for x in self.lds_ord_list:
230 if x.place == old_handle:
231 x.place = new_handle
232
234 """
235 Return the list of all textual attributes of the object.
236
237 @return: Returns the list of all textual attributes of the object.
238 @rtype: list
239 """
240 return [self.gramps_id]
241
243 """
244 Return the list of child objects that may carry textual data.
245
246 @return: Returns the list of child objects that may carry textual data.
247 @rtype: list
248 """
249 add_list = [item for item in self.lds_ord_list if item]
250 return self.media_list + self.attribute_list + \
251 self.source_list + add_list
252
254 """
255 Return the list of child secondary objects that may refer sources.
256
257 @return: Returns the list of child secondary child objects that may
258 refer sources.
259 @rtype: list
260 """
261 check_list = self.media_list + self.attribute_list + \
262 self.lds_ord_list + self.child_ref_list
263 return check_list
264
266 """
267 Return the list of child secondary objects that may refer notes.
268
269 @return: Returns the list of child secondary child objects that may
270 refer notes.
271 @rtype: list
272 """
273 check_list = self.media_list + self.attribute_list + \
274 self.lds_ord_list + self.child_ref_list + self.source_list
275 return check_list
276
278 """
279 Return the list of (classname, handle) tuples for all directly
280 referenced primary objects.
281
282 @return: List of (classname, handle) tuples for referenced objects.
283 @rtype: list
284 """
285 ret = self.get_referenced_note_handles()
286 ret += [('Event', ref.ref) for ref in self.event_ref_list]
287 ret += [('Person', handle) for handle
288 in ([ref.ref for ref in self.child_ref_list] +
289 [self.father_handle, self.mother_handle])
290 if handle]
291 return ret
292
294 """
295 Return the list of child objects which may, directly or through their
296 children, reference primary objects..
297
298 @return: Returns the list of objects refereincing primary objects.
299 @rtype: list
300 """
301 return self.get_sourcref_child_list() + self.source_list
302
304 """
305 Set the relationship type between the people identified as the
306 father and mother in the relationship.
307
308 The type is a tuple whose first item is an integer constant and whose
309 second item is the string. The valid values are:
310
311 - C{FamilyRelType.MARRIED} : indicates a legally recognized married
312 relationship between two individuals. This may be either
313 an opposite or a same sex relationship.
314 - C{FamilyRelType.UNMARRIED} : indicates a relationship between two
315 individuals that is not a legally recognized relationship.
316 - C{FamilyRelType.CIVIL_UNION} : indicates a legally recongnized,
317 non-married relationship between two individuals of the
318 same sex.
319 - C{FamilyRelType.UNKNOWN} : indicates that the type of relationship
320 between the two individuals is not know.
321 - C{FamilyRelType.CUSTOM} : indicates that the type of relationship
322 between the two individuals does not match any of the
323 other types.
324
325 @param relationship_type: (int,str) tuple of the relationship type
326 between the father and mother of the relationship.
327 @type relationship_type: tuple
328 """
329 self.type.set(relationship_type)
330
332 """
333 Return the relationship type between the people identified as the
334 father and mother in the relationship.
335 """
336 return self.type
337
339 """
340 Set the database handle for L{Person} that corresponds to male of the
341 relationship.
342
343 For a same sex relationship, this can represent either of people
344 involved in the relationship.
345
346 @param person_handle: L{Person} database handle
347 @type person_handle: str
348 """
349 self.father_handle = person_handle
350
352 """
353 Return the database handle of the L{Person} identified as the father
354 of the Family.
355
356 @returns: L{Person} database handle
357 @rtype: str
358 """
359 return self.father_handle
360
362 """
363 Set the database handle for L{Person} that corresponds to male of the
364 relationship.
365
366 For a same sex relationship, this can represent either of people
367 involved in the relationship.
368
369 @param person_handle: L{Person} database handle
370 @type person_handle: str
371 """
372 self.mother_handle = person_handle
373
375 """
376 Return the database handle of the L{Person} identified as the mother
377 of the Family.
378
379 @returns: L{Person} database handle
380 @rtype: str
381 """
382 return self.mother_handle
383
385 """
386 Add the database handle for L{Person} to the Family's list of children.
387
388 @param child_ref: Child Reference instance
389 @type child_ref: ChildRef
390 """
391 if not isinstance(child_ref, ChildRef):
392 raise ValueError("expecting ChildRef instance")
393 self.child_ref_list.append(child_ref)
394
396 """
397 Remove the database handle for L{Person} to the Family's list of
398 children if the L{Person} is already in the list.
399
400 @param child_ref: Child Reference instance
401 @type child_ref: ChildRef
402 @return: True if the handle was removed, False if it was not
403 in the list.
404 @rtype: bool
405 """
406 if not isinstance(child_ref, ChildRef):
407 raise ValueError("expecting ChildRef instance")
408 new_list = [ref for ref in self.child_ref_list
409 if ref.ref != child_ref.ref ]
410 self.child_ref_list = new_list
411
413 """
414 Remove the database handle for L{Person} to the Family's list of
415 children if the L{Person} is already in the list.
416
417 @param child_handle: L{Person} database handle
418 @type child_handle: str
419 @return: True if the handle was removed, False if it was not
420 in the list.
421 @rtype: bool
422 """
423 new_list = [ref for ref in self.child_ref_list
424 if ref.ref != child_handle ]
425 self.child_ref_list = new_list
426
428 """
429 Return the list of L{ChildRef} handles identifying the children of the
430 Family.
431
432 @return: Returns the list of L{ChildRef} handles assocated with
433 the Family.
434 @rtype: list
435 """
436 return self.child_ref_list
437
439 """
440 Assign the passed list to the Family's list children.
441
442 @param child_ref_list: List of Child Reference instances to be
443 associated as the Family's list of children.
444 @type child_ref_list: list of L{ChildRef} instances
445 """
446 self.child_ref_list = child_ref_list
447
449 """
450 Add the L{EventRef} to the Family instance's L{EventRef} list.
451
452 This is accomplished by assigning the L{EventRef} for the valid
453 L{Event}in the current database.
454
455 @param event_ref: the L{EventRef} to be added to the
456 Person's L{EventRef} list.
457 @type event_ref: EventRef
458 """
459 if event_ref and not isinstance(event_ref, EventRef):
460 raise ValueError("Expecting EventRef instance")
461 self.event_ref_list.append(event_ref)
462
464 warn( "Use get_event_ref_list instead of get_event_list",
465 DeprecationWarning, 2)
466
467
468 event_handle_list = []
469 for event_ref in self.get_event_ref_list():
470 event_handle_list.append( event_ref.get_reference_handle())
471 return event_handle_list
472
474 """
475 Return the list of L{EventRef} objects associated with L{Event}
476 instances.
477
478 @returns: Returns the list of L{EventRef} objects associated with
479 the Family instance.
480 @rtype: list
481 """
482 return self.event_ref_list
483
485 """
486 Set the Family instance's L{EventRef} list to the passed list.
487
488 @param event_ref_list: List of valid L{EventRef} objects
489 @type event_ref_list: list
490 """
491 self.event_ref_list = event_ref_list
492