* src/ViewManager.py (ViewManager.import_data): Properly process

non-native (plugin) imports; (_do_import): factor common code into
the new method.
* src/GrampsDb/_ReadXML.py (GrampsParser.__init__): Skip updating
if callback is empty (non-callable).
* src/docgen/HtmlDoc.py (HtmlDoc.load_tpkg): Use tarfile.
* src/ArgHandler.py (ArgHandler.cl_import): Use tarfile.
* src/plugins/WritePkg.py (PackageWriter.export): Use tarfile.
* src/plugins/ReadPkg.py (impData): Use tarfile module.


svn: r6012
This commit is contained in:
Alex Roitman 2006-03-01 00:26:29 +00:00
parent 2b96e371e1
commit 6044ca5317
8 changed files with 91 additions and 215 deletions

View File

@ -1,4 +1,14 @@
2006-02-28 Alex Roitman <shura@gramps-project.org> 2006-02-28 Alex Roitman <shura@gramps-project.org>
* src/ViewManager.py (ViewManager.import_data): Properly process
non-native (plugin) imports; (_do_import): factor common code into
the new method.
* src/GrampsDb/_ReadXML.py (GrampsParser.__init__): Skip updating
if callback is empty (non-callable).
* src/docgen/HtmlDoc.py (HtmlDoc.load_tpkg): Use tarfile.
* src/ArgHandler.py (ArgHandler.cl_import): Use tarfile.
* src/plugins/WritePkg.py (PackageWriter.export): Use tarfile.
* src/plugins/ReadPkg.py (impData): Use tarfile module.
* various: merge changes made in gramps20 up until R2_0_10_real tag. * various: merge changes made in gramps20 up until R2_0_10_real tag.
2006-02-25 Don Allingham <don@gramps-project.org> 2006-02-25 Don Allingham <don@gramps-project.org>

View File

@ -456,12 +456,19 @@ class ArgHandler:
os.remove( os.path.join(tmpdir_path,fn) ) os.remove( os.path.join(tmpdir_path,fn) )
try: try:
import TarFile import tarfile
t = TarFile.ReadTarFile(filename,tmpdir_path) archive = tarfile.open(filename)
t.extract() for tarinfo in archive:
t.close() archive.extract(tarinfo,tmpdir_path)
archive.close()
except ReadError, msg:
print "Error reading archive:", msg
os._exit(1)
except CompressError, msg:
print "Error uncompressing archive:", msg
os._exit(1)
except: except:
print "Error extracting into %s" % tmpdir_path print "Error extracting into %s" % tmpdir_path
os._exit(1) os._exit(1)
dbname = os.path.join(tmpdir_path,const.xmlFile) dbname = os.path.join(tmpdir_path,const.xmlFile)

View File

@ -321,6 +321,10 @@ class GrampsParser:
self.media_file_map = {} self.media_file_map = {}
self.callback = callback self.callback = callback
if '__call__' in dir(self.callback): # callback is really callable
self.update = self.update_real
else:
self.update = self.update_empty
self.increment = 100 self.increment = 100
self.event = None self.event = None
self.eventref = None self.eventref = None
@ -1612,7 +1616,10 @@ class GrampsParser:
if self.func: if self.func:
self.tlist.append(data) self.tlist.append(data)
def update(self): def update_empty(self):
pass
def update_real(self):
line = self.p.CurrentLineNumber line = self.p.CurrentLineNumber
newval = int(100*line/self.linecount) newval = int(100*line/self.linecount)
if newval != self.oldval: if newval != self.oldval:

View File

