From e3397ecf9629da7971f44023c436776be70e7900 Mon Sep 17 00:00:00 2001 From: Alex Roitman Date: Wed, 1 Mar 2006 00:26:29 +0000 Subject: [PATCH] * 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 --- gramps2/ChangeLog | 10 ++ gramps2/src/ArgHandler.py | 17 +++- gramps2/src/GrampsDb/_ReadXML.py | 9 +- gramps2/src/TarFile.py | 162 ------------------------------- gramps2/src/ViewManager.py | 44 +++++---- gramps2/src/docgen/HtmlDoc.py | 10 +- gramps2/src/plugins/ReadPkg.py | 14 +-- gramps2/src/plugins/WritePkg.py | 40 +++++--- 8 files changed, 91 insertions(+), 215 deletions(-) delete mode 100644 gramps2/src/TarFile.py diff --git a/gramps2/ChangeLog b/gramps2/ChangeLog index 7a6f42602..720e6cb54 100644 --- a/gramps2/ChangeLog +++ b/gramps2/ChangeLog @@ -1,4 +1,14 @@ 2006-02-28 Alex Roitman + * 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. 2006-02-25 Don Allingham diff --git a/gramps2/src/ArgHandler.py b/gramps2/src/ArgHandler.py index 3b6750d64..cdf4ec6da 100644 --- a/gramps2/src/ArgHandler.py +++ b/gramps2/src/ArgHandler.py @@ -456,12 +456,19 @@ class ArgHandler: os.remove( os.path.join(tmpdir_path,fn) ) try: - import TarFile - t = TarFile.ReadTarFile(filename,tmpdir_path) - t.extract() - t.close() + import tarfile + archive = tarfile.open(filename) + for tarinfo in archive: + 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: - print "Error extracting into %s" % tmpdir_path + print "Error extracting into %s" % tmpdir_path os._exit(1) dbname = os.path.join(tmpdir_path,const.xmlFile) diff --git a/gramps2/src/GrampsDb/_ReadXML.py b/gramps2/src/GrampsDb/_ReadXML.py index d25f10b33..aa05870b3 100644 --- a/gramps2/src/GrampsDb/_ReadXML.py +++ b/gramps2/src/GrampsDb/_ReadXML.py @@ -321,6 +321,10 @@ class GrampsParser: self.media_file_map = {} 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.event = None self.eventref = None @@ -1612,7 +1616,10 @@ class GrampsParser: if self.func: self.tlist.append(data) - def update(self): + def update_empty(self): + pass + + def update_real(self): line = self.p.CurrentLineNumber newval = int(100*line/self.linecount) if newval != self.oldval: diff --git a/gramps2/src/TarFile.py b/gramps2/src/TarFile.py deleted file mode 100644 index c890abb31..000000000 --- a/gramps2/src/TarFile.py +++ /dev/null @@ -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() diff --git a/gramps2/src/ViewManager.py b/gramps2/src/ViewManager.py index 3acf91c87..e0ef0491f 100644 --- a/gramps2/src/ViewManager.py +++ b/gramps2/src/ViewManager.py @@ -929,38 +929,40 @@ class ViewManager: str(msg)) return False - if filetype == const.app_gramps: - 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 - 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) + # First we try our best formats + if filetype in format_list: + self._do_import(choose, + GrampsDb.gramps_db_reader_factory(filetype)) return True + # Then we try all the known plugins (the_path,the_file) = os.path.split(filename) 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: - choose.destroy() - importData(self.state.db,filename) + self._do_import(choose,importData) return True + + # Finally, we give up and declare this an unknown format QuestionDialog.ErrorDialog( _("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() 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): self.toolactions = gtk.ActionGroup('ToolWindow') diff --git a/gramps2/src/docgen/HtmlDoc.py b/gramps2/src/docgen/HtmlDoc.py index 6a80ddd43..d5219dd89 100644 --- a/gramps2/src/docgen/HtmlDoc.py +++ b/gramps2/src/docgen/HtmlDoc.py @@ -1,7 +1,7 @@ # # 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 # 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 ImgManip -import TarFile +import tarfile import const import Errors import BaseDoc @@ -148,8 +148,10 @@ class HtmlDoc(BaseDoc.BaseDoc): stop = re.compile(r"") top_add = 1 bottom_add = 0 - tf = TarFile.ReadTarFile(self.template,None) - self.map = tf.extract_files() + archive = tarfile.open(self.template) + for tarinfo in archive: + self.map[tarinfo.name] = archive.extractfile(tarinfo) + archive.close() templateFile = self.map['template.html'] while 1: line = templateFile.readline() diff --git a/gramps2/src/plugins/ReadPkg.py b/gramps2/src/plugins/ReadPkg.py index 7dffaa3c9..7173cb84a 100644 --- a/gramps2/src/plugins/ReadPkg.py +++ b/gramps2/src/plugins/ReadPkg.py @@ -29,6 +29,7 @@ # #------------------------------------------------------------------------- import os +import tarfile from gettext import gettext as _ #------------------------------------------------------------------------- @@ -44,9 +45,8 @@ import gtk # #------------------------------------------------------------------------- import const -import ReadXML +from GrampsDb import gramps_db_reader_factory 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)) try: - t = TarFile.ReadTarFile(name,tmpdir_path) - t.extract() - t.close() + archive = tarfile.open(name) + for tarinfo in archive: + archive.extract(tarinfo,tmpdir_path) + archive.close() except: ErrorDialog(_("Error extracting into %s") % tmpdir_path) return @@ -86,7 +87,8 @@ def impData(database, name,cb=None,cl=0): imp_db_name = os.path.join(tmpdir_path,const.xmlFile) try: - ReadXML.importData(database,imp_db_name,cb) + importer = gramps_db_reader_factory(const.app_gramps_xml) + importer(database,imp_db_name,cb) except: import DisplayTrace DisplayTrace.DisplayTrace() diff --git a/gramps2/src/plugins/WritePkg.py b/gramps2/src/plugins/WritePkg.py index 7ce8feb6c..9c4363395 100644 --- a/gramps2/src/plugins/WritePkg.py +++ b/gramps2/src/plugins/WritePkg.py @@ -1,7 +1,7 @@ # # 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 # it under the terms of the GNU General Public License as published by @@ -29,6 +29,7 @@ #------------------------------------------------------------------------- import time import os +import tarfile from cStringIO import StringIO from gettext import gettext as _ @@ -45,8 +46,7 @@ import gtk.glade # GRAMPS modules # #------------------------------------------------------------------------- -import WriteXML -import TarFile +from GrampsDb._WriteXML import XmlWriter import Utils from QuestionDialog import MissingMediaDialog @@ -140,9 +140,9 @@ class PackageWriter: def fs_ok_clicked(obj): name = fs_top.get_filename() if os.path.isfile(name): - g = open(name,"rb") - t.add_file(base,mtime,g) - g.close() + ti = tf.gettarinfo(name,base) + ti.mtime = time.time() + tf.addfile(ti) fs_top = gtk.FileSelection("%s - GRAMPS" % _("Select file")) fs_top.hide_fileop_buttons() @@ -152,8 +152,7 @@ class PackageWriter: fs_top.destroy() #--------------------------------------------------------------- - t = TarFile.TarFile(self.filename) - mtime = time.time() + archive = tarfile.open(self.filename,'w:gz') # Write media files first, since the database may be modified # during the process (i.e. when removing object) @@ -162,9 +161,9 @@ class PackageWriter: oldfile = mobject.get_path() base = os.path.basename(oldfile) if os.path.isfile(oldfile): - g = open(oldfile,"rb") - t.add_file(base,mtime,g) - g.close() + tarinfo = archive.gettarinfo(oldfile,base) + tarinfo.mtime = int(time.time()) + archive.addfile(tarinfo,file(oldfile)) else: # File is lost => ask what to do if missmedia_action == 0: @@ -184,14 +183,23 @@ class PackageWriter: select_clicked() # 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() - gfile = WriteXML.XmlWriter(self.db,None,1) - gfile.write_handle(g) - mtime = time.time() - t.add_file("data.gramps",mtime,g) + gfile = XmlWriter(self.db,None,1) + gfile.write_handle(g) #write(new_xml_file) #write_handle(g) + tarinfo = tarfile.TarInfo('data.gramps') + 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() - t.close() return 1 #-------------------------------------------------------------------------