Issue 4276: Gramps cli interface for user with non latin characters in name. Mostly Windows.

svn: r15984
This commit is contained in:
Peter Landgren 2010-10-14 11:46:05 +00:00
parent eb8eee7062
commit ed6f4a0080
8 changed files with 157 additions and 38 deletions

View File

@ -34,7 +34,7 @@ msgstr ""
"Project-Id-Version: sv\n"
"Report-Msgid-Bugs-To: \n"
"POT-Creation-Date: 2010-10-11 13:24+0200\n"
"PO-Revision-Date: 2010-10-11 13:26+0200\n"
"PO-Revision-Date: 2010-10-14 13:44+0200\n"
"Last-Translator: Peter Landgren <peter.talken@telia.com>\n"
"Language-Team: Swedish <kde-i18n-doc@kde.org>\n"
"MIME-Version: 1.0\n"
@ -2258,7 +2258,7 @@ msgstr ""
"gramps\n"
"\n"
"Notera: Dessa exempel gäller för bash-skalet.\n"
"Syntaxen kan vara annorlunda ut för andra skall och i Windows.\n"
"Syntaxen kan vara annorlunda för andra skal och i Windows.\n"
#: ../src/cli/argparser.py:225 ../src/cli/argparser.py:339
msgid "Error parsing the arguments"

View File

