Rev 17: Hooks into the get_missing_command Command hook, and suggests a plugin in http://people.canonical.com/~robertc/baz2.0/plugins/plugin_info/trunk

Robert Collins robertc at robertcollins.net
Mon Mar 1 07:27:48 GMT 2010


At http://people.canonical.com/~robertc/baz2.0/plugins/plugin_info/trunk

------------------------------------------------------------
revno: 17
revision-id: robertc at robertcollins.net-20100301072438-pb66uzqg24zgr3ak
parent: robertc at robertcollins.net-20100301054756-6wtqaxty7tx8iwch
committer: Robert Collins <robertc at robertcollins.net>
branch nick: trunk
timestamp: Mon 2010-03-01 18:24:38 +1100
message:
  Hooks into the get_missing_command Command hook, and suggests a plugin
  when there is a match. (Robert Collins)
=== modified file 'NEWS'
--- a/NEWS	2010-03-01 05:47:56 +0000
+++ b/NEWS	2010-03-01 07:24:38 +0000
@@ -22,6 +22,9 @@
 
     * Cached copy of some common plugin metadata included. (Robert Collins)
 
+    * Hooks into the get_missing_command Command hook, and suggests a plugin
+      when there is a match. (Robert Collins)
+
     * Updated for bzr 1.18+'s new bzr_transport's attribute. (Robert Collins)
 
   BUGFIXES:

=== modified file '__init__.py'
--- a/__init__.py	2010-03-01 01:44:25 +0000
+++ b/__init__.py	2010-03-01 07:24:38 +0000
@@ -1,5 +1,5 @@
 # plugin_info, a plugin for working with information about plugins.
-# Copyright (C) 2008 Canonical Limited.
+# Copyright (C) 2008, 2010 Canonical Limited.
 # 
 # This program is free software; you can redistribute it and/or modify
 # it under the terms of the GNU General Public License version 2 as published
@@ -23,16 +23,26 @@
 :seealso: bzrlib.plugins.plugin_info.extract for metadata extraction routines.
 """
 
-from bzrlib.commands import plugin_cmds
-
-
-plugin_cmds.register_lazy('cmd_plugin_info', [], 
-                          'bzrlib.plugins.plugin_info.commands')
+from bzrlib.commands import Command, plugin_cmds
+
+
+plugin_cmds.register_lazy('cmd_plugin_info', [],
+    'bzrlib.plugins.plugin_info.commands')
 
 
 version_info = (1, 3, 0, 'dev', 0)
 
 
+def get_missing_command(cmd_name):
+    import hooks
+    return hooks.missing_command(cmd_name)
+
+# We could consider hooking list_commands too; to advertise plugin-installable
+# commands.
+Command.hooks.install_named_hook('get_missing_command', get_missing_command,
+    'plugin-info missing command check')
+
+
 def test_suite():
     # Thunk across to load_tests for niceness.
     from bzrlib.tests import TestLoader

=== added file 'hooks.py'
--- a/hooks.py	1970-01-01 00:00:00 +0000
+++ b/hooks.py	2010-03-01 07:24:38 +0000
@@ -0,0 +1,55 @@
+# plugin_info, a plugin for working with information about plugins.
+# Copyright (C) 2010 Canonical Limited.
+# 
+# This program is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License version 2 as published
+# by the Free Software Foundation.
+# 
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+# 
+# You should have received a copy of the GNU General Public License
+# along with this program; if not, write to the Free Software
+# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
+# 
+
+"""Handlers for the hooks that plugin-info registers."""
+
+from  bzrlib.commands import Command
+
+def get_db():
+    import cached
+    return cached.plugins
+
+
+def missing_command(cmd_name):
+    """Look up cmd_name in the plugin-info db."""
+    db = get_db()
+    plugins = []
+    for info in db:
+        if cmd_name in info.commands:
+            plugins.append(info)
+    if plugins:
+        return ReportFound(cmd_name, plugins)
+    return None
+
+
+class ReportFound(Command):
+    
+    def __init__(self, name, plugins):
+        Command.__init__(self)
+        self._name = name
+        self._plugins = plugins
+
+    def name(self):
+        return self._name
+
+    def run_argv_aliases(self, argv, alias_argv=None):
+        self._setup_outf()
+        plugin_names = [info.name for info in self._plugins]
+        self.outf.write("Command %s is supplied by the following plugins:\n  "
+            % self._name)
+        self.outf.write(", ".join(plugin_names))
+        self.outf.write("\n")

=== modified file 'tests/__init__.py'
--- a/tests/__init__.py	2010-03-01 01:15:55 +0000
+++ b/tests/__init__.py	2010-03-01 07:24:38 +0000
@@ -22,6 +22,7 @@
 def load_tests(standard_tests, module, loader):
     test_modules = [
         'blackbox',
+        'hooks',
         'extract',
         ]
     standard_tests.addTests(loader.loadTestsFromModuleNames(

=== added file 'tests/test_hooks.py'
--- a/tests/test_hooks.py	1970-01-01 00:00:00 +0000
+++ b/tests/test_hooks.py	2010-03-01 07:24:38 +0000
@@ -0,0 +1,45 @@
+# plugin_info, a plugin for working with information about plugins.
+# Copyright (C) 2010 Canonical Limited.
+# 
+# This program is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License version 2 as published
+# by the Free Software Foundation.
+# 
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+# 
+# You should have received a copy of the GNU General Public License
+# along with this program; if not, write to the Free Software
+# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
+# 
+
+"""Tests for hook handlers."""
+
+from bzrlib.plugins.plugin_info import extract, hooks
+from bzrlib.tests import TestCaseWithTransport
+
+
+class TestPluginInfo(TestCaseWithTransport):
+
+    def setup_db(self):
+        get_db = hooks.get_db
+        def restore():
+            hooks.get_db = get_db
+        self.addCleanup(restore)
+        def get_db():
+            info = extract.PluginInfo()
+            info.name = "sample-plugin"
+            info.commands = ['foo']
+            return [info]
+        hooks.get_db = get_db
+
+    def test_finds_command(self):
+        self.setup_db()
+        cmd = hooks.missing_command('foo')
+        self.assertNotEqual(None, cmd)
+
+    def test_does_not_find_command(self):
+        self.setup_db()
+        self.assertEqual(None, hooks.missing_command('bar'))




More information about the bazaar-commits mailing list