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
|
|
|
|
#
|
|
|
|
#------------------------------------------------------------------------
|
2010-01-18 10:12:17 +05:30
|
|
|
from gen.ggettext 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
|
2007-06-13 07:57:31 +05:30
|
|
|
from QuestionDialog import ErrorDialog, DBErrorDialog
|
2009-05-15 01:45:59 +05:30
|
|
|
from glade import Glade
|
2009-12-25 01:44:15 +05:30
|
|
|
from libmixin import DbMixin
|
2009-12-30 00:50:16 +05:30
|
|
|
import libgedcom
|
2002-10-20 19:55:16 +05:30
|
|
|
|
2007-09-08 02:54:01 +05:30
|
|
|
try:
|
2009-10-08 06:42:51 +05:30
|
|
|
import config
|
|
|
|
DEFAULT_SOURCE = config.get('preferences.default-source')
|
2007-09-08 02:54:01 +05:30
|
|
|
except ImportError:
|
|
|
|
LOG.warn("No Config module available using defaults.")
|
|
|
|
DEFAULT_SOURCE = False
|
|
|
|
|
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
|
|
|
|
"""
|
2009-09-01 00:12:29 +05:30
|
|
|
|
2009-12-25 01:44:15 +05:30
|
|
|
if DbMixin not in database.__class__.__bases__:
|
|
|
|
database.__class__.__bases__ = (DbMixin,) + \
|
2009-09-01 00:12:29 +05:30
|
|
|
database.__class__.__bases__
|
|
|
|
|
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-05-15 01:45:59 +05:30
|
|
|
top = Glade()
|
2009-04-17 20:28:25 +05:30
|
|
|
code = top.get_object('codeset')
|
2004-09-25 03:35:46 +05:30
|
|
|
code.set_active(0)
|
2009-05-15 01:45:59 +05:30
|
|
|
dialog = top.toplevel
|
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")
|
2009-12-30 00:50:16 +05:30
|
|
|
stage_one = libgedcom.GedcomStageOne(ifile)
|
2007-09-08 02:54:01 +05:30
|
|
|
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)
|
2009-12-30 00:50:16 +05:30
|
|
|
gedparse = libgedcom.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
|