* src/ArgHandler.py: Support for opening in the command-line mode.

* src/const.py.in: Add -O | --open option.


svn: r3991
This commit is contained in:
Alex Roitman
2005-01-29 23:34:09 +00:00
parent 2d626f508b
commit 7228eb98bc
3 changed files with 77 additions and 15 deletions

View File

@ -3,6 +3,9 @@
Use proper response ID; (GraphVizGraphics.__init__): set up self.doc; Use proper response ID; (GraphVizGraphics.__init__): set up self.doc;
(EmptyDoc.init): Add empty method. (EmptyDoc.init): Add empty method.
* src/ArgHandler.py: Support for opening in the command-line mode.
* src/const.py.in: Add -O | --open option.
2005-01-29 Eero Tamminen <eerot@sf> 2005-01-29 Eero Tamminen <eerot@sf>
* src/plugins/StatisticsChart.py: * src/plugins/StatisticsChart.py:
- Added support for Don's Pie charts + color styles for it - Added support for Don's Pie charts + color styles for it

View File

@ -64,27 +64,32 @@ class ArgHandler:
FILE : filename to open. FILE : filename to open.
All following arguments will be ignored. All following arguments will be ignored.
-i, --import=FILE : filename to import. -i, --import=FILE : filename to import.
-O, --open=FILE : filename to open.
-o, --output=FILE : filename to export. -o, --output=FILE : filename to export.
-f, --format=FORMAT : format of the file preceding this option. -f, --format=FORMAT : format of the file preceding this option.
If the filename (no flags) is specified, the interactive session is 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 launched using data from filename. If the filename is not in a natvive
be presented to set up a grdb database. (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
If no filename or -i option is given, a new interactive session (empty If no filename or -i option is given, a new interactive session (empty
database) is launched, since no data is goven anyway. database) is launched, since no data is given anyway.
If -i option is given, but no -o or -a options are given, and interactive If -O or -i option is given, but no -o or -a options are given, an
session is launched with the FILE (specified with -i). interactive session is launched with the FILE (specified with -i).
If both -i and -o or -a are given, interactive session will not be If both input (-O or -i) and processing (-o or -a) options are given,
launched. interactive session will not be launched.
""" """
def __init__(self,parent,args): def __init__(self,parent,args):
self.parent = parent self.parent = parent
self.args = args self.args = args
self.open_gui = None
self.open = None self.open = None
self.exports = [] self.exports = []
self.actions = [] self.actions = []
@ -114,13 +119,34 @@ class ArgHandler:
if leftargs: if leftargs:
# if there were an argument without option, use it as a file to # if there were an argument without option, use it as a file to
# open and return # open and return
self.open = leftargs[0] self.open_gui = leftargs[0]
print "Trying to open: %s ..." % leftargs[0] print "Trying to open: %s ..." % leftargs[0]
return return
for opt_ix in range(len(options)): for opt_ix in range(len(options)):
o,v = options[opt_ix] o,v = options[opt_ix]
if o in ( '-i', '--import'): if o in ( '-O', '--open'):
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'):
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'
elif ftype == "x-directory/normal":
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'):
fname = v fname = v
ftype = GrampsMime.get_type(os.path.abspath(os.path.expanduser(fname))) ftype = GrampsMime.get_type(os.path.abspath(os.path.expanduser(fname)))
if opt_ix<len(options)-1 \ if opt_ix<len(options)-1 \
@ -222,11 +248,11 @@ class ArgHandler:
session, write files, and/or perform actions. session, write files, and/or perform actions.
""" """
if self.open: if self.open_gui:
# Filename was given. Open a session with that file. Forget # Filename was given. Open a session with that file. Forget
# the rest of given arguments. # the rest of given arguments.
success = False success = False
filename = os.path.abspath(os.path.expanduser(self.open)) filename = os.path.abspath(os.path.expanduser(self.open_gui))
filetype = GrampsMime.get_type(filename) filetype = GrampsMime.get_type(filename)
if filetype == const.app_gramps: if filetype == const.app_gramps:
print "Type: GRAMPS database" print "Type: GRAMPS database"
@ -270,9 +296,39 @@ class ArgHandler:
self.parent.build_recent_menu() self.parent.build_recent_menu()
return return
if self.imports: 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.
self.parent.cl = bool(self.exports or self.actions) self.parent.cl = bool(self.exports or self.actions)
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:
self.parent.cl = bool(self.exports or self.actions or self.parent.cl)
# Create dir for imported database(s) # Create dir for imported database(s)
self.impdir_path = os.path.expanduser("~/.gramps/import" ) self.impdir_path = os.path.expanduser("~/.gramps/import" )
self.imp_db_path = os.path.expanduser("~/.gramps/import/import_db.grdb" ) self.imp_db_path = os.path.expanduser("~/.gramps/import/import_db.grdb" )
@ -299,7 +355,7 @@ class ArgHandler:
print "Importing: file %s, format %s." % imp 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 and not self.open:
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,", \
"supply at least one input file to process." "supply at least one input file to process."
@ -319,6 +375,7 @@ class ArgHandler:
print "Cleaning up." print "Cleaning up."
# remove import db after use # remove import db after use
self.parent.db.close() self.parent.db.close()
if self.imports:
os.remove(self.imp_db_path) os.remove(self.imp_db_path)
print "Exiting." print "Exiting."
os._exit(0) os._exit(0)

View File

@ -160,6 +160,7 @@ unknown = _("unknown")
# (longName, shortName, type , default, flags, descrip , argDescrip) # (longName, shortName, type , default, flags, descrip , argDescrip)
popt_table = [ popt_table = [
("open", 'O', str, None, 0, "Open file", "FILENAME"),
("import", 'i', str, None, 0, "Import file", "FILENAME"), ("import", 'i', str, None, 0, "Import file", "FILENAME"),
("output", 'o', str, None, 0, "Write file", "FILENAME"), ("output", 'o', str, None, 0, "Write file", "FILENAME"),
("format", 'f', str, None, 0, 'Specify format', "FORMAT"), ("format", 'f', str, None, 0, 'Specify format', "FORMAT"),
@ -196,6 +197,7 @@ longopts = [
"disable-sound", "disable-sound",
"enable-sound", "enable-sound",
"espeaker=", "espeaker=",
"open=",
"import=", "import=",
"output=", "output=",
"format=", "format=",
@ -203,7 +205,7 @@ longopts = [
"options=", "options=",
] ]
shortopts = "i:o:f:a:p:?" shortopts = "O:i:o:f:a:p:?"
#------------------------------------------------------------------------- #-------------------------------------------------------------------------
# #