2003-10-13 07:25:56 +05:30
|
|
|
#
|
|
|
|
# Gramps - a GTK+/GNOME based genealogy program
|
|
|
|
#
|
2006-03-01 01:24:35 +05:30
|
|
|
# Copyright (C) 2000-2006 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.
|
|
|
|
"""
|
|
|
|
|
2006-04-21 09:44:00 +05:30
|
|
|
#-------------------------------------------------------------------------
|
|
|
|
#
|
|
|
|
# GNOME/GTK
|
|
|
|
#
|
|
|
|
#-------------------------------------------------------------------------
|
|
|
|
import gtk
|
|
|
|
|
2003-10-13 07:25:56 +05:30
|
|
|
#-------------------------------------------------------------------------
|
|
|
|
#
|
|
|
|
# Standard python modules
|
|
|
|
#
|
|
|
|
#-------------------------------------------------------------------------
|
|
|
|
import os
|
|
|
|
import getopt
|
2006-04-07 03:32:46 +05:30
|
|
|
from gettext import gettext as _
|
2003-10-13 07:25:56 +05:30
|
|
|
|
|
|
|
#-------------------------------------------------------------------------
|
|
|
|
#
|
|
|
|
# gramps modules
|
|
|
|
#
|
|
|
|
#-------------------------------------------------------------------------
|
|
|
|
import const
|
2005-12-21 16:57:05 +05:30
|
|
|
import GrampsDb
|
2006-03-03 05:53:04 +05:30
|
|
|
import Mime
|
2004-06-21 10:40:27 +05:30
|
|
|
import QuestionDialog
|
2006-03-03 05:40:52 +05:30
|
|
|
import Config
|
2004-11-25 11:12:31 +05:30
|
|
|
import RecentFiles
|
2006-04-21 09:44:00 +05:30
|
|
|
import Utils
|
|
|
|
|
2006-03-11 06:42:06 +05:30
|
|
|
from PluginUtils import Report, Tool, cl_list, cli_tool_list
|
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.
|
2005-01-30 05:04:09 +05:30
|
|
|
-O, --open=FILE : filename to open.
|
2004-06-21 10:40:27 +05:30
|
|
|
-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
|
2005-01-30 05:04:09 +05:30
|
|
|
launched using data from filename. If the filename is not in a natvive
|
|
|
|
(grdb) format, dialog will be presented to set up a grdb database.
|
|
|
|
In this mode (filename, no flags), the rest of the arguments is ignored.
|
|
|
|
This is a mode suitable by default for GUI launchers, mime type handlers,
|
|
|
|
and the like
|
2004-06-21 10:40:27 +05:30
|
|
|
|
|
|
|
If no filename or -i option is given, a new interactive session (empty
|
2005-01-30 05:04:09 +05:30
|
|
|
database) is launched, since no data is given anyway.
|
2004-06-21 10:40:27 +05:30
|
|
|
|
2005-01-30 05:04:09 +05:30
|
|
|
If -O or -i option is given, but no -o or -a options are given, an
|
|
|
|
interactive session is launched with the FILE (specified with -i).
|
2004-06-21 10:40:27 +05:30
|
|
|
|
2005-01-30 05:04:09 +05:30
|
|
|
If both input (-O or -i) and processing (-o or -a) options are given,
|
|
|
|
interactive session will not be launched.
|
2004-06-21 10:40:27 +05:30
|
|
|
"""
|
2003-10-13 07:25:56 +05:30
|
|
|
|
2005-08-12 21:55:43 +05:30
|
|
|
def __init__(self,state,vm,args):
|
|
|
|
self.state = state
|
|
|
|
self.vm = vm
|
2004-06-21 10:40:27 +05:30
|
|
|
self.args = args
|
|
|
|
|
2005-01-30 05:04:09 +05:30
|
|
|
self.open_gui = None
|
2004-06-21 10:40:27 +05:30
|
|
|
self.open = None
|
2006-04-11 00:00:30 +05:30
|
|
|
self.cl = 0
|
2004-06-21 10:40:27 +05:30
|
|
|
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
|
2005-01-30 05:04:09 +05:30
|
|
|
self.open_gui = leftargs[0]
|
2004-06-21 10:40:27 +05:30
|
|
|
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]
|
2005-01-30 05:04:09 +05:30
|
|
|
if o in ( '-O', '--open'):
|
|
|
|
fname = v
|
2006-03-03 05:53:04 +05:30
|
|
|
ftype = Mime.get_type(
|
2005-08-18 11:28:28 +05:30
|
|
|
os.path.abspath(os.path.expanduser(fname)))
|
2005-01-30 05:04:09 +05:30
|
|
|
if opt_ix<len(options)-1 \
|
|
|
|
and options[opt_ix+1][0] in ( '-f', '--format'):
|
|
|
|
format = options[opt_ix+1][1]
|
|
|
|
if format not in ('gedcom','gramps-xml','grdb'):
|
|
|
|
print "Invalid format: %s" % format
|
|
|
|
print "Ignoring input file: %s" % fname
|
|
|
|
continue
|
|
|
|
elif ftype == const.app_gedcom:
|
|
|
|
format = 'gedcom'
|
2005-08-18 11:28:28 +05:30
|
|
|
elif ftype == const.app_gramps_xml:
|
2005-01-30 05:04:09 +05:30
|
|
|
format = 'gramps-xml'
|
|
|
|
elif ftype == const.app_gramps:
|
|
|
|
format = 'grdb'
|
|
|
|
else:
|
|
|
|
print "Unrecognized format for input file %s" % fname
|
|
|
|
print "Ignoring input file: %s" % fname
|
|
|
|
continue
|
|
|
|
self.open = (fname,format)
|
|
|
|
elif o in ( '-i', '--import'):
|
2004-06-21 10:40:27 +05:30
|
|
|
fname = v
|
2006-03-03 05:53:04 +05:30
|
|
|
ftype = Mime.get_type(
|
2005-08-18 11:28:28 +05:30
|
|
|
os.path.abspath(os.path.expanduser(fname)))
|
2004-06-21 10:40:27 +05:30
|
|
|
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]
|
2005-08-18 11:28:28 +05:30
|
|
|
if format not in ('gedcom',
|
|
|
|
'gramps-xml',
|
|
|
|
'gramps-pkg',
|
|
|
|
'grdb',
|
|
|
|
'geneweb'):
|
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'
|
2005-08-18 11:28:28 +05:30
|
|
|
elif ftype == const.app_gramps_xml:
|
2004-06-21 10:40:27 +05:30
|
|
|
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'
|
2005-02-20 00:35:48 +05:30
|
|
|
elif ftype == const.app_geneweb:
|
|
|
|
format = 'geneweb'
|
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]
|
2005-08-18 11:28:28 +05:30
|
|
|
if outformat not in ('gedcom',
|
|
|
|
'gramps-xml',
|
|
|
|
'gramps-pkg',
|
|
|
|
'grdb',
|
|
|
|
'iso',
|
|
|
|
'wft',
|
|
|
|
'geneweb'):
|
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'
|
2005-02-20 00:35:48 +05:30
|
|
|
elif outfname[-2:].upper() == "GW":
|
|
|
|
outformat = 'geneweb'
|
2005-08-18 11:28:28 +05:30
|
|
|
elif outfname[-6:].upper() == "GRAMPS":
|
2004-06-21 10:40:27 +05:30
|
|
|
outformat = 'gramps-xml'
|
2005-12-06 12:08:09 +05:30
|
|
|
elif outfname[-4:].upper() == "GRDB":
|
|
|
|
outformat = '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
|
2005-12-06 12:08:09 +05:30
|
|
|
if action not in ( 'check', 'summary', 'report', 'tool' ):
|
2004-06-21 10:40:27 +05:30
|
|
|
print "Unknown action: %s. Ignoring." % action
|
|
|
|
continue
|
2004-12-22 07:26:37 +05:30
|
|
|
options_str = ""
|
|
|
|
if opt_ix<len(options)-1 \
|
|
|
|
and options[opt_ix+1][0] in ( '-p', '--options' ):
|
|
|
|
options_str = options[opt_ix+1][1]
|
|
|
|
self.actions.append((action,options_str))
|
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):
|
2004-08-24 04:09:41 +05:30
|
|
|
filename = os.path.normpath(os.path.abspath(filename))
|
2006-03-03 05:53:04 +05:30
|
|
|
filetype = Mime.get_type(filename)
|
2005-08-12 21:55:43 +05:30
|
|
|
if filetype in (const.app_gramps,
|
|
|
|
const.app_gramps_xml,
|
|
|
|
const.app_gedcom):
|
|
|
|
return self.vm.open_native(filename,filetype)
|
2004-08-24 04:09:41 +05:30
|
|
|
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.
|
|
|
|
"""
|
|
|
|
|
2005-01-30 05:04:09 +05:30
|
|
|
if self.open_gui:
|
2004-06-21 10:40:27 +05:30
|
|
|
# Filename was given. Open a session with that file. Forget
|
|
|
|
# the rest of given arguments.
|
2004-11-25 11:12:31 +05:30
|
|
|
success = False
|
2005-01-30 05:04:09 +05:30
|
|
|
filename = os.path.abspath(os.path.expanduser(self.open_gui))
|
2006-03-03 05:53:04 +05:30
|
|
|
filetype = Mime.get_type(filename)
|
2005-02-14 08:39:22 +05:30
|
|
|
if filetype in (const.app_gramps,const.app_gedcom,
|
2006-04-10 23:47:00 +05:30
|
|
|
const.app_gramps_xml):
|
2005-02-14 08:39:22 +05:30
|
|
|
# Say the type outloud
|
|
|
|
if filetype == const.app_gramps:
|
|
|
|
print "Type: GRAMPS database"
|
|
|
|
elif filetype == const.app_gedcom:
|
|
|
|
print "Type: GEDCOM file"
|
|
|
|
elif filetype == const.app_gramps_xml:
|
|
|
|
print "Type: GRAMPS XML 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!"
|
2004-11-25 11:12:31 +05:30
|
|
|
success = True
|
2004-06-21 10:40:27 +05:30
|
|
|
else:
|
|
|
|
print "Cannot open %s. Exiting..."
|
2005-02-14 08:39:22 +05:30
|
|
|
elif filetype in (const.app_gramps_package,):
|
2004-06-21 10:40:27 +05:30
|
|
|
QuestionDialog.OkDialog( _("Opening non-native format"),
|
2005-04-12 07:26:36 +05:30
|
|
|
_("New GRAMPS database has to be set up "
|
|
|
|
"when opening non-native formats. The "
|
|
|
|
"following dialog will let you select "
|
2005-04-12 18:41:23 +05:30
|
|
|
"the new database."),
|
2006-04-10 23:47:00 +05:30
|
|
|
self.vm.window)
|
|
|
|
prompter = NewNativeDbPrompter(self.vm,self.state)
|
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"),
|
2005-08-18 11:28:28 +05:30
|
|
|
_('GRAMPS cannot open non-native data '
|
|
|
|
'without setting up new GRAMPS database.'))
|
2006-04-10 23:47:00 +05:30
|
|
|
print "Cannot continue without native database. Exiting..."
|
2004-08-24 04:09:41 +05:30
|
|
|
os._exit(1)
|
|
|
|
elif filetype == const.app_gramps_package:
|
2004-06-28 23:46:42 +05:30
|
|
|
print "Type: GRAMPS package"
|
2006-04-10 23:47:00 +05:30
|
|
|
self.read_pkg(filename)
|
2004-11-25 11:12:31 +05:30
|
|
|
success = True
|
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(
|
2005-04-12 07:26:36 +05:30
|
|
|
_("Could not open file: %s") % filename,
|
2005-08-18 11:28:28 +05:30
|
|
|
_('File type "%s" is unknown to GRAMPS.\n\n'
|
|
|
|
'Valid 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-11-25 11:12:31 +05:30
|
|
|
if success:
|
2004-11-29 10:45:41 +05:30
|
|
|
# Add the file to the recent items
|
|
|
|
RecentFiles.recent_files(filename,filetype)
|
2005-06-05 09:31:56 +05:30
|
|
|
else:
|
|
|
|
os._exit(1)
|
2004-06-21 10:40:27 +05:30
|
|
|
return
|
|
|
|
|
2005-01-30 05:04:09 +05:30
|
|
|
if self.open:
|
|
|
|
# Filename to open was given. Open it natively (grdb or any of
|
|
|
|
# the InMem formats, without setting up a new database. Then
|
|
|
|
# go on and process the rest of the command line arguments.
|
|
|
|
|
2006-04-10 23:47:00 +05:30
|
|
|
self.cl = bool(self.exports or self.actions)
|
2004-06-23 02:19:22 +05:30
|
|
|
|
2005-01-30 05:04:09 +05:30
|
|
|
name,format = self.open
|
|
|
|
success = False
|
|
|
|
filename = os.path.abspath(os.path.expanduser(name))
|
|
|
|
|
|
|
|
if format == 'grdb':
|
|
|
|
print "Type: GRAMPS database"
|
|
|
|
elif format == 'gedcom':
|
|
|
|
print "Type: GEDCOM"
|
|
|
|
elif format == 'gramps-xml':
|
|
|
|
print "Type: GRAMPS XML"
|
|
|
|
else:
|
|
|
|
print "Unknown file type: %s" % format
|
|
|
|
print "Exiting..."
|
|
|
|
os._exit(1)
|
|
|
|
|
|
|
|
if self.auto_save_load(filename):
|
|
|
|
print "Opened successfully!"
|
|
|
|
success = True
|
|
|
|
else:
|
|
|
|
print "Error opening the file."
|
|
|
|
print "Exiting..."
|
|
|
|
os._exit(1)
|
|
|
|
|
|
|
|
if self.imports:
|
2006-04-10 23:47:00 +05:30
|
|
|
self.cl = bool(self.exports or self.actions or self.cl)
|
2005-01-30 05:04:09 +05:30
|
|
|
|
2004-06-23 02:19:22 +05:30
|
|
|
# Create dir for imported database(s)
|
2006-04-10 23:47:00 +05:30
|
|
|
self.impdir_path = os.path.join(const.home_dir,"import")
|
|
|
|
self.imp_db_path = os.path.join(self.impdir_path,"import_db.grdb")
|
2004-06-23 02:19:22 +05:30
|
|
|
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))
|
|
|
|
|
2006-04-10 23:47:00 +05:30
|
|
|
self.vm.load_database(self.imp_db_path)
|
2004-06-21 10:40:27 +05:30
|
|
|
|
|
|
|
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
|
|
|
|
2005-01-30 05:04:09 +05:30
|
|
|
elif len(self.args) > 1 and not self.open:
|
2004-06-21 10:40:27 +05:30
|
|
|
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
|
|
|
|
2006-04-10 23:47:00 +05:30
|
|
|
if self.cl:
|
|
|
|
for (action,options_str) in self.actions:
|
|
|
|
print "Performing action: %s." % action
|
|
|
|
if options_str:
|
|
|
|
print "Using options string: %s" % options_str
|
|
|
|
self.cl_action(action,options_str)
|
|
|
|
|
|
|
|
for expt in self.exports:
|
|
|
|
print "Exporting: file %s, format %s." % expt
|
|
|
|
self.cl_export(expt[0],expt[1])
|
|
|
|
|
|
|
|
print "Cleaning up."
|
|
|
|
# remove import db after use
|
|
|
|
self.state.db.close()
|
|
|
|
if self.imports:
|
|
|
|
os.remove(self.imp_db_path)
|
|
|
|
print "Exiting."
|
|
|
|
os._exit(0)
|
|
|
|
|
|
|
|
## if self.imports:
|
|
|
|
## self.parent.import_tool_callback()
|
2006-04-25 02:34:01 +05:30
|
|
|
elif Config.get(Config.RECENT_FILE) and Config.get(Config.AUTOLOAD):
|
|
|
|
self.auto_save_load(Config.get(Config.RECENT_FILE))
|
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.
|
|
|
|
"""
|
2005-01-29 11:35:51 +05:30
|
|
|
if format == 'grdb':
|
|
|
|
filename = os.path.normpath(os.path.abspath(filename))
|
|
|
|
try:
|
2006-04-10 23:47:00 +05:30
|
|
|
GrampsDb.gramps_db_reader_factory(const.app_gramps)(
|
|
|
|
self.state.db,filename,None)
|
2005-01-29 11:35:51 +05:30
|
|
|
except:
|
|
|
|
print "Error importing %s" % filename
|
|
|
|
os._exit(1)
|
|
|
|
elif format == 'gedcom':
|
2005-12-21 16:57:05 +05:30
|
|
|
from GrampsDb import GedcomParser
|
2003-10-13 07:25:56 +05:30
|
|
|
filename = os.path.normpath(os.path.abspath(filename))
|
|
|
|
try:
|
2006-04-10 23:47:00 +05:30
|
|
|
g = GedcomParser(self.state.db,filename,None)
|
2003-10-13 07:25:56 +05:30
|
|
|
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:
|
2006-04-10 23:47:00 +05:30
|
|
|
GrampsDb.gramps_db_reader_factory(const.app_gramps_xml)(
|
|
|
|
self.state.db,filename,None,self.cl)
|
2003-10-13 07:25:56 +05:30
|
|
|
except:
|
|
|
|
print "Error importing %s" % filename
|
|
|
|
os._exit(1)
|
2005-02-20 00:35:48 +05:30
|
|
|
elif format == 'geneweb':
|
|
|
|
import ImportGeneWeb
|
|
|
|
filename = os.path.normpath(os.path.abspath(filename))
|
|
|
|
try:
|
2006-04-10 23:47:00 +05:30
|
|
|
ImportGeneWeb.importData(self.state.db,filename,None)
|
2005-02-20 00:35:48 +05:30
|
|
|
except:
|
|
|
|
print "Error importing %s" % filename
|
|
|
|
os._exit(1)
|
2003-10-13 07:25:56 +05:30
|
|
|
elif format == 'gramps-pkg':
|
|
|
|
# Create tempdir, if it does not exist, then check for writability
|
2006-03-29 10:16:26 +05:30
|
|
|
tmpdir_path = os.path.join(const.home_dir,"tmp")
|
2003-10-13 07:25:56 +05:30
|
|
|
if not os.path.isdir(tmpdir_path):
|
|
|
|
try:
|
|
|
|
os.mkdir(tmpdir_path,0700)
|
|
|
|
except:
|
2005-08-18 11:28:28 +05:30
|
|
|
print "Could not create temporary directory %s" \
|
|
|
|
% tmpdir_path
|
2003-10-13 07:25:56 +05:30
|
|
|
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:
|
2006-03-01 05:56:29 +05:30
|
|
|
import tarfile
|
|
|
|
archive = tarfile.open(filename)
|
|
|
|
for tarinfo in archive:
|
|
|
|
archive.extract(tarinfo,tmpdir_path)
|
|
|
|
archive.close()
|
|
|
|
except ReadError, msg:
|
|
|
|
print "Error reading archive:", msg
|
|
|
|
os._exit(1)
|
|
|
|
except CompressError, msg:
|
|
|
|
print "Error uncompressing archive:", msg
|
|
|
|
os._exit(1)
|
2003-10-13 07:25:56 +05:30
|
|
|
except:
|
2006-03-01 05:56:29 +05:30
|
|
|
print "Error extracting into %s" % tmpdir_path
|
2003-10-13 07:25:56 +05:30
|
|
|
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:
|
2006-04-10 23:47:00 +05:30
|
|
|
GrampsDb.gramps_db_reader_factory(const.app_gramps_xml)(
|
|
|
|
self.state.db,dbname,None)
|
2003-10-13 07:25:56 +05:30
|
|
|
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)
|
2006-04-10 23:47:00 +05:30
|
|
|
if not self.cl:
|
|
|
|
return self.vm.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.
|
|
|
|
"""
|
2005-12-06 12:08:09 +05:30
|
|
|
if format == 'grdb':
|
|
|
|
try:
|
2006-04-10 23:47:00 +05:30
|
|
|
GrampsDb.gramps_db_writer_factory(const.app_gramps)(
|
|
|
|
self.state.db,filename)
|
2005-12-06 12:08:09 +05:30
|
|
|
except:
|
|
|
|
print "Error exporting %s" % filename
|
|
|
|
os._exit(1)
|
|
|
|
elif format == 'gedcom':
|
2003-10-13 07:25:56 +05:30
|
|
|
try:
|
2006-04-10 23:47:00 +05:30
|
|
|
gw = GrampsDb.GedcomWriter(self.state.db,None,1,filename)
|
2005-02-20 00:35:48 +05:30
|
|
|
ret = gw.export_data(filename)
|
2003-10-13 07:25:56 +05:30
|
|
|
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))
|
|
|
|
if filename:
|
|
|
|
try:
|
2006-04-10 23:47:00 +05:30
|
|
|
g = GrampsDb.XmlWriter(self.state.db,None,0,1)
|
2005-08-18 11:28:28 +05:30
|
|
|
ret = g.write(filename)
|
2003-10-13 07:25:56 +05:30
|
|
|
except:
|
|
|
|
print "Error exporting %s" % filename
|
|
|
|
os._exit(1)
|
|
|
|
elif format == 'gramps-pkg':
|
|
|
|
try:
|
2005-02-20 00:35:48 +05:30
|
|
|
import WritePkg
|
2006-04-10 23:47:00 +05:30
|
|
|
writer = WritePkg.PackageWriter(self.state.db,filename)
|
2005-02-20 00:35:48 +05:30
|
|
|
ret = writer.export()
|
2003-10-13 07:25:56 +05:30
|
|
|
except:
|
|
|
|
print "Error creating %s" % filename
|
|
|
|
os._exit(1)
|
|
|
|
elif format == 'iso':
|
2005-02-20 04:03:24 +05:30
|
|
|
import WriteCD
|
|
|
|
try:
|
2006-04-10 23:47:00 +05:30
|
|
|
writer = WriteCD.PackageWriter(self.state.db,filename,1)
|
2005-02-20 04:03:24 +05:30
|
|
|
ret = writer.export()
|
|
|
|
except:
|
|
|
|
print "Error exporting %s" % filename
|
|
|
|
os._exit(1)
|
2005-02-20 00:35:48 +05:30
|
|
|
elif format == 'wft':
|
|
|
|
import WriteFtree
|
2003-10-13 07:25:56 +05:30
|
|
|
try:
|
2006-04-10 23:47:00 +05:30
|
|
|
writer = WriteFtree.FtreeWriter(self.state.db,None,1,filename)
|
2005-02-20 00:35:48 +05:30
|
|
|
ret = writer.export_data()
|
2003-10-13 07:25:56 +05:30
|
|
|
except:
|
|
|
|
print "Error exporting %s" % filename
|
|
|
|
os._exit(1)
|
2005-02-20 00:35:48 +05:30
|
|
|
elif format == 'geneweb':
|
|
|
|
import WriteGeneWeb
|
2003-10-13 07:25:56 +05:30
|
|
|
try:
|
2006-04-10 23:47:00 +05:30
|
|
|
writer = WriteGeneWeb.GeneWebWriter(self.state.db,
|
2005-08-18 11:28:28 +05:30
|
|
|
None,1,filename)
|
2005-02-20 00:35:48 +05:30
|
|
|
ret = writer.export_data()
|
2003-10-13 07:25:56 +05:30
|
|
|
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
|
|
|
|
#
|
|
|
|
#-------------------------------------------------------------------------
|
2004-12-22 07:26:37 +05:30
|
|
|
def cl_action(self,action,options_str):
|
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
|
2006-04-10 23:47:00 +05:30
|
|
|
checker = Check.CheckIntegrity(self.state.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
|
2006-04-10 23:47:00 +05:30
|
|
|
text = Summary.build_report(self.state.db,None)
|
2003-10-13 07:25:56 +05:30
|
|
|
print text
|
2004-12-22 07:26:37 +05:30
|
|
|
elif action == "report":
|
|
|
|
try:
|
|
|
|
options_str_dict = dict( [ tuple(chunk.split('='))
|
|
|
|
for chunk in options_str.split(',') ] )
|
|
|
|
except:
|
|
|
|
options_str_dict = {}
|
|
|
|
print "Ignoring invalid options string."
|
|
|
|
|
|
|
|
name = options_str_dict.pop('name',None)
|
|
|
|
if not name:
|
|
|
|
print "Report name not given. Please use name=reportname"
|
|
|
|
os._exit(1)
|
|
|
|
|
2006-03-11 06:42:06 +05:30
|
|
|
for item in cl_list:
|
2004-12-22 07:26:37 +05:30
|
|
|
if name == item[0]:
|
|
|
|
category = item[1]
|
|
|
|
report_class = item[2]
|
|
|
|
options_class = item[3]
|
2005-12-06 12:08:09 +05:30
|
|
|
if category in (Report.CATEGORY_BOOK,Report.CATEGORY_CODE,
|
|
|
|
Report.CATEGORY_WEB):
|
2006-04-10 23:47:00 +05:30
|
|
|
options_class(self.state.db,name,
|
2005-08-18 11:28:28 +05:30
|
|
|
category,options_str_dict)
|
2004-12-22 07:26:37 +05:30
|
|
|
else:
|
2006-04-10 23:47:00 +05:30
|
|
|
Report.cl_report(self.state.db,name,category,
|
2005-12-06 12:08:09 +05:30
|
|
|
report_class,options_class,
|
|
|
|
options_str_dict)
|
2004-12-22 07:26:37 +05:30
|
|
|
return
|
|
|
|
|
|
|
|
print "Unknown report name. Available names are:"
|
2006-03-11 06:42:06 +05:30
|
|
|
for item in cl_list:
|
2004-12-22 07:26:37 +05:30
|
|
|
print " %s" % item[0]
|
2005-12-06 12:08:09 +05:30
|
|
|
elif action == "tool":
|
|
|
|
try:
|
|
|
|
options_str_dict = dict( [ tuple(chunk.split('=')) for
|
|
|
|
chunk in options_str.split(',') ] )
|
|
|
|
except:
|
|
|
|
options_str_dict = {}
|
|
|
|
print "Ignoring invalid options string."
|
|
|
|
|
|
|
|
name = options_str_dict.pop('name',None)
|
|
|
|
if not name:
|
|
|
|
print "Tool name not given. Please use name=toolname"
|
|
|
|
os._exit(1)
|
|
|
|
|
2006-03-11 06:42:06 +05:30
|
|
|
for item in cli_tool_list:
|
2005-12-06 12:08:09 +05:30
|
|
|
if name == item[0]:
|
|
|
|
category = item[1]
|
|
|
|
tool_class = item[2]
|
|
|
|
options_class = item[3]
|
2006-04-10 23:47:00 +05:30
|
|
|
Tool.cli_tool(self.state.db,name,category,
|
2005-12-06 12:08:09 +05:30
|
|
|
tool_class,options_class,options_str_dict)
|
|
|
|
return
|
|
|
|
|
|
|
|
print "Unknown tool name. Available names are:"
|
2006-03-11 06:42:06 +05:30
|
|
|
for item in cli_tool_list:
|
2005-12-06 12:08:09 +05:30
|
|
|
print " %s" % item[0]
|
2003-10-13 07:25:56 +05:30
|
|
|
else:
|
|
|
|
print "Unknown action: %s." % action
|
|
|
|
os._exit(1)
|
2005-12-30 00:06:59 +05:30
|
|
|
|
|
|
|
#-------------------------------------------------------------------------
|
|
|
|
#
|
|
|
|
# NewNativeDbPrompter
|
|
|
|
#
|
|
|
|
#-------------------------------------------------------------------------
|
|
|
|
class NewNativeDbPrompter:
|
|
|
|
"""
|
|
|
|
This class allows to set up a new empty native (grdb) database.
|
|
|
|
The filename is forced to have an '.grdb' extension. If not given,
|
|
|
|
it is appended.
|
|
|
|
"""
|
|
|
|
|
2006-04-10 23:47:00 +05:30
|
|
|
def __init__(self,vm,state):
|
|
|
|
self.vm = vm
|
|
|
|
self.state = state
|
2005-12-30 00:06:59 +05:30
|
|
|
|
|
|
|
def chooser(self):
|
|
|
|
"""
|
|
|
|
Select the new file. Suggest the Untitled_X.grdb name.
|
|
|
|
Return 1 when selection is made and 0 otherwise.
|
|
|
|
"""
|
|
|
|
choose = gtk.FileChooserDialog(_('GRAMPS: Create GRAMPS database'),
|
2006-04-10 23:47:00 +05:30
|
|
|
self.vm.window,
|
|
|
|
gtk.FILE_CHOOSER_ACTION_SAVE,
|
|
|
|
(gtk.STOCK_CANCEL,
|
|
|
|
gtk.RESPONSE_CANCEL,
|
|
|
|
gtk.STOCK_OPEN,
|
|
|
|
gtk.RESPONSE_OK))
|
|
|
|
self.state.db.close()
|
2005-12-30 00:06:59 +05:30
|
|
|
|
|
|
|
# Always add automatic (macth all files) filter
|
|
|
|
add_all_files_filter(choose)
|
|
|
|
add_grdb_filter(choose)
|
|
|
|
|
|
|
|
# Suggested folder: try last open file, import, then last export,
|
|
|
|
# then home.
|
2006-04-25 02:34:01 +05:30
|
|
|
default_dir = os.path.split(Config.get(Config.RECENT_FILE))[0] + os.path.sep
|
2005-12-30 00:06:59 +05:30
|
|
|
if len(default_dir)<=1:
|
2006-04-25 02:34:01 +05:30
|
|
|
default_dir = Config.get(Config.RECENT_IMPORT_DIR)
|
2005-12-30 00:06:59 +05:30
|
|
|
if len(default_dir)<=1:
|
2006-04-25 02:34:01 +05:30
|
|
|
default_dir = Config.get(Config.RECENT_EXPORT_DIR)
|
2005-12-30 00:06:59 +05:30
|
|
|
if len(default_dir)<=1:
|
|
|
|
default_dir = '~/'
|
|
|
|
|
|
|
|
new_filename = Utils.get_new_filename('grdb',default_dir)
|
|
|
|
|
|
|
|
choose.set_current_folder(default_dir)
|
|
|
|
choose.set_current_name(os.path.split(new_filename)[1])
|
|
|
|
|
|
|
|
while (True):
|
|
|
|
response = choose.run()
|
|
|
|
if response == gtk.RESPONSE_OK:
|
|
|
|
filename = choose.get_filename()
|
|
|
|
if filename == None:
|
|
|
|
continue
|
|
|
|
if os.path.splitext(filename)[1] != ".grdb":
|
|
|
|
filename = filename + ".grdb"
|
|
|
|
choose.destroy()
|
2006-04-10 23:47:00 +05:30
|
|
|
self.state.db = GrampsDb.gramps_db_factory(const.app_gramps)()
|
|
|
|
self.vm.read_file(filename)
|
|
|
|
self.state.signal_change()
|
|
|
|
self.change_page(None, None)
|
2005-12-30 00:06:59 +05:30
|
|
|
# Add the file to the recent items
|
|
|
|
RecentFiles.recent_files(filename,const.app_gramps)
|
|
|
|
return True
|
|
|
|
else:
|
|
|
|
choose.destroy()
|
|
|
|
return False
|
|
|
|
choose.destroy()
|
|
|
|
return False
|
|
|
|
|
|
|
|
def add_all_files_filter(chooser):
|
|
|
|
"""
|
|
|
|
Add an all-permitting filter to the file chooser dialog.
|
|
|
|
"""
|
|
|
|
mime_filter = gtk.FileFilter()
|
|
|
|
mime_filter.set_name(_('All files'))
|
|
|
|
mime_filter.add_pattern('*')
|
|
|
|
chooser.add_filter(mime_filter)
|
|
|
|
|
|
|
|
def add_grdb_filter(chooser):
|
|
|
|
"""
|
|
|
|
Add a GRDB filter to the file chooser dialog.
|
|
|
|
"""
|
|
|
|
mime_filter = gtk.FileFilter()
|
|
|
|
mime_filter.set_name(_('GRAMPS databases'))
|
|
|
|
mime_filter.add_mime_type(const.app_gramps)
|
|
|
|
chooser.add_filter(mime_filter)
|
2006-04-10 23:47:00 +05:30
|
|
|
|
|
|
|
def read_pkg(filename):
|
|
|
|
print "FIXME: This is not re-implemented yet."
|