2005-01-29 10:43:29 +05:30
|
|
|
#
|
|
|
|
# Gramps - a GTK+/GNOME based genealogy program
|
|
|
|
#
|
2007-06-02 11:21:39 +05:30
|
|
|
# Copyright (C) 2005-2007 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.
|
|
|
|
#
|
2007-10-07 08:49:35 +05:30
|
|
|
# This program is distributed in the hope that it will be useful,
|
2005-01-29 10:43:29 +05:30
|
|
|
# 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$
|
|
|
|
|
2007-10-07 08:49:35 +05:30
|
|
|
# Written by Alex Roitman,
|
2005-01-29 10:43:29 +05:30
|
|
|
# largely based on ReadXML by Don Allingham
|
|
|
|
|
|
|
|
#-------------------------------------------------------------------------
|
|
|
|
#
|
|
|
|
# Standard Python Modules
|
|
|
|
#
|
|
|
|
#-------------------------------------------------------------------------
|
|
|
|
import os
|
2007-06-25 09:05:48 +05:30
|
|
|
import shutil
|
|
|
|
import tempfile
|
2006-04-07 03:32:46 +05:30
|
|
|
from gettext import gettext as _
|
2005-01-29 10:43:29 +05:30
|
|
|
|
2007-06-02 11:21:39 +05:30
|
|
|
#-------------------------------------------------------------------------
|
|
|
|
#
|
|
|
|
# GTK+ Modules
|
|
|
|
#
|
|
|
|
#-------------------------------------------------------------------------
|
|
|
|
import gtk
|
|
|
|
|
2005-01-29 10:43:29 +05:30
|
|
|
#-------------------------------------------------------------------------
|
|
|
|
#
|
|
|
|
# Gramps Modules
|
|
|
|
#
|
|
|
|
#-------------------------------------------------------------------------
|
2007-12-28 Benny Malengier <benny.malengier@gramps-project.org>
* src/ViewManager.py: remove unused functions, add alt actions left/right
* src/GrampsDb: deleted with _GrampsDbWriteXML.py,
_GrampsDbFactories.py, _GrampsGEDDB.py, _GrampsXMLDB.py,
__init__.py, Makefile.am, _GrampsInMemDB.py, _GrampsBSDDB.py.
No more inmemory databases
* src/GrampsDbUtils: moved here _GrampsDbWriteXML.py and _GrampsBSDDB.py
* src/plugins/ReadGrdb.py:
* src/plugins/Checkpoint.py:
* src/ObjectSelector/_ObjectSelectorWindow.py:
* src/DbManager.py:
* src/GrampsDbUtils/_WriteGrdb.py:
* src/GrampsDbUtils/__init__.py:
* src/GrampsDbUtils/_GrampsDbWRFactories.py:
* src/GrampsDbUtils/_WriteXML.py:
* src/GrampsDbUtils/Makefile.am:
* src/gramps_main.py:
* src/RecentFiles.py: recent-files-gramps.xml, don't overwrite 2.2.x data
* src/ArgHandler.py:
* src/DbLoader.py: remove unused functions
* src/Makefile.am:
* po/POTFILES.in:
* src/GrampsDbUtils/importdbdir.py: allow import of new database via cli
Remove inMem Editing, move remaining to DbUtils, fix ArgHandler
svn: r9613
2007-12-28 20:39:01 +05:30
|
|
|
from GrampsDbUtils._GrampsBSDDB import GrampsBSDDB
|
2005-01-29 10:43:29 +05:30
|
|
|
from QuestionDialog import ErrorDialog
|
2006-05-19 02:46:59 +05:30
|
|
|
from Errors import HandleError
|
|
|
|
from BasicUtils import UpdateCallback
|
2007-06-28 11:11:40 +05:30
|
|
|
from BasicUtils import name_displayer
|
2007-06-02 11:21:39 +05:30
|
|
|
from PluginUtils import register_import
|
2005-01-29 10:43:29 +05:30
|
|
|
|
|
|
|
#-------------------------------------------------------------------------
|
|
|
|
#
|
|
|
|
# Importing data into the currently open database.
|
|
|
|
#
|
|
|
|
#-------------------------------------------------------------------------
|
2007-10-07 08:49:35 +05:30
|
|
|
def importData(database, filename, callback=None, cl=0, use_trans=True):
|
2005-01-29 10:43:29 +05:30
|
|
|
|
2005-12-21 16:57:05 +05:30
|
|
|
other_database = GrampsBSDDB()
|
2007-06-25 09:05:48 +05:30
|
|
|
|
2007-10-07 08:49:35 +05:30
|
|
|
# Since we don't want to modify the file being imported,
|
2007-06-25 09:05:48 +05:30
|
|
|
# we create new temp file into which we will copy the imported file
|
|
|
|
orig_filename = os.path.normpath(filename)
|
|
|
|
new_filename = tempfile.mkstemp()[1]
|
|
|
|
new_env_name = tempfile.mkdtemp()
|
|
|
|
|
|
|
|
# determine old env dir and make db work with new env dir
|
|
|
|
orig_env_name = other_database.make_env_name(orig_filename)
|
|
|
|
other_database.make_env_name = lambda x: new_env_name
|
|
|
|
|
2007-06-26 23:10:03 +05:30
|
|
|
# Copy data
|
2007-10-07 08:49:35 +05:30
|
|
|
shutil.copyfile(orig_filename, new_filename)
|
2007-06-26 23:10:03 +05:30
|
|
|
# Copy env if we need and if it exists
|
|
|
|
if other_database.UseTXN and os.path.isdir(orig_env_name):
|
2007-06-25 09:05:48 +05:30
|
|
|
shutil.rmtree(new_env_name)
|
2007-10-07 08:49:35 +05:30
|
|
|
shutil.copytree(orig_env_name, new_env_name)
|
2007-06-25 09:05:48 +05:30
|
|
|
|
2005-01-29 10:43:29 +05:30
|
|
|
try:
|
2007-10-07 08:49:35 +05:30
|
|
|
other_database.load(new_filename, callback)
|
2005-01-29 10:43:29 +05:30
|
|
|
except:
|
|
|
|
if cl:
|
2007-06-25 09:05:48 +05:30
|
|
|
print "Error: %s could not be opened. Exiting." % new_filename
|
2005-01-29 10:43:29 +05:30
|
|
|
else:
|
2007-06-25 09:05:48 +05:30
|
|
|
ErrorDialog(_("%s could not be opened") % new_filename)
|
2005-01-29 10:43:29 +05:30
|
|
|
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." \
|
2007-10-07 08:49:35 +05:30
|
|
|
% (filename,
|
2005-12-13 07:37:16 +05:30
|
|
|
_("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:
|
2007-10-07 08:49:35 +05:30
|
|
|
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
|
2006-07-28 01:41:56 +05:30
|
|
|
|
2007-10-07 08:49:35 +05:30
|
|
|
# If other_database contains its custom name formats,
|
2006-07-28 01:41:56 +05:30
|
|
|
# we need to do tricks to remap the format numbers
|
|
|
|
if len(other_database.name_formats) > 0:
|
2007-10-07 08:49:35 +05:30
|
|
|
formats_map = remap_name_formats(database, other_database)
|
2007-06-28 11:11:40 +05:30
|
|
|
name_displayer.set_name_format(database.name_formats)
|
2007-10-07 08:49:35 +05:30
|
|
|
get_person = make_peron_name_remapper(other_database, formats_map)
|
2006-07-28 01:41:56 +05:30
|
|
|
else:
|
|
|
|
# No remapping necessary, proceed as usual
|
|
|
|
get_person = other_database.get_person_from_handle
|
2005-02-14 07:02:07 +05:30
|
|
|
|
2006-05-18 09:46:54 +05:30
|
|
|
# Prepare table and method definitions
|
|
|
|
tables = {
|
2007-10-07 08:49:35 +05:30
|
|
|
'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,
|
2006-05-18 09:46:54 +05:30
|
|
|
'other_get_from_handle':
|
2007-10-07 08:49:35 +05:30
|
|
|
get_person,
|
2006-05-18 09:46:54 +05:30
|
|
|
'other_table': other_database.person_map,
|
2007-10-07 08:49:35 +05:30
|
|
|
},
|
|
|
|
'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,
|
2006-05-18 09:46:54 +05:30
|
|
|
'other_get_from_handle':
|
2007-10-07 08:49:35 +05:30
|
|
|
other_database.get_family_from_handle,
|
2006-05-18 09:46:54 +05:30
|
|
|
'other_table': other_database.family_map,
|
2007-10-07 08:49:35 +05:30
|
|
|
},
|
2006-05-18 09:46:54 +05:30
|
|
|
|
2007-10-07 08:49:35 +05:30
|
|
|
'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,
|
2006-05-18 09:46:54 +05:30
|
|
|
'other_get_from_handle':
|
2007-10-07 08:49:35 +05:30
|
|
|
other_database.get_event_from_handle,
|
2006-05-18 09:46:54 +05:30
|
|
|
'other_table': other_database.event_map,
|
2007-10-07 08:49:35 +05:30
|
|
|
},
|
|
|
|
'Source' : {'table' : database.source_map,
|
|
|
|
'id_table' : database.sid_trans,
|
|
|
|
'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':
|
2007-10-07 08:49:35 +05:30
|
|
|
other_database.get_source_from_handle,
|
2006-05-18 09:46:54 +05:30
|
|
|
'other_table': other_database.source_map,
|
2007-10-07 08:49:35 +05:30
|
|
|
},
|
|
|
|
'Place' : {'table' : database.place_map,
|
|
|
|
'id_table' : database.pid_trans,
|
|
|
|
'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':
|
2007-10-07 08:49:35 +05:30
|
|
|
other_database.get_place_from_handle,
|
2006-05-18 09:46:54 +05:30
|
|
|
'other_table': other_database.place_map,
|
2007-10-07 08:49:35 +05:30
|
|
|
},
|
|
|
|
'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,
|
2006-05-18 09:46:54 +05:30
|
|
|
'other_get_from_handle':
|
2007-10-07 08:49:35 +05:30
|
|
|
other_database.get_object_from_handle,
|
2006-05-18 09:46:54 +05:30
|
|
|
'other_table': other_database.media_map,
|
2007-10-07 08:49:35 +05:30
|
|
|
},
|
|
|
|
'Repository' : {'table' : database.repository_map,
|
|
|
|
'id_table' : database.rid_trans,
|
|
|
|
'add_obj' : database.add_repository,
|
2006-05-18 09:46:54 +05:30
|
|
|
'find_next_gramps_id' :
|
2007-10-07 08:49:35 +05:30
|
|
|
database.find_next_repository_gramps_id,
|
2006-05-18 09:46:54 +05:30
|
|
|
'other_get_from_handle':
|
2007-10-07 08:49:35 +05:30
|
|
|
other_database.get_repository_from_handle,
|
|
|
|
'other_table': other_database.repository_map,
|
|
|
|
},
|
|
|
|
'Note' : {'table': database.note_map,
|
|
|
|
'id_table': database.nid_trans,
|
|
|
|
'add_obj': database.add_note,
|
|
|
|
'find_next_gramps_id': database.find_next_note_gramps_id,
|
|
|
|
'other_get_from_handle':
|
|
|
|
other_database.get_note_from_handle,
|
|
|
|
'other_table': other_database.note_map,
|
|
|
|
},
|
2006-05-18 09:46:54 +05:30
|
|
|
}
|
|
|
|
|
2006-05-19 02:46:59 +05:30
|
|
|
uc = UpdateCallback(callback)
|
|
|
|
uc.set_total(len(tables.keys()))
|
|
|
|
|
|
|
|
the_len = 0
|
2006-05-18 09:46:54 +05:30
|
|
|
|
|
|
|
# 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
|
2007-10-07 08:49:35 +05:30
|
|
|
the_len += check_common_handles(table, other_table, msg)
|
2006-05-19 02:46:59 +05:30
|
|
|
uc.update()
|
2006-05-18 09:46:54 +05:30
|
|
|
|
2005-12-13 07:37:16 +05:30
|
|
|
# Proceed with preparing for import
|
|
|
|
if use_trans:
|
2007-10-07 08:49:35 +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()
|
|
|
|
|
2007-10-07 08:49:35 +05:30
|
|
|
# copy all data from new_database to database,
|
2005-01-29 10:43:29 +05:30
|
|
|
# rename gramps IDs of first-class objects when conflicts are found
|
2006-05-19 02:46:59 +05:30
|
|
|
uc.set_total(the_len)
|
|
|
|
|
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']
|
2007-10-07 08:49:35 +05:30
|
|
|
import_table(id_table, add_obj, find_next_gramps_id,
|
|
|
|
other_table, other_get_from_handle, trans, uc)
|
2005-01-29 10:43:29 +05:30
|
|
|
|
2006-05-19 04:26:13 +05:30
|
|
|
# Copy bookmarks over:
|
|
|
|
# we already know that there's no overlap in handles anywhere
|
2007-01-20 10:36:49 +05:30
|
|
|
database.bookmarks.append_list(other_database.bookmarks.get())
|
|
|
|
database.family_bookmarks.append_list(other_database.family_bookmarks.get())
|
|
|
|
database.event_bookmarks.append_list(other_database.event_bookmarks.get())
|
|
|
|
database.source_bookmarks.append_list(other_database.source_bookmarks.get())
|
|
|
|
database.place_bookmarks.append_list(other_database.place_bookmarks.get())
|
|
|
|
database.media_bookmarks.append_list(other_database.media_bookmarks.get())
|
|
|
|
database.repo_bookmarks.append_list(other_database.repo_bookmarks.get())
|
2007-03-28 02:09:42 +05:30
|
|
|
database.note_bookmarks.append_list(other_database.note_bookmarks.get())
|
2006-05-19 04:26:13 +05:30
|
|
|
|
2007-12-09 15:48:59 +05:30
|
|
|
# Copy grouping table
|
|
|
|
group_map = other_database.get_name_group_keys()
|
|
|
|
name_len = len(group_map)
|
|
|
|
if name_len > 0:
|
|
|
|
for key in group_map:
|
|
|
|
value = other_database.get_name_group_mapping(key)
|
|
|
|
if database.has_name_group_key(key) :
|
|
|
|
present = database.get_name_group_mapping(key)
|
|
|
|
if not value == present:
|
|
|
|
msg = _("Your family tree groups name %s together"
|
|
|
|
" with %s, did not change this grouping to %s") % (
|
|
|
|
key, present, value)
|
|
|
|
print msg
|
|
|
|
else:
|
|
|
|
database.set_name_group_mapping(key, value)
|
|
|
|
|
2005-12-13 07:37:16 +05:30
|
|
|
# close the other database and clean things up
|
|
|
|
other_database.close()
|
|
|
|
|
2007-06-25 09:05:48 +05:30
|
|
|
# Remove temp file and env dir
|
|
|
|
os.unlink(new_filename)
|
|
|
|
shutil.rmtree(new_env_name)
|
|
|
|
|
2007-10-07 08:49:35 +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
|
|
|
|
2007-10-07 08:49:35 +05:30
|
|
|
def check_common_handles(table, other_table, msg):
|
|
|
|
# Check for duplicate handles. At the moment we simply exit here,
|
2006-05-18 09:46:54 +05:30
|
|
|
# before modifying any data. In the future we will need to handle
|
|
|
|
# this better. How?
|
2007-06-25 09:05:48 +05:30
|
|
|
handles = set(table.keys())
|
|
|
|
other_handles = set(other_table.keys())
|
2006-05-18 09:46:54 +05:30
|
|
|
if handles.intersection(other_handles):
|
2006-05-19 02:46:59 +05:30
|
|
|
raise HandleError(msg)
|
|
|
|
return len(other_handles)
|
2006-05-18 09:46:54 +05:30
|
|
|
|
2007-10-07 08:49:35 +05:30
|
|
|
def import_table(id_table, add_obj, find_next_gramps_id,
|
|
|
|
other_table, other_get_from_handle, trans, uc):
|
2006-05-18 09:46:54 +05:30
|
|
|
|
|
|
|
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
|
2007-10-07 08:49:35 +05:30
|
|
|
add_obj(obj, trans)
|
2006-05-19 02:46:59 +05:30
|
|
|
uc.update()
|
2006-07-28 01:41:56 +05:30
|
|
|
|
2007-10-07 08:49:35 +05:30
|
|
|
def remap_name_formats(database, other_database):
|
2006-07-28 01:41:56 +05:30
|
|
|
formats_map = {}
|
2007-10-07 08:49:35 +05:30
|
|
|
taken_numbers = [num for (num, name, fmt_str, active)
|
2006-07-28 01:41:56 +05:30
|
|
|
in database.name_formats]
|
2007-10-07 08:49:35 +05:30
|
|
|
for (number, name, fmt_str, act) in other_database.name_formats:
|
2006-07-28 01:41:56 +05:30
|
|
|
if number in taken_numbers:
|
|
|
|
new_number = -1
|
|
|
|
while new_number in taken_numbers:
|
|
|
|
new_number -= 1
|
|
|
|
taken_numbers.append(new_number)
|
|
|
|
formats_map[number] = new_number
|
|
|
|
else:
|
|
|
|
new_number = number
|
2007-10-07 08:49:35 +05:30
|
|
|
database.name_formats.append((new_number, name, fmt_str, act))
|
2006-07-28 01:41:56 +05:30
|
|
|
return formats_map
|
|
|
|
|
2007-10-07 08:49:35 +05:30
|
|
|
def remap_name(person, formats_map):
|
2006-07-28 01:41:56 +05:30
|
|
|
for name in [person.primary_name] + person.alternate_names:
|
|
|
|
try:
|
|
|
|
name.sort_as = formats_map[name.sort_as]
|
|
|
|
except KeyError:
|
|
|
|
pass
|
|
|
|
try:
|
|
|
|
name.display_as = formats_map[name.display_as]
|
|
|
|
except KeyError:
|
|
|
|
pass
|
|
|
|
|
2007-10-07 08:49:35 +05:30
|
|
|
def make_peron_name_remapper(other_database, formats_map):
|
2006-07-28 01:41:56 +05:30
|
|
|
def new_get_person(handle):
|
|
|
|
person = other_database.get_person_from_handle(handle)
|
2007-10-07 08:49:35 +05:30
|
|
|
remap_name(person, formats_map)
|
2006-07-28 01:41:56 +05:30
|
|
|
return person
|
|
|
|
return new_get_person
|
2007-06-02 11:21:39 +05:30
|
|
|
|
|
|
|
#------------------------------------------------------------------------
|
|
|
|
#
|
|
|
|
# Register with the plugin system
|
|
|
|
#
|
|
|
|
#------------------------------------------------------------------------
|
|
|
|
_mime_type = 'application/x-gramps'
|
|
|
|
_filter = gtk.FileFilter()
|
|
|
|
_filter.set_name(_('GRAMPS 2.x database'))
|
|
|
|
_filter.add_mime_type(_mime_type)
|
|
|
|
_format_name = _('GRAMPS 2.x database')
|
|
|
|
|
2008-02-29 04:02:40 +05:30
|
|
|
register_import(importData, _filter, [_mime_type], 0, _format_name)
|