Move the contents of "_GrampsBSDDB.py" into "ReadGrdb.py" because that is the only place it is used. Then, rename "ReadGrdb.py" to "plugins/ImportGrdb.py" for consistency. Also clean up misc import related code.

svn: r11173
This commit is contained in:
Brian Matherly 2008-10-21 04:22:45 +00:00
parent 80c4621e7e
commit dfd426e5ca
11 changed files with 304 additions and 393 deletions

View File

@ -204,7 +204,6 @@ src/GrampsDbUtils/_GedcomStageOne.py
#src/GrampsDbUtils/_GedcomLex.py
src/GrampsDbUtils/_GedcomParse.py
src/GrampsDbUtils/_GedcomTokens.py
src/GrampsDbUtils/_GrampsBSDDB.py
src/GrampsDbUtils/_GrampsDbWriteXML.py
src/GrampsDbUtils/_GrampsDbWRFactories.py
src/GrampsDbUtils/_ReadGedcom.py
@ -281,6 +280,8 @@ src/plugins/GVHourGlass.py
src/plugins/GVRelGraph.py
src/plugins/ImportCSV.py
src/plugins/ImportGeneWeb.py
src/plugins/ImportGpkg.py
src/plugins/ImportGrdb.py
src/plugins/ImportvCard.py
src/plugins/IndivComplete.py
src/plugins/KinshipReport.py
@ -295,7 +296,6 @@ src/plugins/OnThisDay.py
src/plugins/OwnerEditor.py
src/plugins/PatchNames.py
src/plugins/PlaceReport.py
src/plugins/ReadPkg.py
src/plugins/Rebuild.py
src/plugins/References.py
src/plugins/RelCalc.py
@ -327,7 +327,6 @@ src/plugins/WriteFtree.py
src/plugins/WriteGeneWeb.py
src/plugins/WritePkg.py
src/plugins/RebuildRefMap.py
src/plugins/ReadGrdb.py
# PluginUtils package
src/PluginUtils/_GuiOptions.py

View File

@ -28,13 +28,6 @@
Module responsible for handling the command line arguments for GRAMPS.
"""
#-------------------------------------------------------------------------
#
# GNOME/GTK
#
#-------------------------------------------------------------------------
import gtk
#-------------------------------------------------------------------------
#
# Standard python modules
@ -64,7 +57,7 @@ from DbManager import CLIDbManager, NAME_FILE, find_locker_name
from PluginUtils import Tool
from gen.plug import PluginManager
from ReportBase import CATEGORY_BOOK, CATEGORY_CODE, CATEGORY_WEB, cl_report
from ReportBase import CATEGORY_BOOK, CATEGORY_CODE, cl_report
IMPORT_TYPES = (const.APP_GRAMPS_XML, const.APP_GEDCOM,
const.APP_GRAMPS_PKG, const.APP_GENEWEB,
@ -784,26 +777,5 @@ class ArgHandler:
print "Unknown action: %s." % action
sys.exit(1)
def add_all_files_filter(chooser):
"""
Add an all-permitting filter to the file chooser dialog.
"""
mime_filter = gtk.FileFilter()
mime_filter.set_name(_('All files'))
mime_filter.add_pattern('*')
chooser.add_filter(mime_filter)
def add_grdb_filter(chooser):
"""
Add a GRDB filter to the file chooser dialog.
"""
mime_filter = gtk.FileFilter()
mime_filter.set_name(_('GRAMPS databases'))
mime_filter.add_mime_type(const.APP_GRAMPS)
chooser.add_filter(mime_filter)
def read_pkg(filename):
print "FIXME: This is not re-implemented yet."
def empty(val):
pass

View File

@ -119,7 +119,6 @@ class DbLoader:
# Always add automatic (match all files) filter
add_all_files_filter(choose_db_dialog) # *
#add_grdb_filter(choose_db_dialog) # .grdb no longer native!
add_xml_filter(choose_db_dialog) # .gramps
add_gedcom_filter(choose_db_dialog) # .ged
@ -368,15 +367,6 @@ def add_gramps_files_filter(chooser):
mime_filter.add_mime_type(fmt)
chooser.add_filter(mime_filter)
def add_grdb_filter(chooser):
"""
Add a GRDB filter to the file chooser dialog.
"""
mime_filter = gtk.FileFilter()
mime_filter.set_name(_('GRAMPS 2.x databases'))
mime_filter.add_mime_type(const.APP_GRAMPS)
chooser.add_filter(mime_filter)
def add_xml_filter(chooser):
"""
Add a GRAMPS XML filter to the file chooser dialog.

View File

