2005-01-29 10:43:29 +05:30
|
|
|
#
|
|
|
|
# Gramps - a GTK+/GNOME based genealogy program
|
|
|
|
#
|
2006-01-19 10:00:40 +05:30
|
|
|
# Copyright (C) 2005-2006 Donald N. Allingham
|
2005-01-29 10:43:29 +05:30
|
|
|
#
|
|
|
|
# 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$
|
|
|
|
|
|
|
|
# Written by Alex Roitman,
|
|
|
|
# largely based on ReadXML by Don Allingham
|
|
|
|
|
|
|
|
#-------------------------------------------------------------------------
|
|
|
|
#
|
|
|
|
# Standard Python Modules
|
|
|
|
#
|
|
|
|
#-------------------------------------------------------------------------
|
|
|
|
import os
|
2006-04-07 03:32:46 +05:30
|
|
|
from gettext import gettext as _
|
2005-02-14 07:02:07 +05:30
|
|
|
import sets
|
2005-01-29 10:43:29 +05:30
|
|
|
|
|
|
|
#-------------------------------------------------------------------------
|
|
|
|
#
|
|
|
|
# Gramps Modules
|
|
|
|
#
|
|
|
|
#-------------------------------------------------------------------------
|
2005-12-21 16:57:05 +05:30
|
|
|
from _GrampsBSDDB import GrampsBSDDB
|
2005-01-29 10:43:29 +05:30
|
|
|
from QuestionDialog import ErrorDialog
|
|
|
|
import Errors
|
|
|
|
|
|
|
|
#-------------------------------------------------------------------------
|
|
|
|
#
|
|
|
|
# Importing data into the currently open database.
|
|
|
|
#
|
|
|
|
#-------------------------------------------------------------------------
|
|
|
|
def importData(database, filename, callback=None,cl=0,use_trans=True):
|
|
|
|
|
|
|
|
filename = os.path.normpath(filename)
|
|
|
|
|
2005-12-21 16:57:05 +05:30
|
|
|
other_database = GrampsBSDDB()
|
2005-01-29 10:43:29 +05:30
|
|
|
try:
|
|
|
|
other_database.load(filename,callback)
|
|
|
|
except:
|
|
|
|
if cl:
|
|
|
|
print "Error: %s could not be opened. Exiting." % filename
|
|
|
|
else:
|
|
|
|
ErrorDialog(_("%s could not be opened") % filename)
|
|
|
|
return
|
2006-05-18 09:46:54 +05:30
|
|
|
|
2005-06-05 09:31:56 +05:30
|
|
|
if not other_database.version_supported():
|
|
|
|
if cl:
|
2005-12-13 07:37:16 +05:30
|
|
|
print "Error: %s could not be opened.\n%s Exiting." \
|
|
|
|
% (filename,
|
|
|
|
_("The database version is not supported "
|
|
|
|
"by this version of GRAMPS.\n"\
|
|
|
|
"Please upgrade to the corresponding version "
|
|
|
|
"or use XML for porting data between different "
|
|
|
|
"database versions."))
|
2005-06-05 09:31:56 +05:30
|
|
|
else:
|
|
|
|
ErrorDialog(_("%s could not be opened") % filename,
|
2005-12-13 07:37:16 +05:30
|
|
|
_("The Database version is not supported "
|
|
|
|
"by this version of GRAMPS."))
|
2005-06-05 09:31:56 +05:30
|
|
|
return
|
|
|
|
|
2005-02-14 07:02:07 +05:30
|
|
|
|
2006-05-18 09:46:54 +05:30
|
|
|
# Prepare table and method definitions
|
|
|
|
tables = {
|
|
|
|
'Person' : {'table' : database.person_map,
|
|
|
|
'id_table' : database.id_trans,
|
|
|
|
'add_obj' : database.add_person,
|
|
|
|
'find_next_gramps_id' :database.find_next_person_gramps_id,
|
|
|
|
'other_get_from_handle':
|
|
|
|
other_database.get_person_from_handle,
|
|
|
|
'other_table': other_database.person_map,
|
|
|
|
},
|
|
|
|
'Family' : {'table' : database.family_map,
|
|
|
|
'id_table' : database.fid_trans,
|
|
|
|
'add_obj' : database.add_family,
|
|
|
|
'find_next_gramps_id' :database.find_next_family_gramps_id,
|
|
|
|
'other_get_from_handle':
|
|
|
|
other_database.get_family_from_handle,
|
|
|
|
'other_table': other_database.family_map,
|
|
|
|
},
|
|
|
|
|
|
|
|
'Event' : {'table' : database.event_map,
|
|
|
|
'id_table' : database.eid_trans,
|
|
|
|
'add_obj' : database.add_event,
|
|
|
|
'find_next_gramps_id' : database.find_next_event_gramps_id,
|
|
|
|
'other_get_from_handle':
|
|
|
|
other_database.get_event_from_handle,
|
|
|
|
'other_table': other_database.event_map,
|
|
|
|
},
|
2006-05-18 10:31:16 +05:30
|
|
|
'Source' : {'table' : database.source_map,
|
2006-05-18 09:46:54 +05:30
|
|
|
'id_table' : database.sid_trans,
|
2006-05-18 10:31:16 +05:30
|
|
|
'add_obj' : database.add_source,
|
|
|
|
'find_next_gramps_id': database.find_next_source_gramps_id,
|
2006-05-18 09:46:54 +05:30
|
|
|
'other_get_from_handle':
|
2006-05-18 10:31:16 +05:30
|
|
|
other_database.get_source_from_handle,
|
2006-05-18 09:46:54 +05:30
|
|
|
'other_table': other_database.source_map,
|
|
|
|
},
|
2006-05-18 10:31:16 +05:30
|
|
|
'Place' : {'table' : database.place_map,
|
2006-05-18 09:46:54 +05:30
|
|
|
'id_table' : database.pid_trans,
|
2006-05-18 10:31:16 +05:30
|
|
|
'add_obj' : database.add_place,
|
|
|
|
'find_next_gramps_id' :database.find_next_place_gramps_id,
|
2006-05-18 09:46:54 +05:30
|
|
|
'other_get_from_handle':
|
2006-05-18 10:31:16 +05:30
|
|
|
other_database.get_place_from_handle,
|
2006-05-18 09:46:54 +05:30
|
|
|
'other_table': other_database.place_map,
|
|
|
|
},
|
|
|
|
'Media' : {'table' : database.media_map,
|
|
|
|
'id_table' : database.oid_trans,
|
|
|
|
'add_obj' : database.add_object,
|
|
|
|
'find_next_gramps_id' : database.find_next_object_gramps_id,
|
|
|
|
'other_get_from_handle':
|
|
|
|
other_database.get_object_from_handle,
|
|
|
|
'other_table': other_database.media_map,
|
|
|
|
},
|
|
|
|
'Repository' : {'table' : database.repository_map,
|
|
|
|
'id_table' : database.rid_trans,
|
|
|
|
'add_obj' : database.add_repository,
|
|
|
|
'find_next_gramps_id' :
|
|
|
|
database.find_next_repository_gramps_id,
|
|
|
|
'other_get_from_handle':
|
|
|
|
other_database.get_repository_from_handle,
|
|
|
|
'other_table': other_database.repository_map,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
# Check for duplicate handles.
|
|
|
|
for key in tables:
|
|
|
|
table_dict = tables[key]
|
|
|
|
table = table_dict['table']
|
|
|
|
other_table = table_dict['other_table']
|
|
|
|
msg = '%s handles in two databases overlap.' % key
|
|
|
|
check_common_handles(table,other_table,msg)
|
|
|
|
|
2005-12-13 07:37:16 +05:30
|
|
|
# Proceed with preparing for import
|
|
|
|
if use_trans:
|
2006-01-19 10:00:40 +05:30
|
|
|
trans = database.transaction_begin("",batch=True)
|
2005-12-13 07:37:16 +05:30
|
|
|
else:
|
2006-05-18 09:46:54 +05:30
|
|
|
print "Transaction is None! This is no way to go!"
|
2005-12-13 07:37:16 +05:30
|
|
|
trans = None
|
|
|
|
|
|
|
|
database.disable_signals()
|
|
|
|
|
2005-01-29 10:43:29 +05:30
|
|
|
# copy all data from new_database to database,
|
|
|
|
# rename gramps IDs of first-class objects when conflicts are found
|
2006-05-18 09:46:54 +05:30
|
|
|
for key in tables:
|
|
|
|
table_dict = tables[key]
|
|
|
|
id_table = table_dict['id_table']
|
|
|
|
add_obj = table_dict['add_obj']
|
|
|
|
find_next_gramps_id = table_dict['find_next_gramps_id']
|
|
|
|
other_table = table_dict['other_table']
|
|
|
|
other_get_from_handle = table_dict['other_get_from_handle']
|
|
|
|
import_table(id_table,add_obj,find_next_gramps_id,
|
|
|
|
other_table,other_get_from_handle,trans)
|
2005-01-29 10:43:29 +05:30
|
|
|
|
2005-12-13 07:37:16 +05:30
|
|
|
# close the other database and clean things up
|
|
|
|
other_database.close()
|
|
|
|
|
2005-01-29 10:43:29 +05:30
|
|
|
database.transaction_commit(trans,_("Import database"))
|
2005-12-13 07:37:16 +05:30
|
|
|
database.enable_signals()
|
|
|
|
database.request_rebuild()
|
2006-05-18 09:46:54 +05:30
|
|
|
|
|
|
|
def check_common_handles(table,other_table,msg):
|
|
|
|
# Check for duplicate handles. At the moment we simply exit here,
|
|
|
|
# before modifying any data. In the future we will need to handle
|
|
|
|
# this better. How?
|
|
|
|
handles = sets.Set(table.keys())
|
|
|
|
other_handles = sets.Set(other_table.keys())
|
|
|
|
if handles.intersection(other_handles):
|
|
|
|
raise Errors.HandleError(msg)
|
|
|
|
|
|
|
|
def import_table(id_table,add_obj,find_next_gramps_id,
|
|
|
|
other_table,other_get_from_handle,trans):
|
|
|
|
|
|
|
|
for handle in other_table.keys():
|
|
|
|
obj = other_get_from_handle(handle)
|
|
|
|
|
|
|
|
# Then we check gramps_id for conflicts and change it if needed
|
|
|
|
gramps_id = str(obj.gramps_id)
|
|
|
|
if id_table.has_key(gramps_id):
|
|
|
|
gramps_id = find_next_gramps_id()
|
|
|
|
obj.gramps_id = gramps_id
|
|
|
|
add_obj(obj,trans)
|