From e727f0075b593855ab0d61e3b50a30bb4e3af78e Mon Sep 17 00:00:00 2001 From: Alex Roitman Date: Wed, 21 May 2003 18:06:41 +0000 Subject: [PATCH] Finishing command line options svn: r1564 --- gramps2/src/ReadXML.py | 82 ++++++++++++++++++++++------------ gramps2/src/gramps_main.py | 73 ++++++++++++++++++------------ gramps2/src/plugins/Check.py | 23 ++++++---- gramps2/src/plugins/Summary.py | 42 ++++++++++------- gramps2/src/plugins/WriteCD.py | 4 ++ 5 files changed, 144 insertions(+), 80 deletions(-) diff --git a/gramps2/src/ReadXML.py b/gramps2/src/ReadXML.py index b640bffec..ded6d3bfe 100644 --- a/gramps2/src/ReadXML.py +++ b/gramps2/src/ReadXML.py @@ -1,7 +1,7 @@ # # Gramps - a GTK+/GNOME based genealogy program # -# Copyright (C) 2000 Donald N. Allingham +# Copyright (C) 2000-2003 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 @@ -18,10 +18,6 @@ # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA # -# -# Modified by Alex Roitman to handle media object files. -# - #------------------------------------------------------------------------- # # Standard Python Modules @@ -61,7 +57,7 @@ except: # Must takes care of renaming media files according to their new IDs. # #------------------------------------------------------------------------- -def importData(database, filename, callback): +def importData(database, filename, callback,cl=0): filename = os.path.normpath(filename) basefile = os.path.dirname(filename) @@ -90,29 +86,55 @@ def importData(database, filename, callback): else: xml_file = open(filename,"r") except IOError,msg: - ErrorDialog(_("%s could not be opened") % filename,str(msg)) - return 0 + if cl: + print "Error: %s could not be opened Exiting." % filename + print msg + os._exit(1) + else: + ErrorDialog(_("%s could not be opened") % filename,str(msg)) + return 0 except: - ErrorDialog(_("%s could not be opened") % filename) - return 0 + if cl: + print "Error: %s could not be opened. Exiting." % filename + os._exit(1) + else: + ErrorDialog(_("%s could not be opened") % filename) + return 0 try: parser.parse(xml_file) except IOError,msg: - ErrorDialog(_("Error reading %s") % filename,str(msg)) - import traceback - traceback.print_exc() - return 0 + if cl: + print "Error reading %s" % filename + print msg + import traceback + traceback.print_exc() + os._exit(1) + else: + ErrorDialog(_("Error reading %s") % filename,str(msg)) + import traceback + traceback.print_exc() + return 0 except ExpatError, msg: - ErrorDialog(_("Error reading %s") % filename, - _("The file is probably either corrupt or not a valid GRAMPS database.")) - return 0 + if cl: + print "Error reading %s" % filename + print "The file is probably either corrupt or not a valid GRAMPS database." + os._exit(1) + else: + ErrorDialog(_("Error reading %s") % filename, + _("The file is probably either corrupt or not a valid GRAMPS database.")) + return 0 except ValueError, msg: pass except: - import DisplayTrace - DisplayTrace.DisplayTrace() - return 0 + if cl: + import traceback + traceback.print_exc() + os._exit(1) + else: + import DisplayTrace + DisplayTrace.DisplayTrace() + return 0 xml_file.close() @@ -190,14 +212,18 @@ def importData(database, filename, callback): try: shutil.copy2(oldfile,newfile) except: - # File is lost => ask what to do - MissingMediaDialog(_("Media object could not be found"), - _("%(file_name)s is referenced in the database, but no longer exists. " - "The file may have been deleted or moved to a different location. " - "You may choose to either remove the reference from the database, " - "keep the reference to the missing file, or select a new file." - ) % { 'file_name' : oldfile }, - remove_clicked, leave_clicked, select_clicked) + if cl: + print "Warning: media file %s was not found," \ + % os.path.basename(oldfile), "so it was ignored." + else: + # File is lost => ask what to do + MissingMediaDialog(_("Media object could not be found"), + _("%(file_name)s is referenced in the database, but no longer exists. " + "The file may have been deleted or moved to a different location. " + "You may choose to either remove the reference from the database, " + "keep the reference to the missing file, or select a new file." + ) % { 'file_name' : oldfile }, + remove_clicked, leave_clicked, select_clicked) del parser return 1 diff --git a/gramps2/src/gramps_main.py b/gramps2/src/gramps_main.py index 659097feb..638fccfba 100755 --- a/gramps2/src/gramps_main.py +++ b/gramps2/src/gramps_main.py @@ -116,6 +116,7 @@ class Gramps: self.bookmarks = None self.c_details = 6 self.id2col = {} + self.cl = 0 gtk.rc_parse(const.gtkrcFile) @@ -140,7 +141,6 @@ class Gramps: self.relationship = Plugins.relationship_function() self.init_interface() - self.cl = 1 if args: try: options,leftargs = getopt.getopt(args, @@ -153,8 +153,8 @@ class Gramps: if leftargs: print "Unrecognized option: %s" % leftargs[0] os._exit(1) - outfname = '' - action = '' + exports = [] + actions = [] imports = [] for opt_ix in range(len(options)): o = options[opt_ix][0][1] @@ -199,14 +199,15 @@ class Gramps: else: print "Unrecognized format for output file %s" % outfname os._exit(1) + exports.append((outfname,outformat)) elif o == 'a': action = options[opt_ix][1] if action not in [ 'check', 'summary' ]: print "Unknown action: %s." % action os._exit(1) + actions.append(action) - if not (outfname or action): - self.cl = 0 + self.cl = bool(exports or actions) if imports: # Create dir for imported database(s) @@ -215,16 +216,18 @@ class Gramps: try: os.mkdir(self.impdir_path,0700) except: - print "Could not create import directory %s" % impdir_path - return + print "Could not create import directory %s. Exiting." \ + % impdir_path + os._exit(1) elif not os.access(self.impdir_path,os.W_OK): - print "Import directory %s is not writable" % self.impdir_path - return + print "Import directory %s is not writable. Exiting." \ + % self.impdir_path + os._exit(1) # and clean it up before use files = os.listdir(self.impdir_path) ; for fn in files: - if os.path.isfile(fn): - os.remove( os.path.join(self.impdir_path,fn) ) + if os.path.isfile(os.path.join(self.impdir_path,fn)): + os.remove(os.path.join(self.impdir_path,fn)) self.clear_database(0) self.db.setSavePath(self.impdir_path) @@ -232,15 +235,21 @@ class Gramps: print "Importing: file %s, format %s." % (imp[0],imp[1]) self.cl_import(imp[0],imp[1]) - if outfname: - print "Exporting: file %s, format %s." % (outfname,outformat) - self.cl_export(outfname,outformat) + for expt in exports: + print "Exporting: file %s, format %s." % (expt[0],expt[1]) + self.cl_export(expt[0],expt[1]) - if action: + for action in actions: print "Performing action: %s." % action self.cl_action(action) if self.cl: + print "Cleaning up." + # clean import dir up after use + files = os.listdir(self.impdir_path) ; + for fn in files: + if os.path.isfile(os.path.join(self.impdir_path,fn)): + os.remove(os.path.join(self.impdir_path,fn)) print "Exiting." os._exit(0) @@ -960,7 +969,7 @@ class Gramps: elif format == 'gramps': try: dbname = os.path.join(filename,const.xmlFile) - ReadXML.importData(self.db,dbname,None) + ReadXML.importData(self.db,dbname,None,self.cl) except: print "Error importing %s" % filename os._exit(1) @@ -1051,6 +1060,9 @@ class Gramps: g = open(oldfile,"rb") t.add_file(base,mtime,g) g.close() + else: + print "Warning: media file %s was not found," % base,\ + "so it was ignored." except: print "Error exporting media files to %s" % filename os._exit(1) @@ -1074,7 +1086,7 @@ class Gramps: print "Error exporting %s" % filename os._exit(1) else: - print "Invalid format: %s" % format + print "Invalid format: %s" % format os._exit(1) def cl_action(self,action): @@ -1082,15 +1094,16 @@ class Gramps: import Check checker = Check.CheckIntegrity(self.db) checker.check_for_broken_family_links() - checker.cleanup_missing_photos() + checker.cleanup_missing_photos(1) checker.check_parent_relationships() checker.cleanup_empty_families(0) errs = checker.build_report(1) if errs: checker.report(1) elif action == 'summary': - print "Command-line summary is not implemented yet." - os._exit(0) + import Summary + text = Summary.build_report(self.db,None) + print text else: print "Unknown action: %s." % action os._exit(1) @@ -1175,14 +1188,18 @@ class Gramps: if os.path.isfile(oldfile): RelImage.import_media_object(oldfile,filename,base) else: - # File is lost => ask what to do - MissingMediaDialog(_("Media object could not be found"), - _("%(file_name)s is referenced in the database, but no longer exists. " - "The file may have been deleted or moved to a different location. " - "You may choose to either remove the reference from the database, " - "keep the reference to the missing file, or select a new file." - ) % { 'file_name' : oldfile }, - remove_clicked, leave_clicked, select_clicked) + if self.cl: + print "Warning: media file %s was not found," \ + % os.path.basename(oldfile), "so it was ignored." + else: + # File is lost => ask what to do + MissingMediaDialog(_("Media object could not be found"), + _("%(file_name)s is referenced in the database, but no longer exists. " + "The file may have been deleted or moved to a different location. " + "You may choose to either remove the reference from the database, " + "keep the reference to the missing file, or select a new file." + ) % { 'file_name' : oldfile }, + remove_clicked, leave_clicked, select_clicked) def save_file(self,filename,comment): diff --git a/gramps2/src/plugins/Check.py b/gramps2/src/plugins/Check.py index e39ad63c6..038d6214b 100644 --- a/gramps2/src/plugins/Check.py +++ b/gramps2/src/plugins/Check.py @@ -41,7 +41,7 @@ def runTool(database,active_person,callback): try: checker = CheckIntegrity(database) checker.check_for_broken_family_links() - checker.cleanup_missing_photos() + checker.cleanup_missing_photos(0) checker.check_parent_relationships() checker.cleanup_empty_families(0) errs = checker.build_report(0) @@ -94,7 +94,7 @@ class CheckIntegrity: Utils.modified() self.broken_links.append((child,family)) - def cleanup_missing_photos(self): + def cleanup_missing_photos(self,cl=0): #------------------------------------------------------------------------- def remove_clicked(): # File is lost => remove all references and the object itself @@ -160,13 +160,18 @@ class CheckIntegrity: for ObjectId in ObjectMap.keys(): photo_name = ObjectMap[ObjectId].getPath() if not os.path.isfile(photo_name): - MissingMediaDialog(_("Media object could not be found"), - _("%(file_name)s is referenced in the database, but no longer exists. " - "The file may have been deleted or moved to a different location. " - "You may choose to either remove the reference from the database, " - "keep the reference to the missing file, or select a new file." - ) % { 'file_name' : photo_name }, - remove_clicked, leave_clicked, select_clicked) + if cl: + print "Warning: media file %s was not found." \ + % os.path.basename(photo_name) + self.bad_photo.append(ObjectMap[ObjectId]) + else: + MissingMediaDialog(_("Media object could not be found"), + _("%(file_name)s is referenced in the database, but no longer exists. " + "The file may have been deleted or moved to a different location. " + "You may choose to either remove the reference from the database, " + "keep the reference to the missing file, or select a new file." + ) % { 'file_name' : photo_name }, + remove_clicked, leave_clicked, select_clicked) def cleanup_empty_families(self,automatic): for key in self.db.getFamilyMap().keys(): diff --git a/gramps2/src/plugins/Summary.py b/gramps2/src/plugins/Summary.py index e8ba8b404..50b7409cf 100644 --- a/gramps2/src/plugins/Summary.py +++ b/gramps2/src/plugins/Summary.py @@ -1,7 +1,7 @@ # # Gramps - a GTK+/GNOME based genealogy program # -# Copyright (C) 2000 Donald N. Allingham +# Copyright (C) 2000-2003 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 @@ -48,22 +48,10 @@ from gnome.ui import * #------------------------------------------------------------------------ # -# +# Build the text of the report # #------------------------------------------------------------------------ -def report(database,person): - - base = os.path.dirname(__file__) - glade_file = "%s/summary.glade" % base - - topDialog = gtk.glade.XML(glade_file,"summary") - topDialog.signal_autoconnect({ - "destroy_passed_object" : Utils.destroy_passed_object, - }) - - Utils.set_titles(topDialog.get_widget('summary'), - topDialog.get_widget('title'), - _('Database summary')) +def build_report(database,person): personList = database.getPersonMap().values() familyList = database.getFamilyMap().values() @@ -132,6 +120,30 @@ def report(database,person): for p in notfound: text = text + "%s\n" % p + return text + +#------------------------------------------------------------------------ +# +# Output report in a window +# +#------------------------------------------------------------------------ +def report(database,person): + + text = build_report(database,person) + + base = os.path.dirname(__file__) + glade_file = "%s/summary.glade" % base + + topDialog = gtk.glade.XML(glade_file,"summary") + topDialog.signal_autoconnect({ + "destroy_passed_object" : Utils.destroy_passed_object, + }) + + Utils.set_titles(topDialog.get_widget('summary'), + topDialog.get_widget('title'), + _('Database summary')) + + top = topDialog.get_widget("summary") textwindow = topDialog.get_widget("textwindow") textwindow.get_buffer().set_text(text) diff --git a/gramps2/src/plugins/WriteCD.py b/gramps2/src/plugins/WriteCD.py index 6ff7679a9..ca034edc7 100644 --- a/gramps2/src/plugins/WriteCD.py +++ b/gramps2/src/plugins/WriteCD.py @@ -143,6 +143,10 @@ class PackageWriter: self.copy_file(oldfile,'burn:///%s/%s' % (base,root)) if obj.getMimeType()[0:5] == "image": self.make_thumbnail(base,root,obj.getPath()) + else: + print "Warning: media file %s was not found," % root,\ + "so it was ignored." + # Write XML now g = gnome.vfs.create('burn:///%s/data.gramps' % base,gnome.vfs.OPEN_WRITE ) gfile = WriteXML.XmlWriter(self.db,None,1)