From 9b0cf1b97633dbec0cbac115fc7515f9f9df27ab Mon Sep 17 00:00:00 2001 From: Paul Culley Date: Thu, 9 Jan 2020 11:36:38 -0600 Subject: [PATCH] Fix ExportPkg so errors are not lost, and has progress bar for media (#957) * Fix Export Assistant so error messages get correct parent window * Fix ExportPkg so errors are not lost, and has progress bar for media Fixes #11457 --- gramps/gui/plug/export/_exportassistant.py | 10 ++-- gramps/plugins/export/exportpkg.py | 57 +++++++++++----------- 2 files changed, 34 insertions(+), 33 deletions(-) diff --git a/gramps/gui/plug/export/_exportassistant.py b/gramps/gui/plug/export/_exportassistant.py index ac675ac9c..ac1671f0a 100644 --- a/gramps/gui/plug/export/_exportassistant.py +++ b/gramps/gui/plug/export/_exportassistant.py @@ -586,11 +586,11 @@ class ExportAssistant(ManagedWindow, Gtk.Assistant): ix = self.get_selected_format_index() config.set('behavior.recent-export-type', ix) export_function = self.map_exporters[ix].get_export_function() - success = export_function(self.dbstate.db, - filename, - User(error=ErrorDialog, parent=self.uistate.window, - callback=self.callback), - self.option_box_instance) + success = export_function( + self.dbstate.db, filename, + User(error=ErrorDialog, parent=self.window, + callback=self.callback), + self.option_box_instance) except: #an error not catched in the export_function itself success = False diff --git a/gramps/plugins/export/exportpkg.py b/gramps/plugins/export/exportpkg.py index 0ce54969a..ea6cd06ae 100644 --- a/gramps/plugins/export/exportpkg.py +++ b/gramps/plugins/export/exportpkg.py @@ -179,34 +179,35 @@ class PackageWriter: #--------------------------------------------------------------- try: - archive = tarfile.open(self.filename,'w:gz') - except EnvironmentError as msg: + with tarfile.open(self.filename, 'w:gz') as archive: + + # Write media files first, since the database may be modified + # during the process (i.e. when removing object) + handles = self.db.get_media_handles(sort_handles=True) + for indx, m_id in enumerate(handles): + self.user.callback(indx * 100 / len(handles)) + mobject = self.db.get_media_from_handle(m_id) + filename = media_path_full(self.db, mobject.get_path()) + archname = str(mobject.get_path()) + if os.path.isfile(filename) and os.access(filename, + os.R_OK): + archive.add(filename, archname, filter=fix_mtime) + + # Write XML now + with BytesIO() as g: + gfile = XmlWriter(self.db, self.user, 2) + gfile.write_handle(g) + tarinfo = tarfile.TarInfo('data.gramps') + tarinfo.size = len(g.getvalue()) + tarinfo.mtime = time.time() + if not win(): + tarinfo.uid = os.getuid() + tarinfo.gid = os.getgid() + g.seek(0) + archive.addfile(tarinfo, g) + + return True + except (EnvironmentError, OSError) as msg: log.warning(str(msg)) self.user.notify_error(_('Failure writing %s') % self.filename, str(msg)) return 0 - - # Write media files first, since the database may be modified - # during the process (i.e. when removing object) - for m_id in self.db.get_media_handles(sort_handles=True): - mobject = self.db.get_media_from_handle(m_id) - filename = media_path_full(self.db, mobject.get_path()) - archname = str(mobject.get_path()) - if os.path.isfile(filename) and os.access(filename, os.R_OK): - archive.add(filename, archname, filter=fix_mtime) - - # Write XML now - g = BytesIO() - gfile = XmlWriter(self.db, self.user, 2) - gfile.write_handle(g) - tarinfo = tarfile.TarInfo('data.gramps') - tarinfo.size = len(g.getvalue()) - tarinfo.mtime = time.time() - if not win(): - tarinfo.uid = os.getuid() - tarinfo.gid = os.getgid() - g.seek(0) - archive.addfile(tarinfo, g) - archive.close() - g.close() - - return True