Package gen :: Package lib :: Module family
[frames] | no frames]

Source Code for Module gen.lib.family

  1  # 
  2  # Gramps - a GTK+/GNOME based genealogy program 
  3  # 
  4  # Copyright (C) 2000-2007  Donald N. Allingham 
  5  # 
  6  # This program is free software; you can redistribute it and/or modify 
  7  # it under the terms of the GNU General Public License as published by 
  8  # the Free Software Foundation; either version 2 of the License, or 
  9  # (at your option) any later version. 
 10  # 
 11  # This program is distributed in the hope that it will be useful, 
 12  # but WITHOUT ANY WARRANTY; without even the implied warranty of 
 13  # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
 14  # GNU General Public License for more details. 
 15  # 
 16  # You should have received a copy of the GNU General Public License 
 17  # along with this program; if not, write to the Free Software 
 18  # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA 
 19  # 
 20   
 21  # $Id: family.py 10103 2008-02-24 13:55:55Z acraphae $ 
 22   
 23  """ 
 24  Family object for GRAMPS. 
 25  """ 
 26   
 27  #------------------------------------------------------------------------- 
 28  # 
 29  # standard python modules 
 30  # 
 31  #------------------------------------------------------------------------- 
 32  from warnings import warn 
 33  from types import InstanceType 
 34   
 35  #------------------------------------------------------------------------- 
 36  # 
 37  # GRAMPS modules 
 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  # Family class 
 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
79 - def __init__(self):
80 """ 81 Create a new Family instance. 82 83 After initialization, most data items have empty or null values, 84 including the database handle. 85 """ 86 PrimaryObject.__init__(self) 87 SourceBase.__init__(self) 88 NoteBase.__init__(self) 89 MediaBase.__init__(self) 90 AttributeBase.__init__(self) 91 LdsOrdBase.__init__(self) 92 self.father_handle = None 93 self.mother_handle = None 94 self.child_ref_list = [] 95 self.type = FamilyRelType() 96 self.event_ref_list = [] 97 self.complete = 0
98
99 - def serialize(self):
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
129 - def unserialize(self, data):
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
153 - def _has_handle_reference(self, classname, handle):
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
175 - def _remove_handle_references(self, classname, handle_list):
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
201 - def _replace_handle_reference(self, classname, old_handle, new_handle):
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
233 - def get_text_data_list(self):
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
242 - def get_text_data_child_list(self):
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
253 - def get_sourcref_child_list(self):
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
265 - def get_note_child_list(self):
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
277 - def get_referenced_handles(self):
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
293 - def get_handle_referents(self):
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
303 - def set_relationship(self, relationship_type):
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
331 - def get_relationship(self):
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
338 - def set_father_handle(self, person_handle):
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
351 - def get_father_handle(self):
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
361 - def set_mother_handle(self, person_handle):
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
374 - def get_mother_handle(self):
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
384 - def add_child_ref(self, child_ref):
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
395 - def remove_child_ref(self, child_ref):
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
412 - def remove_child_handle(self, child_handle):
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
427 - def get_child_ref_list(self):
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
438 - def set_child_ref_list(self, child_ref_list):
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
448 - def add_event_ref(self, event_ref):
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
463 - def get_event_list(self) :
464 warn( "Use get_event_ref_list instead of get_event_list", 465 DeprecationWarning, 2) 466 # Wrapper for old API 467 # remove when transitition done. 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
473 - def get_event_ref_list(self) :
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
484 - def set_event_ref_list(self, event_ref_list) :
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