[PATCH] better plugins with optional loading

John Arbash Meinel john at arbash-meinel.com
Tue Jun 14 23:18:40 BST 2005


The attached patch basically starts from Lalo's patch, but makes the
plugin loading optional, and loaded at the correct time. This patch is
against bzr-mainline, so you don't have to apply Lalo's patch first.

Basically, it uses the same "register_*" function so that commands have
to register themselves. But it waits to load plugins until it gets into
"run_bzr".

It also adds the function "bzrlib.load_plugins()" so that another client
which uses the bzrlib interface can chose whether or not to load plugins.

This is really just meant to be a fixup to the current situation, where
plugins cannot add their own options, because they are loaded too late.

Long term, I think it is best to go with Martin's idea of using a custom
startup script, possibly with some lazy evaluated objects for faster
load times. Though I feel it is a little bit of premature optimization
right now. The problem is that to make them useful, you need to update
the bzrlib.commands.OPTIONS structure, which means you need to read them
to know what options they take. You start having to add more and more
extra information higher up in the process, which means things start
slowing down again.

I would offer that for plugins that would take a long time to load, you
move the long-loading portion into a separate module, which gets loaded
during the run() function. So you have something like:

my_big_plugin.py:

import bzrlib, bzrlib.commands

class cmd_bigcommand(bzrlib.commands.Command):
    takes_args = []
    takes_options = ['special-option']
    def run(self, special_option=None):
       import thebigcommand
       thebigcommand.run(special_option=special_option)

bzrlib.commands.OPTIONS['special-option'] = str

And then it the path you have
$PLUGIN_PATH/thebigcommand/__init__.py:

def run(special_option=None):
    pass # Do all the important stuff here.

This works even better with the customized startup script, since you can
make "thebigcommand" be a local python file rather than a directory,
since it won't get auto-loaded by the BZR_PLUGIN_PATH search.

John
=:->

-------------- next part --------------
An embedded and charset-unspecified text was scrubbed...
Name: optional-load-plugin.patch
Url: https://lists.ubuntu.com/archives/bazaar/attachments/20050614/c5d3c792/attachment.diff 
-------------- 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/c5d3c792/attachment.pgp 


More information about the bazaar mailing list