aboutsummaryrefslogtreecommitdiffstats
path: root/libbe/ui
diff options
context:
space:
mode:
Diffstat (limited to 'libbe/ui')
-rw-r--r--libbe/ui/__init__.py3
-rw-r--r--libbe/ui/command_line.py75
-rw-r--r--libbe/ui/util/__init__.py3
-rw-r--r--libbe/ui/util/editor.py3
-rw-r--r--libbe/ui/util/pager.py3
-rw-r--r--libbe/ui/util/user.py39
6 files changed, 92 insertions, 34 deletions
diff --git a/libbe/ui/__init__.py b/libbe/ui/__init__.py
index 227269c..45068b4 100644
--- a/libbe/ui/__init__.py
+++ b/libbe/ui/__init__.py
@@ -1,4 +1,5 @@
-# Copyright (C) 2009-2011 W. Trevor King <wking@drexel.edu>
+# Copyright (C) 2009-2011 Chris Ball <cjb@laptop.org>
+# W. Trevor King <wking@drexel.edu>
#
# This file is part of Bugs Everywhere.
#
diff --git a/libbe/ui/command_line.py b/libbe/ui/command_line.py
index 5f42147..d5719a6 100644
--- a/libbe/ui/command_line.py
+++ b/libbe/ui/command_line.py
@@ -1,4 +1,5 @@
-# Copyright (C) 2009-2011 W. Trevor King <wking@drexel.edu>
+# Copyright (C) 2009-2011 Chris Ball <cjb@laptop.org>
+# W. Trevor King <wking@drexel.edu>
#
# This file is part of Bugs Everywhere.
#
@@ -29,6 +30,8 @@ import libbe.command
import libbe.command.util
import libbe.version
import libbe.ui.util.pager
+import libbe.util.encoding
+
if libbe.TESTING == True:
import doctest
@@ -86,11 +89,11 @@ class CmdOptionParser(optparse.OptionParser):
if '_' in name: # reconstruct original option name
options[name.replace('_', '-')] = options.pop(name)
for name,value in options.items():
+ argument = None
+ option = self._option_by_name[name]
+ if option.arg != None:
+ argument = option.arg
if value == '--complete':
- argument = None
- option = self._option_by_name[name]
- if option.arg != None:
- argument = option.arg
fragment = None
indices = [i for i,arg in enumerate(args)
if arg == '--complete']
@@ -108,29 +111,36 @@ class CmdOptionParser(optparse.OptionParser):
if i+1 < len(args):
fragment = args[i+1]
self.complete(argument, fragment)
+ elif argument is not None:
+ value = self.process_raw_argument(argument=argument, value=value)
+ options[name] = value
for i,arg in enumerate(parsed_args):
+ if i > 0 and self.command.name == 'be':
+ break # let this pass through for the command parser to handle
+ elif i < len(self.command.args):
+ argument = self.command.args[i]
+ elif len(self.command.args) == 0:
+ break # command doesn't take arguments
+ else:
+ argument = self.command.args[-1]
+ if argument.repeatable == False:
+ raise libbe.command.UserError('Too many arguments')
if arg == '--complete':
- if i > 0 and self.command.name == 'be':
- break # let this pass through for the command parser to handle
- elif i < len(self.command.args):
- argument = self.command.args[i]
- elif len(self.command.args) == 0:
- break # command doesn't take arguments
- else:
- argument = self.command.args[-1]
- if argument.repeatable == False:
- raise libbe.command.UserError('Too many arguments')
fragment = None
if i < len(parsed_args) - 1:
fragment = parsed_args[i+1]
self.complete(argument, fragment)
+ else:
+ value = self.process_raw_argument(argument=argument, value=arg)
+ parsed_args[i] = value
if len(parsed_args) > len(self.command.args) \
and self.command.args[-1].repeatable == False:
raise libbe.command.UserError('Too many arguments')
for arg in self.command.args[len(parsed_args):]:
if arg.optional == False:
- raise libbe.command.UserError(
- 'Missing required argument %s' % arg.metavar)
+ raise libbe.command.UsageError(
+ command=self.command,
+ message='Missing required argument %s' % arg.metavar)
return (options, parsed_args)
def callback(self, option, opt, value, parser):
@@ -154,6 +164,16 @@ class CmdOptionParser(optparse.OptionParser):
print >> self.command.stdout, '\n'.join(comps)
raise CallbackExit
+ def process_raw_argument(self, argument, value):
+ if value == argument.default:
+ return value
+ if argument.type == 'string':
+ if not hasattr(self, 'argv_encoding'):
+ self.argv_encoding = libbe.util.encoding.get_argv_encoding()
+ return unicode(value, self.argv_encoding)
+ return value
+
+
class BE (libbe.command.Command):
"""Class for parsing the command line arguments for `be`.
This class does not contain a useful _run() method. Call this
@@ -271,6 +291,13 @@ def dispatch(ui, command, args):
'See http://docs.python.org/library/locale.html for details',
])
return 1
+ except libbe.command.UsageError, e:
+ print >> ui.io.stdout, 'Usage Error:\n', e
+ if e.command:
+ print >> ui.io.stdout, e.command.usage()
+ print >> ui.io.stdout, 'For usage information, try'
+ print >> ui.io.stdout, ' be help %s' % e.command_name
+ return 1
except libbe.command.UserError, e:
print >> ui.io.stdout, 'ERROR:\n', e
return 1
@@ -296,15 +323,19 @@ def main():
options,args = parser.parse_args()
except CallbackExit:
return 0
- except libbe.command.UserError, e:
- if str(e).endswith('COMMAND'):
+ except libbe.command.UsageError, e:
+ if isinstance(e.command, BE):
# no command given, print usage string
- print >> ui.io.stdout, 'ERROR:'
- print >> ui.io.stdout, be.usage(), '\n', e
+ print >> ui.io.stdout, 'Usage Error:\n', e
+ print >> ui.io.stdout, be.usage()
print >> ui.io.stdout, 'For example, try'
print >> ui.io.stdout, ' be help'
else:
- print >> ui.io.stdout, 'ERROR:\n', e
+ print >> ui.io.stdout, 'Usage Error:\n', e
+ if e.command:
+ print >> ui.io.stdout, e.command.usage()
+ print >> ui.io.stdout, 'For usage information, try'
+ print >> ui.io.stdout, ' be help %s' % e.command_name
return 1
command_name = args.pop(0)
diff --git a/libbe/ui/util/__init__.py b/libbe/ui/util/__init__.py
index 227269c..45068b4 100644
--- a/libbe/ui/util/__init__.py
+++ b/libbe/ui/util/__init__.py
@@ -1,4 +1,5 @@
-# Copyright (C) 2009-2011 W. Trevor King <wking@drexel.edu>
+# Copyright (C) 2009-2011 Chris Ball <cjb@laptop.org>
+# W. Trevor King <wking@drexel.edu>
#
# This file is part of Bugs Everywhere.
#
diff --git a/libbe/ui/util/editor.py b/libbe/ui/util/editor.py
index dcf73c8..206e9c4 100644
--- a/libbe/ui/util/editor.py
+++ b/libbe/ui/util/editor.py
@@ -1,5 +1,6 @@
# Bugs Everywhere, a distributed bugtracker
-# Copyright (C) 2008-2011 Gianluca Montecchi <gian@grys.it>
+# Copyright (C) 2008-2011 Chris Ball <cjb@laptop.org>
+# Gianluca Montecchi <gian@grys.it>
# W. Trevor King <wking@drexel.edu>
#
# This file is part of Bugs Everywhere.
diff --git a/libbe/ui/util/pager.py b/libbe/ui/util/pager.py
index d82dcef..de3b3fc 100644
--- a/libbe/ui/util/pager.py
+++ b/libbe/ui/util/pager.py
@@ -1,4 +1,5 @@
-# Copyright (C) 2009-2011 W. Trevor King <wking@drexel.edu>
+# Copyright (C) 2009-2011 Chris Ball <cjb@laptop.org>
+# W. Trevor King <wking@drexel.edu>
#
# This file is part of Bugs Everywhere.
#
diff --git a/libbe/ui/util/user.py b/libbe/ui/util/user.py
index 35665e4..261ecdf 100644
--- a/libbe/ui/util/user.py
+++ b/libbe/ui/util/user.py
@@ -1,4 +1,5 @@
-# Copyright (C) 2009-2011 W. Trevor King <wking@drexel.edu>
+# Copyright (C) 2009-2011 Chris Ball <cjb@laptop.org>
+# W. Trevor King <wking@drexel.edu>
#
# This file is part of Bugs Everywhere.
#
@@ -28,32 +29,54 @@ are human-readable tags refering to objects.
try:
from email.utils import formataddr, parseaddr
-except ImportErrror: # adjust to old python < 2.5
+except ImportErrror: # adjust to old python < 2.5
from email.Utils import formataddr, parseaddr
import os
+try:
+ import pwd
+except ImportError: # handle non-Unix systems
+ pwd = None
import re
from socket import gethostname
import libbe
import libbe.storage.util.config
+
def get_fallback_username():
"""Return a username extracted from environmental variables.
"""
name = None
- for env in ["LOGNAME", "USERNAME"]:
+ for env in ['LOGNAME', 'USERNAME']:
+ if os.environ.has_key(env):
+ name = os.environ[env]
+ break
+ if name is None and pwd:
+ pw_ent = pwd.getpwuid(os.getuid())
+ name = pw_ent.pw_name
+ assert name is not None
+ return name
+
+def get_fallback_fullname():
+ """Return a full name extracted from environmental variables.
+ """
+ name = None
+ for env in ['FULLNAME']:
if os.environ.has_key(env):
name = os.environ[env]
break
- assert name != None
+ if pwd and not name:
+ pw_ent = pwd.getpwuid(os.getuid())
+ name = pw_ent.pw_gecos.split(',', 1)[0]
+ if not name:
+ name = get_fallback_username()
return name
def get_fallback_email():
"""Return an email address extracted from environmental variables.
"""
- hostname = gethostname()
- name = get_fallback_username()
- return "%s@%s" % (name, hostname)
+ return os.getenv('EMAIL') or '%s@%s' % (
+ get_fallback_username(), gethostname())
def create_user_id(name, email=None):
"""Create a user ID string from given `name` and `email` strings.
@@ -122,7 +145,7 @@ def get_user_id(storage=None):
user = storage.get_user_id()
if user != None:
return user
- name = get_fallback_username()
+ name = get_fallback_fullname()
email = get_fallback_email()
user = create_user_id(name, email)
return user