Merged upstream
authorDmitriy Morozov <dmitriy@mrzv.org>
Fri, 01 Oct 2010 09:45:26 -0700
changeset 122 76334dcc9ae4
parent 117 e17bf1d5c289 (current diff)
parent 121 836bb50a8660 (diff)
child 132 b072cb473b47
Merged upstream
opster.py
--- a/.hgtags	Tue Sep 14 14:35:54 2010 -0700
+++ b/.hgtags	Fri Oct 01 09:45:26 2010 -0700
@@ -9,3 +9,4 @@
 cc24c392fd45ee296ea1535b8f49aa3a42df3b05 0.9.8
 472a33d66076d746d6c2dedd80fc7b40ac18e5f9 0.9.9
 95653a562a05f97e1a969d916d805bc6cf8a8b9b 0.9.10
+bf6908d12aae4a54ac584df5a6f97f84157030d6 0.9.11
--- a/docs/changelog.rst	Tue Sep 14 14:35:54 2010 -0700
+++ b/docs/changelog.rst	Fri Oct 01 09:45:26 2010 -0700
@@ -1,6 +1,12 @@
 Changelog
 ---------
 
+0.9.11
+~~~~~~
+
+ - fixed exceptions handling
+ - autocompletion improvements (skips middleware, ability of options completion)
+
 0.9.10
 ~~~~~~
 
--- a/opster.py	Tue Sep 14 14:35:54 2010 -0700
+++ b/opster.py	Fri Oct 01 09:45:26 2010 -0700
@@ -6,7 +6,7 @@
 from itertools import imap
 
 __all__ = ['command', 'dispatch']
-__version__ = '0.9.10'
+__version__ = '0.9.11'
 __author__ = 'Alexander Solovyov'
 __email__ = 'piranha@piranha.org.ua'
 
@@ -74,18 +74,12 @@
                 # no catcher here because this is call from Python
                 return call_cmd_regular(func, options_)(*args, **opts)
 
-            try:
-                opts, args = catcher(lambda: parse(argv, options_), help_func)
-            except Abort:
-                return -1
+            opts, args = catcher(lambda: parse(argv, options_), help_func)
 
-            try:
-                if opts.pop('help', False):
-                    return help_func()
-                return catcher(lambda: call_cmd(name_, func)(*args, **opts),
-                               help_func)
-            except Abort:
-                return -1
+            if opts.pop('help', False):
+                return help_func()
+            return catcher(lambda: call_cmd(name_, func)(*args, **opts),
+                           help_func)
 
         return inner
     return wrapper
@@ -125,20 +119,17 @@
 
     autocomplete(cmdtable, args, middleware)
 
-    try:
-        name, func, args, kwargs = catcher(
-            lambda: _dispatch(args, cmdtable, globaloptions),
+    name, func, args, kwargs = catcher(
+        lambda: _dispatch(args, cmdtable, globaloptions),
+        help_func)
+    if name == '_completion':       # skip middleware
+        return catcher(
+            lambda: call_cmd(name, func)(*args, **kwargs),
             help_func)
-        if name == '_completion':       # skip middleware
-            return catcher(
-                lambda: call_cmd(name, func)(*args, **kwargs),
-                help_func)
-        else:
-            return catcher(
-                lambda: call_cmd(name, middleware(func))(*args, **kwargs),
-                help_func)
-    except Abort:
-        return -1
+    else:
+        return catcher(
+            lambda: call_cmd(name, middleware(func))(*args, **kwargs),
+            help_func)
 
 # --------
 # Help
@@ -160,9 +151,9 @@
 
             for cmd, info in cmdtable.items():
                 if cmd.startswith('~'):
-                    continue # do not display hidden commands
+                    continue  # do not display hidden commands
                 if shortlist and not cmd.startswith('^'):
-                    continue # short help contains only marked commands
+                    continue  # short help contains only marked commands
                 cmd = cmd.lstrip('^~')
                 doc = info[0].__doc__ or '(no help text available)'
                 hlp[cmd] = doc.splitlines()[0].rstrip()
@@ -174,7 +165,7 @@
             write('\ncommands:\n\n')
             for cmd in hlplist:
                 doc = hlp[cmd]
-                if False: # verbose?
+                if False:  # verbose?
                     write(' %s:\n     %s\n' % (cmd.replace('|', ', '), doc))
                 else:
                     write(' %-*s  %s\n' % (maxlen, cmd.split('|', 1)[0],
@@ -280,7 +271,8 @@
     shortlist, namelist, funlist = '', [], []
 
     for o in options:
-        short, name, default, comment = o[:4]       # might have the fifth completer element
+        # might have the fifth completer element
+        short, name, default, comment = o[:4]
         if short and len(short) != 1:
             raise FOError('Short option should be only a single'
                           ' character: %s' % short)
@@ -332,7 +324,6 @@
     return state, args
 
 
-
 # --------
 # Subcommand system
 # --------
@@ -460,15 +451,6 @@
         help_func()
     except FOError, e:
         err('%s\n' % e)
-    except KeyboardInterrupt:
-        err('interrupted!\n')
-    except SystemExit:
-        raise
-    except:
-        err('unknown exception encountered')
-        raise
-
-    raise Abort
 
 def call_cmd(name, func):
     def inner(*args, **kwargs):
@@ -531,13 +513,13 @@
     """
 
     # Don't complete if user hasn't sourced bash_completion file.
-    if not os.environ.has_key('OPSTER_AUTO_COMPLETE'):
+    if 'OPSTER_AUTO_COMPLETE' not in os.environ:
         return
     cwords = os.environ['COMP_WORDS'].split()[1:]
     cword = int(os.environ['COMP_CWORD'])
 
     try:
-        current = cwords[cword-1]
+        current = cwords[cword - 1]
     except IndexError:
         current = ''
 
@@ -563,7 +545,7 @@
             short, long, default, help = o[:4]
             completer = o[4] if len(o) > 4 else None
             short, long = '-%s' % short, '--%s' % long
-            options += [short,long]
+            options += [short, long]
 
             if cwords[idx] in (short, long) and completer:
                 args = middleware(completer)(current)
@@ -627,8 +609,5 @@
 class ParseError(CommandException):
     'Raised on error in command line parsing'
 
-class Abort(CommandException):
-    'Abort execution'
-
 class FOError(CommandException):
     'Raised on trouble with opster configuration'