@ -1,162 +0,0 @@
#
# Gramps - a GTK+/GNOME based genealogy program
#
# Copyright (C) 2000-2005 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
#
# $Id$
import gzip
import cStringIO
_BLKSIZE=512
nul = '\0'
#------------------------------------------------------------------------
#
# TarFile
#
#------------------------------------------------------------------------
class TarFile:
def __init__(self,name):
self.name = name
self.f = gzip.open(name,"wb")
self.pos = 0
def add_file(self,filename,mtime,iobuf):
iobuf.seek(0,2)
length = iobuf.tell()
iobuf.seek(0)
buf = filename
buf = buf + '\0'*(100-len(filename))
buf = buf + "0100664" + nul
buf = buf + "0000764" + nul
buf = buf + "0000764" + nul
buf = buf + "%011o" % length + nul
buf = buf + "%011o" % mtime + nul
buf = buf + "%s"
buf = buf + "0" + '\0'*100 + 'ustar \0'
buf = buf + '\0'*32
buf = buf + '\0'*32
buf = buf + '\0'*183
chksum = 0
blank = " "
temp = buf % (blank)
for c in temp:
chksum = chksum + ord(c)
vsum = "%06o " % chksum
vsum = vsum + nul
buf = buf % vsum
self.pos = self.pos + len(buf)
self.f.write(buf)
buf = iobuf.read(length)
self.f.write(buf)
self.pos = self.pos + length
rem = _BLKSIZE - (self.pos % _BLKSIZE)
if rem != 0:
self.f.write('\0' * rem)
self.pos = self.pos + rem
def close(self):
rem = (_BLKSIZE*20) - (self.pos % (_BLKSIZE*20))
if rem != 0:
self.f.write('\0' * rem)
self.f.close()
#------------------------------------------------------------------------
#
# ReadTarFile
#
#------------------------------------------------------------------------
class ReadTarFile:
def __init__(self,name,wd="/tmp"):
self.name = name
self.wd = wd
self.f = gzip.open(name,"rb")
self.pos = 0
def extract_files(self):
data = {}
while 1:
buf = self.f.read(100)
if buf == '':
return
index = 0
for b in buf:
if b != nul:
index = index + 1
else:
if index == 0:
return data
continue
filename = buf[0:index]
if filename == None:
return data
self.f.read(24) # modes
l = self.f.read(12).replace(chr(0),' ')
length = int(l,8)
self.f.read(12)
self.f.read(6)
self.f.read(111)
self.f.read(64)
self.f.read(183)
foo = cStringIO.StringIO()
data[filename] = foo
foo.write(self.f.read(length))
foo.reset()
self.f.read(_BLKSIZE-(length%_BLKSIZE))
return data
def extract(self):
while 1:
buf = self.f.read(100)
if buf == '':
return
index = 0
for b in buf:
if b != nul:
index = index + 1
else:
if index == 0:
return
continue
filename = buf[0:index]
self.f.read(24) # modes
l = self.f.read(12)
length_string = "";
for char in l:
if ord(char) != 0:
length_string = length_string + char
length = int(length_string,8)
self.f.read(12)
self.f.read(6)
self.f.read(111)
self.f.read(64)
self.f.read(183)
foo = open("%s/%s" % (self.wd,filename),"wb")
foo.write(self.f.read(length))
foo.close()
self.f.read(_BLKSIZE-(length%_BLKSIZE))
def close(self):
self.f.close()

View File

@ -929,38 +929,40 @@ class ViewManager:
str(msg)) str(msg))
return False return False
if filetype == const.app_gramps: # First we try our best formats
choose.destroy() if filetype in format_list:
self.window.window.set_cursor(gtk.gdk.Cursor(gtk.gdk.WATCH)) self._do_import(choose,
self.progress.show() GrampsDb.gramps_db_reader_factory(filetype))
GrampsDb.gramps_db_reader_factory(filetype)(self.state.db,filename,self.pulse_progressbar)
self.uistate.clear_history()
self.progress.hide()
self.window.window.set_cursor(None)
return True
elif filetype == const.app_gramps_xml or filetype == const.app_gedcom:
choose.destroy()
self.window.window.set_cursor(gtk.gdk.Cursor(gtk.gdk.WATCH))
self.progress.show()
GrampsDb.gramps_db_reader_factory(filetype)(self.state.db,filename,self.pulse_progressbar)
self.uistate.clear_history()
self.progress.hide()
self.window.window.set_cursor(None)
return True return True
# Then we try all the known plugins
(the_path,the_file) = os.path.split(filename) (the_path,the_file) = os.path.split(filename)
GrampsKeys.save_last_import_dir(the_path) GrampsKeys.save_last_import_dir(the_path)
for (importData,mime_filter,mime_type,native_format,format_name) in PluginMgr.import_list: for (importData,mime_filter,mime_type,
native_format,format_name) in PluginMgr.import_list:
if filetype == mime_type or the_file == mime_type: if filetype == mime_type or the_file == mime_type:
choose.destroy() self._do_import(choose,importData)
importData(self.state.db,filename)
return True return True
# Finally, we give up and declare this an unknown format
QuestionDialog.ErrorDialog( QuestionDialog.ErrorDialog(
_("Could not open file: %s") % filename, _("Could not open file: %s") % filename,
_('File type "%s" is unknown to GRAMPS.\n\nValid types are: GRAMPS database, GRAMPS XML, GRAMPS package, and GEDCOM.') % filetype) _('File type "%s" is unknown to GRAMPS.\n\n'
'Valid types are: GRAMPS database, GRAMPS XML, '
'GRAMPS package, and GEDCOM.') % filetype)
choose.destroy() choose.destroy()
return False return False
def _do_import(self,dialog,importer):
dialog.destroy()
self.window.window.set_cursor(gtk.gdk.Cursor(gtk.gdk.WATCH))
self.progress.show()
GrampsDb.gramps_db_reader_factory(filetype)(
self.state.db,filename,self.pulse_progressbar)
self.uistate.clear_history()
self.progress.hide()
self.window.window.set_cursor(None)
def build_tools_menu(self): def build_tools_menu(self):
self.toolactions = gtk.ActionGroup('ToolWindow') self.toolactions = gtk.ActionGroup('ToolWindow')

