* 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 <database>.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
This commit is contained in:
Alex Roitman 2004-06-22 20:49:22 +00:00
parent 623cec45ad
commit 3be97cd506
5 changed files with 124 additions and 27 deletions

View File

@ -1,3 +1,17 @@
2004-06-22 Alex Roitman <shura@alex.neuro.umn.edu>
* 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 <database>.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 <shura@alex.neuro.umn.edu> 2004-06-21 Alex Roitman <shura@alex.neuro.umn.edu>
* src/data/gramps.keys, src/data/mime: Add package and GEDCOM. * src/data/gramps.keys, src/data/mime: Add package and GEDCOM.
* src/PedView.py: Fix anchors. Switch from storing person objects * src/PedView.py: Fix anchors. Switch from storing person objects

View File

@ -90,6 +90,11 @@ class ArgHandler:
self.parse_args() self.parse_args()
self.handle_args() self.handle_args()
#-------------------------------------------------------------------------
#
# Argument parser: sorts out given arguments
#
#-------------------------------------------------------------------------
def parse_args(self): def parse_args(self):
""" """
Fill in lists with open, exports, imports, and actions options. Fill in lists with open, exports, imports, and actions options.
@ -146,7 +151,7 @@ class ArgHandler:
continue continue
elif outfname[-3:].upper() == "GED": elif outfname[-3:].upper() == "GED":
outformat = 'gedcom' outformat = 'gedcom'
elif outfname[-3:].upper() == "TGZ": elif outfname[-4:].upper() == "GPKG":
outformat = 'gramps-pkg' outformat = 'gramps-pkg'
elif outfname[-3:].upper() == "WFT": elif outfname[-3:].upper() == "WFT":
outformat = 'wft' outformat = 'wft'
@ -173,6 +178,12 @@ class ArgHandler:
continue continue
self.actions.append(action) self.actions.append(action)
#-------------------------------------------------------------------------
#
# Overall argument handler:
# sorts out the sequence and details of operations
#
#-------------------------------------------------------------------------
def handle_args(self): def handle_args(self):
""" """
Depending on the given arguments, import or open data, launch Depending on the given arguments, import or open data, launch
@ -224,18 +235,33 @@ class ArgHandler:
if self.imports: if self.imports:
self.parent.cl = bool(self.exports or self.actions) 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" ) # Create dir for imported database(s)
# remove if it exists self.impdir_path = os.path.expanduser("~/.gramps/import" )
if os.path.isdir(self.imp_db_path): self.imp_db_path = os.path.expanduser("~/.gramps/import/import_db.grdb" )
os.removedirs(self.imp_db_path) if not os.path.isdir(self.impdir_path):
elif os.path.isfile(self.imp_db_path): try:
os.remove(self.imp_db_path) 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) self.parent.load_database(self.imp_db_path)
for imp in self.imports: 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]) self.cl_import(imp[0],imp[1])
elif len(self.args) > 1: elif len(self.args) > 1:
print "No data was given -- will launch interactive session." print "No data was given -- will launch interactive session."
print "To use in the command-line mode,", \ print "To use in the command-line mode,", \
@ -244,7 +270,7 @@ class ArgHandler:
if self.parent.cl: if self.parent.cl:
for expt in self.exports: 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]) self.cl_export(expt[0],expt[1])
for action in self.actions: for action in self.actions:
@ -263,6 +289,11 @@ class ArgHandler:
DbPrompter.DbPrompter(self.parent,0,self.parent.topWindow) DbPrompter.DbPrompter(self.parent,0,self.parent.topWindow)
#-------------------------------------------------------------------------
#
# Import handler
#
#-------------------------------------------------------------------------
def cl_import(self,filename,format): def cl_import(self,filename,format):
""" """
Command-line import routine. Try to import filename using the format. Command-line import routine. Try to import filename using the format.
@ -330,6 +361,11 @@ class ArgHandler:
if not self.parent.cl: if not self.parent.cl:
return self.parent.post_load(self.imp_db_path) return self.parent.post_load(self.imp_db_path)
#-------------------------------------------------------------------------
#
# Export handler
#
#-------------------------------------------------------------------------
def cl_export(self,filename,format): def cl_export(self,filename,format):
""" """
Command-line export routine. Command-line export routine.
@ -370,9 +406,9 @@ class ArgHandler:
try: try:
# Write media files first, since the database may be modified # Write media files first, since the database may be modified
# during the process (i.e. when removing object) # during the process (i.e. when removing object)
ObjectMap = self.parent.db.get_object_map() for m_id in self.parent.db.get_object_keys():
for ObjectId in ObjectMap.keys(): mobject = self.parent.db.try_to_find_object_from_id(m_id)
oldfile = ObjectMap[ObjectId].get_path() oldfile = mobject.get_path()
base = os.path.basename(oldfile) base = os.path.basename(oldfile)
if os.path.isfile(oldfile): if os.path.isfile(oldfile):
g = open(oldfile,"rb") g = open(oldfile,"rb")
@ -414,6 +450,11 @@ class ArgHandler:
print "Invalid format: %s" % format print "Invalid format: %s" % format
os._exit(1) os._exit(1)
#-------------------------------------------------------------------------
#
# Action handler
#
#-------------------------------------------------------------------------
def cl_action(self,action): def cl_action(self,action):
""" """
Command-line action routine. Try to perform specified action. Command-line action routine. Try to perform specified action.

