2
# Gramps - a GTK+/GNOME based genealogy program
4
# Copyright (C) 2000-2004 Donald N. Allingham
6
# This program is free software; you can redistribute it and/or modify
7
# it under the terms of the GNU General Public License as published by
8
# the Free Software Foundation; either version 2 of the License, or
9
# (at your option) any later version.
11
# This program is distributed in the hope that it will be useful,
12
# but WITHOUT ANY WARRANTY; without even the implied warranty of
13
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
# GNU General Public License for more details.
16
# You should have received a copy of the GNU General Public License
17
# along with this program; if not, write to the Free Software
18
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21
# $Id: ReadXML.py,v 1.27 2004/01/17 06:51:49 rshura Exp $
23
#-------------------------------------------------------------------------
25
# Standard Python Modules
27
#-------------------------------------------------------------------------
32
from xml.parsers.expat import ExpatError
34
#-------------------------------------------------------------------------
38
#-------------------------------------------------------------------------
40
from GrampsParser import GrampsParser, GrampsImportParser
41
from QuestionDialog import ErrorDialog, WarningDialog, MissingMediaDialog
42
from gettext import gettext as _
45
#-------------------------------------------------------------------------
47
# Try to detect the presence of gzip
49
#-------------------------------------------------------------------------
56
#-------------------------------------------------------------------------
58
# Importing data into the currently open database.
59
# Must takes care of renaming media files according to their new IDs.
61
#-------------------------------------------------------------------------
62
def importData(database, filename, callback,cl=0):
64
filename = os.path.normpath(filename)
65
basefile = os.path.dirname(filename)
71
parser = GrampsImportParser(database,callback,basefile)
76
f = gzip.open(filename,"r")
81
except ValueError, msg:
88
xml_file = gzip.open(filename,"rb")
90
xml_file = open(filename,"r")
93
print "Error: %s could not be opened Exiting." % filename
97
ErrorDialog(_("%s could not be opened") % filename,str(msg))
101
print "Error: %s could not be opened. Exiting." % filename
104
ErrorDialog(_("%s could not be opened") % filename)
108
parser.parse(xml_file)
111
print "Error reading %s" % filename
114
traceback.print_exc()
117
ErrorDialog(_("Error reading %s") % filename,str(msg))
119
traceback.print_exc()
121
except ExpatError, msg:
123
print "Error reading %s" % filename
124
print "The file is probably either corrupt or not a valid GRAMPS database."
127
ErrorDialog(_("Error reading %s") % filename,
128
_("The file is probably either corrupt or not a valid GRAMPS database."))
130
except ValueError, msg:
135
traceback.print_exc()
139
DisplayTrace.DisplayTrace()
144
#-------------------------------------------------------------------------
145
def remove_clicked():
146
# File is lost => remove all references and the object itself
147
mobj = ObjectMap[NewMediaID]
148
for p in database.getFamilyMap().values():
149
nl = p.getPhotoList()
151
if o.getReference() == mobj:
154
for key in database.getPersonKeys():
155
p = database.getPerson(key)
156
nl = p.getPhotoList()
158
if o.getReference() == mobj:
161
for key in database.getSourceKeys():
162
p = database.getSource(key)
163
nl = p.getPhotoList()
165
if o.getReference() == mobj:
168
for key in database.getPlaceKeys():
169
p = database.getPlace(key)
170
nl = p.getPhotoList()
172
if o.getReference() == mobj:
175
database.removeObject(NewMediaID)
179
# File is lost => do nothing, leave as is
183
def select_clicked():
184
# File is lost => select a file to replace the lost one
185
def fs_close_window(obj):
188
def fs_ok_clicked(obj):
189
name = fs_top.get_filename()
190
if os.path.isfile(name):
191
shutil.copyfile(name,newfile)
193
shutil.copystat(name,newfile)
197
fs_top = gtk.FileSelection("%s - GRAMPS" % _("Select file"))
198
fs_top.hide_fileop_buttons()
199
fs_top.ok_button.connect('clicked',fs_ok_clicked)
200
fs_top.cancel_button.connect('clicked',fs_close_window)
204
#-------------------------------------------------------------------------
206
# Rename media files if they were conflicting with existing ones
207
ObjectMap = database.getObjectMap()
208
newpath = database.getSavePath()
209
for OldMediaID in parser.MediaFileMap.keys():
210
NewMediaID = parser.MediaFileMap[OldMediaID]
211
oldfile = ObjectMap[NewMediaID].getPath()
212
(junk,oldext) = os.path.splitext(os.path.basename(oldfile))
213
oldfile = os.path.join(basefile,OldMediaID+oldext)
214
newfile = os.path.join(newpath,NewMediaID+oldext)
215
ObjectMap[NewMediaID].setPath(newfile)
216
ObjectMap[NewMediaID].setLocal(1)
218
shutil.copyfile(oldfile,newfile)
220
shutil.copystat(oldfile,newfile)
225
print "Warning: media file %s was not found," \
226
% os.path.basename(oldfile), "so it was ignored."
228
# File is lost => ask what to do (if we were not told yet)
229
if missmedia_action == 0:
230
mmd = MissingMediaDialog(_("Media object could not be found"),
231
_("%(file_name)s is referenced in the database, but no longer exists. "
232
"The file may have been deleted or moved to a different location. "
233
"You may choose to either remove the reference from the database, "
234
"keep the reference to the missing file, or select a new file."
235
) % { 'file_name' : oldfile },
236
remove_clicked, leave_clicked, select_clicked)
237
missmedia_action = mmd.default_action
238
elif missmedia_action == 1:
240
elif missmedia_action == 2:
242
elif missmedia_action == 3:
249
#-------------------------------------------------------------------------
251
# Initialization function for the module. Called to start the reading
254
#-------------------------------------------------------------------------
255
def loadData(database, filename, callback=None):
257
basefile = os.path.dirname(filename)
262
filename = os.path.normpath(filename)
264
parser = GrampsParser(database,callback,basefile)
269
f = gzip.open(filename,"r")
279
xml_file = gzip.open(filename,"rb")
281
xml_file = open(filename,"r")
283
filemsg = _("%s could not be opened.") % filename
284
ErrorDialog(filemsg,str(msg))
287
ErrorDialog(_("%s could not be opened.") % filename)
291
parser.parse(xml_file)
293
ErrorDialog(_("Error reading %s") % filename, str(msg))
295
traceback.print_exc()
298
ErrorDialog(_("Error reading %s") % filename)
300
traceback.print_exc()
307
#-------------------------------------------------------------------------
309
# Initialization function for the module. Called to start the reading
312
#-------------------------------------------------------------------------
313
def loadRevision(database, file, filename, revision, callback=None):
315
basefile = os.path.dirname(filename)
320
parser = GrampsParser(database,callback,basefile)
322
filename = _("%s (revision %s)") % (filename,revision)
327
ErrorDialog(_("Error reading %s") % filename,str(msg))
329
traceback.print_exc()
333
DisplayTrace.DisplayTrace()