gen.lib and gen.utils are independent; removed gen.utils.dbutils --- moved db methods to gen.db.base

svn: r13872
This commit is contained in:
Doug Blank 2009-12-21 04:18:31 +00:00
parent 43dc350b8c
commit 872e971fd3
12 changed files with 251 additions and 344 deletions

View File

@ -432,7 +432,7 @@ class MergePeople(object):
# merge the event lists
self.merge_event_lists(new)
gen.utils.set_birth_death_index(self.db, new)
self.db.set_birth_death_index(new)
# copy attributes
new.set_attribute_list(self.p1.get_attribute_list() +

View File

@ -44,6 +44,7 @@ from gettext import gettext as _
# GRAMPS libraries
#
#-------------------------------------------------------------------------
import gen.lib
from gen.lib import (MediaObject, Person, Family, Source, Event, Place,
Repository, Note, GenderStats, Researcher)
from gen.utils.callback import Callback
@ -1521,6 +1522,8 @@ class GrampsDbBase(object):
def get_birth_or_fallback(self, person):
"""
Get BIRTH event from a person, or fallback to an event around
the time of birth.
"""
birth_ref = person.get_birth_ref()
if birth_ref: # regular birth found
@ -1539,6 +1542,8 @@ class GrampsDbBase(object):
def get_death_or_fallback(self, person):
"""
Get a DEATH event from a person, or fallback to an
event around the time of death.
"""
death_ref = person.get_death_ref()
if death_ref: # regular death found
@ -1555,3 +1560,218 @@ class GrampsDbBase(object):
return event
return None
def add_child_to_family(self, family, child,
mrel=gen.lib.ChildRefType(),
frel=gen.lib.ChildRefType(),
trans=None):
"""
Adds a child to a family.
"""
cref = gen.lib.ChildRef()
cref.ref = child.handle
cref.set_father_relation(frel)
cref.set_mother_relation(mrel)
family.add_child_ref(cref)
child.add_parent_family_handle(family.handle)
if trans is None:
need_commit = True
trans = self.transaction_begin()
else:
need_commit = False
self.commit_family(family,trans)
self.commit_person(child,trans)
if need_commit:
self.transaction_commit(trans, _('Add child to family') )
def remove_child_from_family(self, person_handle, family_handle, trans=None):
"""
Remove a person as a child of the family, deleting the family if
it becomes empty.
"""
person = self.get_person_from_handle(person_handle)
family = self.get_family_from_handle(family_handle)
person.remove_parent_family_handle(family_handle)
family.remove_child_handle(person_handle)
if trans is None:
need_commit = True
trans = self.transaction_begin()
else:
need_commit = False
child_list = family.get_child_ref_list()
if (not family.get_father_handle() and not family.get_mother_handle() and
len(child_list) <= 1):
self.remove_family(family_handle, trans)
if child_list:
child = self.get_person_from_handle(child_list[0].ref)
child.remove_parent_family_handle(family_handle)
self.commit_person(child, trans)
else:
self.commit_family(family, trans)
self.commit_person(person, trans)
if need_commit:
self.transaction_commit(trans,_("Remove child from family"))
def delete_person_from_database(self, person, trans):
"""
Deletes a person from the database, cleaning up all associated references.
"""
# clear out the default person if the person is the default person
if self.get_default_person() == person:
self.set_default_person_handle(None)
# loop through the family list
for family_handle in [ f for f in person.get_family_handle_list() if f ]:
family = self.get_family_from_handle(family_handle)
if person.get_handle() == family.get_father_handle():
family.set_father_handle(None)
else:
family.set_mother_handle(None)
if not family.get_father_handle() and not family.get_mother_handle() and \
not family.get_child_ref_list():
self.remove_family(family_handle, trans)
else:
self.commit_family(family, trans)
for family_handle in person.get_parent_family_handle_list():
if family_handle:
family = self.get_family_from_handle(family_handle)
family.remove_child_handle(person.get_handle())
self.commit_family(family, trans)
handle = person.get_handle()
person_list = [
item[1] for item in
self.find_backlink_handles(handle,['Person'])]
for phandle in person_list:
p = self.get_person_from_handle(phandle)
p.remove_handle_references('Person', handle)
self.commit_person(person, trans)
self.remove_person(handle, trans)
def remove_family_relationships(self, family_handle, trans=None):
"""
Remove a family and its relationships.
"""
family = self.get_family_from_handle(family_handle)
if trans is None:
need_commit = True
trans = self.transaction_begin()
else:
need_commit = False
for phandle in [ family.get_father_handle(),
family.get_mother_handle()]:
if phandle:
person = self.get_person_from_handle(phandle)
person.remove_family_handle(family_handle)
self.commit_person(person, trans)
for ref in family.get_child_ref_list():
phandle = ref.ref
person = self.get_person_from_handle(phandle)
person.remove_parent_family_handle(family_handle)
self.commit_person(person, trans)
self.remove_family(family_handle, trans)
if need_commit:
self.transaction_commit(trans, _("Remove Family"))
def remove_parent_from_family(self, person_handle, family_handle, trans=None):
"""
Remove a person as either the father or mother of a family,
deleting the family if it becomes empty.
"""
person = self.get_person_from_handle(person_handle)
family = self.get_family_from_handle(family_handle)
if trans is None:
need_commit = True
trans = self.transaction_begin()
else:
need_commit = False
person.remove_family_handle(family_handle)
if family.get_father_handle() == person_handle:
family.set_father_handle(None)
msg = _("Remove father from family")
elif family.get_mother_handle() == person_handle:
msg = _("Remove mother from family")
family.set_mother_handle(None)
child_list = family.get_child_ref_list()
if (not family.get_father_handle() and not family.get_mother_handle() and
len(child_list) <= 1):
self.remove_family(family_handle, trans)
if child_list:
child = self.get_person_from_handle(child_list[0].ref)
child.remove_parent_family_handle(family_handle)
self.commit_person(child, trans)
else:
self.commit_family(family, trans)
self.commit_person(person, trans)
if need_commit:
self.transaction_commit(trans,msg)
def marriage_from_eventref_list(self, eventref_list):
"""
Get the marriage event from an eventref list.
"""
for eventref in eventref_list:
event = self.get_event_from_handle(eventref.ref)
if event and event.type.is_marriage():
return event
return None
def get_total(self):
"""
Get the total of primary objects.
"""
person_len = self.get_number_of_people()
family_len = self.get_number_of_families()
event_len = self.get_number_of_events()
source_len = self.get_number_of_sources()
place_len = self.get_number_of_places()
repo_len = self.get_number_of_repositories()
obj_len = self.get_number_of_media_objects()
return person_len + family_len + event_len + \
place_len + source_len + obj_len + repo_len
def set_birth_death_index(self, person):
"""
Set the birth and death indices for a person.
"""
birth_ref_index = -1
death_ref_index = -1
event_ref_list = person.get_event_ref_list()
for index in range(len(event_ref_list)):
ref = event_ref_list[index]
event = self.get_event_from_handle(ref.ref)
if (event.type.is_birth()
and ref.role.is_primary()
and (birth_ref_index == -1)):
birth_ref_index = index
elif (event.type.is_death()
and ref.role.is_primary()
and (death_ref_index == -1)):
death_ref_index = index
person.birth_ref_index = birth_ref_index
person.death_ref_index = death_ref_index

View File

@ -190,9 +190,23 @@ class EventType(GrampsType):
def __init__(self, value=None):
GrampsType.__init__(self, value)
def is_birth(self):
"""
Returns True if EventType is BIRTH, False
otherwise.
"""
return self.value == self.BIRTH
def is_death(self):
"""
Returns True if EventType is DEATH, False
otherwise.
"""
return self.value == self.DEATH
def is_birth_fallback(self):
"""
Returns True if EventRoleType is a birth fallback, False
Returns True if EventType is a birth fallback, False
otherwise.
"""
return self.value in [self.CHRISTEN,
@ -200,7 +214,7 @@ class EventType(GrampsType):
def is_death_fallback(self):
"""
Returns True if EventRoleType is a death fallback, False
Returns True if EventType is a death fallback, False
otherwise.
"""
return self.value in [self.BURIAL,
@ -208,12 +222,12 @@ class EventType(GrampsType):
self.CAUSE_DEATH]
def is_marriage(self):
"""
Returns True if EventRoleType is MARRIAGE, False otherwise.
Returns True if EventType is MARRIAGE, False otherwise.
"""
return self.value == self.MARRIAGE
def is_divorce(self):
"""
Returns True if EventRoleType is DIVORCE, False otherwise.
Returns True if EventType is DIVORCE, False otherwise.
"""
return self.value == self.DIVORCE

View File

@ -22,7 +22,6 @@
Generic utilities useful for users of the gen package
"""
from dbutils import *
from progressmon import ProgressMonitor
from longop import LongOpStatus
from callback import Callback

View File

@ -1,313 +0,0 @@
#
# Gramps - a GTK+/GNOME based genealogy program
#
# Copyright (C) 2004-2006 Donald N. Allingham
#
# 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$
from __future__ import with_statement
from gettext import gettext as _
import copy
import gen.lib
from BasicUtils import UpdateCallback
def delete_person_from_database(db, person, trans):
"""
Deletes a person from the database, cleaning up all associated references.
"""
# clear out the default person if the person is the default person
if db.get_default_person() == person:
db.set_default_person_handle(None)
# loop through the family list
for family_handle in [ f for f in person.get_family_handle_list() if f ]:
family = db.get_family_from_handle(family_handle)
if person.get_handle() == family.get_father_handle():
family.set_father_handle(None)
else:
family.set_mother_handle(None)
if not family.get_father_handle() and not family.get_mother_handle() and \
not family.get_child_ref_list():
db.remove_family(family_handle, trans)
else:
db.commit_family(family, trans)
for family_handle in person.get_parent_family_handle_list():
if family_handle:
family = db.get_family_from_handle(family_handle)
family.remove_child_handle(person.get_handle())
db.commit_family(family, trans)
handle = person.get_handle()
person_list = [
item[1] for item in
db.find_backlink_handles(handle,['Person'])]
for phandle in person_list:
p = db.get_person_from_handle(phandle)
p.remove_handle_references('Person', handle)
db.commit_person(person, trans)
db.remove_person(handle, trans)
def remove_family_relationships(db, family_handle, trans=None):
family = db.get_family_from_handle(family_handle)
if trans is None:
need_commit = True
trans = db.transaction_begin()
else:
need_commit = False
for phandle in [ family.get_father_handle(),
family.get_mother_handle()]:
if phandle:
person = db.get_person_from_handle(phandle)
person.remove_family_handle(family_handle)
db.commit_person(person, trans)
for ref in family.get_child_ref_list():
phandle = ref.ref
person = db.get_person_from_handle(phandle)
person.remove_parent_family_handle(family_handle)
db.commit_person(person, trans)
db.remove_family(family_handle, trans)
if need_commit:
db.transaction_commit(trans, _("Remove Family"))
def remove_parent_from_family(db, person_handle, family_handle, trans=None):
"""
Remove a person as either the father or mother of a family,
deleting the family if it becomes empty.
"""
person = db.get_person_from_handle(person_handle)
family = db.get_family_from_handle(family_handle)
if trans is None:
need_commit = True
trans = db.transaction_begin()
else:
need_commit = False
person.remove_family_handle(family_handle)
if family.get_father_handle() == person_handle:
family.set_father_handle(None)
msg = _("Remove father from family")
elif family.get_mother_handle() == person_handle:
msg = _("Remove mother from family")
family.set_mother_handle(None)
child_list = family.get_child_ref_list()
if (not family.get_father_handle() and not family.get_mother_handle() and
len(child_list) <= 1):
db.remove_family(family_handle, trans)
if child_list:
child = db.get_person_from_handle(child_list[0].ref)
child.remove_parent_family_handle(family_handle)
db.commit_person(child, trans)
else:
db.commit_family(family, trans)
db.commit_person(person, trans)
if need_commit:
db.transaction_commit(trans,msg)
def remove_child_from_family(db, person_handle, family_handle, trans=None):
"""
Remove a person as a child of the family, deleting the family if
it becomes empty.
"""
person = db.get_person_from_handle(person_handle)
family = db.get_family_from_handle(family_handle)
person.remove_parent_family_handle(family_handle)
family.remove_child_handle(person_handle)
if trans is None:
need_commit = True
trans = db.transaction_begin()
else:
need_commit = False
child_list = family.get_child_ref_list()
if (not family.get_father_handle() and not family.get_mother_handle() and
len(child_list) <= 1):
db.remove_family(family_handle, trans)
if child_list:
child = db.get_person_from_handle(child_list[0].ref)
child.remove_parent_family_handle(family_handle)
db.commit_person(child, trans)
else:
db.commit_family(family, trans)
db.commit_person(person, trans)
if need_commit:
db.transaction_commit(trans,_("Remove child from family"))
def marriage_from_eventref_list(db, eventref_list):
for eventref in eventref_list:
event = db.get_event_from_handle(eventref.ref)
if event.type == gen.lib.EventType.MARRIAGE:
return event
else:
return None
def add_child_to_family(db, family, child,
mrel=gen.lib.ChildRefType(),
frel=gen.lib.ChildRefType(),
trans=None):
cref = gen.lib.ChildRef()
cref.ref = child.handle
cref.set_father_relation(frel)
cref.set_mother_relation(mrel)
family.add_child_ref(cref)
child.add_parent_family_handle(family.handle)
if trans is None:
need_commit = True
trans = db.transaction_begin()
else:
need_commit = False
db.commit_family(family,trans)
db.commit_person(child,trans)
if need_commit:
db.transaction_commit(trans, _('Add child to family') )
def get_total(db):
person_len = db.get_number_of_people()
family_len = db.get_number_of_families()
event_len = db.get_number_of_events()
source_len = db.get_number_of_sources()
place_len = db.get_number_of_places()
repo_len = db.get_number_of_repositories()
obj_len = db.get_number_of_media_objects()
return person_len + family_len + event_len + \
place_len + source_len + obj_len + repo_len
def db_copy(from_db,to_db,callback):
"""
Copy all data in from_db into to_db.
Both databases must be loaded.
It is assumed that to_db is an empty database,
so no care is taken to prevent handle collision or merge data.
"""
uc = UpdateCallback(callback)
uc.set_total(get_total(from_db))
tables = {
'Person': {'cursor_func': from_db.get_person_cursor,
'add_func' : to_db.add_person,
},
'Family': {'cursor_func': from_db.get_family_cursor,
'add_func' : to_db.add_family,
},
'Event': {'cursor_func': from_db.get_event_cursor,
'add_func' : to_db.add_event,
},
'Place': {'cursor_func': from_db.get_place_cursor,
'add_func' : to_db.add_place,
},
'Source': {'cursor_func': from_db.get_source_cursor,
'add_func' : to_db.add_source,
},
'MediaObject': {'cursor_func': from_db.get_media_cursor,
'add_func' : to_db.add_object,
},
'Repository': {'cursor_func': from_db.get_repository_cursor,
'add_func' : to_db.add_repository,
},
'Note': {'cursor_func': from_db.get_note_cursor,
'add_func': to_db.add_note,
},
}
# Start batch transaction to use async TXN and other tricks
trans = to_db.transaction_begin("", batch=True)
for table_name, table_dict in tables.iteritems():
with table_dict['cursor_func']() as cursor:
add_func = table_dict['add_func']
for handle, data in cursor:
exec('obj = gen.lib.%s()' % table_name)
obj_ = getattr(gen.lib, table_name)()
assert obj_ == obj
obj.unserialize(data)
add_func(obj,trans)
uc.update()
# Copy name grouping
group_map = from_db.get_name_group_keys()
for key in group_map:
value = from_db.get_name_group_mapping(key)
to_db.set_name_group_mapping(key, value)
# Commit batch transaction: does nothing, except undoing the tricks
to_db.transaction_commit(trans, "")
# Copy bookmarks over:
# we already know that there's no overlap in handles anywhere
to_db.bookmarks = copy.deepcopy(from_db.bookmarks)
to_db.family_bookmarks = copy.deepcopy(from_db.family_bookmarks)
to_db.event_bookmarks = copy.deepcopy(from_db.event_bookmarks)
to_db.source_bookmarks = copy.deepcopy(from_db.source_bookmarks)
to_db.place_bookmarks = copy.deepcopy(from_db.place_bookmarks)
to_db.media_bookmarks = copy.deepcopy(from_db.media_bookmarks)
to_db.repo_bookmarks = copy.deepcopy(from_db.repo_bookmarks)
to_db.note_bookmarks = copy.deepcopy(from_db.note_bookmarks)
# Copy name formats
to_db.name_formats = from_db.name_formats
# Copy db owner
to_db.owner = from_db.owner
# Copy other selected metadata
if from_db.get_mediapath() is not None:
to_db.set_mediapath(from_db.get_mediapath())
def set_birth_death_index(db, person):
birth_ref_index = -1
death_ref_index = -1
event_ref_list = person.get_event_ref_list()
for index in range(len(event_ref_list)):
ref = event_ref_list[index]
event = db.get_event_from_handle(ref.ref)
if (event.type == gen.lib.EventType.BIRTH) \
and (ref.role == gen.lib.EventRoleType.PRIMARY) \
and (birth_ref_index == -1):
birth_ref_index = index
elif (event.type == gen.lib.EventType.DEATH) \
and (ref.role == gen.lib.EventRoleType.PRIMARY) \
and (death_ref_index == -1):
death_ref_index = index
person.birth_ref_index = birth_ref_index
person.death_ref_index = death_ref_index

View File

@ -55,7 +55,6 @@ from gui import widgets
from BasicUtils import name_displayer
import Errors
from glade import Glade
from gen.utils import set_birth_death_index
from editprimary import EditPrimary
from editmediaref import EditMediaRef
@ -737,7 +736,7 @@ class EditPerson(EditPrimary):
self._check_for_unknown_gender()
set_birth_death_index(self.db, self.obj)
self.db.set_birth_death_index(self.obj)
trans = self.db.transaction_begin()

View File

@ -133,7 +133,7 @@ class FamilyModel(FlatBaseModel):
erlist = [ gen.lib.EventRef().unserialize(d) for d in data[6] ]
erlist = [x for x in erlist if x.get_role()==EventRoleType.FAMILY or
x.get_role()==EventRoleType.PRIMARY]
event = gen.utils.marriage_from_eventref_list(self.db, erlist)
event = self.db.marriage_from_eventref_list(erlist)
if event:
return DateHandler.displayer.display(event.date)
else:
@ -141,7 +141,7 @@ class FamilyModel(FlatBaseModel):
def sort_marriage(self, data):
erlist = [ gen.lib.EventRef().unserialize(d) for d in data[6] ]
event = gen.utils.marriage_from_eventref_list(self.db, erlist)
event = self.db.marriage_from_eventref_list(erlist)
if event:
return "%09d" % event.date.get_sort_value()
else:

View File

@ -54,7 +54,6 @@ from gen.db.txn import GrampsDbTxn as Transaction
from gen.db.cursor import GrampsCursor
from gen.db.dbconst import *
from gen.db.exceptions import GrampsDbVersionError
from gen.utils import db_copy
import const
from QuestionDialog import ErrorDialog
from Errors import HandleError

View File

@ -40,7 +40,6 @@ from PluginUtils import Tool, MenuToolOptions, PluginWindows
from ReportBase import ReportUtils
from gen.plug.menu import FilterOption, PersonOption, \
EnumeratedListOption, BooleanOption
from gen.utils import set_birth_death_index
#------------------------------------------------------------------------
#
@ -123,7 +122,7 @@ class SortEvents(PluginWindows.ToolManagedWindowBatch):
if self.fam_events:
family_handles.extend(person.get_family_handle_list())
person.set_event_ref_list(event_ref_list)
set_birth_death_index(self.db, person)
self.db.set_birth_death_index(person)
self.db.commit_person(person, trans)
self.change = True
return family_handles

View File

@ -207,9 +207,8 @@ class FamilyView(ListView):
_('_Delete Item'), _('Cancel'))
if q.run():
self.uistate.set_busy_cursor(1)
import gen.utils
for handle in self.selected_handles():
gen.utils.remove_family_relationships(self.dbstate.db, handle)
self.dbstate.db.remove_family_relationships(handle)
self.build_tree()
self.uistate.set_busy_cursor(0)

View File

@ -317,7 +317,7 @@ class PersonView(ListView):
active_name = _("Delete Person (%s)") % name_displayer.display(person)
# delete the person from the database
gen.utils.delete_person_from_database(self.dbstate.db, person, trans)
self.dbstate.db.delete_person_from_database(person, trans)
# remove the person from the list
self.remove_from_person_list(person)

View File

@ -58,7 +58,6 @@ import config
from gui import widgets
from gui.selectors import SelectorFactory
import Errors
import gen.utils
import Bookmarks
import const
@ -1509,10 +1508,7 @@ class RelationshipView(NavigationView):
active_handle = self.dbstate.active.handle
child = self.dbstate.db.get_person_from_handle(active_handle)
gen.utils.add_child_to_family(
self.dbstate.db,
family,
child)
self.dbstate.db.add_child_to_family(family, child)
def select_parents(self, obj):
SelectFamily = SelectorFactory('Family')
@ -1529,10 +1525,7 @@ class RelationshipView(NavigationView):
active_handle = self.dbstate.active.handle
child = self.dbstate.db.get_person_from_handle(active_handle)
gen.utils.add_child_to_family(
self.dbstate.db,
family,
child)
self.dbstate.db.add_child_to_family(family, child)
def add_parents(self, obj):
family = gen.lib.Family()
@ -1566,15 +1559,13 @@ class RelationshipView(NavigationView):
def delete_family(self, obj, event, handle):
if button_activated(event, _LEFT_BUTTON):
gen.utils.remove_parent_from_family(self.dbstate.db,
self.dbstate.active.handle,
handle)
self.dbstate.db.remove_parent_from_family(self.dbstate.active.handle,
handle)
def delete_parent_family(self, obj, event, handle):
if button_activated(event, _LEFT_BUTTON):
gen.utils.remove_child_from_family(self.dbstate.db,
self.dbstate.active.handle,
handle)
self.dbstate.db.remove_child_from_family(self.dbstate.active.handle,
handle)
def change_to(self, obj, handle):
self.dbstate.change_active_handle(handle)