1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21 """
22 Provide a simplified database access interface to the GRAMPS database.
23 """
24
25 from types import NoneType
26
27 import gen.lib
28 import DateHandler
29 import Utils
30
31 from BasicUtils import name_displayer
32 from ReportBase import ReportUtils
33 from gen.lib import EventType
34 import Config
35
37 """
38 Provide a simplified database access system. This system has been designed to
39 ease the development of reports.
40
41 The user needs to take care when using this interface. Since it returns real
42 objects instead of database references, it can consume a significant amount
43 of memory if the user is not careful.
44
45 Example
46 =======
47
48 A typical system of usage would be::
49
50 sa = SimpleAccess(database)
51
52 print "Person : ", sa.name(person)
53 print "Gender : ", sa.gender(person)
54 print "Birth date : ", sa.birth_date(person)
55 print "Birth place : ", sa.birth_place(person)
56 print "Death date : ", sa.death_date(person)
57 print "Death place : ", sa.death_place(person)
58 print "Father : ", sa.name(sa.father(person))
59 print "Mother : ", sa.name(sa.mother(person))
60 print "Spouse : ", sa.name(sa.spouse(person))
61 print "Marriage Type : ", sa.marriage_type(person)
62 print "Marriage Date : ", sa.marriage_date(person)
63 print "Marriage Place: ", sa.marriage_place(person)
64 for child in sa.children(person):
65 print "Child : ", sa.name(child)
66
67 # Print out burial and baptism events
68 for event in sa.events( person , [ "Burial", "Baptism" ]):
69 print "Event : ", sa.event_type(event), sa.event_date(event),
70 print sa.event_place(event)
71
72 This would produce an output that looks like::
73
74 Person : Garner, Lewis Anderson
75 Gender : male
76 Birth date : 6/21/1855
77 Birth place : Great Falls, MT
78 Death date : 6/28/1911
79 Death place : Twin Falls, ID
80 Father : Garner, Robert W.
81 Mother : Zielinski, Phoebe Emily
82 Spouse : Martel, Luella Jacques
83 Marriage Type : Married
84 Marriage Date : 4/1/1875
85 Marriage Place: Paragould, AR
86 Child : Garner, Eugene Stanley
87 Child : Garner, Jesse V.
88 Child : Garner, Raymond E.
89 Child : Garner, Jennie S.
90 Child : Garner, Walter E.
91 Child : Garner, Daniel Webster
92 Child : Garner, Bertha P.
93 Child : Garner, Elizabeth
94 Event : Burial 7/1/1911 Twin Falls, ID
95 """
96
98 """
99 Initialize the SimpleAccess object with the database that will be used.
100
101 @param dbase: GRAMPS database object
102 @type dbase: GrampsDbBase
103 """
104 self.dbase = dbase
105
106 - def name(self, person):
107 """
108 Return the name of the person, or and empty string if the person is None
109
110 @param person: Person object
111 @type person: L{gen.lib.Person}
112 @return: Returns the name of the person based of the program preferences
113 @rtype: unicode
114 """
115 assert(isinstance(person, (gen.lib.Person, NoneType)))
116 if person:
117 return name_displayer.display(person)
118 else:
119 return u''
120
122 """
123 Return the name of the person, or and empty string if the person is None
124
125 @param person: Person object
126 @type person: L{gen.lib.Person}
127 @return: Returns the name of the person based of the program preferences
128 @rtype: unicode
129 """
130 assert(isinstance(person, (gen.lib.Person, NoneType)))
131 if person:
132 surname = person.get_primary_name().get_surname()
133 return surname or Config.get(Config.NO_SURNAME_TEXT)
134 else:
135 return u''
136
138 """
139 Return the first name of the person, or and empty string if the person is None
140
141 @param person: Person object
142 @type person: L{gen.lib.Person}
143 @return: Returns the first name of the person based of the program preferences
144 @rtype: unicode
145 """
146 assert(isinstance(person, (gen.lib.Person, NoneType)))
147 if person:
148 return person.get_primary_name().get_first_name()
149 else:
150 return u''
151
152 - def gid(self, obj):
153 """
154 Return the GRAMPS ID of the person or family
155
156 @param obj: Person or Family object
157 @type obj: L{gen.lib.Person} or L{gen.lib.Family}
158 @return: Returns the GRAMPS Id value of the person or family
159 @rtype: unicode
160 """
161 assert(isinstance(obj, (gen.lib.Person, gen.lib.Family, NoneType)))
162 if obj:
163 return obj.get_gramps_id()
164 else:
165 return u''
166
168 """
169 Return a string representing the gender of the person
170
171 @param person: Person object
172 @type person: L{gen.lib.Person}
173 @return: Returns a string indentifying the person's gender
174 @rtype: unicode
175 """
176 assert(isinstance(person, (gen.lib.Person, NoneType)))
177 if person:
178 return Utils.gender[person.get_gender()]
179 return u''
180
182 """
183 Return a person associated as a parent of the person
184
185 @param person: Person object
186 @type person: L{gen.lib.Person}
187 @param func: function used to extract the appropriate parent
188 @type func: function
189 @return: mother or father of the associated person
190 @rtype: L{gen.lib.Person}
191 """
192 assert(isinstance(person, (gen.lib.Person, NoneType)))
193
194 if person:
195 parent_handle_list = person.get_parent_family_handle_list()
196 if parent_handle_list:
197 family_id = parent_handle_list[0]
198 family = self.dbase.get_family_from_handle(family_id)
199 return self.__family_parent(family, func)
200 return None
201
203 """
204 Return a person associated as a parent of the family
205
206 @param family: Family object
207 @type family: L{gen.lib.Family}
208 @param func: function used to extract the appropriate parent
209 @type func: function
210 @return: mother or father of the associated family
211 @rtype: L{gen.lib.Family}
212 """
213 assert(isinstance(family, (gen.lib.Family, NoneType)))
214
215 if family:
216 handle = func(family)
217 if handle:
218 return self.dbase.get_person_from_handle(handle)
219 return None
220
222 """
223 Return a string describing the date associated with the person
224
225 @param person: Person object
226 @type person: L{gen.lib.Person}
227 @param func: function used to extract the associated date information
228 @type func: function
229 @return: Returns a string describing the date
230 @rtype: unicode
231 """
232 assert(isinstance(person, (gen.lib.Person, NoneType)))
233
234 if person:
235 ref = func(person)
236 if ref:
237 event_handle = ref.get_reference_handle()
238 if event_handle:
239 event = self.dbase.get_event_from_handle(event_handle)
240 date_obj = event.get_date_object()
241 if date_obj:
242 return DateHandler.displayer.display(date_obj)
243 return u''
244
246 """
247 Return the date associated with the person
248
249 @param person: Person object
250 @type person: L{gen.lib.Person}
251 @param func: function used to extract the associated date information
252 @type func: function
253 @return: Returns the date
254 @rtype: l{gen.lib.Date}
255 """
256 assert(isinstance(person, (gen.lib.Person, NoneType)))
257
258 if person:
259 ref = func(person)
260 if ref:
261 event_handle = ref.get_reference_handle()
262 if event_handle:
263 event = self.dbase.get_event_from_handle(event_handle)
264 date_obj = event.get_date_object()
265 if date_obj:
266 return date_obj
267 else:
268 return gen.lib.Date()
269 return gen.lib.Date()
270
272 """
273 Return a string describing the place associated with the person
274
275 @param person: Person object
276 @type person: L{gen.lib.Person}
277 @param func: function used to extract the associated place information
278 @type func: function
279 @return: Returns a string describing the place
280 @rtype: unicode
281 """
282 assert(isinstance(person, (gen.lib.Person, NoneType)))
283
284 if person:
285 ref = func(person)
286 if ref:
287 event_handle = ref.get_reference_handle()
288 if event_handle:
289 event = self.dbase.get_event_from_handle(event_handle)
290 place_handle = event.get_place_handle()
291 return ReportUtils.place_name(self.dbase, place_handle)
292 return u''
293
317
319 """
320 Return a string describing the relationship between the person and
321 his/per primary spouse.
322
323 @param person: Person object
324 @type person: L{gen.lib.Person}
325 @return: Returns a string describing the relationship between the person and
326 his/per primary spouse.
327 @rtype: unicode
328 """
329 assert(isinstance(person, (gen.lib.Person, NoneType)))
330
331 if person:
332 family_handle_list = person.get_family_handle_list()
333 if family_handle_list:
334 family_id = family_handle_list[0]
335 family = self.dbase.get_family_from_handle(family_id)
336 if family:
337 return str(family.get_relationship())
338 return u''
339
341 """
342 Return a string describing the place where the person and his/her spouse
343 where married.
344
345 @param person: Person object
346 @type person: L{gen.lib.Person}
347 @return: Returns a string describing the place where the person and his/her spouse
348 where married.
349 @rtype: unicode
350 """
351 assert(isinstance(person, (gen.lib.Person, NoneType)))
352
353 if person:
354 family_handle_list = person.get_family_handle_list()
355 if family_handle_list:
356 family_id = family_handle_list[0]
357 family = self.dbase.get_family_from_handle(family_id)
358 if family:
359 reflist = family.get_event_ref_list()
360 if reflist:
361 elist = [ self.dbase.get_event_from_handle(ref.ref)
362 for ref in reflist ]
363 events = [ evnt for evnt in elist
364 if int(evnt.get_type()) == EventType.MARRIAGE ]
365 if events:
366 place_handle = events[0].get_place_handle()
367 return ReportUtils.place_name(self.dbase, place_handle)
368 return u''
369
400
424
426 """
427 Return the primary father of the person or the father of the associated
428 family.
429
430 @param obj: Person or Family object
431 @type obj: L{gen.lib.Person} or L{gen.lib.Family}
432 @return: The father in the person's primary family or the father of the
433 family
434 @rtype: L{gen.lib.Person}
435 """
436 if isinstance(obj, gen.lib.Person):
437 return self.__parent(obj, gen.lib.Family.get_father_handle)
438 elif isinstance(obj, gen.lib.Family):
439 return self.__family_parent(obj, gen.lib.Family.get_father_handle)
440 else:
441 return None
442
444 """
445 Returns the primary mother of the person or the mother of the associated
446 family.
447
448 @param obj: Person object
449 @type obj: L{gen.lib.Person} or L{gen.lib.Family}
450 @return: The mother in the person's primary family or the mother of the
451 family
452 @rtype: L{gen.lib.Person}
453 """
454 if isinstance(obj, gen.lib.Person):
455 return self.__parent(obj, gen.lib.Family.get_mother_handle)
456 elif isinstance(obj, gen.lib.Family):
457 return self.__family_parent(obj, gen.lib.Family.get_mother_handle)
458 else:
459 return None
460
462 """
463 Return a string indicating the date when the person's birth.
464
465 @param person: Person object
466 @type person: L{gen.lib.Person}
467 @return: Returns a string indicating the date when the person's birth.
468 @rtype: unicode
469 """
470 return self.__event_date(person, gen.lib.Person.get_birth_ref)
471
473 """
474 Return the date when the person's birth.
475
476 @param person: Person object
477 @type person: L{gen.lib.Person}
478 @return: Returns the date when the person's birth.
479 @rtype: L{gen.lib.Date}
480 """
481 return self.__event_date_obj(person, gen.lib.Person.get_birth_ref)
482
484 """
485 Return a string indicating the place of the person's birth.
486
487 @param person: Person object
488 @type person: L{gen.lib.Person}
489 @return: Returns a string indicating the place of the person's birth.
490 @rtype: unicode
491 """
492 return self.__event_place(person, gen.lib.Person.get_birth_ref)
493
495 """
496 Return a string indicating the date when the person's death.
497
498 @param person: Person object
499 @type person: L{gen.lib.Person}
500 @return: Returns a string indicating the date when the person's death.
501 @rtype: unicode
502 """
503 return self.__event_date(person, gen.lib.Person.get_death_ref)
504
506 """
507 Return the date when the person's death.
508
509 @param person: Person object
510 @type person: L{gen.lib.Person}
511 @return: Returns the date when the person's death.
512 @rtype: L{gen.lib.Date}
513 """
514 return self.__event_date_obj(person, gen.lib.Person.get_death_ref)
515
517 """
518 Return a string indicating the place of the person's death.
519
520 @param person: Person object
521 @type person: L{gen.lib.Person}
522 @return: Returns a string indicating the place of the person's death.
523 @rtype: unicode
524 """
525 return self.__event_place(person, gen.lib.Person.get_death_ref)
526
528 """
529 Return a string indicating the place of the event
530
531 @param event: Event object
532 @type event: L{gen.lib.Event}
533 @return: Returns a string indicating the place of the event
534 @rtype: unicode
535 """
536 assert(isinstance(event, (gen.lib.Event, NoneType)))
537
538 if event:
539 place_handle = event.get_place_handle()
540 return ReportUtils.place_name(self.dbase, place_handle)
541 else:
542 return u''
543
545 """
546 Return a string representation a date_obj
547
548 @param date_obj: Date object
549 @type date_obj: L{gen.lib.Date}
550 @return: Returns a string representation a date_obj
551 @rtype: unicode
552 """
553 if date_obj:
554 return DateHandler.displayer.display(date_obj)
555 else:
556 return u''
557
559 """
560 Return a string indicating the date of the event
561
562 @param event: Event object
563 @type event: L{gen.lib.Event}
564 @return: Returns a string indicating the date of the event
565 @rtype: unicode
566 """
567 assert(isinstance(event, (gen.lib.Event, NoneType)))
568 date_obj = event.get_date_object()
569 if date_obj:
570 return DateHandler.displayer.display(date_obj)
571 else:
572 return u''
573
575 """
576 Return a string indicating the type of the event
577
578 @param event: Event object
579 @type event: L{gen.lib.Event}
580 @return: Returns a string indicating the type of the event
581 @rtype: unicode
582 """
583 assert(isinstance(event, (gen.lib.Event, NoneType)))
584 if event:
585 return str(event.get_type())
586 else:
587 return u''
588
589 - def events(self, obj, restrict=None):
590 """
591 Return a list of events associated with the object. This object
592 can be either a L{gen.lib.Person} or L{gen.lib.Family}.
593
594 @param obj: Person or Family
595 @type obj: L{gen.lib.Person} or L{gen.lib.Family}
596 @param restrict: Optional list of strings that will limit the types
597 of events to those of the specfied types.
598 @type restrict: list
599 @return: list of events assocated with the object
600 @rtype: list
601 """
602 assert(isinstance(obj, (gen.lib.Person, gen.lib.Family, NoneType)))
603 assert(type(restrict) == type([]) or restrict == None)
604
605 if obj:
606 event_handles = [ ref.ref for ref in obj.get_event_ref_list() ]
607 events = [ self.dbase.get_event_from_handle(h)
608 for h in event_handles ]
609 if restrict:
610 restrict = [ r.lower() for r in restrict ]
611 events = [ event for event in events
612 if str(event.get_type()).lower() in restrict ]
613 return events
614 else:
615 return []
616
618 """
619 Return a list of events associated with the object. This object
620 can be either a L{gen.lib.Person} or L{gen.lib.Family}.
621
622 @param obj: Person or Family
623 @type obj: L{gen.lib.Person} or L{gen.lib.Family}
624 @return: list of events assocated with the object
625 @rtype: list
626 """
627 assert(isinstance(obj, (gen.lib.Person, gen.lib.Family, gen.lib.Event, NoneType)))
628
629 if obj:
630 handles = [ ref.ref for ref in obj.get_source_references() ]
631 return [ self.dbase.get_source_from_handle(h) for h in handles ]
632 else:
633 return []
634
636 """
637 Return a list of families in which the person is listed as a parent.
638
639 @param person: Person object
640 @type person: L{gen.lib.Person}
641 @return: list of L{gen.lib.Family} objects in which the person is listed
642 as a parent.
643 @rtype: list
644 """
645 assert(isinstance(person, (gen.lib.Person, NoneType)))
646
647 if person:
648 return [ self.dbase.get_family_from_handle(handle)
649 for handle in person.get_family_handle_list() ]
650 return []
651
653 """
654 Return a list of families in which the person is listed as a child.
655
656 @param person: Person object
657 @type person: L{gen.lib.Person}
658 @return: list of L{gen.lib.Family} objects in which the person is listed
659 as a child.
660 @rtype: list
661 """
662 assert(isinstance(person, (gen.lib.Person, NoneType)))
663
664 if person:
665 return [ self.dbase.get_family_from_handle(handle)
666 for handle in person.get_parent_family_handle_list() ]
667 return []
668
670 """
671 Return a all the objects of a particular type in the database, one
672 at a time as an iterator. The user can treat this just like a list.
673
674 @return: list of objects of a particular type in the database
675 @rtype: list
676 """
677 slist = []
678 cursor = gen_cursor()
679 data = cursor.first()
680 while data:
681 slist.append(data[0])
682 data = cursor.next()
683 cursor.close()
684 for info in slist:
685 obj = get_object(info)
686 yield obj
687
689 """
690 Return a all the people in the database, one at a time as an iterator.
691 The user can treat this just like a list. For example::
692
693 for person in sa.all_people():
694 sa.print(person)
695
696 @return: list of people in the database
697 @rtype: list
698 """
699 slist = []
700 cursor = self.dbase.get_person_cursor()
701 data = cursor.first()
702 while data:
703 slist.append((data[1][3][3], data[0]))
704 data = cursor.next()
705 cursor.close()
706 slist.sort()
707 for info in slist:
708 obj = self.dbase.get_person_from_handle(info[1])
709 yield obj
710
712 """
713 Return all the families in the database, one at a time as an iterator.
714 The user can treat this just like a list. For example::
715
716 for person in sa.all_families():
717 sa.print(sa.father(person))
718
719 @return: list of families in the database
720 @rtype: list
721 """
722 return self.__all_objects(self.dbase.get_family_cursor,
723 self.dbase.get_family_from_handle)
724
726 """
727 Return all the events in the database, one at a time as an iterator.
728 The user can treat this just like a list. For example::
729
730 for person in sa.all_events():
731 sa.print(sa.event_place(event))
732
733 @return: list of events in the database
734 @rtype: list
735 """
736 return self.__all_objects(self.dbase.get_events_cursor,
737 self.dbase.get_event_from_handle)
738
740 """
741 Return all the sources in the database, one at a time as an iterator.
742 The user can treat this just like a list. For example::
743
744 @return: list of sources in the database
745 @rtype: list
746 """
747 return self.__all_objects(self.dbase.get_source_cursor,
748 self.dbase.get_source_from_handle)
749
750 - def title(self, source):
751 """
752 Return the title of the source.
753
754 @param source: Source object
755 @type source: L{gen.lib.Source}
756 @return: title of the source
757 @rtype: unicode
758 """
759 assert(isinstance(source, (gen.lib.Source, NoneType)))
760 if source:
761 return source.get_title()
762 return u''
763
765 """
766 Return the author of the source.
767
768 @param source: Source object
769 @type source: L{gen.lib.Source}
770 @return: author of the source
771 @rtype: unicode
772 """
773 assert(isinstance(source, (gen.lib.Source, NoneType)))
774 if source:
775 return source.get_author()
776 return u''
777
779 """
780 Sort function that will compare two events by their dates.
781
782 @param event1: first event
783 @type event1: L{Event}
784 @param event2: second event
785 @type event2: L{Event}
786 @return: Returns -1 if event1 < event2, 0 if they are equal, and
787 1 if they are the same.
788 @rtype: int
789 """
790 return cmp(event1.get_date_object() , event2.get_date_object())
791