@ -14,7 +14,6 @@ pkgdata_PYTHON = \
_GedcomStageOne.py\
_GedcomTokens.py\
_GedcomUtils.py\
_GrampsBSDDB.py\
_GrampsDbWRFactories.py\
_GrampsDbWriteXML.py\
__init__.py\

View File

@ -80,7 +80,7 @@ EVENT_PERSON_STR = _("%(event_name)s of %(person)s")
# Must takes care of renaming media files according to their new IDs.
#
#-------------------------------------------------------------------------
def importData(database, filename, callback=None, cl=0, use_trans=False):
def importData(database, filename, callback=None, cl=0):
filename = os.path.normpath(filename)
basefile = os.path.dirname(filename)
@ -109,7 +109,7 @@ def importData(database, filename, callback=None, cl=0, use_trans=False):
try:
xml_file.seek(0)
info = parser.parse(xml_file, use_trans, line_cnt, person_cnt)
info = parser.parse(xml_file, line_cnt, person_cnt)
except IOError, msg:
if cl:
print "Error reading %s" % filename
@ -733,7 +733,7 @@ class GrampsParser(UpdateCallback):
self.nidswap[gramps_id] = gramps_id
return self.nidswap[gramps_id]
def parse(self, ifile, use_trans=False, linecount=0, personcount=0):
def parse(self, ifile, linecount=0, personcount=0):
if personcount < 1000:
no_magic = True
else:

View File

@ -31,11 +31,6 @@
# constants
#
#-------------------------------------------------------------------------
APP_GRAMPS = "application/x-gramps"
APP_GRAMPS_XML = "application/x-gramps-xml"
APP_GEDCOM = "application/x-gedcom"
APP_GRAMPS_PKG = "application/x-gramps-package"
APP_GENEWEB = "application/x-geneweb"
PERSON_KEY = 0
FAMILY_KEY = 1

View File

@ -489,7 +489,6 @@ class GrampsDBDir(GrampsDbBase, UpdateCallback):
db.DB_INIT_LOG | db.DB_INIT_TXN | db.DB_THREAD
# As opposed to before, we always try recovery on databases
# in _GrampsBSDDB.py we only do that on existing filenames
env_flags = env_flags | db.DB_RECOVER
# Environment name is now based on the filename
@ -1910,7 +1909,6 @@ class GrampsDBDir(GrampsDbBase, UpdateCallback):
db.DB_INIT_LOG | db.DB_INIT_TXN | db.DB_THREAD
# As opposed to before, we always try recovery on databases
# in _GrampsBSDDB.py we only do that on existing filenames
env_flags = env_flags | db.DB_RECOVER
# Environment name is now based on the filename

View File

@ -57,7 +57,7 @@ from gen.plug import PluginManager, ImportPlugin
#
#
#-------------------------------------------------------------------------
def impData(database, name, cb=None, cl=0):
def impData(database, name, cb=None):
# Create tempdir, if it does not exist, then check for writability
# THE TEMP DIR is named as the filname.gpkg.media and is created
# in the mediapath dir of the family tree we import too

View File