View File

@ -1,7 +1,7 @@
# #
# Gramps - a GTK+/GNOME based genealogy program # Gramps - a GTK+/GNOME based genealogy program
# #
# Copyright (C) 2000-2005 Donald N. Allingham # Copyright (C) 2000-2006 Donald N. Allingham
# #
# This program is free software; you can redistribute it and/or modify # 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 # it under the terms of the GNU General Public License as published by
@ -37,7 +37,7 @@ from gettext import gettext as _
#------------------------------------------------------------------------ #------------------------------------------------------------------------
import PluginMgr import PluginMgr
import ImgManip import ImgManip
import TarFile import tarfile
import const import const
import Errors import Errors
import BaseDoc import BaseDoc
@ -148,8 +148,10 @@ class HtmlDoc(BaseDoc.BaseDoc):
stop = re.compile(r"<!--\s*STOP\s*-->") stop = re.compile(r"<!--\s*STOP\s*-->")
top_add = 1 top_add = 1
bottom_add = 0 bottom_add = 0
tf = TarFile.ReadTarFile(self.template,None) archive = tarfile.open(self.template)
self.map = tf.extract_files() for tarinfo in archive:
self.map[tarinfo.name] = archive.extractfile(tarinfo)
archive.close()
templateFile = self.map['template.html'] templateFile = self.map['template.html']
while 1: while 1:
line = templateFile.readline() line = templateFile.readline()

View File

@ -29,6 +29,7 @@
# #
#------------------------------------------------------------------------- #-------------------------------------------------------------------------
import os import os
import tarfile
from gettext import gettext as _ from gettext import gettext as _
#------------------------------------------------------------------------- #-------------------------------------------------------------------------
@ -44,9 +45,8 @@ import gtk
# #
#------------------------------------------------------------------------- #-------------------------------------------------------------------------
import const import const
import ReadXML from GrampsDb import gramps_db_reader_factory
from QuestionDialog import ErrorDialog from QuestionDialog import ErrorDialog
import TarFile
#------------------------------------------------------------------------- #-------------------------------------------------------------------------
# #
@ -76,9 +76,10 @@ def impData(database, name,cb=None,cl=0):
os.remove(os.path.join(tmpdir_path,filename)) os.remove(os.path.join(tmpdir_path,filename))
try: try:
t = TarFile.ReadTarFile(name,tmpdir_path) archive = tarfile.open(name)
t.extract() for tarinfo in archive:
t.close() archive.extract(tarinfo,tmpdir_path)
archive.close()
except: except:
ErrorDialog(_("Error extracting into %s") % tmpdir_path) ErrorDialog(_("Error extracting into %s") % tmpdir_path)
return return
@ -86,7 +87,8 @@ def impData(database, name,cb=None,cl=0):
imp_db_name = os.path.join(tmpdir_path,const.xmlFile) imp_db_name = os.path.join(tmpdir_path,const.xmlFile)
try: try:
ReadXML.importData(database,imp_db_name,cb) importer = gramps_db_reader_factory(const.app_gramps_xml)
importer(database,imp_db_name,cb)
except: except:
import DisplayTrace import DisplayTrace
DisplayTrace.DisplayTrace() DisplayTrace.DisplayTrace()

