GEPS008: Create new module for name utilities

svn: r19909
This commit is contained in:
Nick Hall 2012-06-24 00:15:17 +00:00
parent e484d54bd8
commit 8b297167b1
15 changed files with 138 additions and 97 deletions

View File

@ -325,6 +325,7 @@ src/gen/simple/_simpletable.py
src/gen/utils/alive.py src/gen/utils/alive.py
src/gen/utils/keyword.py src/gen/utils/keyword.py
src/gen/utils/lds.py src/gen/utils/lds.py
src/gen/utils/name.py
src/gen/utils/place.py src/gen/utils/place.py
src/gen/utils/trans.py src/gen/utils/trans.py
src/gen/utils/unknown.py src/gen/utils/unknown.py

View File

@ -53,6 +53,7 @@ from gen.config import config
from const import GRAMPS_UUID, IMAGE_DIR from const import GRAMPS_UUID, IMAGE_DIR
from gen.constfunc import mac, win from gen.constfunc import mac, win
from gen.ggettext import sgettext as _ from gen.ggettext import sgettext as _
from gen.utils.name import family_name
#------------------------------------------------------------------------- #-------------------------------------------------------------------------
# #
@ -156,83 +157,6 @@ def clearHistory_broken():
def wasHistory_broken(): def wasHistory_broken():
return _history_brokenFlag return _history_brokenFlag
#-------------------------------------------------------------------------
#
# Preset a name with a name of family member
#
#-------------------------------------------------------------------------
def preset_name(basepers, name, sibling=False):
"""Fill up name with all family common names of basepers.
If sibling=True, pa/matronymics are retained.
"""
surnlist = []
primname = basepers.get_primary_name()
prim = False
for surn in primname.get_surname_list():
if (not sibling) and (surn.get_origintype().value in
[gen.lib.NameOriginType.PATRONYMIC,
gen.lib.NameOriginType.MATRONYMIC]):
continue
surnlist.append(gen.lib.Surname(source=surn))
if surn.primary:
prim=True
if not surnlist:
surnlist = [gen.lib.Surname()]
name.set_surname_list(surnlist)
if not prim:
name.set_primary_surname(0)
name.set_family_nick_name(primname.get_family_nick_name())
name.set_group_as(primname.get_group_as())
name.set_sort_as(primname.get_sort_as())
#-------------------------------------------------------------------------
#
# Short hand function to return either the person's name, or an empty
# string if the person is None
#
#-------------------------------------------------------------------------
def family_name(family, db, noname=_("unknown")):
"""Builds a name for the family from the parents names"""
father_handle = family.get_father_handle()
mother_handle = family.get_mother_handle()
father = db.get_person_from_handle(father_handle)
mother = db.get_person_from_handle(mother_handle)
if father and mother:
fname = name_displayer.display(father)
mname = name_displayer.display(mother)
name = _("%(father)s and %(mother)s") % {
"father" : fname,
"mother" : mname}
elif father:
name = name_displayer.display(father)
elif mother:
name = name_displayer.display(mother)
else:
name = noname
return name
def family_upper_name(family, db):
"""Builds a name for the family from the parents names"""
father_handle = family.get_father_handle()
mother_handle = family.get_mother_handle()
father = db.get_person_from_handle(father_handle)
mother = db.get_person_from_handle(mother_handle)
if father and mother:
fname = father.get_primary_name().get_upper_name()
mname = mother.get_primary_name().get_upper_name()
name = _("%(father)s and %(mother)s") % {
'father' : fname,
'mother' : mname
}
elif father:
name = father.get_primary_name().get_upper_name()
else:
name = mother.get_primary_name().get_upper_name()
return name
#------------------------------------------------------------------------- #-------------------------------------------------------------------------
# #
# String Encoding functions # String Encoding functions

View File

@ -17,6 +17,7 @@ pkgpython_PYTHON = \
image.py \ image.py \
keyword.py \ keyword.py \
lds.py \ lds.py \
name.py \
mactrans.py \ mactrans.py \
place.py \ place.py \
referent.py \ referent.py \

112
src/gen/utils/name.py Normal file
View File

