From 3be97cd506cee53bb0e9da2163f856c016b1608c Mon Sep 17 00:00:00 2001 From: Alex Roitman Date: Tue, 22 Jun 2004 20:49:22 +0000 Subject: [PATCH] * src/ArgHandler.py (parse_args): Switch from 'tgz' to 'gpkg' for Gramps package; (handle_args): Use separate dir for all import-produced files; (cl_export): Convert media objects export to the database interface. * src/ReadXML.py (importData): Copy all local (with respect to the old XML way) media object files into .images dir (created if did not previously exist). Change objects' paths accordingly; (GrampsParser.start_object): Do not modify path in the parser. This way we know that the objects are local. * src/RelLib.py (get_event_keys): Add method to GrampsDB class. * src/plugins/WritePkg.py (PackageWriter.export): Convert missing media handling to the database interface. svn: r3227 --- gramps2/ChangeLog | 14 +++++++ gramps2/src/ArgHandler.py | 67 ++++++++++++++++++++++++++------- gramps2/src/ReadXML.py | 33 +++++++++++++--- gramps2/src/RelLib.py | 5 +++ gramps2/src/plugins/WritePkg.py | 32 +++++++++++----- 5 files changed, 124 insertions(+), 27 deletions(-) diff --git a/gramps2/ChangeLog b/gramps2/ChangeLog index e99da21b4..caabefdc2 100644 --- a/gramps2/ChangeLog +++ b/gramps2/ChangeLog @@ -1,3 +1,17 @@ +2004-06-22 Alex Roitman + * src/ArgHandler.py (parse_args): Switch from 'tgz' to 'gpkg' + for Gramps package; (handle_args): Use separate dir for all + import-produced files; (cl_export): Convert media objects export + to the database interface. + * src/ReadXML.py (importData): Copy all local (with respect to + the old XML way) media object files into .images dir + (created if did not previously exist). Change objects' paths + accordingly; (GrampsParser.start_object): Do not modify path + in the parser. This way we know that the objects are local. + * src/RelLib.py (get_event_keys): Add method to GrampsDB class. + * src/plugins/WritePkg.py (PackageWriter.export): + Convert missing media handling to the database interface. + 2004-06-21 Alex Roitman * src/data/gramps.keys, src/data/mime: Add package and GEDCOM. * src/PedView.py: Fix anchors. Switch from storing person objects diff --git a/gramps2/src/ArgHandler.py b/gramps2/src/ArgHandler.py index 21f5b2383..89a960545 100644 --- a/gramps2/src/ArgHandler.py +++ b/gramps2/src/ArgHandler.py @@ -90,6 +90,11 @@ class ArgHandler: self.parse_args() self.handle_args() + #------------------------------------------------------------------------- + # + # Argument parser: sorts out given arguments + # + #------------------------------------------------------------------------- def parse_args(self): """ Fill in lists with open, exports, imports, and actions options. @@ -146,7 +151,7 @@ class ArgHandler: continue elif outfname[-3:].upper() == "GED": outformat = 'gedcom' - elif outfname[-3:].upper() == "TGZ": + elif outfname[-4:].upper() == "GPKG": outformat = 'gramps-pkg' elif outfname[-3:].upper() == "WFT": outformat = 'wft' @@ -173,6 +178,12 @@ class ArgHandler: continue self.actions.append(action) + #------------------------------------------------------------------------- + # + # Overall argument handler: + # sorts out the sequence and details of operations + # + #------------------------------------------------------------------------- def handle_args(self): """ Depending on the given arguments, import or open data, launch @@ -224,18 +235,33 @@ class ArgHandler: if self.imports: self.parent.cl = bool(self.exports or self.actions) - # Create file for imported database(s) - self.imp_db_path = os.path.expanduser("~/.gramps/import_db" ) - # remove if it exists - if os.path.isdir(self.imp_db_path): - os.removedirs(self.imp_db_path) - elif os.path.isfile(self.imp_db_path): - os.remove(self.imp_db_path) + + # Create dir for imported database(s) + self.impdir_path = os.path.expanduser("~/.gramps/import" ) + self.imp_db_path = os.path.expanduser("~/.gramps/import/import_db.grdb" ) + if not os.path.isdir(self.impdir_path): + try: + os.mkdir(self.impdir_path,0700) + except: + print "Could not create import directory %s. Exiting." \ + % self.impdir_path + os._exit(1) + elif not os.access(self.impdir_path,os.W_OK): + 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(os.path.join(self.impdir_path,fn)): + os.remove(os.path.join(self.impdir_path,fn)) + self.parent.load_database(self.imp_db_path) for imp in self.imports: - print "Importing: file %s, format %s." % imp #(imp[0],imp[1]) + print "Importing: file %s, format %s." % imp self.cl_import(imp[0],imp[1]) + elif len(self.args) > 1: print "No data was given -- will launch interactive session." print "To use in the command-line mode,", \ @@ -244,7 +270,7 @@ class ArgHandler: if self.parent.cl: for expt in self.exports: - print "Exporting: file %s, format %s." % expt #(expt[0],expt[1]) + print "Exporting: file %s, format %s." % expt self.cl_export(expt[0],expt[1]) for action in self.actions: @@ -263,6 +289,11 @@ class ArgHandler: DbPrompter.DbPrompter(self.parent,0,self.parent.topWindow) + #------------------------------------------------------------------------- + # + # Import handler + # + #------------------------------------------------------------------------- def cl_import(self,filename,format): """ Command-line import routine. Try to import filename using the format. @@ -330,6 +361,11 @@ class ArgHandler: if not self.parent.cl: return self.parent.post_load(self.imp_db_path) + #------------------------------------------------------------------------- + # + # Export handler + # + #------------------------------------------------------------------------- def cl_export(self,filename,format): """ Command-line export routine. @@ -370,9 +406,9 @@ class ArgHandler: try: # Write media files first, since the database may be modified # during the process (i.e. when removing object) - ObjectMap = self.parent.db.get_object_map() - for ObjectId in ObjectMap.keys(): - oldfile = ObjectMap[ObjectId].get_path() + for m_id in self.parent.db.get_object_keys(): + mobject = self.parent.db.try_to_find_object_from_id(m_id) + oldfile = mobject.get_path() base = os.path.basename(oldfile) if os.path.isfile(oldfile): g = open(oldfile,"rb") @@ -414,6 +450,11 @@ class ArgHandler: print "Invalid format: %s" % format os._exit(1) + #------------------------------------------------------------------------- + # + # Action handler + # + #------------------------------------------------------------------------- def cl_action(self,action): """ Command-line action routine. Try to perform specified action. diff --git a/gramps2/src/ReadXML.py b/gramps2/src/ReadXML.py index be7b038e2..b87df3843 100644 --- a/gramps2/src/ReadXML.py +++ b/gramps2/src/ReadXML.py @@ -142,6 +142,32 @@ def importData(database, filename, callback,cl=0): xml_file.close() + # copy all local images into .images directory + db_dir = os.path.abspath(os.path.dirname(database.get_save_path())) + db_base = os.path.basename(database.get_save_path()) + img_dir = "%s/%s.images" % (db_dir,db_base) + first = not os.path.exists(img_dir) + + for m_id in database.get_object_keys(): + mobject = database.try_to_find_object_from_id(m_id) + oldfile = mobject.get_path() + if oldfile[0] != '/': + if first: + os.mkdir(img_dir) + first = 0 + newfile = "%s/%s" % (img_dir,oldfile) + try: + oldfilename = "%s/%s" % (basefile,oldfile) + shutil.copyfile(oldfilename,newfile) + try: + shutil.copystat(oldfilename,newfile) + except: + pass + mobject.set_path(newfile) + database.commit_media_object(mobject,None) + except: + pass + #------------------------------------------------------------------------- # def remove_clicked(): # # File is lost => remove all references and the object itself @@ -690,10 +716,7 @@ class GrampsParser: self.object.set_description(attrs['description']) src = attrs["src"] if src: - if src[0] != '/': - self.object.set_path("%s/%s" % (self.base,src)) - else: - self.object.set_path(src) + self.object.set_path(src) def stop_people(self,*tag): pass @@ -1090,7 +1113,7 @@ class GrampsParser: self.tlist = [] try: - f,self.func = self.func_map[tag] + f,self.func = self.func_map[tag] if f: f(attrs) except KeyError: diff --git a/gramps2/src/RelLib.py b/gramps2/src/RelLib.py index e6d014f9a..5de40548a 100644 --- a/gramps2/src/RelLib.py +++ b/gramps2/src/RelLib.py @@ -3367,6 +3367,11 @@ class GrampsDB: return self.media_map.keys() return [] + def get_event_keys(self): + if self.event_map: + return self.event_map.keys() + return [] + def sortbysource(self,f,s): f1 = self.source_map[f][1].upper() s1 = self.source_map[s][1].upper() diff --git a/gramps2/src/plugins/WritePkg.py b/gramps2/src/plugins/WritePkg.py index 10e8a647b..1c973e7ea 100644 --- a/gramps2/src/plugins/WritePkg.py +++ b/gramps2/src/plugins/WritePkg.py @@ -109,36 +109,49 @@ class PackageWriter: #-------------------------------------------------------------- def remove_clicked(): # File is lost => remove all references and the object itself - mobj = self.db.find_family_from_id(ObjectId) + mobj = self.db.try_to_find_object_from_id(m_id) for p_id in self.db.get_family_keys(): p = self.db.find_family_from_id(p_id) nl = p.get_media_list() for o in nl: - if o.get_reference() == mobj: + if o.get_reference_id() == m_id: nl.remove(o) p.set_media_list(nl) + self.db.commit_family(p,None) for key in self.db.get_person_keys(): p = self.db.try_to_find_person_from_id(key) nl = p.get_media_list() for o in nl: - if o.get_reference() == mobj: + if o.get_reference_id() == m_id: nl.remove(o) + print key p.set_media_list(nl) + self.db.commit_person(p,None) for key in self.db.get_source_keys(): p = self.db.try_to_find_source_from_id(key) nl = p.get_media_list() for o in nl: - if o.get_reference() == mobj: + if o.get_reference_id() == m_id: nl.remove(o) p.set_media_list(nl) + self.db.commit_source(p,None) for key in self.db.get_place_id_keys(): p = self.db.try_to_find_place_from_id(key) nl = p.get_media_list() for o in nl: - if o.get_reference() == mobj: + if o.get_reference_id() == m_id: nl.remove(o) p.set_media_list(nl) - self.db.remove_object(ObjectId) + self.db.commit_place(p,None) + for key in self.db.get_event_keys(): + p = self.db.find_event_from_id(key) + nl = p.get_media_list() + for o in nl: + if o.get_reference_id() == m_id: + nl.remove(o) + p.set_media_list(nl) + self.db.commit_event(p,None) + self.db.remove_object(m_id,None) def leave_clicked(): # File is lost => do nothing, leave as is @@ -170,8 +183,9 @@ class PackageWriter: # Write media files first, since the database may be modified # during the process (i.e. when removing object) - for ObjectId in self.db.get_object_keys(): - oldfile = self.db.try_to_find_object_from_id(ObjectId).get_path() + for m_id in self.db.get_object_keys(): + mobject = self.db.try_to_find_object_from_id(m_id) + oldfile = mobject.get_path() base = os.path.basename(oldfile) if os.path.isfile(oldfile): g = open(oldfile,"rb") @@ -181,7 +195,7 @@ class PackageWriter: # File is lost => ask what to do if missmedia_action == 0: mmd = MissingMediaDialog(_("Media object could not be found"), - _("%(file_name)s is referenced in the database, but no longer exists. " + _("%(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."