Merge pull request #1425 from cdhorn/ch-pylint-gen-utils-db
This commit is contained in:
commit
81c1189141
@ -21,75 +21,86 @@
|
|||||||
"""
|
"""
|
||||||
Utilities for getting information from the database.
|
Utilities for getting information from the database.
|
||||||
"""
|
"""
|
||||||
#-------------------------------------------------------------------------
|
|
||||||
|
# -------------------------------------------------------------------------
|
||||||
#
|
#
|
||||||
# Standard python modules
|
# Standard python modules
|
||||||
#
|
#
|
||||||
#-------------------------------------------------------------------------
|
# -------------------------------------------------------------------------
|
||||||
import logging
|
import logging
|
||||||
LOG = logging.getLogger(".gui.utils.db")
|
|
||||||
|
|
||||||
#-------------------------------------------------------------------------
|
# -------------------------------------------------------------------------
|
||||||
#
|
#
|
||||||
# Gramps modules
|
# Gramps modules
|
||||||
#
|
#
|
||||||
#-------------------------------------------------------------------------
|
# -------------------------------------------------------------------------
|
||||||
from ..lib.nameorigintype import NameOriginType
|
from ..const import GRAMPS_LOCALE as glocale
|
||||||
from ..lib.surname import Surname
|
|
||||||
from ..display.name import displayer as name_displayer
|
from ..display.name import displayer as name_displayer
|
||||||
from ..display.place import displayer as place_displayer
|
from ..display.place import displayer as place_displayer
|
||||||
from ..const import GRAMPS_LOCALE as glocale
|
from ..lib import EventType, EventRoleType, NameOriginType, Surname
|
||||||
|
|
||||||
_ = glocale.translation.sgettext
|
_ = glocale.translation.sgettext
|
||||||
|
|
||||||
#-------------------------------------------------------------------------
|
LOG = logging.getLogger(".gui.utils.db")
|
||||||
|
|
||||||
|
PRIMARY_EVENT_ROLES = [EventRoleType.FAMILY, EventRoleType.PRIMARY]
|
||||||
|
|
||||||
|
|
||||||
|
# -------------------------------------------------------------------------
|
||||||
#
|
#
|
||||||
# Fallback functions
|
# Fallback functions
|
||||||
#
|
#
|
||||||
#-------------------------------------------------------------------------
|
# -------------------------------------------------------------------------
|
||||||
def get_birth_or_fallback(db, person, format=None):
|
def get_birth_or_fallback(db, person, format=None):
|
||||||
"""
|
"""
|
||||||
Get BIRTH event from a person, or fallback to an event around
|
Get BIRTH event from a person, or fallback to an event around
|
||||||
the time of birth.
|
the time of birth.
|
||||||
"""
|
"""
|
||||||
birth_ref = person.get_birth_ref()
|
birth_ref = person.get_birth_ref()
|
||||||
if birth_ref: # regular birth found
|
if birth_ref: # regular birth found
|
||||||
event = db.get_event_from_handle(birth_ref.ref)
|
event = db.get_event_from_handle(birth_ref.ref)
|
||||||
if event:
|
if event:
|
||||||
return event
|
return event
|
||||||
|
|
||||||
# now search the event list for fallbacks
|
# now search the event list for fallbacks
|
||||||
for event_ref in person.get_primary_event_ref_list():
|
for event_ref in person.get_primary_event_ref_list():
|
||||||
if event_ref:
|
event = db.get_event_from_handle(event_ref.ref)
|
||||||
event = db.get_event_from_handle(event_ref.ref)
|
if (
|
||||||
if (event
|
event
|
||||||
and event.type.is_birth_fallback()
|
and event.type.is_birth_fallback()
|
||||||
and event_ref.role.is_primary()):
|
and event_ref.role.is_primary()
|
||||||
if format:
|
):
|
||||||
event.date.format = format
|
if format:
|
||||||
return event
|
event.date.format = format
|
||||||
|
return event
|
||||||
return None
|
return None
|
||||||
|
|
||||||
|
|
||||||
def get_death_or_fallback(db, person, format=None):
|
def get_death_or_fallback(db, person, format=None):
|
||||||
"""
|
"""
|
||||||
Get a DEATH event from a person, or fallback to an
|
Get a DEATH event from a person, or fallback to an
|
||||||
event around the time of death.
|
event around the time of death.
|
||||||
"""
|
"""
|
||||||
death_ref = person.get_death_ref()
|
death_ref = person.get_death_ref()
|
||||||
if death_ref: # regular death found
|
if death_ref: # regular death found
|
||||||
event = db.get_event_from_handle(death_ref.ref)
|
event = db.get_event_from_handle(death_ref.ref)
|
||||||
if event:
|
if event:
|
||||||
return event
|
return event
|
||||||
|
|
||||||
# now search the event list for fallbacks
|
# now search the event list for fallbacks
|
||||||
for event_ref in person.get_primary_event_ref_list():
|
for event_ref in person.get_primary_event_ref_list():
|
||||||
if event_ref:
|
event = db.get_event_from_handle(event_ref.ref)
|
||||||
event = db.get_event_from_handle(event_ref.ref)
|
if (
|
||||||
if (event
|
event
|
||||||
and event.type.is_death_fallback()
|
and event.type.is_death_fallback()
|
||||||
and event_ref.role.is_primary()):
|
and event_ref.role.is_primary()
|
||||||
if format:
|
):
|
||||||
event.date.format = format
|
if format:
|
||||||
return event
|
event.date.format = format
|
||||||
|
return event
|
||||||
return None
|
return None
|
||||||
|
|
||||||
|
|
||||||
def get_age(db, person, fallback=True, calendar="gregorian"):
|
def get_age(db, person, fallback=True, calendar="gregorian"):
|
||||||
"""
|
"""
|
||||||
Compute the age of person.
|
Compute the age of person.
|
||||||
@ -108,24 +119,23 @@ def get_age(db, person, fallback=True, calendar="gregorian"):
|
|||||||
death = get_death_or_fallback(db, person)
|
death = get_death_or_fallback(db, person)
|
||||||
else:
|
else:
|
||||||
birth_ref = person.get_birth_ref()
|
birth_ref = person.get_birth_ref()
|
||||||
if birth_ref: # regular birth found
|
if birth_ref: # regular birth found
|
||||||
event = db.get_event_from_handle(birth_ref.ref)
|
birth = db.get_event_from_handle(birth_ref.ref)
|
||||||
if event:
|
|
||||||
birth = event
|
|
||||||
death_ref = person.get_death_ref()
|
death_ref = person.get_death_ref()
|
||||||
if death_ref: # regular death found
|
if death_ref: # regular death found
|
||||||
event = db.get_event_from_handle(death_ref.ref)
|
death = db.get_event_from_handle(death_ref.ref)
|
||||||
if event:
|
|
||||||
death = event
|
|
||||||
age = None
|
age = None
|
||||||
if birth is not None:
|
if birth is not None:
|
||||||
birth_date = birth.get_date_object().to_calendar("gregorian")
|
birth_date = birth.get_date_object().to_calendar("gregorian")
|
||||||
if (birth_date and birth_date.get_valid()
|
if birth_date and birth_date.get_valid() and not birth_date.is_empty():
|
||||||
and not birth_date.is_empty()):
|
|
||||||
if death is not None:
|
if death is not None:
|
||||||
death_date = death.get_date_object().to_calendar("gregorian")
|
death_date = death.get_date_object().to_calendar("gregorian")
|
||||||
if (death_date and death_date.get_valid()
|
if (
|
||||||
and not death_date.is_empty()):
|
death_date
|
||||||
|
and death_date.get_valid()
|
||||||
|
and not death_date.is_empty()
|
||||||
|
):
|
||||||
age = death_date - birth_date
|
age = death_date - birth_date
|
||||||
if not age.is_valid():
|
if not age.is_valid():
|
||||||
age = None
|
age = None
|
||||||
@ -133,6 +143,7 @@ def get_age(db, person, fallback=True, calendar="gregorian"):
|
|||||||
age = age.tuple()
|
age = age.tuple()
|
||||||
return age
|
return age
|
||||||
|
|
||||||
|
|
||||||
def get_timeperiod(db, person):
|
def get_timeperiod(db, person):
|
||||||
"""
|
"""
|
||||||
Compute the timeperiod a person lived in
|
Compute the timeperiod a person lived in
|
||||||
@ -143,114 +154,118 @@ def get_timeperiod(db, person):
|
|||||||
if isinstance(person, str):
|
if isinstance(person, str):
|
||||||
# a handle is passed
|
# a handle is passed
|
||||||
person = db.get_person_from_handle(person)
|
person = db.get_person_from_handle(person)
|
||||||
|
|
||||||
# the period is the year of birth
|
# the period is the year of birth
|
||||||
birth = get_birth_or_fallback(db, person)
|
birth = get_birth_or_fallback(db, person)
|
||||||
if birth is not None:
|
if birth is not None:
|
||||||
birth_date = birth.get_date_object().to_calendar("gregorian")
|
birth_date = birth.get_date_object().to_calendar("gregorian")
|
||||||
if (birth_date and birth_date.get_valid()
|
if birth_date and birth_date.get_valid() and not birth_date.is_empty():
|
||||||
and not birth_date.is_empty()):
|
|
||||||
return birth_date.get_year()
|
return birth_date.get_year()
|
||||||
|
|
||||||
death = get_death_or_fallback(db, person)
|
death = get_death_or_fallback(db, person)
|
||||||
# no birth, period is death - 20
|
# no birth, period is death - 20
|
||||||
if death is not None:
|
if death is not None:
|
||||||
death_date = death.get_date_object().to_calendar("gregorian")
|
death_date = death.get_date_object().to_calendar("gregorian")
|
||||||
if (death_date and death_date.get_valid()
|
if death_date and death_date.get_valid() and not death_date.is_empty():
|
||||||
and not death_date.is_empty()):
|
|
||||||
return death_date.get_year() - 20
|
return death_date.get_year() - 20
|
||||||
|
|
||||||
# no birth and death, look for another event date we can use
|
# no birth and death, look for another event date we can use
|
||||||
for event_ref in person.get_primary_event_ref_list():
|
for event_ref in person.get_primary_event_ref_list():
|
||||||
if event_ref:
|
event = db.get_event_from_handle(event_ref.ref)
|
||||||
event = db.get_event_from_handle(event_ref.ref)
|
if event:
|
||||||
if event:
|
event_date = event.get_date_object().to_calendar("gregorian")
|
||||||
event_date = event.get_date_object().to_calendar("gregorian")
|
if (
|
||||||
if (event_date and event_date.get_valid()
|
event_date
|
||||||
and not event_date.is_empty()):
|
and event_date.get_valid()
|
||||||
return event_date.get_year()
|
and not event_date.is_empty()
|
||||||
|
):
|
||||||
|
return event_date.get_year()
|
||||||
return None
|
return None
|
||||||
|
|
||||||
|
|
||||||
def get_event_ref(db, family, event_type):
|
def get_event_ref(db, family, event_type):
|
||||||
"""
|
"""
|
||||||
Return a reference to a primary family event of the given event type.
|
Return a reference to a primary family event of the given event type.
|
||||||
"""
|
"""
|
||||||
from ..lib.eventroletype import EventRoleType
|
|
||||||
if family:
|
if family:
|
||||||
for event_ref in family.get_event_ref_list():
|
for event_ref in family.get_event_ref_list():
|
||||||
event = db.get_event_from_handle(event_ref.ref)
|
event = db.get_event_from_handle(event_ref.ref)
|
||||||
if (event and event.get_type() == event_type and
|
if (
|
||||||
(event_ref.get_role() == EventRoleType.FAMILY or
|
event
|
||||||
event_ref.get_role() == EventRoleType.PRIMARY)):
|
and event.get_type() == event_type
|
||||||
|
and event_ref.get_role() in PRIMARY_EVENT_ROLES
|
||||||
|
):
|
||||||
return event_ref
|
return event_ref
|
||||||
return None
|
return None
|
||||||
|
|
||||||
|
|
||||||
def get_primary_event_ref_list(db, family):
|
def get_primary_event_ref_list(db, family):
|
||||||
"""
|
"""
|
||||||
Return a reference to the primary events of the family.
|
Return a reference to the primary events of the family.
|
||||||
"""
|
"""
|
||||||
from ..lib.eventroletype import EventRoleType
|
events = []
|
||||||
retval = []
|
|
||||||
for event_ref in family.get_event_ref_list():
|
for event_ref in family.get_event_ref_list():
|
||||||
event = db.get_event_from_handle(event_ref.ref)
|
event = db.get_event_from_handle(event_ref.ref)
|
||||||
if (event and
|
if event and event_ref.get_role() in PRIMARY_EVENT_ROLES:
|
||||||
(event_ref.get_role() == EventRoleType.FAMILY or
|
events.append(event_ref)
|
||||||
event_ref.get_role() == EventRoleType.PRIMARY)):
|
return events
|
||||||
retval.append(event_ref)
|
|
||||||
return retval
|
|
||||||
|
|
||||||
def get_marriage_or_fallback(db, family, format=None):
|
def get_marriage_or_fallback(db, family, format=None):
|
||||||
"""
|
"""
|
||||||
Get a MARRIAGE event from a family, or fallback to an
|
Get a MARRIAGE event from a family, or fallback to an
|
||||||
alternative event type.
|
alternative event type.
|
||||||
"""
|
"""
|
||||||
from ..lib.eventroletype import EventRoleType
|
|
||||||
from ..lib.eventtype import EventType
|
|
||||||
marriage_ref = get_event_ref(db, family, EventType.MARRIAGE)
|
marriage_ref = get_event_ref(db, family, EventType.MARRIAGE)
|
||||||
if marriage_ref: # regular marriage found
|
if marriage_ref: # regular marriage found
|
||||||
event = db.get_event_from_handle(marriage_ref.ref)
|
event = db.get_event_from_handle(marriage_ref.ref)
|
||||||
if event:
|
if event:
|
||||||
return event
|
return event
|
||||||
|
|
||||||
# now search the event list for fallbacks
|
# now search the event list for fallbacks
|
||||||
for event_ref in get_primary_event_ref_list(db, family):
|
for event_ref in get_primary_event_ref_list(db, family):
|
||||||
if event_ref:
|
event = db.get_event_from_handle(event_ref.ref)
|
||||||
event = db.get_event_from_handle(event_ref.ref)
|
if (
|
||||||
if (event
|
event
|
||||||
and event.type.is_marriage_fallback()
|
and event.type.is_marriage_fallback()
|
||||||
and (event_ref.role == EventRoleType.FAMILY or
|
and event_ref.role in PRIMARY_EVENT_ROLES
|
||||||
event_ref.role == EventRoleType.PRIMARY)):
|
):
|
||||||
if format:
|
if format:
|
||||||
event.date.format = format
|
event.date.format = format
|
||||||
return event
|
return event
|
||||||
return None
|
return None
|
||||||
|
|
||||||
|
|
||||||
def get_divorce_or_fallback(db, family, format=None):
|
def get_divorce_or_fallback(db, family, format=None):
|
||||||
"""
|
"""
|
||||||
Get a DIVORCE event from a family, or fallback to an
|
Get a DIVORCE event from a family, or fallback to an
|
||||||
alternative event type.
|
alternative event type.
|
||||||
"""
|
"""
|
||||||
from ..lib.eventroletype import EventRoleType
|
|
||||||
from ..lib.eventtype import EventType
|
|
||||||
divorce_ref = get_event_ref(db, family, EventType.DIVORCE)
|
divorce_ref = get_event_ref(db, family, EventType.DIVORCE)
|
||||||
if divorce_ref: # regular marriage found
|
if divorce_ref: # regular marriage found
|
||||||
event = db.get_event_from_handle(divorce_ref.ref)
|
event = db.get_event_from_handle(divorce_ref.ref)
|
||||||
if event:
|
if event:
|
||||||
return event
|
return event
|
||||||
|
|
||||||
# now search the event list for fallbacks
|
# now search the event list for fallbacks
|
||||||
for event_ref in get_primary_event_ref_list(db, family):
|
for event_ref in get_primary_event_ref_list(db, family):
|
||||||
if event_ref:
|
event = db.get_event_from_handle(event_ref.ref)
|
||||||
event = db.get_event_from_handle(event_ref.ref)
|
if (
|
||||||
if (event
|
event
|
||||||
and event.type.is_divorce_fallback()
|
and event.type.is_divorce_fallback()
|
||||||
and (event_ref.role == EventRoleType.FAMILY or
|
and event_ref.role in PRIMARY_EVENT_ROLES
|
||||||
event_ref.role == EventRoleType.PRIMARY)):
|
):
|
||||||
if format:
|
if format:
|
||||||
event.date.format = format
|
event.date.format = format
|
||||||
return event
|
return event
|
||||||
return None
|
return None
|
||||||
|
|
||||||
#-------------------------------------------------------------------------
|
|
||||||
|
# -------------------------------------------------------------------------
|
||||||
#
|
#
|
||||||
# Function to return the name of the main participant of an event
|
# Function to return the name of the main participant of an event
|
||||||
#
|
#
|
||||||
#-------------------------------------------------------------------------
|
# -------------------------------------------------------------------------
|
||||||
def get_participant_from_event(db, event_handle, all_=False):
|
def get_participant_from_event(db, event_handle, all_=False):
|
||||||
"""
|
"""
|
||||||
Obtain the first primary or family participant to an event we find in the
|
Obtain the first primary or family participant to an event we find in the
|
||||||
@ -261,22 +276,27 @@ def get_participant_from_event(db, event_handle, all_=False):
|
|||||||
"""
|
"""
|
||||||
participant = ""
|
participant = ""
|
||||||
ellipses = False
|
ellipses = False
|
||||||
result_list = list(db.find_backlink_handles(
|
result_list = list(
|
||||||
event_handle, include_classes=['Person', 'Family']))
|
db.find_backlink_handles(
|
||||||
|
event_handle, include_classes=["Person", "Family"]
|
||||||
|
)
|
||||||
|
)
|
||||||
|
|
||||||
#obtain handles without duplicates
|
# obtain handles without duplicates
|
||||||
people = set([x[1] for x in result_list if x[0] == 'Person'])
|
people = set([x[1] for x in result_list if x[0] == "Person"])
|
||||||
families = set([x[1] for x in result_list if x[0] == 'Family'])
|
families = set([x[1] for x in result_list if x[0] == "Family"])
|
||||||
for personhandle in people:
|
for person_handle in people:
|
||||||
person = db.get_person_from_handle(personhandle)
|
person = db.get_person_from_handle(person_handle)
|
||||||
if not person:
|
if not person:
|
||||||
continue
|
continue
|
||||||
for event_ref in person.get_event_ref_list():
|
for event_ref in person.get_event_ref_list():
|
||||||
if event_handle == event_ref.ref and \
|
if (
|
||||||
event_ref.get_role().is_primary():
|
event_handle == event_ref.ref
|
||||||
|
and event_ref.get_role().is_primary()
|
||||||
|
):
|
||||||
if participant:
|
if participant:
|
||||||
if all_:
|
if all_:
|
||||||
participant += ', %s' % name_displayer.display(person)
|
participant += f", {name_displayer.display(person)}"
|
||||||
else:
|
else:
|
||||||
ellipses = True
|
ellipses = True
|
||||||
else:
|
else:
|
||||||
@ -285,16 +305,18 @@ def get_participant_from_event(db, event_handle, all_=False):
|
|||||||
if ellipses:
|
if ellipses:
|
||||||
break
|
break
|
||||||
if ellipses:
|
if ellipses:
|
||||||
return _('%s, ...') % participant
|
return _("%s, ...") % participant
|
||||||
|
|
||||||
for familyhandle in families:
|
for family_handle in families:
|
||||||
family = db.get_family_from_handle(familyhandle)
|
family = db.get_family_from_handle(family_handle)
|
||||||
for event_ref in family.get_event_ref_list():
|
for event_ref in family.get_event_ref_list():
|
||||||
if event_handle == event_ref.ref and \
|
if (
|
||||||
event_ref.get_role().is_family():
|
event_handle == event_ref.ref
|
||||||
|
and event_ref.get_role().is_family()
|
||||||
|
):
|
||||||
if participant:
|
if participant:
|
||||||
if all_:
|
if all_:
|
||||||
participant += ', %s' % family_name(family, db)
|
participant += f", {family_name(family, db)}"
|
||||||
else:
|
else:
|
||||||
ellipses = True
|
ellipses = True
|
||||||
else:
|
else:
|
||||||
@ -304,151 +326,160 @@ def get_participant_from_event(db, event_handle, all_=False):
|
|||||||
break
|
break
|
||||||
|
|
||||||
if ellipses:
|
if ellipses:
|
||||||
return _('%s, ...') % participant
|
return _("%s, ...") % participant
|
||||||
else:
|
return participant
|
||||||
return participant
|
|
||||||
|
|
||||||
#-------------------------------------------------------------------------
|
|
||||||
|
# -------------------------------------------------------------------------
|
||||||
#
|
#
|
||||||
# Function to return a label to display the active object in the status bar
|
# Function to return a label to display the active object in the status bar
|
||||||
# and to describe bookmarked objects.
|
# and to describe bookmarked objects.
|
||||||
#
|
#
|
||||||
#-------------------------------------------------------------------------
|
# -------------------------------------------------------------------------
|
||||||
def navigation_label(db, nav_type, handle):
|
def navigation_label(db, nav_type, handle_or_obj):
|
||||||
|
"""
|
||||||
|
Return a descriptive label for an object.
|
||||||
|
"""
|
||||||
|
if isinstance(handle_or_obj, str):
|
||||||
|
get_method = db.method("get_%s_from_handle", nav_type)
|
||||||
|
obj = get_method(handle_or_obj)
|
||||||
|
else:
|
||||||
|
obj = handle_or_obj
|
||||||
|
|
||||||
|
if obj is None:
|
||||||
|
return (None, None)
|
||||||
|
|
||||||
label = None
|
label = None
|
||||||
obj = None
|
if nav_type == "Person":
|
||||||
if nav_type == 'Person':
|
label = name_displayer.display(obj)
|
||||||
obj = db.get_person_from_handle(handle)
|
elif nav_type == "Family":
|
||||||
if obj:
|
label = family_name(obj, db)
|
||||||
label = name_displayer.display(obj)
|
elif nav_type == "Event":
|
||||||
elif nav_type == 'Family':
|
who = get_participant_from_event(db, obj.get_handle())
|
||||||
obj = db.get_family_from_handle(handle)
|
desc = obj.get_description()
|
||||||
if obj:
|
label = obj.get_type()
|
||||||
label = family_name(obj, db)
|
if desc:
|
||||||
elif nav_type == 'Event':
|
label = f"{label} - {desc}"
|
||||||
obj = db.get_event_from_handle(handle)
|
if who:
|
||||||
if obj:
|
label = f"{label} - {who}"
|
||||||
who = get_participant_from_event(db, handle)
|
elif nav_type == "Place":
|
||||||
desc = obj.get_description()
|
label = place_displayer.display(db, obj)
|
||||||
label = obj.get_type()
|
elif nav_type == "Source":
|
||||||
if desc:
|
label = obj.get_title()
|
||||||
label = '%s - %s' % (label, desc)
|
elif nav_type == "Citation":
|
||||||
if who:
|
label = obj.get_page()
|
||||||
label = '%s - %s' % (label, who)
|
src = db.get_source_from_handle(obj.get_reference_handle())
|
||||||
elif nav_type == 'Place':
|
if src:
|
||||||
obj = db.get_place_from_handle(handle)
|
label = f"{src.get_title()} {label}"
|
||||||
if obj:
|
elif nav_type == "Repository":
|
||||||
label = place_displayer.display(db, obj)
|
label = obj.get_name()
|
||||||
elif nav_type == 'Source':
|
elif nav_type == "Media":
|
||||||
obj = db.get_source_from_handle(handle)
|
label = obj.get_description()
|
||||||
if obj:
|
elif nav_type == "Note":
|
||||||
label = obj.get_title()
|
label = obj.get()
|
||||||
elif nav_type == 'Citation':
|
# When strings are cut, make sure they are unicode
|
||||||
obj = db.get_citation_from_handle(handle)
|
# otherwise you may end of with cutting within an utf-8 sequence
|
||||||
if obj:
|
label = str(label)
|
||||||
label = obj.get_page()
|
label = " ".join(label.split())
|
||||||
src = db.get_source_from_handle(obj.get_reference_handle())
|
if len(label) > 40:
|
||||||
if src:
|
label = label[:40] + "..."
|
||||||
label = src.get_title() + " " + label
|
|
||||||
elif nav_type == 'Repository':
|
|
||||||
obj = db.get_repository_from_handle(handle)
|
|
||||||
if obj:
|
|
||||||
label = obj.get_name()
|
|
||||||
elif nav_type == 'Media' or nav_type == 'Media':
|
|
||||||
obj = db.get_media_from_handle(handle)
|
|
||||||
if obj:
|
|
||||||
label = obj.get_description()
|
|
||||||
elif nav_type == 'Note':
|
|
||||||
obj = db.get_note_from_handle(handle)
|
|
||||||
if obj:
|
|
||||||
label = obj.get()
|
|
||||||
# When strings are cut, make sure they are unicode
|
|
||||||
#otherwise you may end of with cutting within an utf-8 sequence
|
|
||||||
label = str(label)
|
|
||||||
label = " ".join(label.split())
|
|
||||||
if len(label) > 40:
|
|
||||||
label = label[:40] + "..."
|
|
||||||
|
|
||||||
if label and obj:
|
if label and obj:
|
||||||
label = '[%s] %s' % (obj.get_gramps_id(), label)
|
label = f"[{obj.get_gramps_id()}] {label}"
|
||||||
|
|
||||||
return (label, obj)
|
return (label, obj)
|
||||||
|
|
||||||
#-------------------------------------------------------------------------
|
|
||||||
|
# -------------------------------------------------------------------------
|
||||||
#
|
#
|
||||||
# Function to return children's list of a person
|
# Function to return children's list of a person
|
||||||
#
|
#
|
||||||
#-------------------------------------------------------------------------
|
# -------------------------------------------------------------------------
|
||||||
def find_children(db,p):
|
def find_children(db, person):
|
||||||
"""
|
"""
|
||||||
Return the list of all children's IDs for a person.
|
Return the list of all children's IDs for a person.
|
||||||
"""
|
"""
|
||||||
childlist = []
|
children = set()
|
||||||
for family_handle in p.get_family_handle_list():
|
for family_handle in person.get_family_handle_list():
|
||||||
family = db.get_family_from_handle(family_handle)
|
family = db.get_family_from_handle(family_handle)
|
||||||
if family:
|
if family:
|
||||||
for child_ref in family.get_child_ref_list():
|
for child_ref in family.get_child_ref_list():
|
||||||
childlist.append(child_ref.ref)
|
children.add(child_ref.ref)
|
||||||
return childlist
|
return list(children)
|
||||||
|
|
||||||
#-------------------------------------------------------------------------
|
|
||||||
|
# -------------------------------------------------------------------------
|
||||||
#
|
#
|
||||||
# Function to return parent's list of a person
|
# Function to return parent's list of a person
|
||||||
#
|
#
|
||||||
#-------------------------------------------------------------------------
|
# -------------------------------------------------------------------------
|
||||||
def find_parents(db,p):
|
def find_parents(db, person):
|
||||||
"""
|
"""
|
||||||
Return the unique list of all parents' IDs for a person.
|
Return the unique list of all parents' IDs for a person.
|
||||||
"""
|
"""
|
||||||
parentlist = []
|
parents = set()
|
||||||
for f in p.get_parent_family_handle_list():
|
for family_handle in person.get_parent_family_handle_list():
|
||||||
family = db.get_family_from_handle(f)
|
family = db.get_family_from_handle(family_handle)
|
||||||
father_handle = family.get_father_handle()
|
father_handle = family.get_father_handle()
|
||||||
|
if father_handle:
|
||||||
|
parents.add(father_handle)
|
||||||
mother_handle = family.get_mother_handle()
|
mother_handle = family.get_mother_handle()
|
||||||
if father_handle not in parentlist:
|
if mother_handle:
|
||||||
parentlist.append(father_handle)
|
parents.add(mother_handle)
|
||||||
if mother_handle not in parentlist:
|
return list(parents)
|
||||||
parentlist.append(mother_handle)
|
|
||||||
return parentlist
|
|
||||||
|
|
||||||
#-------------------------------------------------------------------------
|
|
||||||
|
# -------------------------------------------------------------------------
|
||||||
#
|
#
|
||||||
# Function to return persons, that share the same event.
|
# Function to return persons, that share the same event.
|
||||||
# This for example links witnesses to the tree
|
# This for example links witnesses to the tree
|
||||||
#
|
#
|
||||||
#-------------------------------------------------------------------------
|
# -------------------------------------------------------------------------
|
||||||
def find_witnessed_people(db,p):
|
def find_witnessed_people(db, person):
|
||||||
people = []
|
"""
|
||||||
for event_ref in p.get_event_ref_list():
|
Return list of all person handles associated with an event.
|
||||||
for l in db.find_backlink_handles( event_ref.ref):
|
"""
|
||||||
if l[0] == 'Person' and l[1] != p.get_handle() and l[1] not in people:
|
person_handle = person.get_handle()
|
||||||
people.append(l[1])
|
people = {
|
||||||
if l[0] == 'Family':
|
person_ref.ref
|
||||||
fam = db.get_family_from_handle(l[1])
|
for person_ref in person.get_person_ref_list()
|
||||||
if fam:
|
if person_ref.ref != person_handle
|
||||||
father_handle = fam.get_father_handle()
|
}
|
||||||
if father_handle and father_handle != p.get_handle() and father_handle not in people:
|
for event_ref in person.get_event_ref_list():
|
||||||
people.append(father_handle)
|
for backlink in db.find_backlink_handles(
|
||||||
mother_handle = fam.get_mother_handle()
|
event_ref.ref, include_classes=["Person", "Family"]
|
||||||
if mother_handle and mother_handle != p.get_handle() and mother_handle not in people:
|
):
|
||||||
people.append(mother_handle)
|
if backlink[0] == "Person":
|
||||||
for f in p.get_family_handle_list():
|
if backlink[1] != person_handle:
|
||||||
family = db.get_family_from_handle(f)
|
people.add(backlink[1])
|
||||||
for event_ref in family.get_event_ref_list():
|
continue
|
||||||
for l in db.find_backlink_handles( event_ref.ref):
|
if backlink[0] == "Family":
|
||||||
if l[0] == 'Person' and l[1] != p.get_handle() and l[1] not in people:
|
family = db.get_family_from_handle(backlink[1])
|
||||||
people.append(l[1])
|
if family:
|
||||||
for pref in p.get_person_ref_list():
|
father_handle = family.get_father_handle()
|
||||||
if pref.ref != p.get_handle and pref.ref not in people:
|
if father_handle and father_handle != person_handle:
|
||||||
people.append(pref.ref)
|
people.add(father_handle)
|
||||||
return people
|
mother_handle = family.get_mother_handle()
|
||||||
|
if mother_handle and mother_handle != person_handle:
|
||||||
|
people.add(mother_handle)
|
||||||
|
|
||||||
#-------------------------------------------------------------------------
|
for family_handle in person.get_family_handle_list():
|
||||||
|
family = db.get_family_from_handle(family_handle)
|
||||||
|
for event_ref in family.get_event_ref_list():
|
||||||
|
for backlink in db.find_backlink_handles(
|
||||||
|
event_ref.ref, include_classes=["Person"]
|
||||||
|
):
|
||||||
|
if backlink[1] != person_handle:
|
||||||
|
people.add(backlink[1])
|
||||||
|
|
||||||
|
return list(people)
|
||||||
|
|
||||||
|
|
||||||
|
# -------------------------------------------------------------------------
|
||||||
#
|
#
|
||||||
# Iterate over ancestors.
|
# Iterate over ancestors.
|
||||||
#
|
#
|
||||||
#-------------------------------------------------------------------------
|
# -------------------------------------------------------------------------
|
||||||
def for_each_ancestor(db, start, func, data):
|
def for_each_ancestor(db, start, func, data):
|
||||||
"""
|
"""
|
||||||
Recursively iterate (breadth-first) over ancestors of
|
Recursively iterate (breadth-first) over ancestors of
|
||||||
@ -460,95 +491,104 @@ def for_each_ancestor(db, start, func, data):
|
|||||||
todo = start
|
todo = start
|
||||||
done_ids = set()
|
done_ids = set()
|
||||||
while len(todo):
|
while len(todo):
|
||||||
p_handle = todo.pop()
|
person_handle = todo.pop()
|
||||||
p = db.get_person_from_handle(p_handle)
|
|
||||||
# Don't process the same handle twice. This can happen
|
# Don't process the same handle twice. This can happen
|
||||||
# if there is a cycle in the database, or if the
|
# if there is a cycle in the database, or if the
|
||||||
# initial list contains X and some of X's ancestors.
|
# initial list contains X and some of X's ancestors.
|
||||||
if p_handle in done_ids:
|
if person_handle in done_ids:
|
||||||
continue
|
continue
|
||||||
done_ids.add(p_handle)
|
if func(data, person_handle):
|
||||||
if func(data, p_handle):
|
|
||||||
return 1
|
return 1
|
||||||
for fam_handle in p.get_parent_family_handle_list():
|
person = db.get_person_from_handle(person_handle)
|
||||||
fam = db.get_family_from_handle(fam_handle)
|
for family_handle in person.get_parent_family_handle_list():
|
||||||
if fam:
|
family = db.get_family_from_handle(family_handle)
|
||||||
f_handle = fam.get_father_handle()
|
if family:
|
||||||
m_handle = fam.get_mother_handle()
|
father_handle = family.get_father_handle()
|
||||||
if f_handle: todo.append(f_handle)
|
if father_handle:
|
||||||
if m_handle: todo.append(m_handle)
|
todo.append(father_handle)
|
||||||
|
mother_handle = family.get_mother_handle()
|
||||||
|
if mother_handle:
|
||||||
|
todo.append(mother_handle)
|
||||||
|
done_ids.add(person_handle)
|
||||||
return 0
|
return 0
|
||||||
|
|
||||||
#-------------------------------------------------------------------------
|
|
||||||
|
# -------------------------------------------------------------------------
|
||||||
#
|
#
|
||||||
# Preset a name with a name of family member
|
# Preset a name with a name of family member
|
||||||
#
|
#
|
||||||
#-------------------------------------------------------------------------
|
# -------------------------------------------------------------------------
|
||||||
def preset_name(basepers, name, sibling=False):
|
def preset_name(base_person, name, sibling=False):
|
||||||
"""Fill up name with all family common names of basepers.
|
"""
|
||||||
|
Fill up name with all family common names of base person.
|
||||||
If sibling=True, pa/matronymics are retained.
|
If sibling=True, pa/matronymics are retained.
|
||||||
"""
|
"""
|
||||||
surnlist = []
|
surnames = []
|
||||||
primname = basepers.get_primary_name()
|
primary_name = base_person.get_primary_name()
|
||||||
prim = False
|
primary = False
|
||||||
for surn in primname.get_surname_list():
|
for surname in primary_name.get_surname_list():
|
||||||
if (not sibling) and (surn.get_origintype().value in
|
if (not sibling) and (
|
||||||
[NameOriginType.PATRONYMIC,
|
surname.get_origintype().value
|
||||||
NameOriginType.MATRONYMIC]):
|
in [NameOriginType.PATRONYMIC, NameOriginType.MATRONYMIC]
|
||||||
|
):
|
||||||
continue
|
continue
|
||||||
surnlist.append(Surname(source=surn))
|
surnames.append(Surname(source=surname))
|
||||||
if surn.primary:
|
if surname.primary:
|
||||||
prim=True
|
primary = True
|
||||||
if not surnlist:
|
if not surnames:
|
||||||
surnlist = [Surname()]
|
surnames = [Surname()]
|
||||||
name.set_surname_list(surnlist)
|
name.set_surname_list(surnames)
|
||||||
if not prim:
|
if not primary:
|
||||||
name.set_primary_surname(0)
|
name.set_primary_surname(0)
|
||||||
name.set_family_nick_name(primname.get_family_nick_name())
|
name.set_family_nick_name(primary_name.get_family_nick_name())
|
||||||
name.set_group_as(primname.get_group_as())
|
name.set_group_as(primary_name.get_group_as())
|
||||||
name.set_sort_as(primname.get_sort_as())
|
name.set_sort_as(primary_name.get_sort_as())
|
||||||
|
|
||||||
#-------------------------------------------------------------------------
|
|
||||||
|
# -------------------------------------------------------------------------
|
||||||
#
|
#
|
||||||
# Short hand function to return either the person's name, or an empty
|
# Short hand function to return either the person's name, or an empty
|
||||||
# string if the person is None
|
# string if the person is None
|
||||||
#
|
#
|
||||||
#-------------------------------------------------------------------------
|
# -------------------------------------------------------------------------
|
||||||
def family_name(family, db, noname=_("unknown")):
|
def family_name(family, db, noname=_("unknown")):
|
||||||
"""Builds a name for the family from the parents names"""
|
"""
|
||||||
|
Builds a name for the family from the parents names.
|
||||||
|
"""
|
||||||
father = None
|
father = None
|
||||||
mother = None
|
|
||||||
father_handle = family.get_father_handle()
|
father_handle = family.get_father_handle()
|
||||||
mother_handle = family.get_mother_handle()
|
|
||||||
if father_handle:
|
if father_handle:
|
||||||
father = db.get_person_from_handle(father_handle)
|
father = db.get_person_from_handle(father_handle)
|
||||||
|
|
||||||
|
mother = None
|
||||||
|
mother_handle = family.get_mother_handle()
|
||||||
if mother_handle:
|
if mother_handle:
|
||||||
mother = db.get_person_from_handle(mother_handle)
|
mother = db.get_person_from_handle(mother_handle)
|
||||||
|
|
||||||
if father and mother:
|
if father and mother:
|
||||||
fname = name_displayer.display(father)
|
fname = name_displayer.display(father)
|
||||||
mname = name_displayer.display(mother)
|
mname = name_displayer.display(mother)
|
||||||
name = _("%(father)s and %(mother)s") % {
|
return _("%(father)s and %(mother)s") % {
|
||||||
"father" : fname,
|
"father": fname,
|
||||||
"mother" : mname}
|
"mother": mname,
|
||||||
elif father:
|
}
|
||||||
name = name_displayer.display(father)
|
if father:
|
||||||
elif mother:
|
return name_displayer.display(father)
|
||||||
name = name_displayer.display(mother)
|
if mother:
|
||||||
else:
|
return name_displayer.display(mother)
|
||||||
name = noname
|
return noname
|
||||||
return name
|
|
||||||
|
|
||||||
#-------------------------------------------------------------------------
|
|
||||||
|
# -------------------------------------------------------------------------
|
||||||
#
|
#
|
||||||
# Referents functions
|
# Referents functions
|
||||||
#
|
#
|
||||||
#-------------------------------------------------------------------------
|
# -------------------------------------------------------------------------
|
||||||
def get_referents(handle, db, primary_objects):
|
def get_referents(handle, db, primary_objects):
|
||||||
""" Find objects that refer to an object.
|
"""
|
||||||
|
Find objects that refer to an object.
|
||||||
|
|
||||||
This function is the base for other get_<object>_referents functions.
|
This function is the base for other get_<object>_referents functions.
|
||||||
|
|
||||||
"""
|
"""
|
||||||
# Use one pass through the reference map to grab all the references
|
# Use one pass through the reference map to grab all the references
|
||||||
object_list = list(db.find_backlink_handles(handle))
|
object_list = list(db.find_backlink_handles(handle))
|
||||||
@ -558,32 +598,43 @@ def get_referents(handle, db, primary_objects):
|
|||||||
|
|
||||||
for primary in primary_objects:
|
for primary in primary_objects:
|
||||||
primary_list = [item[1] for item in object_list if item[0] == primary]
|
primary_list = [item[1] for item in object_list if item[0] == primary]
|
||||||
the_lists = the_lists + (primary_list, )
|
the_lists = the_lists + (primary_list,)
|
||||||
return the_lists
|
return the_lists
|
||||||
|
|
||||||
|
|
||||||
def get_source_referents(source_handle, db):
|
def get_source_referents(source_handle, db):
|
||||||
""" Find objects that refer the source.
|
"""
|
||||||
|
Find objects that refer the source.
|
||||||
|
|
||||||
This function finds all primary objects that refer (directly or through
|
This function finds all primary objects that refer (directly or through
|
||||||
secondary child-objects) to a given source handle in a given database.
|
secondary child-objects) to a given source handle in a given database.
|
||||||
|
|
||||||
Only Citations can refer to sources, so that is all we need to check
|
Only Citations can refer to sources, so that is all we need to check
|
||||||
"""
|
"""
|
||||||
_primaries = ('Citation',)
|
_primaries = ("Citation",)
|
||||||
|
|
||||||
|
return get_referents(source_handle, db, _primaries)
|
||||||
|
|
||||||
return (get_referents(source_handle, db, _primaries))
|
|
||||||
|
|
||||||
def get_citation_referents(citation_handle, db):
|
def get_citation_referents(citation_handle, db):
|
||||||
""" Find objects that refer the citation.
|
"""
|
||||||
|
Find objects that refer the citation.
|
||||||
|
|
||||||
This function finds all primary objects that refer (directly or through
|
This function finds all primary objects that refer (directly or through
|
||||||
secondary child-objects) to a given citation handle in a given database.
|
secondary child-objects) to a given citation handle in a given database.
|
||||||
|
|
||||||
"""
|
"""
|
||||||
_primaries = ('Person', 'Family', 'Event', 'Place',
|
_primaries = (
|
||||||
'Source', 'Media', 'Repository')
|
"Person",
|
||||||
|
"Family",
|
||||||
|
"Event",
|
||||||
|
"Place",
|
||||||
|
"Source",
|
||||||
|
"Media",
|
||||||
|
"Repository",
|
||||||
|
)
|
||||||
|
|
||||||
|
return get_referents(citation_handle, db, _primaries)
|
||||||
|
|
||||||
return (get_referents(citation_handle, db, _primaries))
|
|
||||||
|
|
||||||
def get_source_and_citation_referents(source_handle, db):
|
def get_source_and_citation_referents(source_handle, db):
|
||||||
"""
|
"""
|
||||||
@ -621,40 +672,50 @@ def get_source_and_citation_referents(source_handle, db):
|
|||||||
| )
|
| )
|
||||||
"""
|
"""
|
||||||
the_lists = get_source_referents(source_handle, db)
|
the_lists = get_source_referents(source_handle, db)
|
||||||
LOG.debug('source referents %s' % [the_lists])
|
LOG.debug("source referents %s", [the_lists])
|
||||||
# now, for each citation, get the objects that refer to that citation
|
# now, for each citation, get the objects that refer to that citation
|
||||||
citation_referents_list = []
|
citation_referents_list = []
|
||||||
for citation in the_lists[0]:
|
for citation in the_lists[0]:
|
||||||
LOG.debug('citation %s' % citation)
|
LOG.debug("citation %s", citation)
|
||||||
refs = get_citation_referents(citation, db)
|
refs = get_citation_referents(citation, db)
|
||||||
citation_referents_list += [(citation, refs)]
|
citation_referents_list += [(citation, refs)]
|
||||||
LOG.debug('citation_referents_list %s' % [citation_referents_list])
|
LOG.debug("citation_referents_list %s", [citation_referents_list])
|
||||||
|
|
||||||
(citation_list) = the_lists
|
(citation_list) = the_lists
|
||||||
the_lists = (citation_list, citation_referents_list)
|
the_lists = (citation_list, citation_referents_list)
|
||||||
|
|
||||||
LOG.debug('the_lists %s' % [the_lists])
|
LOG.debug("the_lists %s", [the_lists])
|
||||||
return the_lists
|
return the_lists
|
||||||
|
|
||||||
|
|
||||||
def get_media_referents(media_handle, db):
|
def get_media_referents(media_handle, db):
|
||||||
""" Find objects that refer the media object.
|
"""
|
||||||
|
Find objects that refer the media object.
|
||||||
|
|
||||||
This function finds all primary objects that refer
|
This function finds all primary objects that refer
|
||||||
to a given media handle in a given database.
|
to a given media handle in a given database.
|
||||||
|
|
||||||
"""
|
"""
|
||||||
_primaries = ('Person', 'Family', 'Event', 'Place', 'Source', 'Citation')
|
_primaries = ("Person", "Family", "Event", "Place", "Source", "Citation")
|
||||||
|
|
||||||
|
return get_referents(media_handle, db, _primaries)
|
||||||
|
|
||||||
return (get_referents(media_handle, db, _primaries))
|
|
||||||
|
|
||||||
def get_note_referents(note_handle, db):
|
def get_note_referents(note_handle, db):
|
||||||
""" Find objects that refer a note object.
|
"""
|
||||||
|
Find objects that refer a note object.
|
||||||
|
|
||||||
This function finds all primary objects that refer
|
This function finds all primary objects that refer
|
||||||
to a given note handle in a given database.
|
to a given note handle in a given database.
|
||||||
|
|
||||||
"""
|
"""
|
||||||
_primaries = ('Person', 'Family', 'Event', 'Place',
|
_primaries = (
|
||||||
'Source', 'Citation', 'Media', 'Repository')
|
"Person",
|
||||||
|
"Family",
|
||||||
|
"Event",
|
||||||
|
"Place",
|
||||||
|
"Source",
|
||||||
|
"Citation",
|
||||||
|
"Media",
|
||||||
|
"Repository",
|
||||||
|
)
|
||||||
|
|
||||||
return (get_referents(note_handle, db, _primaries))
|
return get_referents(note_handle, db, _primaries)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user