command options in plugins?

John A Meinel john at arbash-meinel.com
Tue Jun 14 17:09:34 BST 2005


Aaron Bentley wrote:

> Hi all,
>
> Is there a way to specify command options for Python plugins that bzr
> doesn't know about?  Simply adding an entry to bzrlib.commands.OPTIONS
> doesn't work, because plugins are loaded only after parsing is complete.
>
> I'm trying to provide all the scripts in bzrtools as plugins for the
> next bzrtools release*, but some of the options, like --max-count or
> --skip-symlinks are not standard bzr options.
>
> It's a bit of a chicken-egg problem, because the --builtin option should
> control whether plugins are loaded and thus, how options are parsed.
> Perhaps it would make sense to handle --builtin before normal option
> parsing.
>
Actually, I think you are right. The code should look more like:
try:
  if argv[1] != 'builtin':
    argv = argv[2:]
    _load_plugins()
  else:
    argv = argv[1:]
  args, opts = parse_args(argv)
  ...

This would allow you to add your new option flags into
bzrlib.commands.OPTIONS before parse_args is run.
It would also be better for this to use the _plugins global dictionary,
and possibly the register function that Lalo offered.

What would be nice is some way to have plugins loaded at the same time
that bzrlib is loaded, but you need a way to pass the "don't load
options" to bzrlib.
The only way I can think of to do that is to hijack __builtins__, since
that is the only place for truly global options that can be read at
import time.

Then you would change the "bzr" command line portion with:

if sys.argv[1] == 'builtin':
    __builtin__['bzrlib_no_plugins'] = True
    sys.argv = sys.argv[:1] + sys.argv[2:]
else:
    __builtin__['bzrlib_no_plugins'] = False

And then in "bzrlib/__init__.py" you could do:
if not __builtin__.has_key('bzrlib_no_plugins') or not
__builtin__['bzrlib_no_plugins']:
    _load_plugins()

But that is a little bit ugly.

> Aaron
> ---
> * Scripts will also be provided.

I vote for moving the plugin loading into run_bzr() before the
parse_args() command, with an externally viewable function, so that an
alternative client can call "bzrlib.load_plugins()" at an appropriate time.

That allows plugins to hook in before any real work has been done for
the bzr commandline, and external programs can decide if they want to
support plugins or not.

John
=:->

-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 253 bytes
Desc: OpenPGP digital signature
Url : https://lists.ubuntu.com/archives/bazaar/attachments/20050614/2b6ea12c/attachment.pgp 


More information about the bazaar mailing list