use entry points for completion plugins

This commit is contained in:
Doug Hellmann 2013-11-12 18:14:37 -05:00
parent 9b77b62a68
commit 1192cf679b
3 changed files with 23 additions and 10 deletions

View File

@ -5,6 +5,8 @@
import logging
import six
import stevedore
from cliff import command
@ -134,6 +136,12 @@ class CompleteCommand(command.Command):
log = logging.getLogger(__name__ + '.CompleteCommand')
def __init__(self, app, app_args):
super(CompleteCommand, self).__init__(app, app_args)
self._formatters = stevedore.ExtensionManager(
namespace='cliff.formatter.completion',
)
def get_parser(self, prog_name):
parser = super(CompleteCommand, self).get_parser(prog_name)
parser.add_argument(
@ -146,7 +154,7 @@ class CompleteCommand(command.Command):
"--shell",
default='bash',
metavar='<shell>',
choices=['bash', 'none'],
choices=sorted(self._formatters.names()),
help="Shell being used. Use none for data only (default: bash)"
)
return parser
@ -165,14 +173,12 @@ class CompleteCommand(command.Command):
def take_action(self, parsed_args):
self.log.debug('take_action(%s)' % parsed_args)
if parsed_args.name:
name = parsed_args.name
else:
name = self.app.NAME
if parsed_args.shell == "none":
shell = CompleteNoCode(name, self.app.stdout)
else:
shell = CompleteBash(name, self.app.stdout)
name = parsed_args.name or self.app.NAME
try:
shell_factory = self._formatters[parsed_args.shell].plugin
except KeyError:
raise RuntimeError('Unknown shell syntax %r' % parsed_args.shell)
shell = shell_factory(name, self.app.stdout)
dicto = CompleteDictionary()
for cmd in self.app.command_manager:

View File

@ -120,6 +120,7 @@ def test_complete_command_take_action():
sot, app, cmd_mgr = given_complete_command()
parsed_args = mock.Mock()
parsed_args.name = "test_take"
parsed_args.shell = "bash"
content = app.stdout.content
assert 0 == sot.take_action(parsed_args)
assert "_test_take()\n" in content[0]

View File

@ -17,8 +17,10 @@ except IOError:
install_requires = [
'PrettyTable>=0.6,<0.8',
'pyparsing>=2.0.1',
'cmd2>=0.6.7',
'pyparsing>=2.0.1',
'six',
'stevedore',
]
try:
@ -173,6 +175,10 @@ setup(
'table = cliff.formatters.table:TableFormatter',
'shell = cliff.formatters.shell:ShellFormatter',
],
'cliff.formatter.completion': [
'bash = cliff.complete:CompleteBash',
'none = cliff.complete:CompleteNoCode',
],
},
zip_safe=False,