2002-10-20 14:25:16 +00:00
|
|
|
#
|
|
|
|
# Gramps - a GTK+/GNOME based genealogy program
|
|
|
|
#
|
2006-02-02 14:53:31 +00:00
|
|
|
# Copyright (C) 2000-2006 Donald N. Allingham
|
2002-10-20 14:25:16 +00:00
|
|
|
#
|
|
|
|
# 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
|
|
|
|
#
|
|
|
|
|
2004-06-28 13:20:33 +00:00
|
|
|
# $Id$
|
2003-10-23 15:33:57 +00:00
|
|
|
|
2002-10-20 14:25:16 +00:00
|
|
|
"Import from GEDCOM"
|
|
|
|
|
|
|
|
import os
|
2007-02-14 00:00:31 +00:00
|
|
|
import gtk
|
2006-01-25 05:07:10 +00:00
|
|
|
|
2003-01-15 05:25:50 +00:00
|
|
|
import Errors
|
2007-02-20 23:58:39 +00:00
|
|
|
from _GedcomParse import GedcomParser, StageOne
|
2007-06-13 02:27:31 +00:00
|
|
|
from QuestionDialog import ErrorDialog, DBErrorDialog
|
2002-10-20 14:25:16 +00:00
|
|
|
|
|
|
|
#-------------------------------------------------------------------------
|
|
|
|
#
|
|
|
|
#
|
|
|
|
#
|
|
|
|
#-------------------------------------------------------------------------
|
2006-01-22 02:06:46 +00:00
|
|
|
def importData(database, filename, callback=None, use_trans=False):
|
2002-10-20 14:25:16 +00:00
|
|
|
|
2006-09-24 04:37:59 +00:00
|
|
|
try:
|
2007-02-14 00:00:31 +00:00
|
|
|
ifile = open(filename, "r")
|
2006-09-24 04:37:59 +00:00
|
|
|
except IOError:
|
|
|
|
return
|
2004-09-24 22:05:46 +00:00
|
|
|
|
|
|
|
ansel = False
|
|
|
|
gramps = False
|
2005-01-16 04:00:35 +00:00
|
|
|
for index in range(50):
|
2007-02-14 00:00:31 +00:00
|
|
|
line = ifile.readline().split()
|
2004-10-10 21:16:44 +00:00
|
|
|
if len(line) == 0:
|
|
|
|
break
|
2006-02-16 05:06:40 +00:00
|
|
|
if len(line) > 2 and line[1][0:4] == 'CHAR' and line[2] == "ANSEL":
|
2004-09-24 22:05:46 +00:00
|
|
|
ansel = True
|
2006-02-16 05:06:40 +00:00
|
|
|
if len(line) > 2 and line[1][0:4] == 'SOUR' and line[2] == "GRAMPS":
|
2004-09-24 22:05:46 +00:00
|
|
|
gramps = True
|
2007-02-14 00:00:31 +00:00
|
|
|
ifile.close()
|
2004-09-24 22:05:46 +00:00
|
|
|
|
|
|
|
if not gramps and ansel:
|
2006-01-13 02:35:11 +00:00
|
|
|
glade_file = "%s/gedcomimport.glade" % os.path.dirname(__file__)
|
2007-02-14 00:00:31 +00:00
|
|
|
top = gtk.glade.XML(glade_file, 'encoding','gramps')
|
2004-09-24 22:05:46 +00:00
|
|
|
code = top.get_widget('codeset')
|
|
|
|
code.set_active(0)
|
|
|
|
dialog = top.get_widget('encoding')
|
|
|
|
dialog.run()
|
2007-02-14 00:00:31 +00:00
|
|
|
code_set = code.get_active()
|
2004-09-24 22:05:46 +00:00
|
|
|
dialog.destroy()
|
|
|
|
else:
|
2007-02-14 00:00:31 +00:00
|
|
|
code_set = None
|
2007-02-25 05:26:32 +00:00
|
|
|
|
2007-02-14 00:00:31 +00:00
|
|
|
import2(database, filename, callback, code_set, use_trans)
|
2004-09-24 22:05:46 +00:00
|
|
|
|
2007-02-14 00:00:31 +00:00
|
|
|
def import2(database, filename, callback, code_set, use_trans):
|
2002-10-20 14:25:16 +00:00
|
|
|
# add some checking here
|
|
|
|
try:
|
2007-02-14 00:00:31 +00:00
|
|
|
ifile = open(filename,"rU")
|
2007-02-19 22:50:09 +00:00
|
|
|
np = StageOne(ifile)
|
|
|
|
np.parse()
|
2007-02-25 05:26:32 +00:00
|
|
|
|
|
|
|
if code_set:
|
|
|
|
np.set_encoding(code_set)
|
|
|
|
|
2007-02-19 22:50:09 +00:00
|
|
|
ifile.seek(0)
|
2007-02-20 23:58:39 +00:00
|
|
|
gedparse = GedcomParser(database, ifile, filename, callback, np)
|
2007-02-14 00:00:31 +00:00
|
|
|
except IOError, msg:
|
|
|
|
ErrorDialog(_("%s could not be opened\n") % filename, str(msg))
|
2002-10-20 14:25:16 +00:00
|
|
|
return
|
2007-02-23 06:03:02 +00:00
|
|
|
except Errors.GedcomError, msg:
|
|
|
|
ErrorDialog(_("Invalid GEDCOM file"),
|
|
|
|
_("%s could not be imported") % filename + "\n" + str(msg))
|
|
|
|
return
|
2002-10-20 14:25:16 +00:00
|
|
|
|
2004-10-19 03:19:25 +00:00
|
|
|
if database.get_number_of_people() == 0:
|
|
|
|
use_trans = False
|
|
|
|
|
2003-01-15 05:25:50 +00:00
|
|
|
try:
|
2007-02-14 00:00:31 +00:00
|
|
|
read_only = database.readonly
|
2006-12-16 03:47:03 +00:00
|
|
|
database.readonly = False
|
2007-02-14 00:00:31 +00:00
|
|
|
close = gedparse.parse_gedcom_file(use_trans)
|
|
|
|
database.readonly = read_only
|
|
|
|
ifile.close()
|
|
|
|
except IOError, msg:
|
|
|
|
msg = _("%s could not be opened\n") % filename
|
|
|
|
ErrorDialog(msg, str(msg))
|
2003-01-15 05:25:50 +00:00
|
|
|
return
|
2007-06-13 02:27:31 +00:00
|
|
|
except Errors.DbError, msg:
|
|
|
|
DBErrorDialog(str(msg.value))
|
2005-12-06 06:38:09 +00:00
|
|
|
return
|
2006-11-11 05:02:26 +00:00
|
|
|
except Errors.GedcomError, msg:
|
|
|
|
ErrorDialog(_('Error reading GEDCOM file'), str(msg))
|
|
|
|
return
|
2006-03-05 04:31:24 +00:00
|
|
|
|
2007-02-14 00:00:31 +00:00
|
|
|
def import_from_string(database, text, callback, code_set, use_trans):
|
|
|
|
# add some checking here
|
2004-09-17 03:30:04 +00:00
|
|
|
|
2007-02-14 00:00:31 +00:00
|
|
|
from cStringIO import StringIO
|
2002-10-20 14:25:16 +00:00
|
|
|
|
2007-02-14 00:00:31 +00:00
|
|
|
ifile = StringIO(text)
|
2002-10-20 14:25:16 +00:00
|
|
|
|
2007-02-14 00:00:31 +00:00
|
|
|
try:
|
|
|
|
np = NoteParser(ifile, False, code_set)
|
|
|
|
ifile.seek(0)
|
|
|
|
gedparse = GedcomParser(database, ifile, "inline-string", callback,
|
|
|
|
code_set, np.get_map(), np.get_lines(),
|
|
|
|
np.get_persons())
|
|
|
|
except IOError, msg:
|
|
|
|
ErrorDialog(_("%s could not be opened\n") % "inline-string", str(msg))
|
2004-06-27 03:10:06 +00:00
|
|
|
return
|
2006-06-05 18:49:31 +00:00
|
|
|
|
2007-02-14 00:00:31 +00:00
|
|
|
if database.get_number_of_people() == 0:
|
|
|
|
use_trans = False
|
2007-01-12 05:16:29 +00:00
|
|
|
|
2007-02-14 00:00:31 +00:00
|
|
|
try:
|
|
|
|
read_only = database.readonly
|
|
|
|
database.readonly = False
|
|
|
|
gedparse.parse_gedcom_file(use_trans)
|
|
|
|
database.readonly = read_only
|
|
|
|
ifile.close()
|
|
|
|
except IOError, msg:
|
|
|
|
msg = _("%s could not be opened\n") % 'inline-string'
|
|
|
|
ErrorDialog(msg, str(msg))
|
|
|
|
return
|
2007-06-13 02:27:31 +00:00
|
|
|
except Errors.DbError, msg:
|
2007-02-14 00:00:31 +00:00
|
|
|
WarningDialog(_('Database corruption detected'),
|
|
|
|
_('A problem was detected with the database. Please '
|
|
|
|
'run the Check and Repair Database tool to fix the '
|
|
|
|
'problem.'))
|
|
|
|
return
|
|
|
|
except Errors.GedcomError, msg:
|
|
|
|
ErrorDialog(_('Error reading GEDCOM file'), str(msg))
|
2006-02-19 04:45:54 +00:00
|
|
|
return
|
2006-01-27 05:01:48 +00:00
|
|
|
|