@ -0,0 +1,112 @@
#
# Gramps - a GTK+/GNOME based genealogy program
#
# Copyright (C) 2000-2007 Donald N. Allingham
# Copyright (C) 2009 Gary Burton
# Copyright (C) 2011 Tim G L Lyons
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
#
# $Id$
"""
Name related utility functions
"""
#-------------------------------------------------------------------------
#
# Gramps modules
#
#-------------------------------------------------------------------------
import gen.lib
from gen.display.name import displayer as name_displayer
from gen.ggettext import sgettext as _
#-------------------------------------------------------------------------
#
# Preset a name with a name of family member
#
#-------------------------------------------------------------------------
def preset_name(basepers, name, sibling=False):
"""Fill up name with all family common names of basepers.
If sibling=True, pa/matronymics are retained.
"""
surnlist = []
primname = basepers.get_primary_name()
prim = False
for surn in primname.get_surname_list():
if (not sibling) and (surn.get_origintype().value in
[gen.lib.NameOriginType.PATRONYMIC,
gen.lib.NameOriginType.MATRONYMIC]):
continue
surnlist.append(gen.lib.Surname(source=surn))
if surn.primary:
prim=True
if not surnlist:
surnlist = [gen.lib.Surname()]
name.set_surname_list(surnlist)
if not prim:
name.set_primary_surname(0)
name.set_family_nick_name(primname.get_family_nick_name())
name.set_group_as(primname.get_group_as())
name.set_sort_as(primname.get_sort_as())
#-------------------------------------------------------------------------
#
# Short hand function to return either the person's name, or an empty
# string if the person is None
#
#-------------------------------------------------------------------------
def family_name(family, db, noname=_("unknown")):
"""Builds a name for the family from the parents names"""
father_handle = family.get_father_handle()
mother_handle = family.get_mother_handle()
father = db.get_person_from_handle(father_handle)
mother = db.get_person_from_handle(mother_handle)
if father and mother:
fname = name_displayer.display(father)
mname = name_displayer.display(mother)
name = _("%(father)s and %(mother)s") % {
"father" : fname,
"mother" : mname}
elif father:
name = name_displayer.display(father)
elif mother:
name = name_displayer.display(mother)
else:
name = noname
return name
def family_upper_name(family, db):
"""Builds a name for the family from the parents names"""
father_handle = family.get_father_handle()
mother_handle = family.get_mother_handle()
father = db.get_person_from_handle(father_handle)
mother = db.get_person_from_handle(mother_handle)
if father and mother:
fname = father.get_primary_name().get_upper_name()
mname = mother.get_primary_name().get_upper_name()
name = _("%(father)s and %(mother)s") % {
'father' : fname,
'mother' : mname
}
elif father:
name = father.get_primary_name().get_upper_name()
else:
name = mother.get_primary_name().get_upper_name()
return name

View File

@ -37,6 +37,7 @@ from gen.ggettext import gettext as _
# #
#------------------------------------------------------------------------- #-------------------------------------------------------------------------
from gen.display.name import displayer as name_displayer from gen.display.name import displayer as name_displayer
from gen.utils.name import family_name
import Utils import Utils
#------------------------------------------------------------------------- #-------------------------------------------------------------------------
@ -84,7 +85,7 @@ class BackRefModel(gtk.ListStore):
continue continue
gid = p.gramps_id gid = p.gramps_id
handle = p.handle handle = p.handle
name = Utils.family_name(p, self.db) name = family_name(p, self.db)
elif dtype == 'Source': elif dtype == 'Source':
p = self.db.get_source_from_handle(ref[1]) p = self.db.get_source_from_handle(ref[1])
if not p: if not p:

View File

@ -83,7 +83,7 @@ from gui.dialog import (ErrorDialog, RunDatabaseRepair, WarningDialog,
MessageHideDialog) MessageHideDialog)
from gen.utils import get_birth_or_fallback, get_death_or_fallback from gen.utils import get_birth_or_fallback, get_death_or_fallback
from gui.selectors import SelectorFactory from gui.selectors import SelectorFactory
from Utils import preset_name from gen.utils.name import preset_name, family_name
SelectPerson = SelectorFactory('Person') SelectPerson = SelectorFactory('Person')
@ -484,7 +484,7 @@ class EditFamily(EditPrimary):
def get_menu_title(self): def get_menu_title(self):
if self.obj and self.obj.get_handle(): if self.obj and self.obj.get_handle():
dialog_title = Utils.family_name(self.obj, self.db, _("New Family")) dialog_title = family_name(self.obj, self.db, _("New Family"))
dialog_title = _("Family") + ': ' + dialog_title dialog_title = _("Family") + ': ' + dialog_title
else: else:
dialog_title = _("New Family") dialog_title = _("New Family")

View File

