Merge lp:~nmb/bzr/894609-lazy-hook-names into lp:bzr

Proposed by Neil Martinsen-Burrell
Status: Merged
Approved by: Vincent Ladeuil
Approved revision: no longer in the source branch.
Merged at revision: 6332
Proposed branch: lp:~nmb/bzr/894609-lazy-hook-names
Merge into: lp:bzr
Diff against target: 98 lines (+41/-1)
3 files modified
bzrlib/hooks.py (+16/-1)
bzrlib/tests/blackbox/test_hooks.py (+22/-0)
doc/en/release-notes/bzr-2.5.txt (+3/-0)
To merge this branch: bzr merge lp:~nmb/bzr/894609-lazy-hook-names
Reviewer Review Type Date Requested Status
Martin Pool Approve
Review via email: mp+83669@code.launchpad.net

Commit message

allow lazily registered hooks to have their names appear in "bzr hooks" (bug 894609)

Description of the change

This allows lazily registered hooks to have their names appear in "bzr hooks". It uses __name__ and __module__ of a callable to find the name when the hook was registered lazily. It doesn't work for all lazy hooks, particularly those registered with hooks.install_lazy_named_hook or those not defined at the top level of a module, but it is an improvement on the present situation.

To post a comment you must log in.
Revision history for this message
Martin Pool (mbp) wrote :

That looks reasonable, thanks.

  vote approve

review: Approve
Revision history for this message
Martin Pool (mbp) wrote :

sent to pqm by email

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'bzrlib/hooks.py'
2--- bzrlib/hooks.py 2011-10-11 10:21:35 +0000
3+++ bzrlib/hooks.py 2011-11-28 19:09:26 +0000
4@@ -127,6 +127,7 @@
5 """
6 dict.__init__(self)
7 self._callable_names = {}
8+ self._lazy_callable_names = {}
9 self._module = module
10 self._member_name = member_name
11
12@@ -196,7 +197,15 @@
13 the code names are rarely meaningful for end users and this is not
14 intended for debugging.
15 """
16- return self._callable_names.get(a_callable, "No hook name")
17+ name = self._callable_names.get(a_callable, None)
18+ if name is None and a_callable is not None:
19+ name = self._lazy_callable_names.get((a_callable.__module__,
20+ a_callable.__name__),
21+ None)
22+ if name is None:
23+ return 'No hook name'
24+ return name
25+
26
27 def install_named_hook_lazy(self, hook_name, callable_module,
28 callable_member, name):
29@@ -221,6 +230,8 @@
30 self)
31 else:
32 hook_lazy(callable_module, callable_member, name)
33+ if name is not None:
34+ self.name_hook_lazy(callable_module, callable_member, name)
35
36 def install_named_hook(self, hook_name, a_callable, name):
37 """Install a_callable in to the hook hook_name, and label it name.
38@@ -266,6 +277,10 @@
39 """Associate name with a_callable to show users what is running."""
40 self._callable_names[a_callable] = name
41
42+ def name_hook_lazy(self, callable_module, callable_member, callable_name):
43+ self._lazy_callable_names[(callable_module, callable_member)]= \
44+ callable_name
45+
46
47 class HookPoint(object):
48 """A single hook that clients can register to be called back when it fires.
49
50=== modified file 'bzrlib/tests/blackbox/test_hooks.py'
51--- bzrlib/tests/blackbox/test_hooks.py 2009-03-23 14:59:43 +0000
52+++ bzrlib/tests/blackbox/test_hooks.py 2011-11-28 19:09:26 +0000
53@@ -19,6 +19,8 @@
54 from bzrlib.branch import Branch
55 from bzrlib.tests import TestCaseWithTransport
56
57+def _foo_hook():
58+ pass
59
60 class TestHooks(TestCaseWithTransport):
61
62@@ -51,3 +53,23 @@
63
64 def test_hooks_no_branch(self):
65 self.run_bzr('hooks')
66+
67+ def test_hooks_lazy_with_unnamed_hook(self):
68+ self.make_branch('.')
69+ def foo(): return
70+ Branch.hooks.install_named_hook_lazy('set_rh',
71+ 'bzrlib.tests.blackbox.test_hooks',
72+ '_foo_hook',
73+ None)
74+ out, err = self.run_bzr('hooks')
75+ self._check_hooks_output(out, {'set_rh': ["No hook name"]})
76+
77+ def test_hooks_lazy_with_named_hook(self):
78+ self.make_branch('.')
79+ def foo(): return
80+ Branch.hooks.install_named_hook_lazy('set_rh',
81+ 'bzrlib.tests.blackbox.test_hooks',
82+ '_foo_hook',
83+ 'hook has a name')
84+ out, err = self.run_bzr('hooks')
85+ self._check_hooks_output(out, {'set_rh': ["hook has a name"]})
86
87=== modified file 'doc/en/release-notes/bzr-2.5.txt'
88--- doc/en/release-notes/bzr-2.5.txt 2011-11-28 15:59:39 +0000
89+++ doc/en/release-notes/bzr-2.5.txt 2011-11-28 19:09:26 +0000
90@@ -69,6 +69,9 @@
91 and gives a better error message if the time zone offset is not given.
92 (Matt Giuca, #892657)
93
94+* Provide names for lazily registered hooks.
95+ (Neil Martinsen-Burrell, #894609)
96+
97 * Resolve regression from colocated branch path handling, by ensuring that
98 unreserved characters are unquoted in URLs. (Martin Packman, #842223)
99