2003-05-06 07:21:47 +05:30
|
|
|
|
2002-10-20 19:55:16 +05:30
|
|
|
# Gramps - a GTK+/GNOME based genealogy program
|
|
|
|
#
|
|
|
|
# Copyright (C) 2000 Donald N. Allingham
|
|
|
|
#
|
|
|
|
# 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
|
|
|
|
#
|
|
|
|
|
2003-05-06 07:21:47 +05:30
|
|
|
#
|
|
|
|
# Modified by Alex Roitman to handle media object files.
|
|
|
|
#
|
|
|
|
|
2002-10-20 19:55:16 +05:30
|
|
|
#-------------------------------------------------------------------------
|
|
|
|
#
|
|
|
|
# Standard Python Modules
|
|
|
|
#
|
|
|
|
#-------------------------------------------------------------------------
|
|
|
|
import string
|
|
|
|
import os
|
2003-05-06 20:46:04 +05:30
|
|
|
import shutil
|
2003-01-19 11:55:20 +05:30
|
|
|
from xml.parsers.expat import ExpatError
|
2002-10-20 19:55:16 +05:30
|
|
|
|
|
|
|
#-------------------------------------------------------------------------
|
|
|
|
#
|
|
|
|
# Gramps Modules
|
|
|
|
#
|
|
|
|
#-------------------------------------------------------------------------
|
2003-01-10 10:51:32 +05:30
|
|
|
import RelLib
|
2002-10-20 19:55:16 +05:30
|
|
|
from GrampsParser import GrampsParser, GrampsImportParser
|
2003-01-02 10:01:52 +05:30
|
|
|
from QuestionDialog import ErrorDialog, WarningDialog
|
2002-10-21 06:48:07 +05:30
|
|
|
from intl import gettext as _
|
2002-10-20 19:55:16 +05:30
|
|
|
|
|
|
|
#-------------------------------------------------------------------------
|
|
|
|
#
|
|
|
|
# Try to detect the presence of gzip
|
|
|
|
#
|
|
|
|
#-------------------------------------------------------------------------
|
|
|
|
try:
|
|
|
|
import gzip
|
|
|
|
gzip_ok = 1
|
|
|
|
except:
|
|
|
|
gzip_ok = 0
|
|
|
|
|
|
|
|
#-------------------------------------------------------------------------
|
|
|
|
#
|
2003-05-06 20:46:04 +05:30
|
|
|
# Importing data into the currently open database.
|
|
|
|
# Must takes care of renaming media files according to their new IDs.
|
2002-10-20 19:55:16 +05:30
|
|
|
#
|
|
|
|
#-------------------------------------------------------------------------
|
|
|
|
def importData(database, filename, callback):
|
|
|
|
|
2003-03-28 07:58:04 +05:30
|
|
|
filename = os.path.normpath(filename)
|
2002-10-20 19:55:16 +05:30
|
|
|
basefile = os.path.dirname(filename)
|
|
|
|
database.smap = {}
|
|
|
|
database.pmap = {}
|
|
|
|
database.fmap = {}
|
|
|
|
|
|
|
|
parser = GrampsImportParser(database,callback,basefile)
|
|
|
|
|
|
|
|
if gzip_ok:
|
|
|
|
use_gzip = 1
|
|
|
|
try:
|
|
|
|
f = gzip.open(filename,"r")
|
|
|
|
f.read(1)
|
|
|
|
f.close()
|
|
|
|
except IOError,msg:
|
|
|
|
use_gzip = 0
|
2003-04-20 09:22:54 +05:30
|
|
|
except ValueError, msg:
|
|
|
|
use_gzip = 1
|
2002-10-20 19:55:16 +05:30
|
|
|
else:
|
|
|
|
use_gzip = 0
|
|
|
|
|
|
|
|
try:
|
|
|
|
if use_gzip:
|
|
|
|
xml_file = gzip.open(filename,"rb")
|
|
|
|
else:
|
|
|
|
xml_file = open(filename,"r")
|
|
|
|
except IOError,msg:
|
2003-02-24 10:21:57 +05:30
|
|
|
ErrorDialog(_("%s could not be opened") % filename,str(msg))
|
2002-10-20 19:55:16 +05:30
|
|
|
return 0
|
|
|
|
except:
|
2003-02-24 10:21:57 +05:30
|
|
|
ErrorDialog(_("%s could not be opened") % filename)
|
2002-10-20 19:55:16 +05:30
|
|
|
return 0
|
|
|
|
|
|
|
|
try:
|
|
|
|
parser.parse(xml_file)
|
|
|
|
except IOError,msg:
|
2003-02-24 10:21:57 +05:30
|
|
|
ErrorDialog(_("Error reading %s") % filename,str(msg))
|
2002-10-20 19:55:16 +05:30
|
|
|
import traceback
|
|
|
|
traceback.print_exc()
|
|
|
|
return 0
|
2003-01-19 11:55:20 +05:30
|
|
|
except ExpatError, msg:
|
2003-02-24 10:21:57 +05:30
|
|
|
ErrorDialog(_("Error reading %s") % filename,
|
|
|
|
_("The file is probably either corrupt or not a valid GRAMPS database."))
|
2003-01-19 11:55:20 +05:30
|
|
|
return 0
|
|
|
|
except ValueError, msg:
|
2003-04-20 09:22:54 +05:30
|
|
|
pass
|
2002-10-20 19:55:16 +05:30
|
|
|
except:
|
|
|
|
import DisplayTrace
|
|
|
|
DisplayTrace.DisplayTrace()
|
|
|
|
return 0
|
|
|
|
|
|
|
|
xml_file.close()
|
2003-05-05 23:05:30 +05:30
|
|
|
|
|
|
|
# Rename media files if they were conflicting with existing ones
|
2003-05-07 09:44:50 +05:30
|
|
|
ObjectMap = database.getObjectMap()
|
2003-05-06 20:46:04 +05:30
|
|
|
newpath = database.getSavePath()
|
|
|
|
for OldMediaID in parser.MediaFileMap.keys():
|
2003-05-05 23:05:30 +05:30
|
|
|
NewMediaID = parser.MediaFileMap[OldMediaID]
|
2003-05-06 20:46:04 +05:30
|
|
|
oldfile = ObjectMap[NewMediaID].getPath()
|
|
|
|
oldpath = os.path.dirname(oldfile)
|
|
|
|
(junk,oldext) = os.path.splitext(os.path.basename(oldfile))
|
|
|
|
oldfile = os.path.join(basefile,OldMediaID+oldext)
|
|
|
|
newfile = os.path.join(newpath,NewMediaID+oldext)
|
2003-05-07 09:44:50 +05:30
|
|
|
try:
|
|
|
|
shutil.copy2(oldfile,newfile)
|
|
|
|
ObjectMap[NewMediaID].setPath(os.path.join(newfile))
|
|
|
|
ObjectMap[NewMediaID].setLocal(1)
|
|
|
|
except:
|
|
|
|
# File is lost => remove all references and the object itself
|
|
|
|
mobj = ObjectMap[NewMediaID]
|
|
|
|
for p in database.getFamilyMap().values():
|
|
|
|
nl = p.getPhotoList()
|
|
|
|
for o in nl:
|
|
|
|
if o.getReference() == mobj:
|
|
|
|
nl.remove(o)
|
|
|
|
p.setPhotoList(nl)
|
|
|
|
for key in database.getPersonKeys():
|
|
|
|
p = database.getPerson(key)
|
|
|
|
nl = p.getPhotoList()
|
|
|
|
for o in nl:
|
|
|
|
if o.getReference() == mobj:
|
|
|
|
nl.remove(o)
|
|
|
|
p.setPhotoList(nl)
|
|
|
|
for key in database.getSourceKeys():
|
|
|
|
p = database.getSource(key)
|
|
|
|
nl = p.getPhotoList()
|
|
|
|
for o in nl:
|
|
|
|
if o.getReference() == mobj:
|
|
|
|
nl.remove(o)
|
|
|
|
p.setPhotoList(nl)
|
|
|
|
for key in database.getPlaceKeys():
|
|
|
|
p = database.getPlace(key)
|
|
|
|
nl = p.getPhotoList()
|
|
|
|
for o in nl:
|
|
|
|
if o.getReference() == mobj:
|
|
|
|
nl.remove(o)
|
|
|
|
p.setPhotoList(nl)
|
|
|
|
|
|
|
|
database.removeObject(NewMediaID)
|
2003-05-05 23:05:30 +05:30
|
|
|
|
2002-10-20 19:55:16 +05:30
|
|
|
return 1
|
|
|
|
|
|
|
|
#-------------------------------------------------------------------------
|
|
|
|
#
|
|
|
|
# Initialization function for the module. Called to start the reading
|
|
|
|
# of data.
|
|
|
|
#
|
|
|
|
#-------------------------------------------------------------------------
|
|
|
|
def loadData(database, filename, callback=None):
|
|
|
|
|
|
|
|
basefile = os.path.dirname(filename)
|
|
|
|
database.smap = {}
|
|
|
|
database.pmap = {}
|
|
|
|
database.fmap = {}
|
|
|
|
|
|
|
|
filename = os.path.normpath(filename)
|
|
|
|
|
|
|
|
parser = GrampsParser(database,callback,basefile)
|
|
|
|
|
|
|
|
if gzip_ok:
|
|
|
|
use_gzip = 1
|
|
|
|
try:
|
|
|
|
f = gzip.open(filename,"r")
|
|
|
|
f.read(1)
|
|
|
|
f.close()
|
|
|
|
except IOError,msg:
|
|
|
|
use_gzip = 0
|
|
|
|
else:
|
|
|
|
use_gzip = 0
|
|
|
|
|
|
|
|
try:
|
|
|
|
if use_gzip:
|
|
|
|
xml_file = gzip.open(filename,"rb")
|
|
|
|
else:
|
|
|
|
xml_file = open(filename,"r")
|
|
|
|
except IOError,msg:
|
2003-02-24 10:21:57 +05:30
|
|
|
filemsg = _("%s could not be opened.") % filename
|
|
|
|
ErrorDialog(filemsg,str(msg))
|
2002-10-20 19:55:16 +05:30
|
|
|
return 0
|
|
|
|
except:
|
2003-02-24 10:21:57 +05:30
|
|
|
ErrorDialog(_("%s could not be opened.") % filename)
|
2002-10-20 19:55:16 +05:30
|
|
|
return 0
|
|
|
|
|
|
|
|
try:
|
|
|
|
parser.parse(xml_file)
|
|
|
|
except IOError,msg:
|
2003-02-24 10:21:57 +05:30
|
|
|
ErrorDialog(_("Error reading %s") % filename, str(msg))
|
2002-10-20 19:55:16 +05:30
|
|
|
import traceback
|
|
|
|
traceback.print_exc()
|
|
|
|
return 0
|
|
|
|
except:
|
2002-10-21 06:48:07 +05:30
|
|
|
ErrorDialog(_("Error reading %s") % filename)
|
2002-10-20 19:55:16 +05:30
|
|
|
import traceback
|
|
|
|
traceback.print_exc()
|
|
|
|
return 0
|
|
|
|
|
|
|
|
xml_file.close()
|
|
|
|
return 1
|
|
|
|
|
|
|
|
#-------------------------------------------------------------------------
|
|
|
|
#
|
|
|
|
# Initialization function for the module. Called to start the reading
|
|
|
|
# of data.
|
|
|
|
#
|
|
|
|
#-------------------------------------------------------------------------
|
|
|
|
def loadRevision(database, file, filename, revision, callback=None):
|
|
|
|
|
|
|
|
basefile = os.path.dirname(filename)
|
|
|
|
database.smap = {}
|
|
|
|
database.pmap = {}
|
|
|
|
database.fmap = {}
|
|
|
|
|
|
|
|
parser = GrampsParser(database,callback,basefile)
|
|
|
|
|
|
|
|
filename = _("%s (revision %s)") % (filename,revision)
|
|
|
|
|
|
|
|
try:
|
|
|
|
parser.parse(file)
|
|
|
|
except IOError,msg:
|
2003-02-24 10:21:57 +05:30
|
|
|
ErrorDialog(_("Error reading %s") % filename,str(msg))
|
2002-10-20 19:55:16 +05:30
|
|
|
import traceback
|
|
|
|
traceback.print_exc()
|
|
|
|
return 0
|
|
|
|
except:
|
|
|
|
import DisplayTrace
|
|
|
|
DisplayTrace.DisplayTrace()
|
|
|
|
return 0
|
|
|
|
|
|
|
|
file.close()
|
|
|
|
return 1
|