Rev 2445: HelpCommandContext now implementes get_topics. in file:///home/robertc/source/baz/help-contexts/

Robert Collins robertc at robertcollins.net
Fri Apr 20 03:20:07 BST 2007


At file:///home/robertc/source/baz/help-contexts/

------------------------------------------------------------
revno: 2445
revision-id: robertc at robertcollins.net-20070420022005-a291lv2j0c18urai
parent: robertc at robertcollins.net-20070420015710-apsxzfznrnpg6x17
committer: Robert Collins <robertc at robertcollins.net>
branch nick: help-contexts
timestamp: Fri 2007-04-20 12:20:05 +1000
message:
  HelpCommandContext now implementes get_topics.
modified:
  bzrlib/commands.py             bzr.py-20050309040720-d10f4714595cf8c3
  bzrlib/tests/test_help.py      test_help.py-20070419045354-6q6rq15j9e2n5fna-1
=== modified file 'bzrlib/commands.py'
--- a/bzrlib/commands.py	2007-04-20 01:57:10 +0000
+++ b/bzrlib/commands.py	2007-04-20 02:20:05 +0000
@@ -137,6 +137,14 @@
     plugins_override
         If true, plugin commands can override builtins.
     """
+    try:
+        return _get_cmd_object(cmd_name, plugins_override)
+    except KeyError:
+        raise errors.BzrCommandError('unknown command "%s"' % cmd_name)
+
+
+def _get_cmd_object(cmd_name, plugins_override=True):
+    """Worker for get_cmd_object which raises KeyError rather than BzrCommandError."""
     from bzrlib.externalcommand import ExternalCommand
 
     # We want only 'ascii' command names, but the user may have typed
@@ -159,8 +167,7 @@
     cmd_obj = ExternalCommand.find_command(cmd_name)
     if cmd_obj:
         return cmd_obj
-
-    raise errors.BzrCommandError('unknown command "%s"' % cmd_name)
+    raise KeyError
 
 
 class Command(object):
@@ -712,6 +719,20 @@
 class HelpCommandContext(object):
     """A context for bzr help that returns commands."""
 
+    def get_topics(self, topic):
+        """Search for topic amongst commands.
+
+        :param topic: A topic to search for.
+        :return: A list which is either empty or contains a single
+            Command entry.
+        """
+        try:
+            cmd = _get_cmd_object(topic)
+        except KeyError:
+            return []
+        else:
+            return [cmd]
+
 
 if __name__ == '__main__':
     sys.exit(main(sys.argv))

=== modified file 'bzrlib/tests/test_help.py'
--- a/bzrlib/tests/test_help.py	2007-04-20 01:57:10 +0000
+++ b/bzrlib/tests/test_help.py	2007-04-20 02:20:05 +0000
@@ -19,6 +19,7 @@
 from cStringIO import StringIO
 
 from bzrlib import (
+    builtins,
     commands,
     errors,
     help,
@@ -103,6 +104,23 @@
     def test_default_constructable(self):
         context = commands.HelpCommandContext()
 
+    def test_get_topics_None(self):
+        """Searching for None returns an empty list."""
+        context = commands.HelpCommandContext()
+        self.assertEqual([], context.get_topics(None))
+
+    def test_get_topics_rocks(self):
+        """Searching for 'rocks' returns the cmd_rocks command instance."""
+        context = commands.HelpCommandContext()
+        topics = context.get_topics('rocks')
+        self.assertEqual(1, len(topics))
+        self.assertIsInstance(topics[0], builtins.cmd_rocks)
+
+    def test_get_topics_no_topic(self):
+        """Searching for something that is not a command returns []."""
+        context = commands.HelpCommandContext()
+        self.assertEqual([], context.get_topics('nothing by this name'))
+
 
 class TestHelpContexts(tests.TestCase):
     """Tests for the HelpContexts class."""



More information about the bazaar-commits mailing list