Skip to content

Commit 74a2705

Browse files
author
Dean Troyer
committed
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
1 parent 5dcc3b6 commit 74a2705

2 files changed

Lines changed: 34 additions & 3 deletions

File tree

‎openstackclient/common/commandmanager.py‎

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Copyright 2012-2013 OpenStack, LLC.
1+
# Copyright 2012-2013 OpenStack Foundation
22
#
33
# Licensed under the Apache License, Version 2.0 (the "License"); you may
44
# not use this file except in compliance with the License. You may obtain
@@ -28,15 +28,29 @@ class CommandManager(cliff.commandmanager.CommandManager):
2828
"""Alters Cliff's default CommandManager behaviour to load additional
2929
command groups after initialization.
3030
"""
31+
def __init__(self, namespace, convert_underscores=True):
32+
self.group_list = []
33+
super(CommandManager, self).__init__(namespace, convert_underscores)
34+
3135
def _load_commands(self, group=None):
3236
if not group:
3337
group = self.namespace
38+
self.group_list.append(group)
3439
for ep in pkg_resources.iter_entry_points(group):
3540
LOG.debug('found command %r' % ep.name)
36-
self.commands[ep.name.replace('_', ' ')] = ep
41+
cmd_name = (
42+
ep.name.replace('_', ' ')
43+
if self.convert_underscores
44+
else ep.name
45+
)
46+
self.commands[cmd_name] = ep
3747
return
3848

3949
def add_command_group(self, group=None):
4050
"""Adds another group of command entrypoints"""
4151
if group:
4252
self._load_commands(group)
53+
54+
def get_command_groups(self):
55+
"""Returns a list of the loaded command groups"""
56+
return self.group_list

‎openstackclient/tests/common/test_commandmanager.py‎

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Copyright 2012-2013 OpenStack, LLC.
1+
# Copyright 2012-2013 OpenStack Foundation
22
#
33
# Licensed under the Apache License, Version 2.0 (the "License"); you may
44
# not use this file except in compliance with the License. You may obtain
@@ -40,9 +40,11 @@ def _load_commands(self, group=None):
4040
if not group:
4141
self.commands['one'] = FAKE_CMD_ONE
4242
self.commands['two'] = FAKE_CMD_TWO
43+
self.group_list.append(self.namespace)
4344
else:
4445
self.commands['alpha'] = FAKE_CMD_ALPHA
4546
self.commands['beta'] = FAKE_CMD_BETA
47+
self.group_list.append(group)
4648

4749

4850
class TestCommandManager(utils.TestCase):
@@ -69,3 +71,18 @@ def test_add_command_group(self):
6971
# Ensure that the original commands were not overwritten
7072
cmd_two, name, args = mgr.find_command(['two'])
7173
self.assertEqual(cmd_two, FAKE_CMD_TWO)
74+
75+
def test_get_command_groups(self):
76+
mgr = FakeCommandManager('test')
77+
78+
# Make sure add_command() still functions
79+
mock_cmd_one = mock.Mock()
80+
mgr.add_command('mock', mock_cmd_one)
81+
cmd_mock, name, args = mgr.find_command(['mock'])
82+
self.assertEqual(cmd_mock, mock_cmd_one)
83+
84+
# Load another command group
85+
mgr.add_command_group('latin')
86+
87+
gl = mgr.get_command_groups()
88+
self.assertEqual(['test', 'latin'], gl)

0 commit comments

Comments
 (0)