~rosco2/ubuntu/wily/gramps/bug-1492304

« back to all changes in this revision

Viewing changes to src/ReadXML.py

  • Committer: Bazaar Package Importer
  • Author(s): James A. Treacy
  • Date: 2004-06-16 16:53:36 UTC
  • Revision ID: james.westby@ubuntu.com-20040616165336-kjzczqef4gnxrn2b
Tags: upstream-1.0.4
ImportĀ upstreamĀ versionĀ 1.0.4

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#
 
2
# Gramps - a GTK+/GNOME based genealogy program
 
3
#
 
4
# Copyright (C) 2000-2004  Donald N. Allingham
 
5
#
 
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.
 
10
#
 
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.
 
15
#
 
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
 
19
#
 
20
 
 
21
# $Id: ReadXML.py,v 1.27 2004/01/17 06:51:49 rshura Exp $
 
22
 
 
23
#-------------------------------------------------------------------------
 
24
#
 
25
# Standard Python Modules
 
26
#
 
27
#-------------------------------------------------------------------------
 
28
import string
 
29
import os
 
30
import gtk
 
31
import shutil
 
32
from xml.parsers.expat import ExpatError
 
33
 
 
34
#-------------------------------------------------------------------------
 
35
#
 
36
# Gramps Modules
 
37
#
 
38
#-------------------------------------------------------------------------
 
39
import RelLib
 
40
from GrampsParser import GrampsParser, GrampsImportParser
 
41
from QuestionDialog import ErrorDialog, WarningDialog, MissingMediaDialog
 
42
from gettext import gettext as _
 
43
import Utils
 
44
 
 
45
#-------------------------------------------------------------------------
 
46
#
 
47
# Try to detect the presence of gzip
 
48
#
 
49
#-------------------------------------------------------------------------
 
50
try:
 
51
    import gzip
 
52
    gzip_ok = 1
 
53
except:
 
54
    gzip_ok = 0
 
55
 
 
56
#-------------------------------------------------------------------------
 
57
#
 
58
# Importing data into the currently open database. 
 
59
# Must takes care of renaming media files according to their new IDs.
 
60
#
 
61
#-------------------------------------------------------------------------
 
62
def importData(database, filename, callback,cl=0):
 
63
 
 
64
    filename = os.path.normpath(filename)
 
65
    basefile = os.path.dirname(filename)
 
66
    database.smap = {}
 
67
    database.pmap = {}
 
68
    database.fmap = {}
 
69
    missmedia_action = 0
 
70
 
 
71
    parser = GrampsImportParser(database,callback,basefile)
 
72
 
 
73
    if gzip_ok:
 
74
        use_gzip = 1
 
75
        try:
 
76
            f = gzip.open(filename,"r")
 
77
            f.read(1)
 
78
            f.close()
 
79
        except IOError,msg:
 
80
            use_gzip = 0
 
81
        except ValueError, msg:
 
82
            use_gzip = 1
 
83
    else:
 
84
        use_gzip = 0
 
85
 
 
86
    try:
 
87
        if use_gzip:
 
88
            xml_file = gzip.open(filename,"rb")
 
89
        else:
 
90
            xml_file = open(filename,"r")
 
91
    except IOError,msg:
 
92
        if cl:
 
93
            print "Error: %s could not be opened Exiting." % filename
 
94
            print msg
 
95
            os._exit(1)
 
96
        else:
 
97
            ErrorDialog(_("%s could not be opened") % filename,str(msg))
 
98
            return 0
 
99
    except:
 
100
        if cl:
 
101
            print "Error: %s could not be opened. Exiting." % filename
 
102
            os._exit(1)
 
103
        else:
 
104
            ErrorDialog(_("%s could not be opened") % filename)
 
105
            return 0
 
106
        
 
107
    try:
 
108
        parser.parse(xml_file)
 
109
    except IOError,msg:
 
110
        if cl:
 
111
            print "Error reading %s" % filename
 
112
            print msg
 
113
            import traceback
 
114
            traceback.print_exc()
 
115
            os._exit(1)
 
116
        else:
 
117
            ErrorDialog(_("Error reading %s") % filename,str(msg))
 
118
            import traceback
 
119
            traceback.print_exc()
 
120
            return 0
 
121
    except ExpatError, msg:
 
122
        if cl:
 
123
            print "Error reading %s" % filename
 
124
            print "The file is probably either corrupt or not a valid GRAMPS database."
 
125
            os._exit(1)
 
126
        else:
 
127
            ErrorDialog(_("Error reading %s") % filename,
 
128
                        _("The file is probably either corrupt or not a valid GRAMPS database."))
 
129
            return 0
 
130
    except ValueError, msg:
 
131
        pass
 
132
    except:
 
133
        if cl:
 
134
            import traceback
 
135
            traceback.print_exc()
 
136
            os._exit(1)
 
137
        else:
 
138
            import DisplayTrace
 
139
            DisplayTrace.DisplayTrace()
 
140
            return 0
 
141
 
 
142
    xml_file.close()
 
143
    
 
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()
 
150
            for o in nl:
 
151
                if o.getReference() == mobj:
 
152
                    nl.remove(o) 
 
153
            p.setPhotoList(nl)
 
154
        for key in database.getPersonKeys():
 
155
            p = database.getPerson(key)
 
156
            nl = p.getPhotoList()
 