@ -68,6 +68,7 @@ from gen.filters import rules
from gui.autocomp import StandardCustomSelector, fill_entry from gui.autocomp import StandardCustomSelector, fill_entry
from gui.selectors import SelectorFactory from gui.selectors import SelectorFactory
from gen.display.name import displayer as _nd from gen.display.name import displayer as _nd
from gen.utils.name import family_name
import Utils import Utils
#------------------------------------------------------------------------- #-------------------------------------------------------------------------
@ -339,7 +340,7 @@ class MyID(gtk.HBox):
name = _nd.display_name(person.get_primary_name()) name = _nd.display_name(person.get_primary_name())
elif self.namespace == 'Family': elif self.namespace == 'Family':
family = self.db.get_family_from_gramps_id(gramps_id) family = self.db.get_family_from_gramps_id(gramps_id)
name = Utils.family_name(family, self.db) name = family_name(family, self.db)
elif self.namespace == 'Event': elif self.namespace == 'Event':
event = self.db.get_event_from_gramps_id(gramps_id) event = self.db.get_event_from_gramps_id(gramps_id)
name = str(event.get_type) name = str(event.get_type)
@ -918,7 +919,7 @@ class ShowResults(ManagedWindow):
gid = person.get_gramps_id() gid = person.get_gramps_id()
elif self.namespace == 'Family': elif self.namespace == 'Family':
family = self.db.get_family_from_handle(handle) family = self.db.get_family_from_handle(handle)
name = Utils.family_name(family, self.db) name = family_name(family, self.db)
gid = family.get_gramps_id() gid = family.get_gramps_id()
elif self.namespace == 'Event': elif self.namespace == 'Event':
event = self.db.get_event_from_handle(handle) event = self.db.get_event_from_handle(handle)
@ -957,7 +958,7 @@ class ShowResults(ManagedWindow):
name = self.db.get_person_from_handle(handle).get_primary_name() name = self.db.get_person_from_handle(handle).get_primary_name()
sortname = _nd.sort_string(name) sortname = _nd.sort_string(name)
elif self.namespace == 'Family': elif self.namespace == 'Family':
sortname = Utils.family_name( sortname = family_name(
self.db.get_family_from_handle(handle),self.db) self.db.get_family_from_handle(handle),self.db)
elif self.namespace == 'Event': elif self.namespace == 'Event':
sortname = self.db.get_event_from_handle(handle).get_description() sortname = self.db.get_event_from_handle(handle).get_description()

View File

@ -48,7 +48,7 @@ log = logging.getLogger(".ExportVCal")
# #
#------------------------------------------------------------------------- #-------------------------------------------------------------------------
from gui.plug.export import WriterOptionBox from gui.plug.export import WriterOptionBox
import Utils from gen.utils.name import family_name
from gen.lib import Date, EventType from gen.lib import Date, EventType
from gui.glade import Glade from gui.glade import Glade
@ -133,8 +133,7 @@ class CalendarWriter(object):
m_date = event.get_date_object() m_date = event.get_date_object()
place_handle = event.get_place_handle() place_handle = event.get_place_handle()
# feature requests 2356, 1657: avoid genitive form # feature requests 2356, 1657: avoid genitive form
text = _("Marriage of %s") % Utils.family_name(family, text = _("Marriage of %s") % family_name(family, self.db)
self.db)
if place_handle: if place_handle:
place = self.db.get_place_from_handle(place_handle) place = self.db.get_place_from_handle(place_handle)
self.write_vevent( text, m_date, place.get_title()) self.write_vevent( text, m_date, place.get_title())

View File

@ -30,7 +30,7 @@ from gen.db import PERSON_KEY, FAMILY_KEY, TXNDEL
from gen.plug import Gramplet from gen.plug import Gramplet
from gen.ggettext import sgettext as _ from gen.ggettext import sgettext as _
from gen.display.name import displayer as name_displayer from gen.display.name import displayer as name_displayer
from Utils import family_name from gen.utils.name import family_name
#------------------------------------------------------------------------ #------------------------------------------------------------------------
# #

View File

