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 _
|
2007-02-14 05:30:31 +05:30
|
|
|
import gtk
|
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-09-08 02:54:01 +05:30
|
|
|
from _GedcomParse import GedcomParser
|
|
|
|
from _GedcomStageOne import StageOne
|
2007-06-13 07:57:31 +05:30
|
|
|
from QuestionDialog import ErrorDialog, DBErrorDialog
|
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
|
|
|
|
|
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
|
|
|
#
|
|
|
|
#-------------------------------------------------------------------------
|
2006-01-22 07:36:46 +05:30
|
|
|
def importData(database, filename, callback=None, use_trans=False):
|
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:
|
2006-01-13 08:05:11 +05:30
|
|
|
glade_file = "%s/gedcomimport.glade" % os.path.dirname(__file__)
|
2008-02-18 18:01:31 +05:30
|
|
|
top = gtk.glade.XML(glade_file, 'encoding', 'gramps')
|
2004-09-25 03:35:46 +05:30
|
|
|
code = top.get_widget('codeset')
|
|
|
|
code.set_active(0)
|
|
|
|
dialog = top.get_widget('encoding')
|
|
|
|
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 = ""
|
2007-02-14 05:30:31 +05:30
|
|
|
import2(database, filename, callback, code_set, use_trans)
|
2004-09-25 03:35:46 +05:30
|
|
|
|
2007-09-08 02:54:01 +05:30
|
|
|
#-------------------------------------------------------------------------
|
|
|
|
#
|
|
|
|
# import2
|
|
|
|
#
|
|
|
|
#-------------------------------------------------------------------------
|
2007-02-14 05:30:31 +05:30
|
|
|
def import2(database, filename, callback, code_set, use_trans):
|
2007-09-08 02:54:01 +05:30
|
|
|
"""
|
2008-02-18 18:01:31 +05:30
|
|
|
Import the gedcom file.
|
2007-09-08 02:54:01 +05:30
|
|
|
"""
|
2007-07-18 09:47:30 +05:30
|
|
|
|
|
|
|
assert(type(code_set) == str or type(code_set) == unicode)
|
|
|
|
|
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
|
|
|
|
2004-10-19 08:49:25 +05:30
|
|
|
if database.get_number_of_people() == 0:
|
|
|
|
use_trans = False
|
|
|
|
|
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
|
2007-09-08 02:54:01 +05:30
|
|
|
gedparse.parse_gedcom_file(use_trans)
|
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
|
|
|
|