2003-10-13 07:25:56 +05:30
|
|
|
#
|
|
|
|
# Gramps - a GTK+/GNOME based genealogy program
|
|
|
|
#
|
2004-06-21 10:40:27 +05:30
|
|
|
# Copyright (C) 2000-2004 Donald N. Allingham
|
2003-10-13 07:25:56 +05:30
|
|
|
#
|
|
|
|
# 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
|
|
|
|
|
|
|
|
"""
|
|
|
|
Module responsible for handling the command line arguments for GRAMPS.
|
|
|
|
"""
|
|
|
|
|
|
|
|
#-------------------------------------------------------------------------
|
|
|
|
#
|
|
|
|
# Standard python modules
|
|
|
|
#
|
|
|
|
#-------------------------------------------------------------------------
|
|
|
|
import os
|
|
|
|
import getopt
|
2004-08-07 10:46:57 +05:30
|
|
|
from gettext import gettext as _
|
2003-10-13 07:25:56 +05:30
|
|
|
|
|
|
|
#-------------------------------------------------------------------------
|
|
|
|
#
|
|
|
|
# gramps modules
|
|
|
|
#
|
|
|
|
#-------------------------------------------------------------------------
|
|
|
|
import const
|
|
|
|
import ReadXML
|
2004-06-21 10:40:27 +05:30
|
|
|
import GrampsMime
|
|
|
|
import DbPrompter
|
|
|
|
import QuestionDialog
|
|
|
|
import GrampsCfg
|
2003-10-13 07:25:56 +05:30
|
|
|
|
|
|
|
#-------------------------------------------------------------------------
|
|
|
|
#
|
|
|
|
# ArgHandler
|
|
|
|
#
|
|
|
|
#-------------------------------------------------------------------------
|
|
|
|
class ArgHandler:
|
2004-06-21 10:40:27 +05:30
|
|
|
"""
|
|
|
|
This class is responsible for handling command line arguments (if any)
|
|
|
|
given to gramps. The valid arguments are:
|
|
|
|
|
|
|
|
FILE : filename to open.
|
|
|
|
All following arguments will be ignored.
|
|
|
|
-i, --import=FILE : filename to import.
|
|
|
|
-o, --output=FILE : filename to export.
|
|
|
|
-f, --format=FORMAT : format of the file preceding this option.
|
|
|
|
|
|
|
|
If the filename (no flags) is specified, the interactive session is
|
|
|
|
launched using data from filename. If the filename is not a natvive (grdb) format, dialog will
|
|
|
|
be presented to set up a grdb database.
|
|
|
|
|
|
|
|
If no filename or -i option is given, a new interactive session (empty
|
|
|
|
database) is launched, since no data is goven anyway.
|
|
|
|
|
|
|
|
If -i option is given, but no -o or -a options are given, and interactive
|
|
|
|
session is launched with the FILE (specified with -i).
|
|
|
|
|
|
|
|
If both -i and -o or -a are given, interactive session will not be
|
|
|
|
launched.
|
|
|
|
"""
|
2003-10-13 07:25:56 +05:30
|
|
|
|
|
|
|
def __init__(self,parent,args):
|
|
|
|
self.parent = parent
|
2004-06-21 10:40:27 +05:30
|
|
|
self.args = args
|
|
|
|
|
|
|
|
self.open = None
|
|
|
|
self.exports = []
|
|
|
|
self.actions = []
|
|
|
|
self.imports = []
|
|
|
|
|
|
|
|
self.parse_args()
|
|
|
|
self.handle_args()
|
|
|
|
|
2004-06-23 02:19:22 +05:30
|
|
|
#-------------------------------------------------------------------------
|
|
|
|
#
|
|
|
|
# Argument parser: sorts out given arguments
|
|
|
|
#
|
|
|
|
#-------------------------------------------------------------------------
|
2004-06-21 10:40:27 +05:30
|
|
|
def parse_args(self):
|
|
|
|
"""
|
|
|
|
Fill in lists with open, exports, imports, and actions options.
|
|
|
|
"""
|
2003-10-13 07:25:56 +05:30
|
|
|
|
|
|
|
try:
|
2004-06-21 10:40:27 +05:30
|
|
|
options,leftargs = getopt.getopt(self.args[1:],
|
2003-10-13 07:25:56 +05:30
|
|
|
const.shortopts,const.longopts)
|
2004-06-21 10:40:27 +05:30
|
|
|
except getopt.GetoptError:
|
|
|
|
# return without filling anything if we could not parse the args
|
|
|
|
print "Error parsing arguments: %s " % self.args[1:]
|
|
|
|
return
|
|
|
|
|
2003-10-13 07:25:56 +05:30
|
|
|
if leftargs:
|
2004-06-21 10:40:27 +05:30
|
|
|
# if there were an argument without option, use it as a file to
|
|
|
|
# open and return
|
|
|
|
self.open = leftargs[0]
|
|
|
|
print "Trying to open: %s ..." % leftargs[0]
|
|
|
|
return
|
|
|
|
|
2003-10-13 07:25:56 +05:30
|
|
|
for opt_ix in range(len(options)):
|
2004-06-21 10:40:27 +05:30
|
|
|
o,v = options[opt_ix]
|
|
|
|
if o in ( '-i', '--import'):
|
|
|
|
fname = v
|
|
|
|
ftype = GrampsMime.get_type(os.path.abspath(os.path.expanduser(fname)))
|
|
|
|
if opt_ix<len(options)-1 \
|
|
|
|
and options[opt_ix+1][0] in ( '-f', '--format'):
|
2003-10-13 07:25:56 +05:30
|
|
|
format = options[opt_ix+1][1]
|
2004-06-21 10:40:27 +05:30
|
|
|
if format not in ('gedcom','gramps-xml','gramps-pkg','grdb'):
|
2003-10-13 07:25:56 +05:30
|
|
|
print "Invalid format: %s" % format
|
2004-06-21 10:40:27 +05:30
|
|
|
print "Ignoring input file: %s" % fname
|
|
|
|
continue
|
2004-08-24 04:09:41 +05:30
|
|
|
elif ftype == const.app_gedcom:
|
2003-10-13 07:25:56 +05:30
|
|
|
format = 'gedcom'
|
2004-08-24 04:09:41 +05:30
|
|
|
elif ftype == const.app_gramps_package:
|
2003-10-13 07:25:56 +05:30
|
|
|
format = 'gramps-pkg'
|
2004-06-21 10:40:27 +05:30
|
|
|
elif ftype == "x-directory/normal":
|
|
|
|
format = 'gramps-xml'
|
2004-08-24 04:09:41 +05:30
|
|
|
elif ftype == const.app_gramps:
|
2004-06-21 10:40:27 +05:30
|
|
|
format = 'grdb'
|
2003-10-13 07:25:56 +05:30
|
|
|
else:
|
|
|
|
print "Unrecognized format for input file %s" % fname
|
2004-06-21 10:40:27 +05:30
|
|
|
print "Ignoring input file: %s" % fname
|
|
|
|
continue
|
|
|
|
self.imports.append((fname,format))
|
|
|
|
elif o in ( '-o', '--output' ):
|
|
|
|
outfname = v
|
|
|
|
if opt_ix<len(options)-1 \
|
|
|
|
and options[opt_ix+1][0] in ( '-f', '--format'):
|
2003-10-13 07:25:56 +05:30
|
|
|
outformat = options[opt_ix+1][1]
|
2004-06-21 10:40:27 +05:30
|
|
|
if outformat not in ('gedcom','gramps-xml','gramps-pkg','grdb','iso','wft'):
|
2003-10-13 07:25:56 +05:30
|
|
|
print "Invalid format: %s" % outformat
|
2004-06-21 10:40:27 +05:30
|
|
|
print "Ignoring output file: %s" % outfname
|
|
|
|
continue
|
2003-10-13 07:25:56 +05:30
|
|
|
elif outfname[-3:].upper() == "GED":
|
|
|
|
outformat = 'gedcom'
|
2004-06-23 02:19:22 +05:30
|
|
|
elif outfname[-4:].upper() == "GPKG":
|
2003-10-13 07:25:56 +05:30
|
|
|
outformat = 'gramps-pkg'
|
|
|
|
elif outfname[-3:].upper() == "WFT":
|
|
|
|
outformat = 'wft'
|
|
|
|
elif not os.path.isfile(outfname):
|
|
|
|
if not os.path.isdir(outfname):
|
|
|
|
try:
|
|
|
|
os.makedirs(outfname,0700)
|
|
|
|
except:
|
|
|
|
print "Cannot create directory %s" % outfname
|
2004-06-21 10:40:27 +05:30
|
|
|
print "Ignoring output file: %s" % outfname
|
|
|
|
continue
|
|
|
|
outformat = 'gramps-xml'
|
|
|
|
elif fname[-3:].upper() == "GRDB":
|
|
|
|
format = 'grdb'
|
2003-10-13 07:25:56 +05:30
|
|
|
else:
|
|
|
|
print "Unrecognized format for output file %s" % outfname
|
2004-06-21 10:40:27 +05:30
|
|
|
print "Ignoring output file: %s" % outfname
|
|
|
|
continue
|
|
|
|
self.exports.append((outfname,outformat))
|
|
|
|
elif o in ( '-a', '--action' ):
|
|
|
|
action = v
|
|
|
|
if action not in ( 'check', 'summary' ):
|
|
|
|
print "Unknown action: %s. Ignoring." % action
|
|
|
|
continue
|
|
|
|
self.actions.append(action)
|
2003-10-13 07:25:56 +05:30
|
|
|
|
2004-08-24 04:09:41 +05:30
|
|
|
#-------------------------------------------------------------------------
|
|
|
|
#
|
|
|
|
# open data in native format
|
|
|
|
#
|
|
|
|
#-------------------------------------------------------------------------
|
2004-07-28 07:59:07 +05:30
|
|
|
def auto_save_load(self,filename):
|
|
|
|
self.parent.active_person = None
|
2004-08-24 04:09:41 +05:30
|
|
|
filename = os.path.normpath(os.path.abspath(filename))
|
|
|
|
filetype = GrampsMime.get_type(filename)
|
|
|
|
if filetype == const.app_gramps:
|
|
|
|
self.parent.read_file(filename)
|
|
|
|
return 1
|
|
|
|
elif filetype == const.app_gramps_xml:
|
|
|
|
import GrampsXMLDB
|
|
|
|
self.parent.db = GrampsXMLDB.GrampsXMLDB()
|
|
|
|
self.parent.read_file(filename)
|
|
|
|
return 1
|
|
|
|
elif filetype == const.app_gedcom:
|
|
|
|
import GrampsGEDDB
|
|
|
|
self.parent.db = GrampsGEDDB.GrampsGEDDB()
|
|
|
|
self.parent.read_file(filename)
|
|
|
|
return 1
|
|
|
|
else:
|
|
|
|
return 0
|
2004-07-28 07:59:07 +05:30
|
|
|
|
2004-06-23 02:19:22 +05:30
|
|
|
#-------------------------------------------------------------------------
|
|
|
|
#
|
|
|
|
# Overall argument handler:
|
|
|
|
# sorts out the sequence and details of operations
|
|
|
|
#
|
|
|
|
#-------------------------------------------------------------------------
|
2004-06-21 10:40:27 +05:30
|
|
|
def handle_args(self):
|
|
|
|
"""
|
|
|
|
Depending on the given arguments, import or open data, launch
|
|
|
|
session, write files, and/or perform actions.
|
|
|
|
"""
|
|
|
|
|
|
|
|
if self.open:
|
|
|
|
# Filename was given. Open a session with that file. Forget
|
|
|
|
# the rest of given arguments.
|
|
|
|
filename = os.path.abspath(os.path.expanduser(self.open))
|
|
|
|
filetype = GrampsMime.get_type(filename)
|
2004-08-24 04:09:41 +05:30
|
|
|
if filetype == const.app_gramps:
|
2004-06-21 10:40:27 +05:30
|
|
|
print "Type: GRAMPS database"
|
2004-07-28 07:59:07 +05:30
|
|
|
if self.auto_save_load(filename):
|
2004-06-21 10:40:27 +05:30
|
|
|
print "Opened successfully!"
|
|
|
|
else:
|
|
|
|
print "Cannot open %s. Exiting..."
|
2004-08-24 04:09:41 +05:30
|
|
|
elif filetype in (const.app_gedcom,"x-directory/normal",
|
|
|
|
const.app_gramps_package):
|
2004-06-21 10:40:27 +05:30
|
|
|
QuestionDialog.OkDialog( _("Opening non-native format"),
|
2004-06-28 23:46:42 +05:30
|
|
|
_("New GRAMPS database has to be set up when opening non-native formats. The following dialog will let you select the new database."),
|
2004-06-21 10:40:27 +05:30
|
|
|
self.parent.topWindow)
|
2004-06-28 23:46:42 +05:30
|
|
|
prompter = DbPrompter.NewNativeDbPrompter(self.parent)
|
2004-08-24 04:09:41 +05:30
|
|
|
if not prompter.chooser():
|
2004-06-28 23:46:42 +05:30
|
|
|
QuestionDialog.ErrorDialog(
|
|
|
|
_("New GRAMPS database was not set up"),
|
|
|
|
_('GRAMPS cannot open non-native data without setting up new GRAMPS database.'))
|
|
|
|
print "Cannot continue without native database. Exiting..."
|
2004-08-24 04:09:41 +05:30
|
|
|
os._exit(1)
|
|
|
|
elif filetype == const.app_gedcom:
|
2004-06-28 23:46:42 +05:30
|
|
|
print "Type: GEDCOM"
|
|
|
|
self.parent.read_gedcom(filename)
|
2004-08-24 04:09:41 +05:30
|
|
|
elif filetype == "x-directory/normal":
|
2004-06-28 23:46:42 +05:30
|
|
|
print "Type: GRAMPS XML"
|
|
|
|
self.parent.read_xml(filename)
|
2004-08-24 04:09:41 +05:30
|
|
|
elif filetype == const.app_gramps_package:
|
2004-06-28 23:46:42 +05:30
|
|
|
print "Type: GRAMPS package"
|
|
|
|
self.parent.read_pkg(filename)
|
2004-08-24 04:09:41 +05:30
|
|
|
else:
|
2004-06-21 10:40:27 +05:30
|
|
|
print "Unknown file type: %s" % filetype
|
|
|
|
QuestionDialog.ErrorDialog(
|
2004-06-28 23:46:42 +05:30
|
|
|
_("Cannot open file: unknown type"),
|
|
|
|
_('File type "%s" is unknown to GRAMPS.\n\nValid types are: GRAMPS database, GRAMPS XML, GRAMPS package, and GEDCOM.') % filetype)
|
2004-06-21 10:40:27 +05:30
|
|
|
print "Exiting..."
|
2004-08-24 04:09:41 +05:30
|
|
|
os._exit(1)
|
2004-06-21 10:40:27 +05:30
|
|
|
return
|
|
|
|
|
|
|
|
if self.imports:
|
|
|
|
self.parent.cl = bool(self.exports or self.actions)
|
2004-06-23 02:19:22 +05:30
|
|
|
|
|
|
|
# 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))
|
|
|
|
|
2004-06-21 10:40:27 +05:30
|
|
|
self.parent.load_database(self.imp_db_path)
|
|
|
|
|
|
|
|
for imp in self.imports:
|
2004-06-23 02:19:22 +05:30
|
|
|
print "Importing: file %s, format %s." % imp
|
2003-10-13 07:25:56 +05:30
|
|
|
self.cl_import(imp[0],imp[1])
|
2004-06-23 02:19:22 +05:30
|
|
|
|
2004-06-21 10:40:27 +05:30
|
|
|
elif len(self.args) > 1:
|
|
|
|
print "No data was given -- will launch interactive session."
|
2003-10-13 07:25:56 +05:30
|
|
|
print "To use in the command-line mode,", \
|
|
|
|
"supply at least one input file to process."
|
2004-06-21 10:40:27 +05:30
|
|
|
print "Launching interactive session..."
|
2003-10-13 07:25:56 +05:30
|
|
|
|
|
|
|
if self.parent.cl:
|
2004-06-21 10:40:27 +05:30
|
|
|
for expt in self.exports:
|
2004-06-23 02:19:22 +05:30
|
|
|
print "Exporting: file %s, format %s." % expt
|
2003-10-13 07:25:56 +05:30
|
|
|
self.cl_export(expt[0],expt[1])
|
|
|
|
|
2004-06-21 10:40:27 +05:30
|
|
|
for action in self.actions:
|
2003-10-13 07:25:56 +05:30
|
|
|
print "Performing action: %s." % action
|
|
|
|
self.cl_action(action)
|
|
|
|
|
|
|
|
print "Cleaning up."
|
2004-06-21 10:40:27 +05:30
|
|
|
# remove import db after use
|
|
|
|
os.remove(self.imp_db_path)
|
2003-10-13 07:25:56 +05:30
|
|
|
print "Exiting."
|
|
|
|
os._exit(0)
|
2004-06-24 08:59:38 +05:30
|
|
|
|
|
|
|
if self.imports:
|
|
|
|
self.parent.import_tool_callback()
|
2004-07-15 08:25:22 +05:30
|
|
|
elif GrampsCfg.get_lastfile() and GrampsCfg.get_autoload():
|
2004-07-28 07:59:07 +05:30
|
|
|
if self.auto_save_load(GrampsCfg.get_lastfile()) == 0:
|
2004-06-28 23:46:42 +05:30
|
|
|
DbPrompter.DbPrompter(self.parent,0)
|
2004-06-21 10:40:27 +05:30
|
|
|
else:
|
2004-08-24 04:09:41 +05:30
|
|
|
DbPrompter.DbPrompter(self.parent,0)
|
2003-10-13 07:25:56 +05:30
|
|
|
|
|
|
|
|
2004-06-23 02:19:22 +05:30
|
|
|
#-------------------------------------------------------------------------
|
|
|
|
#
|
|
|
|
# Import handler
|
|
|
|
#
|
|
|
|
#-------------------------------------------------------------------------
|
2003-10-13 07:25:56 +05:30
|
|
|
def cl_import(self,filename,format):
|
2004-06-21 10:40:27 +05:30
|
|
|
"""
|
|
|
|
Command-line import routine. Try to import filename using the format.
|
|
|
|
Any errors will cause the os._exit(1) call.
|
|
|
|
"""
|
2003-10-13 07:25:56 +05:30
|
|
|
if format == 'gedcom':
|
|
|
|
import ReadGedcom
|
|
|
|
filename = os.path.normpath(os.path.abspath(filename))
|
|
|
|
try:
|
|
|
|
g = ReadGedcom.GedcomParser(self.parent.db,filename,None)
|
|
|
|
g.parse_gedcom_file()
|
|
|
|
g.resolve_refns()
|
|
|
|
del g
|
|
|
|
except:
|
|
|
|
print "Error importing %s" % filename
|
|
|
|
os._exit(1)
|
2004-06-21 10:40:27 +05:30
|
|
|
elif format == 'gramps-xml':
|
2003-10-13 07:25:56 +05:30
|
|
|
try:
|
|
|
|
dbname = os.path.join(filename,const.xmlFile)
|
|
|
|
ReadXML.importData(self.parent.db,dbname,None,self.parent.cl)
|
|
|
|
except:
|
|
|
|
print "Error importing %s" % filename
|
|
|
|
os._exit(1)
|
|
|
|
elif format == 'gramps-pkg':
|
|
|
|
# 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):
|
|
|
|
try:
|
|
|
|
os.mkdir(tmpdir_path,0700)
|
|
|
|
except:
|
|
|
|
print "Could not create temporary directory %s" % tmpdir_path
|
|
|
|
os._exit(1)
|
|
|
|
elif not os.access(tmpdir_path,os.W_OK):
|
|
|
|
print "Temporary directory %s is not writable" % tmpdir_path
|
|
|
|
os._exit(1)
|
|
|
|
else: # tempdir exists and writable -- clean it up if not empty
|
2004-06-22 04:54:51 +05:30
|
|
|
files = os.listdir(tmpdir_path) ;
|
2003-10-13 07:25:56 +05:30
|
|
|
for fn in files:
|
|
|
|
os.remove( os.path.join(tmpdir_path,fn) )
|
|
|
|
|
|
|
|
try:
|
|
|
|
import TarFile
|
|
|
|
t = TarFile.ReadTarFile(filename,tmpdir_path)
|
2004-06-22 04:54:51 +05:30
|
|
|
t.extract()
|
|
|
|
t.close()
|
2003-10-13 07:25:56 +05:30
|
|
|
except:
|
|
|
|
print "Error extracting into %s" % tmpdir_path
|
|
|
|
os._exit(1)
|
|
|
|
|
2004-06-24 08:59:38 +05:30
|
|
|
dbname = os.path.join(tmpdir_path,const.xmlFile)
|
2003-10-13 07:25:56 +05:30
|
|
|
|
|
|
|
try:
|
|
|
|
ReadXML.importData(self.parent.db,dbname,None)
|
|
|
|
except:
|
|
|
|
print "Error importing %s" % filename
|
|
|
|
os._exit(1)
|
|
|
|
# Clean up tempdir after ourselves
|
|
|
|
files = os.listdir(tmpdir_path)
|
|
|
|
for fn in files:
|
|
|
|
os.remove(os.path.join(tmpdir_path,fn))
|
|
|
|
os.rmdir(tmpdir_path)
|
|
|
|
else:
|
|
|
|
print "Invalid format: %s" % format
|
|
|
|
os._exit(1)
|
|
|
|
if not self.parent.cl:
|
2004-06-21 10:40:27 +05:30
|
|
|
return self.parent.post_load(self.imp_db_path)
|
2003-10-13 07:25:56 +05:30
|
|
|
|
2004-06-23 02:19:22 +05:30
|
|
|
#-------------------------------------------------------------------------
|
|
|
|
#
|
|
|
|
# Export handler
|
|
|
|
#
|
|
|
|
#-------------------------------------------------------------------------
|
2003-10-13 07:25:56 +05:30
|
|
|
def cl_export(self,filename,format):
|
2004-06-21 10:40:27 +05:30
|
|
|
"""
|
|
|
|
Command-line export routine.
|
|
|
|
Try to write into filename using the format.
|
|
|
|
Any errors will cause the os._exit(1) call.
|
|
|
|
"""
|
2003-10-13 07:25:56 +05:30
|
|
|
if format == 'gedcom':
|
|
|
|
import WriteGedcom
|
|
|
|
try:
|
|
|
|
g = WriteGedcom.GedcomWriter(self.parent.db,None,1,filename)
|
|
|
|
del g
|
|
|
|
except:
|
|
|
|
print "Error exporting %s" % filename
|
|
|
|
os._exit(1)
|
2004-06-21 10:40:27 +05:30
|
|
|
elif format == 'gramps-xml':
|
2003-10-13 07:25:56 +05:30
|
|
|
filename = os.path.normpath(os.path.abspath(filename))
|
|
|
|
dbname = os.path.join(filename,const.xmlFile)
|
|
|
|
if filename:
|
|
|
|
try:
|
|
|
|
self.parent.save_media(filename)
|
|
|
|
self.parent.db.save(dbname,None)
|
|
|
|
except:
|
|
|
|
print "Error exporting %s" % filename
|
|
|
|
os._exit(1)
|
|
|
|
elif format == 'gramps-pkg':
|
|
|
|
import TarFile
|
|
|
|
import time
|
|
|
|
import WriteXML
|
|
|
|
from cStringIO import StringIO
|
|
|
|
|
|
|
|
try:
|
|
|
|
t = TarFile.TarFile(filename)
|
|
|
|
mtime = time.time()
|
|
|
|
except:
|
|
|
|
print "Error creating %s" % filename
|
|
|
|
os._exit(1)
|
|
|
|
|
|
|
|
try:
|
|
|
|
# Write media files first, since the database may be modified
|
|
|
|
# during the process (i.e. when removing object)
|
2004-08-13 10:04:07 +05:30
|
|
|
for m_id in self.parent.db.get_media_object_handles():
|
2004-08-07 10:46:57 +05:30
|
|
|
mobject = self.parent.db.get_object_from_handle(m_id)
|
2004-06-23 02:19:22 +05:30
|
|
|
oldfile = mobject.get_path()
|
2003-10-13 07:25:56 +05:30
|
|
|
base = os.path.basename(oldfile)
|
|
|
|
if os.path.isfile(oldfile):
|
|
|
|
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)
|
|
|
|
try:
|
|
|
|
# Write XML now
|
|
|
|
g = StringIO()
|
|
|
|
gfile = WriteXML.XmlWriter(self.parent.db,None,1)
|
|
|
|
gfile.write_handle(g)
|
|
|
|
mtime = time.time()
|
|
|
|
t.add_file("data.gramps",mtime,g)
|
|
|
|
g.close()
|
|
|
|
t.close()
|
|
|
|
except:
|
|
|
|
print "Error exporting data to %s" % filename
|
|
|
|
os._exit(1)
|
|
|
|
elif format == 'iso':
|
|
|
|
import WriteCD
|
|
|
|
try:
|
|
|
|
WriteCD.PackageWriter(self.parent.db,1,filename)
|
|
|
|
except:
|
|
|
|
print "Error exporting %s" % filename
|
|
|
|
os._exit(1)
|
|
|
|
elif format == 'wft':
|
|
|
|
import WriteFtree
|
|
|
|
try:
|
|
|
|
WriteFtree.FtreeWriter(self.parent.db,None,1,filename)
|
|
|
|
except:
|
|
|
|
print "Error exporting %s" % filename
|
|
|
|
os._exit(1)
|
|
|
|
else:
|
|
|
|
print "Invalid format: %s" % format
|
|
|
|
os._exit(1)
|
|
|
|
|
2004-06-23 02:19:22 +05:30
|
|
|
#-------------------------------------------------------------------------
|
|
|
|
#
|
|
|
|
# Action handler
|
|
|
|
#
|
|
|
|
#-------------------------------------------------------------------------
|
2003-10-13 07:25:56 +05:30
|
|
|
def cl_action(self,action):
|
2004-06-21 10:40:27 +05:30
|
|
|
"""
|
|
|
|
Command-line action routine. Try to perform specified action.
|
|
|
|
Any errors will cause the os._exit(1) call.
|
|
|
|
"""
|
2003-10-13 07:25:56 +05:30
|
|
|
if action == 'check':
|
|
|
|
import Check
|
2004-06-22 04:52:55 +05:30
|
|
|
checker = Check.CheckIntegrity(self.parent.db,None,None)
|
2003-10-13 07:25:56 +05:30
|
|
|
checker.check_for_broken_family_links()
|
|
|
|
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':
|
|
|
|
import Summary
|
|
|
|
text = Summary.build_report(self.parent.db,None)
|
|
|
|
print text
|
|
|
|
else:
|
|
|
|
print "Unknown action: %s." % action
|
|
|
|
os._exit(1)
|