@ -49,6 +49,7 @@ from gen.db import DbTxn
from gen.db.write import CLASS_TO_KEY_MAP from gen.db.write import CLASS_TO_KEY_MAP
from gen.errors import GrampsImportError from gen.errors import GrampsImportError
import Utils import Utils
from gen.utils.name import family_name
from gen.utils.unknown import make_unknown, create_explanation_note from gen.utils.unknown import make_unknown, create_explanation_note
import gen.datehandler import gen.datehandler
from gen.display.name import displayer as name_displayer from gen.display.name import displayer as name_displayer
@ -2434,7 +2435,7 @@ class GrampsParser(UpdateCallback):
if self.family: if self.family:
text = EVENT_FAMILY_STR % { text = EVENT_FAMILY_STR % {
'event_name' : str(self.event.get_type()), 'event_name' : str(self.event.get_type()),
'family' : Utils.family_name(self.family, self.db), 'family' : family_name(self.family, self.db),
} }
elif self.person: elif self.person:
text = EVENT_PERSON_STR % { text = EVENT_PERSON_STR % {

View File

@ -66,6 +66,7 @@ import gen.lib
from gen.db import DbTxn from gen.db import DbTxn
from gen.config import config from gen.config import config
import Utils import Utils
from gen.utils.name import family_name
from gen.utils.unknown import make_unknown from gen.utils.unknown import make_unknown
from gen.utils.file import (get_unicode_path_from_file_chooser, from gen.utils.file import (get_unicode_path_from_file_chooser,
media_path_full, media_path_full,
@ -1904,7 +1905,7 @@ class CheckIntegrity(object):
cn = _("Non existing child") cn = _("Non existing child")
try: try:
family = self.db.get_family_from_handle(family_handle) family = self.db.get_family_from_handle(family_handle)
pn = Utils.family_name(family, self.db) pn = family_name(family, self.db)
except: except:
pn = _("Unknown") pn = _("Unknown")
self.text.write('\t') self.text.write('\t')
@ -1927,7 +1928,7 @@ class CheckIntegrity(object):
cn = _("Non existing person") cn = _("Non existing person")
family = self.db.get_family_from_handle(family_handle) family = self.db.get_family_from_handle(family_handle)
if family: if family:
pn = Utils.family_name(family, self.db) pn = family_name(family, self.db)
else: else:
pn = family_handle pn = family_handle
self.text.write('\t') self.text.write('\t')
@ -1950,7 +1951,7 @@ class CheckIntegrity(object):
cn = _("Non existing person") cn = _("Non existing person")
family = self.db.get_family_from_handle(family_handle) family = self.db.get_family_from_handle(family_handle)
if family: if family:
pn = Utils.family_name(family, self.db) pn = family_name(family, self.db)
else: else:
pn = _("None") pn = _("None")
self.text.write('\t') self.text.write('\t')

View File

@ -46,7 +46,7 @@ from gen.ggettext import ngettext
from gui.managedwindow import ManagedWindow from gui.managedwindow import ManagedWindow
import gen.lib import gen.lib
from gen.db import DbTxn from gen.db import DbTxn
import Utils from gen.utils.name import family_name
from gui.plug import tool from gui.plug import tool
from gen.display.name import displayer as name_displayer from gen.display.name import displayer as name_displayer
@ -146,7 +146,7 @@ def family_event_name(event, family, dbase):
if not event.get_description(): if not event.get_description():
text = EVENT_FAMILY_STR % { text = EVENT_FAMILY_STR % {
'event_name' : str(event.get_type()), 'event_name' : str(event.get_type()),
'family' : Utils.family_name(family, dbase), 'family' : family_name(family, dbase),
} }
event.set_description(text) event.set_description(text)

View File

@ -57,7 +57,7 @@ import gobject
import const import const
import gen.lib import gen.lib
from gui.editors import EditPerson, EditFamily from gui.editors import EditPerson, EditFamily
import Utils from gen.utils.name import family_name
from gui.display import display_help from gui.display import display_help
from gui.managedwindow import ManagedWindow from gui.managedwindow import ManagedWindow
from gen.updatecallback import UpdateCallback from gen.updatecallback import UpdateCallback
@ -830,7 +830,7 @@ class FamilyRule(Rule):
""" """
TYPE = 'Family' TYPE = 'Family'
def get_name(self): def get_name(self):
return Utils.family_name(self.obj,self.db) return family_name(self.obj,self.db)
#------------------------------------------------------------------------- #-------------------------------------------------------------------------
# #

View File

@ -37,7 +37,7 @@ from gui.views.treemodels.peoplemodel import PersonTreeModel
import gen.lib import gen.lib
from gen.errors import WindowActiveError from gen.errors import WindowActiveError
from gui.editors import EditPerson from gui.editors import EditPerson
from Utils import preset_name from gen.utils.name import preset_name
#------------------------------------------------------------------------- #-------------------------------------------------------------------------
# #

View File

@ -63,7 +63,7 @@ from gui.selectors import SelectorFactory
from gen.errors import WindowActiveError from gen.errors import WindowActiveError
from gui.views.bookmarks import PersonBookmarks from gui.views.bookmarks import PersonBookmarks
import const import const
from Utils import preset_name from gen.utils.name import preset_name
from gen.utils import get_birth_or_fallback, get_death_or_fallback from gen.utils import get_birth_or_fallback, get_death_or_fallback
from gui.listmodel import ListModel from gui.listmodel import ListModel
from gui.managedwindow import ManagedWindow from gui.managedwindow import ManagedWindow