@ -1,14 +1,15 @@
#
# Gramps - a GTK+/GNOME based genealogy program
#
# Copyright (C) 2000-2008 Donald N. Allingham
# Copyright (C) 2005-2007 Donald N. Allingham
# Copyright (C) 2008 Brian G. Matherly
#
# 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,
# 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.
@ -20,28 +21,27 @@
# $Id$
"""
Provide the Berkeley DB (BSDDB) database backend for GRAMPS for GRAMPS up
to version 2.2.x
"""
# Written by Alex Roitman,
# largely based on ReadXML by Don Allingham
#-------------------------------------------------------------------------
#
# Standard python modules
# Standard Python Modules
#
#-------------------------------------------------------------------------
import cPickle as pickle
import os
import time
import shutil
import tempfile
from gettext import gettext as _
import cPickle as pickle
import time
from bsddb import dbshelve, db
import logging
__LOG = logging.getLogger(".GrampsDb")
#-------------------------------------------------------------------------
#
# Gramps modules
# Gramps Modules
#
#-------------------------------------------------------------------------
from gen.lib import (GenderStats, Source, Person, Family, Event, Place,
@ -56,9 +56,17 @@ from gen.db.dbconst import (REFERENCE_KEY, PERSON_COL_KEY, EVENT_COL_KEY,
from gen.db.exceptions import FileVersionError
from gen.utils import db_copy
import const
from QuestionDialog import ErrorDialog
from Errors import HandleError
from BasicUtils import UpdateCallback
from BasicUtils import name_displayer
from gen.plug import PluginManager, ImportPlugin
#-------------------------------------------------------------------------
#
# Constants
#
#-------------------------------------------------------------------------
_MINVERSION = 9
_DBVERSION = 13
@ -67,7 +75,6 @@ _DBVERSION = 13
# Secondary index functions
#
#--------------------------------------------------------------------------
def find_surname(key, data):
"""
Return the surname from the data stream. Used for building a secondary
@ -516,7 +523,7 @@ class GrampsBSDDB(GrampsDbBase, UpdateCallback):
db_copy(other_database, self, callback)
return 1
def make_env_name(self,full_name):
def make_env_name(self, full_name):
if self.UseTXN:
# Environment name is now based on the filename
drive, tmp_name = os.path.splitdrive(full_name)
@ -2408,3 +2415,277 @@ def convert_location_11(loc):
street = u''
new_location_base = (street, city, county, state, country, postal, phone)
return (new_location_base, parish)
#-------------------------------------------------------------------------
#
# Importing data into the currently open database.
#
#-------------------------------------------------------------------------
def importData(database, filename, callback=None, cl=0):
other_database = GrampsBSDDB()
# Since we don't want to modify the file being imported,
# 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
# Copy data
shutil.copyfile(orig_filename, new_filename)
# Copy env if we need and if it exists
if other_database.UseTXN and os.path.isdir(orig_env_name):
shutil.rmtree(new_env_name)
shutil.copytree(orig_env_name, new_env_name)
try:
other_database.load(new_filename, callback)
except:
if cl:
print "Error: %s could not be opened. Exiting." % new_filename
else:
import traceback
print traceback.print_exc()
ErrorDialog(_("%s could not be opened") % new_filename)
return
if not other_database.version_supported():
if cl:
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."))
else:
ErrorDialog(_("%s could not be opened") % filename,
_("The Database version is not supported "
"by this version of GRAMPS."))
return
# If other_database contains its custom name formats,
# we need to do tricks to remap the format numbers
if len(other_database.name_formats) > 0:
formats_map = remap_name_formats(database, other_database)
name_displayer.set_name_format(database.name_formats)
get_person = make_peron_name_remapper(other_database, formats_map)
else:
# No remapping necessary, proceed as usual
get_person = other_database.get_person_from_handle
# 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':
get_person,
'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,
},
'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,
'other_get_from_handle':
other_database.get_source_from_handle,
'other_table': other_database.source_map,
},
'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,
'other_get_from_handle':
other_database.get_place_from_handle,
'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,
},
'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,
},
}
uc = UpdateCallback(callback)
uc.set_total(len(tables.keys()))
the_len = 0
# 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
the_len += check_common_handles(table, other_table, msg)
uc.update()
# Proceed with preparing for import
trans = database.transaction_begin("", batch=True)
database.disable_signals()
# copy all data from new_database to database,
# rename gramps IDs of first-class objects when conflicts are found
uc.set_total(the_len)
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, uc)
# Copy bookmarks over:
# we already know that there's no overlap in handles anywhere
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())
database.note_bookmarks.append_list(other_database.note_bookmarks.get())
# 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)
# close the other database and clean things up
other_database.close()
# Remove temp file and env dir
os.unlink(new_filename)
shutil.rmtree(new_env_name)
database.transaction_commit(trans, _("Import database"))
database.enable_signals()
database.request_rebuild()
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 = set(table.keys())
other_handles = set(other_table.keys())
if handles.intersection(other_handles):
raise HandleError(msg)
return len(other_handles)
def import_table(id_table, add_obj, find_next_gramps_id,
other_table, other_get_from_handle, trans, uc):
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 gramps_id in id_table:
gramps_id = find_next_gramps_id()
obj.gramps_id = gramps_id
add_obj(obj, trans)
uc.update()
def remap_name_formats(database, other_database):
formats_map = {}
taken_numbers = [num for (num, name, fmt_str, active)
in database.name_formats]
for (number, name, fmt_str, act) in other_database.name_formats:
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
database.name_formats.append((new_number, name, fmt_str, act))
return formats_map
def remap_name(person, formats_map):
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
def make_peron_name_remapper(other_database, formats_map):
def new_get_person(handle):
person = other_database.get_person_from_handle(handle)
remap_name(person, formats_map)
return person
return new_get_person
#------------------------------------------------------------------------
#
# Register with the plugin system
#
#------------------------------------------------------------------------
pmgr = PluginManager.get_instance()
plugin = ImportPlugin(name = _('GRAMPS 2.x database'),
description = _("Import data from GRAMPS 2.x "
"database files"),
import_function = importData,
mime_types = ['application/x-gramps'])
pmgr.register_plugin(plugin)

View File

@ -43,6 +43,8 @@ pkgdata_PYTHON = \
GVRelGraph.py\
ImportCSV.py\
ImportGeneWeb.py\
ImportGpkg.py\
ImportGrdb.py\
ImportvCard.py\
IndivComplete.py\
KinshipReport.py\
@ -57,8 +59,6 @@ pkgdata_PYTHON = \
OwnerEditor.py\
PatchNames.py\
PlaceReport.py\
ReadGrdb.py\
ReadPkg.py\
Rebuild.py\
RebuildRefMap.py\
References.py\

View File

@ -1,323 +0,0 @@
#
# Gramps - a GTK+/GNOME based genealogy program
#
# Copyright (C) 2005-2007 Donald N. Allingham
# Copyright (C) 2008 Brian G. Matherly
#
# 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
import shutil
import tempfile
from gettext import gettext as _
#-------------------------------------------------------------------------
#
# Gramps Modules
#
#-------------------------------------------------------------------------
from GrampsDbUtils._GrampsBSDDB import GrampsBSDDB
from QuestionDialog import ErrorDialog
from Errors import HandleError
from BasicUtils import UpdateCallback
from BasicUtils import name_displayer
from gen.plug import PluginManager, ImportPlugin
#-------------------------------------------------------------------------
#
# Importing data into the currently open database.
#
#-------------------------------------------------------------------------
def importData(database, filename, callback=None, cl=0, use_trans=True):
other_database = GrampsBSDDB()
# Since we don't want to modify the file being imported,
# 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
# Copy data
shutil.copyfile(orig_filename, new_filename)
# Copy env if we need and if it exists
if other_database.UseTXN and os.path.isdir(orig_env_name):
shutil.rmtree(new_env_name)
shutil.copytree(orig_env_name, new_env_name)
try:
other_database.load(new_filename, callback)
except:
if cl:
print "Error: %s could not be opened. Exiting." % new_filename
else:
ErrorDialog(_("%s could not be opened") % new_filename)
return
if not other_database.version_supported():
if cl:
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."))
else:
ErrorDialog(_("%s could not be opened") % filename,
_("The Database version is not supported "
"by this version of GRAMPS."))
return
# If other_database contains its custom name formats,
# we need to do tricks to remap the format numbers
if len(other_database.name_formats) > 0:
formats_map = remap_name_formats(database, other_database)
name_displayer.set_name_format(database.name_formats)
get_person = make_peron_name_remapper(other_database, formats_map)
else:
# No remapping necessary, proceed as usual
get_person = other_database.get_person_from_handle
# 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':
get_person,
'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,
},
'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,
'other_get_from_handle':
other_database.get_source_from_handle,
'other_table': other_database.source_map,
},
'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,
'other_get_from_handle':
other_database.get_place_from_handle,
'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,
},
'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,
},
}
uc = UpdateCallback(callback)
uc.set_total(len(tables.keys()))
the_len = 0
# 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
the_len += check_common_handles(table, other_table, msg)
uc.update()
# Proceed with preparing for import
if use_trans:
trans = database.transaction_begin("", batch=True)
else:
print "Transaction is None! This is no way to go!"
trans = None
database.disable_signals()
# copy all data from new_database to database,
# rename gramps IDs of first-class objects when conflicts are found
uc.set_total(the_len)
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, uc)
# Copy bookmarks over:
# we already know that there's no overlap in handles anywhere
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())
database.note_bookmarks.append_list(other_database.note_bookmarks.get())
# 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)
# close the other database and clean things up
other_database.close()
# Remove temp file and env dir
os.unlink(new_filename)
shutil.rmtree(new_env_name)
database.transaction_commit(trans, _("Import database"))
database.enable_signals()
database.request_rebuild()
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 = set(table.keys())
other_handles = set(other_table.keys())
if handles.intersection(other_handles):
raise HandleError(msg)
return len(other_handles)
def import_table(id_table, add_obj, find_next_gramps_id,
other_table, other_get_from_handle, trans, uc):
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 gramps_id in id_table:
gramps_id = find_next_gramps_id()
obj.gramps_id = gramps_id
add_obj(obj, trans)
uc.update()
def remap_name_formats(database, other_database):
formats_map = {}
taken_numbers = [num for (num, name, fmt_str, active)
in database.name_formats]
for (number, name, fmt_str, act) in other_database.name_formats:
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
database.name_formats.append((new_number, name, fmt_str, act))
return formats_map
def remap_name(person, formats_map):
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
def make_peron_name_remapper(other_database, formats_map):
def new_get_person(handle):
person = other_database.get_person_from_handle(handle)
remap_name(person, formats_map)
return person
return new_get_person
#------------------------------------------------------------------------
#
# Register with the plugin system
#
#------------------------------------------------------------------------
pmgr = PluginManager.get_instance()
plugin = ImportPlugin(name = _('GRAMPS 2.x database'),
description = _("Import data from GRAMPS 2.x "
"database files"),
import_function = importData,
mime_types = ['application/x-gramps'])
pmgr.register_plugin(plugin)