From 74a27056b346e04dbad91fd632045223b16ef6ec Mon Sep 17 00:00:00 2001
From: Dean Troyer <dtroyer@gmail.com>
Date: Tue, 3 Dec 2013 17:24:40 -0600
Subject: [PATCH] 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
---
 openstackclient/common/commandmanager.py      | 18 ++++++++++++++++--
 .../tests/common/test_commandmanager.py       | 19 ++++++++++++++++++-
 2 files changed, 34 insertions(+), 3 deletions(-)

diff --git a/openstackclient/common/commandmanager.py b/openstackclient/common/commandmanager.py
index 06073d93cf..553bc920ab 100644
--- a/openstackclient/common/commandmanager.py
+++ b/openstackclient/common/commandmanager.py
@@ -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
diff --git a/openstackclient/tests/common/test_commandmanager.py b/openstackclient/tests/common/test_commandmanager.py
index 4953c29754..088ea21ea8 100644
--- a/openstackclient/tests/common/test_commandmanager.py
+++ b/openstackclient/tests/common/test_commandmanager.py
@@ -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)