[RFC/MERGE] Allow two plugins to register the same command.
Aaron Bentley
aaron.bentley at utoronto.ca
Tue May 30 17:45:25 BST 2006
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
So, the second one to be registered gets prefixed with the module name?
That seems unpredictable to me. Perhaps it's better to prefix them
both, add a dummy command that says "disambiguate me, please!", and let
the user use aliases to specify which one should get the short name.
Also, perhaps it's worth allowing all commands to be specified using the
module name.
Aaron
Michael Ellerman wrote:
> 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]
>
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.1 (GNU/Linux)
Comment: Using GnuPG with Thunderbird - http://enigmail.mozdev.org
iD8DBQFEfHal0F+nu1YWqI0RAo9lAJ9gfGLyQ6gmf4QLVZzi2Xk74HY8wgCcCi/9
5tmO0vic2DXZ6Qk8BdG0CxU=
=IrB2
-----END PGP SIGNATURE-----
More information about the bazaar
mailing list