2004-07-31 00:26:49 +05:30
|
|
|
#
|
|
|
|
# Gramps - a GTK+/GNOME based genealogy program
|
|
|
|
#
|
|
|
|
# Copyright (C) 2000-2004 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$
|
|
|
|
|
2004-08-01 09:51:31 +05:30
|
|
|
import os
|
2004-08-24 09:18:15 +05:30
|
|
|
import time
|
2004-08-27 03:24:14 +05:30
|
|
|
import locale
|
2004-08-01 09:51:31 +05:30
|
|
|
|
2004-07-31 00:26:49 +05:30
|
|
|
from RelLib import *
|
2004-08-01 09:51:31 +05:30
|
|
|
from GrampsDbBase import *
|
|
|
|
from bsddb import dbshelve, db
|
2004-07-31 00:26:49 +05:30
|
|
|
|
2004-08-01 09:51:31 +05:30
|
|
|
def find_surname(key,data):
|
2004-10-01 00:02:56 +05:30
|
|
|
return str(data[3].get_surname())
|
2004-07-31 00:26:49 +05:30
|
|
|
|
2004-08-01 09:51:31 +05:30
|
|
|
def find_idmap(key,data):
|
|
|
|
return str(data[1])
|
|
|
|
|
2004-08-20 07:50:06 +05:30
|
|
|
def find_fidmap(key,data):
|
|
|
|
return str(data[1])
|
|
|
|
|
2004-08-01 09:51:31 +05:30
|
|
|
def find_eventname(key,data):
|
|
|
|
return str(data[1])
|
2004-07-31 00:26:49 +05:30
|
|
|
|
|
|
|
#-------------------------------------------------------------------------
|
|
|
|
#
|
|
|
|
# GrampsBSDDB
|
|
|
|
#
|
|
|
|
#-------------------------------------------------------------------------
|
2004-08-01 09:51:31 +05:30
|
|
|
class GrampsBSDDB(GrampsDbBase):
|
2004-07-31 00:26:49 +05:30
|
|
|
"""GRAMPS database object. This object is a base class for other
|
|
|
|
objects."""
|
|
|
|
|
|
|
|
def __init__(self):
|
|
|
|
"""creates a new GrampsDB"""
|
2004-08-01 09:51:31 +05:30
|
|
|
GrampsDbBase.__init__(self)
|
2004-07-31 00:26:49 +05:30
|
|
|
|
2004-10-19 08:49:25 +05:30
|
|
|
def dbopen(self,name,dbname):
|
|
|
|
dbmap = dbshelve.DBShelf(self.env)
|
|
|
|
dbmap.open(name, dbname, db.DB_HASH, db.DB_CREATE, 0666)
|
|
|
|
return dbmap
|
|
|
|
|
2004-07-31 00:26:49 +05:30
|
|
|
def load(self,name,callback):
|
|
|
|
if self.person_map:
|
|
|
|
self.close()
|
|
|
|
|
|
|
|
self.env = db.DBEnv()
|
2004-10-16 10:40:35 +05:30
|
|
|
self.env.set_cachesize(0,0x2000000) # 2MB
|
2004-07-31 00:26:49 +05:30
|
|
|
flags = db.DB_CREATE|db.DB_INIT_MPOOL|db.DB_PRIVATE
|
|
|
|
|
|
|
|
self.undolog = "%s.log" % name
|
|
|
|
self.env.open(os.path.dirname(name), flags)
|
|
|
|
|
|
|
|
name = os.path.basename(name)
|
2004-10-19 08:49:25 +05:30
|
|
|
|
|
|
|
self.family_map = self.dbopen(name, "family")
|
|
|
|
self.place_map = self.dbopen(name, "places")
|
|
|
|
self.source_map = self.dbopen(name, "sources")
|
|
|
|
self.media_map = self.dbopen(name, "media")
|
|
|
|
self.event_map = self.dbopen(name, "events")
|
|
|
|
self.metadata = self.dbopen(name, "meta")
|
|
|
|
self.person_map = self.dbopen(name, "person")
|
2004-07-31 00:26:49 +05:30
|
|
|
|
|
|
|
self.surnames = db.DB(self.env)
|
|
|
|
self.surnames.set_flags(db.DB_DUP)
|
|
|
|
self.surnames.open(name, "surnames", db.DB_HASH, flags=db.DB_CREATE)
|
|
|
|
|
2004-10-01 00:02:56 +05:30
|
|
|
self.name_group = db.DB(self.env)
|
|
|
|
self.name_group.set_flags(db.DB_DUP)
|
|
|
|
self.name_group.open(name, "name_group", db.DB_HASH, flags=db.DB_CREATE)
|
|
|
|
|
2004-07-31 00:26:49 +05:30
|
|
|
self.id_trans = db.DB(self.env)
|
|
|
|
self.id_trans.set_flags(db.DB_DUP)
|
|
|
|
self.id_trans.open(name, "idtrans", db.DB_HASH, flags=db.DB_CREATE)
|
|
|
|
|
2004-08-20 07:50:06 +05:30
|
|
|
self.fid_trans = db.DB(self.env)
|
|
|
|
self.fid_trans.set_flags(db.DB_DUP)
|
|
|
|
self.fid_trans.open(name, "fidtrans", db.DB_HASH, flags=db.DB_CREATE)
|
|
|
|
|
2004-08-24 09:18:15 +05:30
|
|
|
self.pid_trans = db.DB(self.env)
|
|
|
|
self.pid_trans.set_flags(db.DB_DUP)
|
|
|
|
self.pid_trans.open(name, "pidtrans", db.DB_HASH, flags=db.DB_CREATE)
|
|
|
|
|
|
|
|
self.sid_trans = db.DB(self.env)
|
|
|
|
self.sid_trans.set_flags(db.DB_DUP)
|
|
|
|
self.sid_trans.open(name, "sidtrans", db.DB_HASH, flags=db.DB_CREATE)
|
|
|
|
|
|
|
|
self.oid_trans = db.DB(self.env)
|
|
|
|
self.oid_trans.set_flags(db.DB_DUP)
|
|
|
|
self.oid_trans.open(name, "oidtrans", db.DB_HASH, flags=db.DB_CREATE)
|
|
|
|
|
2004-07-31 00:26:49 +05:30
|
|
|
self.eventnames = db.DB(self.env)
|
|
|
|
self.eventnames.set_flags(db.DB_DUP)
|
|
|
|
self.eventnames.open(name, "eventnames", db.DB_HASH, flags=db.DB_CREATE)
|
2004-08-24 09:18:15 +05:30
|
|
|
self.person_map.associate(self.surnames, find_surname, db.DB_CREATE)
|
|
|
|
self.person_map.associate(self.id_trans, find_idmap, db.DB_CREATE)
|
|
|
|
self.family_map.associate(self.fid_trans, find_idmap, db.DB_CREATE)
|
|
|
|
self.place_map.associate(self.pid_trans, find_idmap, db.DB_CREATE)
|
|
|
|
self.media_map.associate(self.oid_trans, find_idmap, db.DB_CREATE)
|
|
|
|
self.source_map.associate(self.sid_trans, find_idmap, db.DB_CREATE)
|
2004-07-31 00:26:49 +05:30
|
|
|
self.event_map.associate(self.eventnames, find_eventname, db.DB_CREATE)
|
|
|
|
|
|
|
|
self.undodb = db.DB()
|
|
|
|
self.undodb.open(self.undolog, db.DB_RECNO, db.DB_CREATE)
|
|
|
|
|
|
|
|
self.bookmarks = self.metadata.get('bookmarks')
|
|
|
|
if self.bookmarks == None:
|
|
|
|
self.bookmarks = []
|
2004-08-07 10:46:57 +05:30
|
|
|
|
|
|
|
self.genderStats = GenderStats(self.metadata.get('gender_stats'))
|
2004-07-31 00:26:49 +05:30
|
|
|
return 1
|
|
|
|
|
2004-10-13 09:21:27 +05:30
|
|
|
def abort_changes(self):
|
|
|
|
while self.undo():
|
|
|
|
pass
|
|
|
|
self.close()
|
|
|
|
|
2004-07-31 00:26:49 +05:30
|
|
|
def close(self):
|
2004-10-01 00:02:56 +05:30
|
|
|
self.name_group.close()
|
2004-07-31 00:26:49 +05:30
|
|
|
self.person_map.close()
|
|
|
|
self.family_map.close()
|
|
|
|
self.place_map.close()
|
|
|
|
self.source_map.close()
|
|
|
|
self.media_map.close()
|
|
|
|
self.event_map.close()
|
|
|
|
self.metadata['bookmarks'] = self.bookmarks
|
2004-08-07 10:46:57 +05:30
|
|
|
self.metadata['gender_stats'] = self.genderStats.save_stats()
|
2004-07-31 00:26:49 +05:30
|
|
|
self.metadata.close()
|
|
|
|
self.surnames.close()
|
|
|
|
self.eventnames.close()
|
|
|
|
self.id_trans.close()
|
2004-08-20 07:50:06 +05:30
|
|
|
self.fid_trans.close()
|
2004-08-24 09:18:15 +05:30
|
|
|
self.oid_trans.close()
|
|
|
|
self.sid_trans.close()
|
|
|
|
self.pid_trans.close()
|
2004-07-31 00:26:49 +05:30
|
|
|
self.env.close()
|
|
|
|
self.undodb.close()
|
|
|
|
|
|
|
|
try:
|
|
|
|
os.remove(self.undolog)
|
|
|
|
except:
|
|
|
|
pass
|
|
|
|
|
|
|
|
self.person_map = None
|
|
|
|
self.family_map = None
|
|
|
|
self.place_map = None
|
|
|
|
self.source_map = None
|
|
|
|
self.media_map = None
|
|
|
|
self.event_map = None
|
|
|
|
self.surnames = None
|
|
|
|
self.env = None
|
|
|
|
self.metadata = None
|
|
|
|
|
2004-10-01 00:02:56 +05:30
|
|
|
def set_name_group_mapping(self,name,group):
|
|
|
|
name = str(name)
|
|
|
|
if not group and self.name_group.has_key(name):
|
|
|
|
self.name_group.delete(name)
|
|
|
|
else:
|
|
|
|
self.name_group[name] = group
|
|
|
|
|
2004-08-13 10:04:07 +05:30
|
|
|
def get_surname_list(self):
|
2004-07-31 00:26:49 +05:30
|
|
|
names = self.surnames.keys()
|
|
|
|
a = {}
|
|
|
|
for name in names:
|
|
|
|
a[unicode(name)] = 1
|
|
|
|
vals = a.keys()
|
2004-08-27 03:24:14 +05:30
|
|
|
vals.sort(locale.strcoll)
|
2004-07-31 00:26:49 +05:30
|
|
|
return vals
|
|
|
|
|
2004-08-13 10:04:07 +05:30
|
|
|
def get_person_event_type_list(self):
|
2004-07-31 00:26:49 +05:30
|
|
|
names = self.eventnames.keys()
|
|
|
|
a = {}
|
|
|
|
for name in names:
|
|
|
|
a[unicode(name)] = 1
|
|
|
|
vals = a.keys()
|
|
|
|
vals.sort()
|
|
|
|
return vals
|
|
|
|
|
2004-08-13 10:04:07 +05:30
|
|
|
def remove_person(self,handle,transaction):
|
2004-09-21 08:47:07 +05:30
|
|
|
person = self.get_person_from_handle(handle)
|
|
|
|
self.genderStats.uncount_person (person)
|
2004-07-31 00:26:49 +05:30
|
|
|
if transaction != None:
|
2004-09-21 08:47:07 +05:30
|
|
|
transaction.add(PERSON_KEY,handle,person.serialize())
|
|
|
|
self.person_map.delete(str(handle))
|
2004-07-31 00:26:49 +05:30
|
|
|
|
2004-08-13 10:04:07 +05:30
|
|
|
def remove_source(self,handle,transaction):
|
2004-07-31 00:26:49 +05:30
|
|
|
if transaction != None:
|
|
|
|
old_data = self.source_map.get(str(handle))
|
|
|
|
transaction.add(SOURCE_KEY,handle,old_data)
|
|
|
|
self.source_map.delete(str(handle))
|
|
|
|
|
2004-09-19 09:41:34 +05:30
|
|
|
def remove_family(self,handle,transaction):
|
2004-08-13 10:04:07 +05:30
|
|
|
if transaction != None:
|
|
|
|
old_data = self.family_map.get(str(handle))
|
|
|
|
transaction.add(FAMILY_KEY,handle,old_data)
|
|
|
|
self.family_map.delete(str(handle))
|
|
|
|
|
|
|
|
def remove_event(self,handle,transaction):
|
2004-07-31 00:26:49 +05:30
|
|
|
if transaction != None:
|
|
|
|
old_data = self.event_map.get(str(handle))
|
|
|
|
transaction.add(EVENT_KEY,handle,old_data)
|
|
|
|
self.event_map.delete(str(handle))
|
|
|
|
|
2004-08-30 09:20:33 +05:30
|
|
|
def remove_place(self,handle,transaction):
|
|
|
|
if transaction != None:
|
|
|
|
old_data = self.place_map.get(handle)
|
|
|
|
transaction.add(PLACE_KEY,handle,old_data)
|
2004-09-21 08:47:07 +05:30
|
|
|
self.place_map.delete(str(handle))
|
2004-08-30 09:20:33 +05:30
|
|
|
|
2004-08-20 07:50:06 +05:30
|
|
|
def get_person_from_gramps_id(self,val):
|
|
|
|
"""finds a Person in the database from the passed gramps' ID.
|
|
|
|
If no such Person exists, a new Person is added to the database."""
|
|
|
|
|
|
|
|
data = self.id_trans.get(str(val))
|
|
|
|
if data:
|
|
|
|
person = Person()
|
|
|
|
person.unserialize(cPickle.loads(data))
|
|
|
|
return person
|
|
|
|
else:
|
|
|
|
return None
|
|
|
|
|
|
|
|
def get_family_from_gramps_id(self,val):
|
2004-08-24 09:18:15 +05:30
|
|
|
"""finds a Family in the database from the passed gramps' ID.
|
|
|
|
If no such Family exists, a new Person is added to the database."""
|
2004-08-20 07:50:06 +05:30
|
|
|
|
|
|
|
data = self.fid_trans.get(str(val))
|
|
|
|
if data:
|
|
|
|
family = Family()
|
|
|
|
family.unserialize(cPickle.loads(data))
|
|
|
|
return family
|
|
|
|
else:
|
|
|
|
return None
|
2004-08-24 09:18:15 +05:30
|
|
|
|
|
|
|
def get_place_from_gramps_id(self,val):
|
|
|
|
"""finds a Place in the database from the passed gramps' ID.
|
|
|
|
If no such Place exists, a new Person is added to the database."""
|
|
|
|
|
|
|
|
data = self.pid_trans.get(str(val))
|
|
|
|
if data:
|
|
|
|
place = Place()
|
|
|
|
place.unserialize(cPickle.loads(data))
|
|
|
|
return place
|
|
|
|
else:
|
|
|
|
return None
|
|
|
|
|
|
|
|
def get_source_from_gramps_id(self,val):
|
|
|
|
"""finds a Source in the database from the passed gramps' ID.
|
|
|
|
If no such Source exists, a new Person is added to the database."""
|
|
|
|
|
|
|
|
data = self.sid_trans.get(str(val))
|
|
|
|
if data:
|
|
|
|
source = Source()
|
|
|
|
source.unserialize(cPickle.loads(data))
|
|
|
|
return source
|
|
|
|
else:
|
|
|
|
return None
|
|
|
|
|
|
|
|
def get_object_from_gramps_id(self,val):
|
|
|
|
"""finds a MediaObject in the database from the passed gramps' ID.
|
|
|
|
If no such MediaObject exists, a new Person is added to the database."""
|
|
|
|
|
|
|
|
data = self.oid_trans.get(str(val))
|
|
|
|
if data:
|
|
|
|
obj = MediaObject()
|
|
|
|
obj.unserialize(cPickle.loads(data))
|
|
|
|
return obj
|
|
|
|
else:
|
|
|
|
return None
|