From 3c108b27b785e2457bde6e8323afc89921cbff7b Mon Sep 17 00:00:00 2001 From: Alex Roitman Date: Sat, 3 Jul 2004 04:46:51 +0000 Subject: [PATCH] * src/DbPrompter.py: Handle package export. * src/plugins/WritePkg.py: Register with the new scheme. * src/plugins/ReadPkg.py: Minor cleanups. * src/WriteXML.py: Minor cleanups. svn: r3249 --- gramps2/ChangeLog | 5 +++ gramps2/src/DbPrompter.py | 61 ++++++++++++++++++++------------- gramps2/src/WriteXML.py | 2 +- gramps2/src/plugins/ReadPkg.py | 5 +-- gramps2/src/plugins/WritePkg.py | 46 +++++++------------------ 5 files changed, 57 insertions(+), 62 deletions(-) diff --git a/gramps2/ChangeLog b/gramps2/ChangeLog index e92acd2bd..fb0b28ce7 100644 --- a/gramps2/ChangeLog +++ b/gramps2/ChangeLog @@ -4,6 +4,11 @@ Connect handler to the filter change event; (get_new_filename): Make a module function (outside any class). + * src/DbPrompter.py: Handle package export. + * src/plugins/WritePkg.py: Register with the new scheme. + * src/plugins/ReadPkg.py: Minor cleanups. + * src/WriteXML.py: Minor cleanups. + 2004-07-01 Don Allingham * src/gramps_main.py: disable find old "find" function * src/gramps.glade: enable TreeView find function diff --git a/gramps2/src/DbPrompter.py b/gramps2/src/DbPrompter.py index 90ee9ed38..62c31cfa4 100644 --- a/gramps2/src/DbPrompter.py +++ b/gramps2/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() + new_filename = get_new_filename('grdb') choose.set_filename(new_filename) choose.set_current_name(os.path.split(new_filename)[1]) @@ -344,25 +344,25 @@ class SaveAsDbPrompter: gtk.RESPONSE_OK)) self.choose.set_local_only(gtk.FALSE) # Always add automatic (macth all files) filter - filter = gtk.FileFilter() - filter.set_name(_('By extension')) - filter.add_pattern('*') - self.choose.add_filter(filter) + all_filter = gtk.FileFilter() + all_filter.set_name(_('By extension')) + all_filter.add_pattern('*') + self.choose.add_filter(all_filter) # Always add native format filter - filter = gtk.FileFilter() - filter.set_name(_('GRAMPS databases')) - filter.add_mime_type('application/x-gramps') - self.choose.add_filter(filter) + native_filter = gtk.FileFilter() + native_filter.set_name(_('GRAMPS databases')) + native_filter.add_mime_type('application/x-gramps') + self.choose.add_filter(native_filter) - chooser_default = self.choose.get_children()[0].get_children()[0].get_children()[0] - chooser_default.connect('notify::filter',self.change_suggested_name) + chooser_default = self.choose.get_children()[0].get_children()[0].get_children()[0] + chooser_default.connect('notify::filter',self.change_suggested_name) - # Add more data type selections if opening existing db + # Add more data type selections from export plugins for (exportData,filter,pattern_list) in Plugins._exports: self.choose.add_filter(filter) - new_filename = get_new_filename() + new_filename = get_new_filename('grdb') self.choose.set_filename(new_filename) self.choose.set_current_name(os.path.split(new_filename)[1]) @@ -372,8 +372,13 @@ class SaveAsDbPrompter: filename = os.path.normpath(os.path.abspath(filename)) (junk,the_file) = os.path.split(filename) the_ext = os.path.splitext(filename)[1] + the_filter = self.choose.get_filter() - if the_ext in ('.grdb', '.GRDB'): + # Save as grdb if either explictly selected by the filter + # or the filter is ALL and the extension is grdb + if the_filter.get_name() == native_filter.get_name() \ + or (the_filter.get_name() == all_filter.get_name() + and the_ext in ('.grdb', '.GRDB')): self.choose.destroy() try: shutil.copyfile(self.parent.db.get_save_path(),filename) @@ -384,7 +389,11 @@ class SaveAsDbPrompter: return 0 for (exportData,filter,pattern_list) in Plugins._exports: - if the_ext in pattern_list: + # Save as this type if either explictly selected by the filter + # or the filter is ALL and the extension is in this type's list + if the_filter.get_name() == filter.get_name() \ + or (the_filter.get_name() == all_filter.get_name() + and the_ext in pattern_list): self.choose.destroy() exportData(self.parent.db,filename) return 1 @@ -403,20 +412,24 @@ class SaveAsDbPrompter: """ the_filter = self.choose.get_filter() if the_filter.get_name().find('XML') + 1: - self.choose.set_current_name('data.gramps') + new_filename = 'data.gramps' + elif the_filter.get_name() == _('GRAMPS packages'): + new_filename = get_new_filename('gpkg') + new_filename = os.path.split(new_filename)[1] else: - new_filename = get_new_filename() - self.choose.set_current_name(os.path.split(new_filename)[1]) + new_filename = get_new_filename('grdb') + new_filename = os.path.split(new_filename)[1] + self.choose.set_current_name() - #------------------------------------------------------------------------- # # # #------------------------------------------------------------------------- -def get_new_filename(): - ix = 1 - while os.path.isfile(os.path.expanduser('~/Untitled_%d.grdb' % ix ) ): - ix = ix + 1 - return os.path.expanduser('~/Untitled_%d.grdb' % ix ) +_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/gramps2/src/WriteXML.py b/gramps2/src/WriteXML.py index 51693ad53..6a75cc086 100644 --- a/gramps2/src/WriteXML.py +++ b/gramps2/src/WriteXML.py @@ -815,7 +815,7 @@ _mime_type = 'data.gramps' _filter = gtk.FileFilter() _filter.set_name(_('GRAMPS XML databases')) _filter.add_pattern(_mime_type) -_ext_list = '.gramps' +_ext_list = ('.gramps',) from Plugins import register_export register_export(exportData,_filter,_ext_list) diff --git a/gramps2/src/plugins/ReadPkg.py b/gramps2/src/plugins/ReadPkg.py index c03d1850b..2d483d592 100644 --- a/gramps2/src/plugins/ReadPkg.py +++ b/gramps2/src/plugins/ReadPkg.py @@ -50,7 +50,6 @@ _title_string = _("GRAMPS package") # #------------------------------------------------------------------------- def impData(database, name,cb=None,cl=0): - print "name1:", name # Create tempdir, if it does not exist, then check for writability tmpdir_path = os.path.expanduser("~/.gramps/tmp" ) if not os.path.isdir(tmpdir_path): @@ -68,7 +67,6 @@ def impData(database, name,cb=None,cl=0): for filename in files: os.remove( os.path.join(tmpdir_path,filename) ) - print "name2:", name try: t = TarFile.ReadTarFile(name,tmpdir_path) t.extract() @@ -186,9 +184,8 @@ def impData(database, name,cb=None,cl=0): #------------------------------------------------------------------------ _mime_type = 'application/x-gramps-package' _filter = gtk.FileFilter() -_filter.set_name(_('GRAMPS package')) +_filter.set_name(_('GRAMPS packages')) _filter.add_mime_type(_mime_type) from Plugins import register_import register_import(impData,_filter,_mime_type) -#register_import(readData,_title_string) diff --git a/gramps2/src/plugins/WritePkg.py b/gramps2/src/plugins/WritePkg.py index 6d7a6578d..55f80393b 100644 --- a/gramps2/src/plugins/WritePkg.py +++ b/gramps2/src/plugins/WritePkg.py @@ -58,9 +58,9 @@ _title_string = _("Export to GRAMPS package") # writeData # #------------------------------------------------------------------------- -def writeData(database,person): +def writeData(database,filename): try: - PackageWriter(database) + PackageWriter(database,filename) except: import DisplayTrace DisplayTrace.DisplayTrace() @@ -72,38 +72,13 @@ def writeData(database,person): #------------------------------------------------------------------------- class PackageWriter: - def __init__(self,database): + def __init__(self,database,filename): self.db = database - choose = gtk.FileChooserDialog(_('Save GRAMPS Package'), - None, - gtk.FILE_CHOOSER_ACTION_SAVE, - (gtk.STOCK_CANCEL, - gtk.RESPONSE_CANCEL, - gtk.STOCK_OPEN, - gtk.RESPONSE_OK)) - - choose.set_local_only(gtk.FALSE) - filter = gtk.FileFilter() - filter.set_name(_('GRAMPS package')) - filter.add_pattern('*.gpkg') - choose.add_filter(filter) - - response = choose.run() - - if response == gtk.RESPONSE_OK: - name = choose.get_filename() - if os.path.splitext(name)[1] != ".gpkg": - name = name + ".gpkg" - choose.destroy() - self.export(name) - else: - choose.destroy() + if os.path.splitext(filename)[1] != ".gpkg": + filename = filename + ".gpkg" + self.export(filename) - def on_help_clicked(self,obj): - """Display the relevant portion of GRAMPS manual""" - gnome.help_display('gramps-manual','export-data') - def export(self, filename): missmedia_action = 0 #-------------------------------------------------------------- @@ -222,6 +197,11 @@ class PackageWriter: # Register the plugin # #------------------------------------------------------------------------- -from Plugins import register_export +_mime_type = 'application/x-gramps-package' +_filter = gtk.FileFilter() +_filter.set_name(_('GRAMPS packages')) +_filter.add_mime_type(_mime_type) +_ext_list = ('.gpkg',) -register_export(writeData,_title_string) +from Plugins import register_export +register_export(writeData,_filter,_ext_list)