|
1 | 1 | """Gramps utility functions.""" |
2 | 2 |
|
3 | | -from typing import Dict, Optional |
| 3 | +from typing import Dict, Optional, Union |
4 | 4 |
|
5 | 5 | from gramps.gen.const import GRAMPS_LOCALE |
6 | 6 | from gramps.gen.db.base import DbReadBase |
|
24 | 24 | dd = GRAMPS_LOCALE.date_displayer |
25 | 25 |
|
26 | 26 |
|
27 | | -def get_person_by_handle(db_handle: DbReadBase, handle: Handle) -> Optional[Person]: |
| 27 | +def get_person_by_handle(db_handle: DbReadBase, handle: Handle) -> Union[Person, Dict]: |
28 | 28 | """Safe get person by handle.""" |
29 | 29 | try: |
30 | 30 | return db_handle.get_person_from_handle(handle) |
31 | 31 | except HandleError: |
32 | | - return None |
| 32 | + return {} |
33 | 33 |
|
34 | 34 |
|
35 | | -def get_place_by_handle(db_handle: DbReadBase, handle: Handle) -> Optional[Place]: |
| 35 | +def get_place_by_handle(db_handle: DbReadBase, handle: Handle) -> Union[Place, Dict]: |
36 | 36 | """Safe get place by handle.""" |
37 | 37 | try: |
38 | 38 | return db_handle.get_place_from_handle(handle) |
39 | 39 | except HandleError: |
40 | | - return None |
| 40 | + return {} |
41 | 41 |
|
42 | 42 |
|
43 | 43 | def get_family_by_handle( |
44 | 44 | db_handle: DbReadBase, handle: Handle, args: Optional[Dict] = None |
45 | | -) -> Optional[Family]: |
| 45 | +) -> Union[Family, Dict]: |
46 | 46 | """Get a family and optional extended attributes.""" |
47 | 47 | try: |
48 | 48 | obj = db_handle.get_family_from_handle(handle) |
49 | 49 | except HandleError: |
50 | | - return None |
| 50 | + return {} |
51 | 51 | args = args or {} |
52 | 52 | if "extend" in args: |
53 | 53 | obj.extended = get_extended_attributes(db_handle, obj, args) |
@@ -81,20 +81,18 @@ def get_sex_profile(person: Person) -> str: |
81 | 81 | def get_event_profile_for_object(db_handle: DbReadBase, event: Event) -> Dict: |
82 | 82 | """Get event profile given an Event.""" |
83 | 83 | return { |
84 | | - "type": str(event.type), |
| 84 | + "type": event.type.xml_str(), |
85 | 85 | "date": dd.display(event.date), |
86 | 86 | "place": pd.display_event(db_handle, event), |
87 | 87 | } |
88 | 88 |
|
89 | 89 |
|
90 | | -def get_event_profile_for_handle( |
91 | | - db_handle: DbReadBase, handle: Handle |
92 | | -) -> Optional[Dict]: |
| 90 | +def get_event_profile_for_handle(db_handle: DbReadBase, handle: Handle) -> Dict: |
93 | 91 | """Get event profile given a handle.""" |
94 | 92 | try: |
95 | 93 | obj = db_handle.get_event_from_handle(handle) |
96 | 94 | except HandleError: |
97 | | - return None |
| 95 | + return {} |
98 | 96 | return get_event_profile_for_object(db_handle, obj) |
99 | 97 |
|
100 | 98 |
|
@@ -173,12 +171,12 @@ def get_person_profile_for_handle( |
173 | 171 | handle: Handle, |
174 | 172 | with_family: bool = True, |
175 | 173 | with_events: bool = True, |
176 | | -) -> Optional[Person]: |
| 174 | +) -> Union[Person, Dict]: |
177 | 175 | """Get person profile given a handle.""" |
178 | 176 | try: |
179 | 177 | obj = db_handle.get_person_from_handle(handle) |
180 | 178 | except HandleError: |
181 | | - return None |
| 179 | + return {} |
182 | 180 | return get_person_profile_for_object(db_handle, obj, with_family, with_events) |
183 | 181 |
|
184 | 182 |
|
@@ -214,12 +212,12 @@ def get_family_profile_for_object( |
214 | 212 |
|
215 | 213 | def get_family_profile_for_handle( |
216 | 214 | db_handle: DbReadBase, handle: Handle, with_events: bool = True |
217 | | -) -> Optional[Family]: |
| 215 | +) -> Union[Family, Dict]: |
218 | 216 | """Get family profile given a handle.""" |
219 | 217 | try: |
220 | 218 | obj = db_handle.get_family_from_handle(handle) |
221 | 219 | except HandleError: |
222 | | - return None |
| 220 | + return {} |
223 | 221 | return get_family_profile_for_object(db_handle, obj, with_events) |
224 | 222 |
|
225 | 223 |
|
|
0 commit comments