@ -95,9 +95,11 @@ class ArgHandler(object):
if self.errorfunc:
self.errorfunc(msg1)
else:
print msg1
# Need to convert to system file encoding before printing
# For non latin characters in path/file/user names
print msg1.encode(sys.getfilesystemencoding())
if msg2 is not None:
print msg2
print msg2.encode(sys.getfilesystemencoding())
#-------------------------------------------------------------------------
# Argument parser: sorts out given arguments
@ -118,6 +120,7 @@ class ArgHandler(object):
"""
if value is None:
return None
value = Utils.get_unicode_path_from_env_var(value)
db_path = self.__deduce_db_path(value)
if db_path:
@ -137,6 +140,9 @@ class ArgHandler(object):
Handle the "-i" or "--import" option.
Only Files supported by a plugin can be imported, so not Family Trees.
"""
# Need to convert path/filename to unicode before opening
# For non latin characters in Windows path/file/user names
value = Utils.get_unicode_path_from_env_var(value)
fname = value
fullpath = os.path.abspath(os.path.expanduser(fname))
if not os.path.exists(fullpath):
@ -171,6 +177,9 @@ class ArgHandler(object):
"""
if self.gui:
return
# Need to covert path/filename to unicode before openingh
# For non latin characters in Windows path/file/user names
value = Utils.get_unicode_path_from_env_var(value)
fname = value
fullpath = os.path.abspath(os.path.expanduser(fname))
if os.path.exists(fullpath):
@ -179,8 +188,9 @@ class ArgHandler(object):
{'name' : fullpath})
answer = None
while not answer:
answer = raw_input(_('OK to overwrite? (yes/no) '))
if answer.upper() in ('Y', 'YES', _('YES')):
answer = raw_input(_('OK to overwrite? (yes/no) ') \
.encode(sys.getfilesystemencoding()))
if answer.upper() in ('Y', 'YES', _('YES').upper()):
self.__error( _("Will overwrite the existing file: %s")
% fullpath)
else:
@ -313,7 +323,11 @@ class ArgHandler(object):
self.cl_action(action, options_str)
for expt in self.exports:
print "Exporting: file %s, format %s." % expt
# Need to convert path/filename to str before printing
# For non latin characters in Windows path/file/user names
fn = expt[0].encode(sys.getfilesystemencoding())
fmt = str(expt[1])
print "Exporting: file %s, format %s." % (fn, fmt)
self.cl_export(expt[0], expt[1])
if cleanup:
@ -345,8 +359,8 @@ class ArgHandler(object):
if self.gui:
self.imp_db_path, title = self.dbman.create_new_db_cli()
else:
self.imp_db_path = Utils.get_empty_tempdir("import_dbdir")
self.imp_db_path = Utils.get_empty_tempdir("import_dbdir") \
.encode(sys.getfilesystemencoding())
newdb = gen.db.DbBsddb()
newdb.write_version(self.imp_db_path)
@ -359,7 +373,9 @@ class ArgHandler(object):
sys.exit(0)
for imp in self.imports:
print "Importing: file %s, format %s." % imp
fn = imp[0].encode(sys.getfilesystemencoding())
fmt = str(imp[1])
print "Importing: file %s, format %s." % (fn, fmt)
self.cl_import(imp[0], imp[1])
def __open_action(self):
@ -483,9 +499,9 @@ class ArgHandler(object):
if len(pdata.id) <= 25:
print " %s%s- %s" % ( pdata.id,
" " * (26 - len(pdata.id)),
pdata.name)
pdata.name.encode(sys.getfilesystemencoding()))
else:
print " %s\t- %s" % (pdata.id, pdata.name)
print " %s\t- %s" % (pdata.id, pdata.name.encode(sys.getfilesystemencoding()))
elif action == "tool":
from gui.plug import tool
@ -521,9 +537,9 @@ class ArgHandler(object):
if len(pdata.id) <= 25:
print " %s%s- %s" % ( pdata.id,
" " * (26 - len(pdata.id)),
pdata.name)
pdata.name.encode(sys.getfilesystemencoding()))
else:
print " %s\t- %s" % (pdata.id, pdata.name)
print " %s\t- %s" % (pdata.id, pdata.name.encode(sys.getfilesystemencoding()))
else:
print "Unknown action: %s." % action
sys.exit(0)

View File

@ -1,3 +1,4 @@
# -*- coding: utf-8 -*-
#
# Gramps - a GTK+/GNOME based genealogy program
#
@ -46,14 +47,16 @@ import logging
import const
import config
from gen.utils.configmanager import safe_eval
import Utils
# Note: Make sure to edit const.py POPT_TABLE too!
# Note: Make sure to edit const.py.in POPT_TABLE too!
_HELP = _("""
Usage: gramps.py [OPTION...]
--load-modules=MODULE1,MODULE2,... Dynamic modules to load
Help options
-?, --help Show this help message
--usage Display brief usage message
Application options
-O, --open=FAMILY_TREE Open family tree
@ -70,6 +73,51 @@ Application options
-v, --version Show versions and settings
""")
_USAGE = _("""
Example of usage of Gramps command line interface
1. To import four databases (whose formats can be determined from their names)
and then check the resulting database for errors, one may type:
gramps -i file1.ged -i file2.gpkg -i ~/db3.gramps -i file4.wft -a tool -p name=check.
2. To explicitly specify the formats in the above example, append filenames with appropriate -f options:
gramps -i file1.ged -f gedcom -i file2.gpkg -f gramps-pkg -i ~/db3.gramps -f gramps-xml -i file4.wft -f wft -a tool -p name=check.
3. To record the database resulting from all imports, supply -e flag
(use -f if the filename does not allow Gramps to guess the format):
gramps -i file1.ged -i file2.gpkg -e ~/new-package -f gramps-pkg
4. To save any error messages of the above example into files outfile and errfile, run:
gramps -i file1.ged -i file2.dpkg -e ~/new-package -f gramps-pkg >outfile 2>errfile
5. To import three databases and start interactive Gramps session with the result:
gramps -i file1.ged -i file2.gpkg -i ~/db3.gramps
6. To open a database and, based on that data, generate timeline report in PDF format
putting the output into the my_timeline.pdf file:
gramps -O 'Family Tree 1' -a report -p name=timeline,off=pdf,of=my_timeline.pdf
7. To generate a summary of a database:
gramps -O 'Family Tree 1' -a report -p name=summary
8. Listing report options
Use the name=timeline,show=all to find out about all available options for the timeline report.
To find out details of a particular option, use show=option_name , e.g. name=timeline,show=off string.
To learn about available report names, use name=show string.
9. To convert a family tree on the fly to a .gramps xml file:
gramps -O 'Family Tree 1' -e output.gramps -f gramps-xml
10. To generate a web site into an other locale (in german):
LANGUAGE=de_DE; LANG=de_DE.UTF-8 gramps -O 'Family Tree 1' -a report -p name=navwebpage,target=/../de
11. Finally, to start normal interactive session type:
gramps
Note: These examples are for bash shell.
Syntax may be different for other shells and for Windows.
""")
#-------------------------------------------------------------------------
# ArgParser
#-------------------------------------------------------------------------
@ -87,8 +135,7 @@ class ArgParser(object):
4/ -e, --export=FILE, export a family tree in required format, optionally
provide -f to indicate format
5/ -f, --format=FORMAT : format after a -i or -e option
6/ -a, --action: An action (possible: 'check', 'summary', 'report',
'tool')
6/ -a, --action: An action (possible: 'report', 'tool')
7/ -p, --options=OPTIONS_STRING : specify options
8/ -u, --force-unlock: A locked database can be unlocked by giving this
argument when opening it
@ -124,6 +171,7 @@ class ArgParser(object):
self.list = False
self.list_more = False
self.help = False
self.usage = False
self.force_unlock = False
self.errors = []
@ -146,22 +194,37 @@ class ArgParser(object):
4/ -e, --export: export a family tree in required format, optionally provide
-f to indicate format
5/ -f, --format=FORMAT : format after a -i or -e option
6/ -a, --action: An action (possible: 'check', 'summary', 'report',
'tool')
6/ -a, --action: An action (possible: 'report', 'tool')
7/ -p, --options=OPTIONS_STRING : specify options
8/ -u, --force-unlock: A locked database can be unlocked by giving this
argument when opening it
"""
try:
# Convert arguments to unicode, otherwise getopt will not work
# if a non latin character is used as an option (by mistake).
# getopt will try to treat the first char in an utf-8 sequence. Example:
# -Ärik is '-\xc3\x84rik' and getopt will respond :
# option -\xc3 not recognized
for arg in range(len(self.args) - 1):
self.args[arg+1] = Utils.get_unicode_path_from_env_var(self.args[arg + 1])
options, leftargs = getopt.getopt(self.args[1:],
const.SHORTOPTS, const.LONGOPTS)
except getopt.GetoptError, msg:
# Extract the arguments in the list.
# The % operator replaces the list elements with repr() of the list elemements
# which is OK for latin characters, but not for non latin characters in list elements
cliargs = "[ "
for arg in range(len(self.args) - 1):
cliargs += self.args[arg + 1] + " "
cliargs += "]"
# Must first do str() of the msg object.
msg = unicode(str(msg))
self.errors += [(_('Error parsing the arguments'),
str(msg) + '\n' +
msg + '\n' +
_("Error parsing the arguments: %s \n"
"Type gramps --help for an overview of commands, or "
"read the manual pages.") % self.args[1:])]
"read the manual pages.") % cliargs)]
return
if leftargs:
@ -198,7 +261,7 @@ class ArgParser(object):
self.exports.append((value, family_tree_format))
elif option in ( '-a', '--action' ):
action = value
if action not in ( 'check', 'summary', 'report', 'tool' ):
if action not in ( 'report', 'tool' ):
print "Unknown action: %s. Ignoring." % action
continue
options_str = ""
@ -240,7 +303,8 @@ class ArgParser(object):
print "Gramps: no such config setting: '%s'" % setting_name
need_to_quit = True
else:
print "Gramps config settings from %s:" % config.config.filename
print "Gramps config settings from %s:" % \
config.config.filename.encode(sys.getfilesystemencoding())
for section in config.config.data:
for setting in config.config.data[section]:
print "%s.%s=%s" % (
@ -248,10 +312,13 @@ class ArgParser(object):
repr(config.config.data[section][setting]))
print
cleandbg += [opt_ix]
sys.exit(0)
elif option in ('-h', '-?', '--help'):
self.help = True
elif option in ('-u', '--force-unlock'):
self.force_unlock = True
elif option in ('--usage'):
self.usage = True
#clean options list
cleandbg.reverse()
@ -260,10 +327,17 @@ class ArgParser(object):
if len(options) > 0 and self.open is None and self.imports == [] \
and not (self.list or self.list_more or self.help):
# Extract and convert to unicode the arguments in the list.
# The % operator replaces the list elements with repr() of the list elemements
# which is OK for latin characters, but not for non latin characters in list elements
cliargs = "[ "
for arg in range(len(self.args) - 1):
cliargs += Utils.get_unicode_path_from_env_var(self.args[arg + 1]) + " "
cliargs += "]"
self.errors += [(_('Error parsing the arguments'),
_("Error parsing the arguments: %s \n"
"To use in the command-line mode," \
"supply at least one input file to process.") % self.args[1:])]
"supply at least one input file to process.") % cliargs)]
if need_to_quit:
sys.exit(0)
@ -302,6 +376,15 @@ class ArgParser(object):
If the user gives the --help or -h option, print the output to terminal.
"""
if self.help:
print _HELP
# Convert Help messages to file system encoding before printing
print _HELP.encode(sys.getfilesystemencoding())
sys.exit(0)
def print_usage(self):
"""
If the user gives the --usage print the output to terminal.
"""
if self.usage:
# Convert Help messages to file system encoding before printing
print _USAGE.encode(sys.getfilesystemencoding())
sys.exit(0)

View File

@ -299,14 +299,24 @@ def startcli(errors, argparser):
"""
if errors:
#already errors encountered. Show first one on terminal and exit
print _('Error encountered: %s') % errors[0][0]
print _(' Details: %s') % errors[0][1]
# Convert error message to file system encoding before print
errmsg = _('Error encountered: %s') % errors[0][0]
errmsg = errmsg.encode(sys.getfilesystemencoding())
print errmsg
errmsg = _(' Details: %s') % errors[0][1]
errmsg = errmsg.encode(sys.getfilesystemencoding())
print errmsg
sys.exit(1)
if argparser.errors:
print _('Error encountered in argument parsing: %s') \
# Convert error message to file system encoding before print
errmsg = _('Error encountered in argument parsing: %s') \
% argparser.errors[0][0]
print _(' Details: %s') % argparser.errors[0][1]
errmsg = errmsg.encode(sys.getfilesystemencoding())
print errmsg
errmsg = _(' Details: %s') % argparser.errors[0][1]
errmsg = errmsg.encode(sys.getfilesystemencoding())
print errmsg
sys.exit(1)
#we need to keep track of the db state