View File

@ -142,6 +142,32 @@ def importData(database, filename, callback,cl=0):
xml_file.close() xml_file.close()
# copy all local images into <database>.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(): # def remove_clicked():
# # File is lost => remove all references and the object itself # # File is lost => remove all references and the object itself
@ -690,9 +716,6 @@ class GrampsParser:
self.object.set_description(attrs['description']) self.object.set_description(attrs['description'])
src = attrs["src"] src = attrs["src"]
if 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): def stop_people(self,*tag):

View File

@ -3367,6 +3367,11 @@ class GrampsDB:
return self.media_map.keys() return self.media_map.keys()
return [] return []
def get_event_keys(self):
if self.event_map:
return self.event_map.keys()
return []
def sortbysource(self,f,s): def sortbysource(self,f,s):
f1 = self.source_map[f][1].upper() f1 = self.source_map[f][1].upper()
s1 = self.source_map[s][1].upper() s1 = self.source_map[s][1].upper()

View File

@ -109,36 +109,49 @@ class PackageWriter:
#-------------------------------------------------------------- #--------------------------------------------------------------
def remove_clicked(): def remove_clicked():
# File is lost => remove all references and the object itself # 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(): for p_id in self.db.get_family_keys():
p = self.db.find_family_from_id(p_id) p = self.db.find_family_from_id(p_id)
nl = p.get_media_list() nl = p.get_media_list()
for o in nl: for o in nl:
if o.get_reference() == mobj: if o.get_reference_id() == m_id:
nl.remove(o) nl.remove(o)
p.set_media_list(nl) p.set_media_list(nl)
self.db.commit_family(p,None)
for key in self.db.get_person_keys(): for key in self.db.get_person_keys():
p = self.db.try_to_find_person_from_id(key) p = self.db.try_to_find_person_from_id(key)
nl = p.get_media_list() nl = p.get_media_list()
for o in nl: for o in nl:
if o.get_reference() == mobj: if o.get_reference_id() == m_id:
nl.remove(o) nl.remove(o)
print key
p.set_media_list(nl) p.set_media_list(nl)
self.db.commit_person(p,None)
for key in self.db.get_source_keys(): for key in self.db.get_source_keys():
p = self.db.try_to_find_source_from_id(key) p = self.db.try_to_find_source_from_id(key)
nl = p.get_media_list() nl = p.get_media_list()
for o in nl: for o in nl:
if o.get_reference() == mobj: if o.get_reference_id() == m_id:
nl.remove(o) nl.remove(o)
p.set_media_list(nl) p.set_media_list(nl)
self.db.commit_source(p,None)
for key in self.db.get_place_id_keys(): for key in self.db.get_place_id_keys():
p = self.db.try_to_find_place_from_id(key) p = self.db.try_to_find_place_from_id(key)
nl = p.get_media_list() nl = p.get_media_list()
for o in nl: for o in nl:
if o.get_reference() == mobj: if o.get_reference_id() == m_id:
nl.remove(o) nl.remove(o)
p.set_media_list(nl) 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(): def leave_clicked():
# File is lost => do nothing, leave as is # File is lost => do nothing, leave as is
@ -170,8 +183,9 @@ class PackageWriter:
# Write media files first, since the database may be modified # Write media files first, since the database may be modified
# during the process (i.e. when removing object) # during the process (i.e. when removing object)
for ObjectId in self.db.get_object_keys(): for m_id in self.db.get_object_keys():
oldfile = self.db.try_to_find_object_from_id(ObjectId).get_path() mobject = self.db.try_to_find_object_from_id(m_id)
oldfile = mobject.get_path()
base = os.path.basename(oldfile) base = os.path.basename(oldfile)
if os.path.isfile(oldfile): if os.path.isfile(oldfile):
g = open(oldfile,"rb") g = open(oldfile,"rb")