157
            for o in nl:
 
158
                if o.getReference() == mobj:
 
159
                    nl.remove(o) 
 
160
            p.setPhotoList(nl)
 
161
        for key in database.getSourceKeys():
 
162
            p = database.getSource(key)
 
163
            nl = p.getPhotoList()
 
164
            for o in nl:
 
165
                if o.getReference() == mobj:
 
166
                    nl.remove(o) 
 
167
            p.setPhotoList(nl)
 
168
        for key in database.getPlaceKeys():
 
169
            p = database.getPlace(key)
 
170
            nl = p.getPhotoList()
 
171
            for o in nl:
 
172
                if o.getReference() == mobj:
 
173
                    nl.remove(o) 
 
174
            p.setPhotoList(nl)
 
175
        database.removeObject(NewMediaID) 
 
176
 
 
177
 
 
178
    def leave_clicked():
 
179
        # File is lost => do nothing, leave as is
 
180
        pass
 
181
 
 
182
 
 
183
    def select_clicked():
 
184
        # File is lost => select a file to replace the lost one
 
185
        def fs_close_window(obj):
 
186
            pass
 
187
 
 
188
        def fs_ok_clicked(obj):
 
189
            name = fs_top.get_filename()
 
190
            if os.path.isfile(name):
 
191
                shutil.copyfile(name,newfile)
 
192
                try:
 
193
                    shutil.copystat(name,newfile)
 
194
                except:
 
195
                    pass
 
196
 
 
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)
 
201
        fs_top.run()
 
202
        fs_top.destroy()
 
203
 
 
204
#-------------------------------------------------------------------------
 
205
 
 
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)
 
217
        try:
 
218
            shutil.copyfile(oldfile,newfile)
 
219
            try:
 
220
                shutil.copystat(oldfile,newfile)
 
221
            except:
 
222
                pass
 
223
        except:
 
224
            if cl:
 
225
                print "Warning: media file %s was not found," \
 
226
                    % os.path.basename(oldfile), "so it was ignored."
 
227
            else:
 
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:
 
239
                    remove_clicked()
 
240
                elif missmedia_action == 2:
 
241
                    leave_clicked()
 
242
                elif missmedia_action == 3:
 
243
                    select_clicked()
 
244
 
 
245
    del parser
 
246
    return 1
 
247
    
 
248
 
 
249
#-------------------------------------------------------------------------
 
250
#
 
251
# Initialization function for the module.  Called to start the reading
 
252
# of data.
 
253
#
 
254
#-------------------------------------------------------------------------
 
255
def loadData(database, filename, callback=None):
 
256
 
 
257
    basefile = os.path.dirname(filename)
 
258
    database.smap = {}
 
259
    database.pmap = {}
 
260
    database.fmap = {}
 
261
 
 
262
    filename = os.path.normpath(filename)
 
263
 
 
264
    parser = GrampsParser(database,callback,basefile)
 
265
 
 
266
    if gzip_ok:
 
267
        use_gzip = 1
 
268
        try:
 
269
            f = gzip.open(filename,"r")
 
270
            f.read(1)
 
271
            f.close()
 
272
        except IOError,msg:
 
273
            use_gzip = 0
 
274
    else:
 
275
        use_gzip = 0
 
276
 
 
277
    try:
 
278
        if use_gzip:
 
279
            xml_file = gzip.open(filename,"rb")
 
280
        else:
 
281
            xml_file = open(filename,"r")
 
282
    except IOError,msg:
 
283
        filemsg = _("%s could not be opened.") % filename
 
284
        ErrorDialog(filemsg,str(msg))
 
285
        return 0
 
286
    except:
 
287
        ErrorDialog(_("%s could not be opened.") % filename)
 
288
        return 0
 
289
 
 
290
    try:
 
291
        parser.parse(xml_file)
 
292
    except IOError,msg:
 
293
        ErrorDialog(_("Error reading %s") % filename, str(msg))
 
294
        import traceback
 
295
        traceback.print_exc()
 
296
        return 0
 
297
    except:
 
298
        ErrorDialog(_("Error reading %s") % filename)
 
299
        import traceback
 
300
        traceback.print_exc()
 
301
        return 0
 
302
 
 
303
    xml_file.close()
 
304
    del parser
 
305
    return 1
 
306
 
 
307
#-------------------------------------------------------------------------
 
308
#
 
309
# Initialization function for the module.  Called to start the reading
 
310
# of data.
 
311
#
 
312
#-------------------------------------------------------------------------
 
313
def loadRevision(database, file, filename, revision, callback=None):
 
314
 
 
315
    basefile = os.path.dirname(filename)
 
316
    database.smap = {}
 
317
    database.pmap = {}
 
318
    database.fmap = {}
 
319
 
 
320
    parser = GrampsParser(database,callback,basefile)
 
321
 
 
322
    filename = _("%s (revision %s)") % (filename,revision)
 
323
    
 
324
    try:
 
325
        parser.parse(file)
 
326
    except IOError,msg:
 
327
        ErrorDialog(_("Error reading %s") % filename,str(msg))
 
328
        import traceback
 
329
        traceback.print_exc()
 
330
        return 0
 
331
    except:
 
332
        import DisplayTrace
 
333
        DisplayTrace.DisplayTrace()
 
334
        return 0
 
335
 
 
336
    file.close()
 
337
    return 1