View File

@ -374,9 +374,11 @@ class CommandLineReport(object):
opt = self.options_help[key]
# Make the output nicer to read, assume that tab has 8 spaces
tabs = '\t' if len(key) < 10 else '\t'*2
print " %s%s%s (%s)" % (key, tabs, opt[1], opt[0])
optmsg = " %s%s%s (%s)" % (key, tabs, opt[1], opt[0])
print optmsg.encode(sys.getfilesystemencoding())
else:
print " %s" % key
optmsg = " %s" % key
print optmsg.encode(sys.getfilesystemencoding())
print " Use 'show=option' to see description and acceptable values"
elif self.show in self.options_help:
opt = self.options_help[self.show]
@ -386,9 +388,11 @@ class CommandLineReport(object):
vals = opt[2]
if isinstance(vals, (list, tuple)):
for val in vals:
print " %s" % val
optmsg = " %s" % key
print optmsg.encode(sys.getfilesystemencoding())
else:
print " %s" % opt[2]
optmsg = " %s" % opt[2]
print optmsg.encode(sys.getfilesystemencoding())
else:
#there was a show option given, but the option is invalid

View File

@ -229,7 +229,7 @@ NO_GIVEN = "(%s)" % _("none")
#
#-------------------------------------------------------------------------
# Note: Make sure to edit ArgHandler.py _help string too!
# Note: Make sure to edit argparser.py _help string too!
# (longName, shortName, type , default, flags, descrip , argDescrip)
POPT_TABLE = [
("config", 'c', str, None, 0, "Set config setting", ""),
@ -243,7 +243,7 @@ POPT_TABLE = [
("", 'l', None, None, 0, 'List Family Trees', ""),
("", 'L', None, None, 0, 'List Family Tree Details', ""),
("force-unlock", 'u', None, None, 0, 'Force unlock of family tree', ""),
("settings", 's', None, None, 0, 'Show settings and versions', ""),
("version", 'v', None, None, 0, 'Show versions and settings', ""),
]
LONGOPTS = [
@ -280,6 +280,7 @@ LONGOPTS = [
"sm-config-prefix=",
"sm-disable",
"sync",
"usage",
"version",
]

View File

@ -275,6 +275,7 @@ def run():
else:
#CLI use of GRAMPS
argpars.print_help()
argpars.print_usage()
from cli.grampscli import startcli
startcli(error, argpars)

View File

@ -31,6 +31,7 @@
#-------------------------------------------------------------------------
from __future__ import with_statement
import os
import sys
import cStringIO
from gen.ggettext import gettext as _
@ -552,8 +553,10 @@ class CheckIntegrity(object):
photo_name = Utils.media_path_full(self.db, obj.get_path())
if photo_name is not None and photo_name != "" and not Utils.find_file(photo_name):
if cl:
# Convert to file system encoding before prining
fn = os.path.basename(photo_name).encode(sys.getfilesystemencoding())
print "Warning: media file %s was not found." \
% os.path.basename(photo_name)
% fn
self.bad_photo.append(ObjectId)
else:
if missmedia_action == 0:
@ -1476,7 +1479,8 @@ class Report(ManagedWindow.ManagedWindow):
def __init__(self, uistate, text, cl=0):
if cl:
print text
# Convert to file system encoding before prining
print text.encode(sys.getfilesystemencoding())
return
ManagedWindow.ManagedWindow.__init__(self, uistate, [], self)