Lazily registering hooks?

John Arbash Meinel john at arbash-meinel.com
Sun May 30 20:01:29 BST 2010


-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Jelmer Vernooij wrote:
> On Sun, 2010-05-30 at 20:24 +0200, Gary van der Merwe wrote:
>> On Sun, May 30, 2010 at 7:01 PM, Jelmer Vernooij <jelmer at samba.org> wrote:
>>> On Sun, 2010-05-30 at 11:56 -0500, John Arbash Meinel wrote:
>>>> The best I can think of still just ends up with a central registry. The
>>>> Hook code could be localized, but it would end up saying "what bits were
>>>> interested in my hook", and then call back to have them loaded.
>>> That doesn't seem too bad actually, especially given this could all be done
>>> under the covers in bzrlib.hooks (and the plugins would have to be updated, of
>>> course).
>> We could have deprecated properties where the old hook was, pointing
>> to the new centralized hook. Then old plugins will just give a
>> deprecated warning, rather than fail with a AttributeError.
> I think what John proposed was to have a dictionary with lazily loaded
> hooks in bzrlib.hooks. When a particular hookpoint itself gets loaded it
> uses that dictionary to find all its hooks that were lazily loaded. This
> way the hooks can still exist in e.g. bzrlib.branch and you don't have
> to patch bzrlib.hooks every time you add a new hook. 
> 
> John, please correct me if this is different from what you had in mind. 
> 
> Cheers,
> 
> Jelmer
> 

Right, so the plugin would do:

from bzrlib import hooks

hooks.register_me(
	'bzrlib.branch.Branch.hooks', 'post_branch_tip_changed',
	'bzrlib.plugins.myplugin.module', 'hook_function',
	'hook_name')

And the Branch.hooks would do something like:
 to_run = hooks.get_hooks_for('bzrlib.branch.Branch.hooks')

Something along those lines. Note that typos fail silently here, which
isn't great. But that is what test suites are for :).

John
=:->

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.9 (Cygwin)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org/

iEYEARECAAYFAkwCtgkACgkQJdeBCYSNAANiZQCgkpwu+cxlIDr21wz6r6AfG1Ut
oc0AoMXY4ICizDYJPSeKs54m5K5M/0e0
=7jAe
-----END PGP SIGNATURE-----



More information about the bazaar mailing list