Update OSC's CommandManager subclass

cliff.commandmanager.CommandManager gained an option, update
openstackclient.common.commandmanager.ComamndManager to match.

Also add CommandManager.get_command_groups() to return a list of the
currently loaded command groups.  I expect this to be useful in
upcoming client diagnostic commands for plugins/extensions.

If these turn out to be generally useful we'll propose them to
upstream cliff.

Change-Id: Ic15a7ca0ef975ca679e753be861be7c628b8e10c
This commit is contained in:
Dean Troyer 2013-12-03 17:24:40 -06:00
parent 5dcc3b6164
commit 74a27056b3
2 changed files with 34 additions and 3 deletions

View File

@ -1,4 +1,4 @@
# Copyright 2012-2013 OpenStack, LLC.
# Copyright 2012-2013 OpenStack Foundation
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
@ -28,15 +28,29 @@ class CommandManager(cliff.commandmanager.CommandManager):
"""Alters Cliff's default CommandManager behaviour to load additional
command groups after initialization.
"""
def __init__(self, namespace, convert_underscores=True):
self.group_list = []
super(CommandManager, self).__init__(namespace, convert_underscores)
def _load_commands(self, group=None):
if not group:
group = self.namespace
self.group_list.append(group)
for ep in pkg_resources.iter_entry_points(group):
LOG.debug('found command %r' % ep.name)
self.commands[ep.name.replace('_', ' ')] = ep
cmd_name = (
ep.name.replace('_', ' ')
if self.convert_underscores
else ep.name
)
self.commands[cmd_name] = ep
return
def add_command_group(self, group=None):
"""Adds another group of command entrypoints"""
if group:
self._load_commands(group)
def get_command_groups(self):
"""Returns a list of the loaded command groups"""
return self.group_list

View File

@ -1,4 +1,4 @@
# Copyright 2012-2013 OpenStack, LLC.
# Copyright 2012-2013 OpenStack Foundation
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
@ -40,9 +40,11 @@ class FakeCommandManager(commandmanager.CommandManager):
if not group:
self.commands['one'] = FAKE_CMD_ONE
self.commands['two'] = FAKE_CMD_TWO
self.group_list.append(self.namespace)
else:
self.commands['alpha'] = FAKE_CMD_ALPHA
self.commands['beta'] = FAKE_CMD_BETA
self.group_list.append(group)
class TestCommandManager(utils.TestCase):
@ -69,3 +71,18 @@ class TestCommandManager(utils.TestCase):
# Ensure that the original commands were not overwritten
cmd_two, name, args = mgr.find_command(['two'])
self.assertEqual(cmd_two, FAKE_CMD_TWO)
def test_get_command_groups(self):
mgr = FakeCommandManager('test')
# Make sure add_command() still functions
mock_cmd_one = mock.Mock()
mgr.add_command('mock', mock_cmd_one)
cmd_mock, name, args = mgr.find_command(['mock'])
self.assertEqual(cmd_mock, mock_cmd_one)
# Load another command group
mgr.add_command_group('latin')
gl = mgr.get_command_groups()
self.assertEqual(['test', 'latin'], gl)