From 4491c62a4a91db73a6aefeeccd1366d76e792aef Mon Sep 17 00:00:00 2001 From: Alex Roitman Date: Fri, 9 Jul 2004 18:19:47 +0000 Subject: [PATCH] * src/gramps_main.py (on_export_activate): Switch to new Exporter. * src/WriteXML.py: Change registration. Return 1 on success and 0 when on failure. * src/Utils.py (get_new_filename): Add function. * src/DbPrompter.py (get_new_filename): Remove function. * src/Exporter.py: Add to CVS. * src/Makefile.am: Ship Exporter.py. * src/Plugins.py (register_export): Change to the new scheme. svn: r3254 --- ChangeLog | 10 ++ src/DbPrompter.py | 21 +---- src/Exporter.py | 227 +++++++++++++++++++++++++++++++++++++++++++++ src/Makefile.am | 3 +- src/Plugins.py | 6 +- src/Utils.py | 13 +++ src/WriteXML.py | 31 ++++--- src/gramps_main.py | 4 +- 8 files changed, 279 insertions(+), 36 deletions(-) create mode 100644 src/Exporter.py diff --git a/ChangeLog b/ChangeLog index cfeae0230..a29bd521c 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,13 @@ +2004-07-09 Alex Roitman + * src/gramps_main.py (on_export_activate): Switch to new Exporter. + * src/WriteXML.py: Change registration. Return 1 on success and + 0 when on failure. + * src/Utils.py (get_new_filename): Add function. + * src/DbPrompter.py (get_new_filename): Remove function. + * src/Exporter.py: Add to CVS. + * src/Makefile.am: Ship Exporter.py. + * src/Plugins.py (register_export): Change to the new scheme. + 2004-07-08 Don Allingham * src/ChooseParents.py: build an exclusion list of those obviously not the parents diff --git a/src/DbPrompter.py b/src/DbPrompter.py index 62c31cfa4..f58d89ff0 100644 --- a/src/DbPrompter.py +++ b/src/DbPrompter.py @@ -293,7 +293,7 @@ class NewNativeDbPrompter: filter.add_mime_type('application/x-gramps') choose.add_filter(filter) - new_filename = get_new_filename('grdb') + new_filename = Utils.get_new_filename('grdb') choose.set_filename(new_filename) choose.set_current_name(os.path.split(new_filename)[1]) @@ -362,7 +362,7 @@ class SaveAsDbPrompter: for (exportData,filter,pattern_list) in Plugins._exports: self.choose.add_filter(filter) - new_filename = get_new_filename('grdb') + new_filename = Utils.get_new_filename('grdb') self.choose.set_filename(new_filename) self.choose.set_current_name(os.path.split(new_filename)[1]) @@ -414,22 +414,9 @@ class SaveAsDbPrompter: if the_filter.get_name().find('XML') + 1: new_filename = 'data.gramps' elif the_filter.get_name() == _('GRAMPS packages'): - new_filename = get_new_filename('gpkg') + new_filename = Utils.get_new_filename('gpkg') new_filename = os.path.split(new_filename)[1] else: - new_filename = get_new_filename('grdb') + new_filename = Utils.get_new_filename('grdb') new_filename = os.path.split(new_filename)[1] self.choose.set_current_name() - -#------------------------------------------------------------------------- -# -# -# -#------------------------------------------------------------------------- -_NEW_NAME_PATTERN = '~/Untitled_%d.%s' - -def get_new_filename(ext): - ix = 1 - while os.path.isfile(os.path.expanduser(_NEW_NAME_PATTERN % (ix,ext) )): - ix = ix + 1 - return os.path.expanduser(_NEW_NAME_PATTERN % (ix,ext)) diff --git a/src/Exporter.py b/src/Exporter.py new file mode 100644 index 000000000..366551e81 --- /dev/null +++ b/src/Exporter.py @@ -0,0 +1,227 @@ +# +# Gramps - a GTK+/GNOME based genealogy program +# +# Copyright (C) 2004 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$ + +# +# Written by Alex Roitman, 2004 +# + +#------------------------------------------------------------------------- +# +# Python modules +# +#------------------------------------------------------------------------- +import os +import shutil +from gettext import gettext as _ + +#------------------------------------------------------------------------- +# +# Gnome modules +# +#------------------------------------------------------------------------- +import gtk +import gtk.glade +import gnome + +#------------------------------------------------------------------------- +# +# gramps modules +# +#------------------------------------------------------------------------- +import const +import Utils +import Plugins +import QuestionDialog + +#------------------------------------------------------------------------- +# +# Exporter +# +#------------------------------------------------------------------------- +class Exporter: + """ + This class creates Gnome Druid to guide the user through the various + Save as/Export options. The overall goal is to keep things simple by + presenting few choice options on each druid page. + + The export formats and options are obtained from the plugins, with the + exception of a native save. Native save as just copies file to another + name. + """ + + def __init__(self,parent,parent_window): + self.parent = parent + self.parent_window = parent_window + + self.build_exports() + + self.w = gtk.Window() + + self.fg_color = gtk.gdk.color_parse('#7d684a') + self.bg_color = gtk.gdk.color_parse('#e1dbc5') + self.logo = gtk.gdk.pixbuf_new_from_file("%s/gramps.png" % const.rootDir) + self.splash = gtk.gdk.pixbuf_new_from_file("%s/splash.jpg" % const.rootDir) + + d = gnome.ui.Druid() + self.w.add(d) + d.add(self.build_info_page()) + d.add(self.build_format_page()) + d.add(self.build_file_sel_page()) + self.last_page = self.build_last_page() + d.add(self.last_page) + + d.connect('cancel',self.close) + self.w.connect("destroy_event",self.close) + self.w.set_transient_for(self.parent_window) + + self.w.show_all() + + def close(self,obj,obj2=None): + self.w.destroy() + + def help(self,obj): + #FIXME: point to the correct section when it exists + gnome.help_display('gramps-manual','index') + + def build_info_page(self): + p = gnome.ui.DruidPageEdge(0) + p.set_title(_('Saving your data')) + p.set_title_color(self.fg_color) + p.set_bg_color(self.bg_color) + p.set_logo(self.logo) + p.set_watermark(self.splash) + p.set_text(_('Under normal circumstances, GRAMPS does not require you ' + 'to directly save your changes. All changes you make are ' + 'immediately saved to the database.\n\n' + 'This process will help you save a copy of your data ' + 'in any of the several formats supported by GRAMPS. ' + 'This can be used to make a copy of your data, backup ' + 'your data, or convert it to a format that will allow ' + 'you to trasnfer it to a different program.\n\n' + 'If you change your mind during this process, you ' + 'can safely press Cancel button at any time and your ' + 'present database will still be intact.')) + return p + + def build_last_page(self): + p = gnome.ui.DruidPageEdge(1) + p.set_title_color(self.fg_color) + p.set_bg_color(self.bg_color) + p.set_logo(self.logo) + p.set_watermark(self.splash) + p.connect('prepare',self.save) + p.connect('finish',self.close) + return p + + def save(self,obj,obj2): + filename = self.chooser.get_filename() + success = self.exports[self.ix][0](self.parent.db,filename) + if success: + self.last_page.set_title(_('Your data has been saved')) + self.last_page.set_text(_('You may press Apply button ' + 'now to continue.')) + else: + self.last_page.set_title(_('Saving failed')) + self.last_page.set_text(_('There was an error ' + 'while saving your data. Please go back and try again.')) + + def build_format_page(self): + self.format_buttons = [] + + p = gnome.ui.DruidPageStandard() + p.set_title(_('Choosing the format to save')) + p.set_title_foreground(self.fg_color) + p.set_background(self.bg_color) + p.set_logo(self.logo) + + box = gtk.VBox() + box.set_spacing(12) + p.append_item("",box,"") + + table = gtk.Table(2*len(self.exports),2) + table.set_row_spacings(6) + table.set_col_spacings(6) + + group = None + for ix in range(len(self.exports)): + title = self.exports[ix][1] + description= self.exports[ix][2] + + button = gtk.RadioButton(group,title) + if not group: + group = button + button.connect('toggled',self.on_format_toggled) + self.format_buttons.append(button) + table.attach(button,0,2,2*ix,2*ix+1) + label = gtk.Label(description) + label.set_line_wrap(gtk.TRUE) + table.attach(label,1,2,2*ix+1,2*ix+2) + + box.add(table) + box.show_all() + + return p + + def build_file_sel_page(self): + p = gnome.ui.DruidPageStandard() + p.set_title(_('Selecting the file name')) + p.set_title_foreground(self.fg_color) + p.set_background(self.bg_color) + p.set_logo(self.logo) + + self.chooser = gtk.FileChooserWidget(gtk.FILE_CHOOSER_ACTION_SAVE) + self.on_format_toggled(self.format_buttons[0]) + p.append_item("",self.chooser,"") + + return p + + def native_export(self,database,filename): + try: + shutil.copyfile(database.get_save_path(),filename) + return 1 + except IOError, msg: + QuestionDialog.ErrorDialog( _("Could not write file: %s") % filename, + _('System message was: %s') % msg ) + return 0 + + def on_format_toggled(self,obj): + if not obj.get_active(): + return + self.ix = self.format_buttons.index(obj) + ext = self.exports[self.ix][4] + new_filename = Utils.get_new_filename(ext) + self.chooser.set_filename(new_filename) + self.chooser.set_current_name(os.path.split(new_filename)[1]) + + def build_exports(self): + native_title = _('GRAMPS _GRDB database') + native_description =_('The GRAMPS GRDB database is a format ' + 'that GRAMPS uses to store information. ' + 'Selecting this option will allow you to ' + 'make a copy of the current database.') + native_config = None + native_ext = 'grdb' + native_export = self.native_export + + self.exports = [ (native_export,native_title,native_description, + native_config,native_ext) ] + self.exports = self.exports + [ item for item in Plugins._exports ] diff --git a/src/Makefile.am b/src/Makefile.am index 433ef93b6..97848dc74 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -90,7 +90,8 @@ gdir_PYTHON = \ Witness.py\ WriteXML.py\ SelectPerson.py\ - ArgHandler.py + ArgHandler.py\ + Exporter.py # Could use GNU make's ':=' syntax for nice wildcard use. # If not using GNU make, then list all files individually diff --git a/src/Plugins.py b/src/Plugins.py index adfdc811a..10f446872 100644 --- a/src/Plugins.py +++ b/src/Plugins.py @@ -460,13 +460,13 @@ def reload_plugins(obj): # Plugin registering # #------------------------------------------------------------------------- -def register_export(task, ffilter, pattern_list=None): +def register_export(exportData,_title,_description='',_config=None,_filename=''): """ Register an export filter, taking the task, file filter, and the list of patterns for the filename matching. """ - if pattern_list: - _exports.append((task, ffilter, pattern_list)) + if _description and _filename: + _exports.append((exportData,_title,_description,_config,_filename)) def register_import(task, ffilter, mime=None): """Register an import filter, taking the task and file filter""" diff --git a/src/Utils.py b/src/Utils.py index 160404045..a77d04c4b 100644 --- a/src/Utils.py +++ b/src/Utils.py @@ -545,3 +545,16 @@ def create_id(): s += chr(val%57+65) val = int(val/57) return s + +#------------------------------------------------------------------------- +# +# +# +#------------------------------------------------------------------------- +_NEW_NAME_PATTERN = '~/Untitled_%d.%s' + +def get_new_filename(ext): + ix = 1 + while os.path.isfile(os.path.expanduser(_NEW_NAME_PATTERN % (ix,ext) )): + ix = ix + 1 + return os.path.expanduser(_NEW_NAME_PATTERN % (ix,ext)) diff --git a/src/WriteXML.py b/src/WriteXML.py index 6a75cc086..0cd6b7fc0 100644 --- a/src/WriteXML.py +++ b/src/WriteXML.py @@ -74,6 +74,7 @@ except: # #------------------------------------------------------------------------- def exportData(database, filename, callback=None): + ret = 0 if os.path.isfile(filename): try: shutil.copyfile(filename, filename + ".bak") @@ -85,7 +86,7 @@ def exportData(database, filename, callback=None): try: g = XmlWriter(database,callback,0,compress) - g.write(filename) + ret = g.write(filename) except: import DisplayTrace @@ -97,6 +98,8 @@ def exportData(database, filename, callback=None): shutil.copystat(filename + ".bak", filename) except: pass + + return ret #------------------------------------------------------------------------- # @@ -144,7 +147,7 @@ class XmlWriter: "have permission to write to the directory. " "Please make sure you have write access to the " "directory and try again.")) - return + return 0 if os.path.exists(filename): if not os.access(filename,os.W_OK): @@ -153,7 +156,7 @@ class XmlWriter: "have permission to write to the file. " "Please make sure you have write access to the " "file and try again.")) - return + return 0 self.fileroot = os.path.dirname(filename) try: @@ -166,12 +169,13 @@ class XmlWriter: g = open(filename,"w") except IOError,msg: ErrorDialog(_('Failure writing %s') % filename,msg) - return + return 0 self.g = codecs.getwriter("utf8")(g) self.write_xml_data() g.close() + return 1 def write_handle(self,handle): """ @@ -189,7 +193,7 @@ class XmlWriter: self.g = codecs.getwriter("utf8")(g) self.write_xml_data() - g.close() + g.close() def write_xml_data(self): @@ -421,7 +425,7 @@ class XmlWriter: self.g.write('<%s>' % val) self.g.write(self.fix(string.rstrip(text))) self.g.write("\n" % val) - + def write_text(self,val,text,indent=0): if not text: return @@ -431,7 +435,7 @@ class XmlWriter: self.g.write('<%s>' % val) self.g.write(self.fix(string.rstrip(text))) self.g.write("\n" % val) - + def dump_event(self,event,index=1): if event: self.dump_my_event(event.get_name(),event,index) @@ -811,11 +815,12 @@ def conf_priv(obj): # # #------------------------------------------------------------------------- -_mime_type = 'data.gramps' -_filter = gtk.FileFilter() -_filter.set_name(_('GRAMPS XML databases')) -_filter.add_pattern(_mime_type) -_ext_list = ('.gramps',) +_title = _('GRAMPS _XML database') +_description = _('The GRAMPS XML database is a format used by older ' + 'versions of GRAMPS. It is read-write compatible with ' + 'the present GRAMPS database format.') +_config = None +_filename = 'gramps' from Plugins import register_export -register_export(exportData,_filter,_ext_list) +register_export(exportData,_title,_description,_config,_filename) diff --git a/src/gramps_main.py b/src/gramps_main.py index 987e4dedc..bdbdacdfc 100755 --- a/src/gramps_main.py +++ b/src/gramps_main.py @@ -71,6 +71,7 @@ import Find import DbPrompter import TipOfDay import ArgHandler +import Exporter from QuestionDialog import * @@ -1367,8 +1368,7 @@ class Gramps: prompter.chooser() def on_export_activate(self,obj): - prompter = DbPrompter.SaveAsDbPrompter(self,self.topWindow) - prompter.chooser() + Exporter.Exporter(self,self.topWindow) def on_revert_activate(self,obj): pass