2002-10-20 19:55:16 +05:30
|
|
|
#
|
|
|
|
# Gramps - a GTK+/GNOME based genealogy program
|
|
|
|
#
|
2006-02-02 20:23:31 +05:30
|
|
|
# Copyright (C) 2000-2006 Donald N. Allingham
|
2002-10-20 19:55:16 +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
|
|
|
|
#
|
|
|
|
|
2008-02-18 18:01:31 +05:30
|
|
|
# $Id:_ReadGedcom.py 9912 2008-01-22 09:17:46Z acraphae $
|
2003-10-23 21:03:57 +05:30
|
|
|
|
2002-10-20 19:55:16 +05:30
|
|
|
"Import from GEDCOM"
|
|
|
|
|
2007-09-08 02:54:01 +05:30
|
|
|
#------------------------------------------------------------------------
|
|
|
|
#
|
|
|
|
# python modules
|
|
|
|
#
|
|
|
|
#------------------------------------------------------------------------
|
2002-10-20 19:55:16 +05:30
|
|
|
import os
|
2007-09-08 02:54:01 +05:30
|
|
|
from gettext import gettext as _
|
2006-01-25 10:37:10 +05:30
|
|
|
|
2007-09-08 02:54:01 +05:30
|
|
|
#------------------------------------------------------------------------
|
|
|
|
#
|
|
|
|
# Set up logging
|
|
|
|
#
|
|
|
|
#------------------------------------------------------------------------
|
|
|
|
import logging
|
|
|
|
LOG = logging.getLogger(".GedcomImport")
|
|
|
|
|
|
|
|
#------------------------------------------------------------------------
|
|
|
|
#
|
|
|
|
# GRAMPS modules
|
|
|
|
#
|
|
|
|
#------------------------------------------------------------------------
|
2003-01-15 10:55:50 +05:30
|
|
|
import Errors
|
2008-11-04 09:42:51 +05:30
|
|
|
from GrampsDbUtils._GedcomParse import GedcomParser
|
|
|
|
from GrampsDbUtils._GedcomStageOne import StageOne
|
2007-06-13 07:57:31 +05:30
|
|
|
from QuestionDialog import ErrorDialog, DBErrorDialog
|
2008-11-04 09:42:51 +05:30
|
|
|
from gen.plug import PluginManager, ImportPlugin
|
2002-10-20 19:55:16 +05:30
|
|
|
|
2007-09-08 02:54:01 +05:30
|
|
|
try:
|
|
|
|
import Config
|
|
|
|
DEFAULT_SOURCE = Config.get(Config.DEFAULT_SOURCE)
|
|
|
|
except ImportError:
|
|
|
|
LOG.warn("No Config module available using defaults.")
|
|
|
|
DEFAULT_SOURCE = False
|
|
|
|
|
2009-04-17 20:28:25 +05:30
|
|
|
#-------------------------------------------------------------------------
|
|
|
|
#
|
|
|
|
# Constants
|
|
|
|
#
|
|
|
|
#-------------------------------------------------------------------------
|
|
|
|
_GLADE_FILE = "ImportGedcom.glade"
|
|
|
|
|
2002-10-20 19:55:16 +05:30
|
|
|
#-------------------------------------------------------------------------
|
|
|
|
#
|
2007-09-08 02:54:01 +05:30
|
|
|
# importData
|
2002-10-20 19:55:16 +05:30
|
|
|
#
|
|
|
|
#-------------------------------------------------------------------------
|
2008-11-04 09:42:51 +05:30
|
|
|
def importData(database, filename, callback=None):
|
2007-09-08 02:54:01 +05:30
|
|
|
"""
|
|
|
|
Try to handle ANSEL encoded files that are not really ANSEL encoded
|
|
|
|
"""
|
2006-09-24 10:07:59 +05:30
|
|
|
try:
|
2007-02-14 05:30:31 +05:30
|
|
|
ifile = open(filename, "r")
|
2006-09-24 10:07:59 +05:30
|
|
|
except IOError:
|
|
|
|
return
|
2004-09-25 03:35:46 +05:30
|
|
|
|
|
|
|
ansel = False
|
|
|
|
gramps = False
|
2005-01-16 09:30:35 +05:30
|
|
|
for index in range(50):
|
2007-02-14 05:30:31 +05:30
|
|
|
line = ifile.readline().split()
|
2004-10-11 02:46:44 +05:30
|
|
|
if len(line) == 0:
|
|
|
|
break
|
2006-02-16 10:36:40 +05:30
|
|
|
if len(line) > 2 and line[1][0:4] == 'CHAR' and line[2] == "ANSEL":
|
2004-09-25 03:35:46 +05:30
|
|
|
ansel = True
|
2006-02-16 10:36:40 +05:30
|
|
|
if len(line) > 2 and line[1][0:4] == 'SOUR' and line[2] == "GRAMPS":
|
2004-09-25 03:35:46 +05:30
|
|
|
gramps = True
|
2007-02-14 05:30:31 +05:30
|
|
|
ifile.close()
|
2004-09-25 03:35:46 +05:30
|
|
|
|
|
|
|
if not gramps and ansel:
|
2009-04-17 20:28:25 +05:30
|
|
|
glade_file = os.path.join(
|
|
|
|
os.path.split(__file__)[0],
|
|
|
|
_GLADE_FILE)
|
|
|
|
top = gtk.Builder()
|
|
|
|
top.add_from_file(glade_file)
|
|
|
|
code = top.get_object('codeset')
|
2004-09-25 03:35:46 +05:30
|
|
|
code.set_active(0)
|
2009-04-17 20:28:25 +05:30
|
|
|
dialog = top.get_object('encoding')
|
2004-09-25 03:35:46 +05:30
|
|
|
dialog.run()
|
2007-07-18 10:03:20 +05:30
|
|
|
enc = ['ANSEL', 'ANSEL', 'ANSI', 'ASCII', 'UTF-8']
|
|
|
|
code_set = enc[ code.get_active()]
|
2004-09-25 03:35:46 +05:30
|
|
|
dialog.destroy()
|
|
|
|
else:
|
2007-07-18 09:47:30 +05:30
|
|
|
code_set = ""
|
|
|
|
|
2008-05-26 01:25:47 +05:30
|
|
|
assert(isinstance(code_set, basestring))
|
2007-07-18 09:47:30 +05:30
|
|
|
|
2002-10-20 19:55:16 +05:30
|
|
|
try:
|
2008-02-18 18:01:31 +05:30
|
|
|
ifile = open(filename, "rU")
|
2007-09-08 02:54:01 +05:30
|
|
|
stage_one = StageOne(ifile)
|
|
|
|
stage_one.parse()
|
2007-02-25 10:56:32 +05:30
|
|
|
|
|
|
|
if code_set:
|
2007-09-08 02:54:01 +05:30
|
|
|
stage_one.set_encoding(code_set)
|
|
|
|
ifile.seek(0)
|
|
|
|
gedparse = GedcomParser(database, ifile, filename, callback,
|
|
|
|
stage_one, DEFAULT_SOURCE)
|
2007-02-14 05:30:31 +05:30
|
|
|
except IOError, msg:
|
|
|
|
ErrorDialog(_("%s could not be opened\n") % filename, str(msg))
|
2002-10-20 19:55:16 +05:30
|
|
|
return
|
2007-02-23 11:33:02 +05:30
|
|
|
except Errors.GedcomError, msg:
|
|
|
|
ErrorDialog(_("Invalid GEDCOM file"),
|
|
|
|
_("%s could not be imported") % filename + "\n" + str(msg))
|
|
|
|
return
|
2002-10-20 19:55:16 +05:30
|
|
|
|
2003-01-15 10:55:50 +05:30
|
|
|
try:
|
2007-02-14 05:30:31 +05:30
|
|
|
read_only = database.readonly
|
2006-12-16 09:17:03 +05:30
|
|
|
database.readonly = False
|
2008-11-04 09:42:51 +05:30
|
|
|
gedparse.parse_gedcom_file(False)
|
2007-02-14 05:30:31 +05:30
|
|
|
database.readonly = read_only
|
|
|
|
ifile.close()
|
|
|
|
except IOError, msg:
|
|
|
|
msg = _("%s could not be opened\n") % filename
|
|
|
|
ErrorDialog(msg, str(msg))
|
2003-01-15 10:55:50 +05:30
|
|
|
return
|
2007-06-13 07:57:31 +05:30
|
|
|
except Errors.DbError, msg:
|
|
|
|
DBErrorDialog(str(msg.value))
|
2005-12-06 12:08:09 +05:30
|
|
|
return
|
2006-11-11 10:32:26 +05:30
|
|
|
except Errors.GedcomError, msg:
|
|
|
|
ErrorDialog(_('Error reading GEDCOM file'), str(msg))
|
|
|
|
return
|
2006-03-05 10:01:24 +05:30
|
|
|
|
2008-11-04 09:42:51 +05:30
|
|
|
#------------------------------------------------------------------------
|
|
|
|
#
|
|
|
|
# Register with the plugin system
|
|
|
|
#
|
|
|
|
#------------------------------------------------------------------------
|
|
|
|
_description = _('GEDCOM is used to transfer data between genealogy programs. '
|
|
|
|
'Most genealogy software will accept a GEDCOM file as input.')
|
|
|
|
|
|
|
|
pmgr = PluginManager.get_instance()
|
2008-12-01 18:28:59 +05:30
|
|
|
plugin = ImportPlugin(name = _('GEDCOM'),
|
2008-11-04 09:42:51 +05:30
|
|
|
description = _description,
|
|
|
|
import_function = importData,
|
|
|
|
extension = "ged" )
|
2009-04-17 20:28:25 +05:30
|
|
|
pmgr.register_plugin(plugin)
|