View File

@ -1,7 +1,7 @@
# #
# Gramps - a GTK+/GNOME based genealogy program # Gramps - a GTK+/GNOME based genealogy program
# #
# Copyright (C) 2000-2005 Donald N. Allingham # Copyright (C) 2000-2006 Donald N. Allingham
# #
# This program is free software; you can redistribute it and/or modify # 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 # it under the terms of the GNU General Public License as published by
@ -29,6 +29,7 @@
#------------------------------------------------------------------------- #-------------------------------------------------------------------------
import time import time
import os import os
import tarfile
from cStringIO import StringIO from cStringIO import StringIO
from gettext import gettext as _ from gettext import gettext as _
@ -45,8 +46,7 @@ import gtk.glade
# GRAMPS modules # GRAMPS modules
# #
#------------------------------------------------------------------------- #-------------------------------------------------------------------------
import WriteXML from GrampsDb._WriteXML import XmlWriter
import TarFile
import Utils import Utils
from QuestionDialog import MissingMediaDialog from QuestionDialog import MissingMediaDialog
@ -140,9 +140,9 @@ class PackageWriter:
def fs_ok_clicked(obj): def fs_ok_clicked(obj):
name = fs_top.get_filename() name = fs_top.get_filename()
if os.path.isfile(name): if os.path.isfile(name):
g = open(name,"rb") ti = tf.gettarinfo(name,base)
t.add_file(base,mtime,g) ti.mtime = time.time()
g.close() tf.addfile(ti)
fs_top = gtk.FileSelection("%s - GRAMPS" % _("Select file")) fs_top = gtk.FileSelection("%s - GRAMPS" % _("Select file"))
fs_top.hide_fileop_buttons() fs_top.hide_fileop_buttons()
@ -152,8 +152,7 @@ class PackageWriter:
fs_top.destroy() fs_top.destroy()
#--------------------------------------------------------------- #---------------------------------------------------------------
t = TarFile.TarFile(self.filename) archive = tarfile.open(self.filename,'w:gz')
mtime = time.time()
# Write media files first, since the database may be modified # Write media files first, since the database may be modified
# during the process (i.e. when removing object) # during the process (i.e. when removing object)
@ -162,9 +161,9 @@ class PackageWriter:
oldfile = mobject.get_path() oldfile = mobject.get_path()
base = os.path.basename(oldfile) base = os.path.basename(oldfile)
if os.path.isfile(oldfile): if os.path.isfile(oldfile):
g = open(oldfile,"rb") tarinfo = archive.gettarinfo(oldfile,base)
t.add_file(base,mtime,g) tarinfo.mtime = int(time.time())
g.close() archive.addfile(tarinfo,file(oldfile))
else: else:
# File is lost => ask what to do # File is lost => ask what to do
if missmedia_action == 0: if missmedia_action == 0:
@ -184,14 +183,23 @@ class PackageWriter:
select_clicked() select_clicked()
# Write XML now # Write XML now
# import tempfile
# tempdir = tempfile.mkdtemp('.tmp','gramps-',
# os.path.dirname(self.filename))
# new_xml_file = os.path.join(tempdir,'data.gramps')
g = StringIO() g = StringIO()
gfile = WriteXML.XmlWriter(self.db,None,1) gfile = XmlWriter(self.db,None,1)
gfile.write_handle(g) gfile.write_handle(g) #write(new_xml_file) #write_handle(g)
mtime = time.time() tarinfo = tarfile.TarInfo('data.gramps')
t.add_file("data.gramps",mtime,g) tarinfo.size = len(g.getvalue())
tarinfo.mtime = time.time()
tarinfo.uid = os.getuid()
tarinfo.gid = os.getgid()
g.seek(0)
archive.addfile(tarinfo,g)
archive.close()
g.close() g.close()
t.close()
return 1 return 1
#------------------------------------------------------------------------- #-------------------------------------------------------------------------