[RFC/MERGE] Allow two plugins to register the same command.

Michael Ellerman michael at ellerman.id.au
Tue May 30 17:26:12 BST 2006


Hiya,

What do people think about this one?
Also at http://michael.ellerman.id.au/bzr/branches/mpe-dev/

It allows me to have .bazaar/plugins/01_shelf and
.bazaar/plugins/bzrtools (both of which inclue the 'shelf' command),
and they coexist nicely.

cheers

=== modified file 'bzrlib/commands.py'
--- bzrlib/commands.py
+++ bzrlib/commands.py
@@ -62,18 +62,26 @@
     if cmd_name.startswith("cmd_"):
         cmd_name = _unsquish_command_name(cmd_name)

-    if not plugin_cmds.has_key(cmd_name):
-        plugin_cmds[cmd_name] = cmd
-        mutter('registered plugin command %s', cmd_name)
-        if decorate and cmd_name in builtin_command_names():
-            return _builtin_commands()[cmd_name]
-    elif decorate:
-        result = plugin_cmds[cmd_name]
-        plugin_cmds[cmd_name] = cmd
-        return result
-    else:
-        log_error('Two plugins defined the same command: %r' % cmd_name)
-        log_error('Not loading the one in %r' % sys.modules[cmd.__module__])
+    decorated_command = None
+
+    if decorate:
+        if plugin_cmds.has_key(cmd_name):
+            result = plugin_cmds[cmd_name]
+        elif cmd_name in builtin_command_names():
+            result = _builtin_commands()[cmd_name]
+    elif plugin_cmds.has_key(cmd_name):
+        # Prefix the command with the module name to avoid a clash
+        module_name = cmd.__module__.replace('.', '-')
+        cmd_name = '%s-%s' % (module_name, cmd_name)
+
+        if plugin_cmds.has_key(cmd_name):
+            log_error('%s defined the command %s more than once!' % \
+                     (cmd.__module__, cmd.__name__))
+            return None
+
+    plugin_cmds[cmd_name] = cmd
+    mutter('registered plugin command %s', cmd_name)
+    return decorated_command


 def _squish_command_name(cmd):

=== modified file 'bzrlib/help.py'
--- bzrlib/help.py
+++ bzrlib/help.py
@@ -63,12 +63,12 @@
         help_on_command(topic, outfile = outfile)


-def command_usage(cmd_object):
+def command_usage(cmdname, cmd_object):
     """Return single-line grammar for command.

     Only describes arguments, not options.
     """
-    s = 'bzr ' + cmd_object.name() + ' '
+    s = 'bzr ' + cmdname + ' '
     for aname in cmd_object.takes_args:
         aname = aname.upper()
         if aname[-1] in ['$', '+']:
@@ -99,7 +99,7 @@
     if doc == None:
         raise NotImplementedError("sorry, no detailed help yet for
%r" % cmdname)

-    print >>outfile, 'usage:', command_usage(cmd_object)
+    print >>outfile, 'usage:', command_usage(cmdname, cmd_object)

     if cmd_object.aliases:
         print >>outfile, 'aliases:',
@@ -155,7 +155,7 @@
         cmd_object = get_cmd_object(cmd_name)
         if cmd_object.hidden:
             continue
-        print >>outfile, command_usage(cmd_object)
+        print >>outfile, command_usage(cmd_name, cmd_object)
         cmd_help = cmd_object.help()
         if cmd_help:
             firstline = cmd_help.split('\n', 1)[0]




More information about the bazaar mailing list