3331.3.6
by Martin Pool
merge trunk |
1 |
# Copyright (C) 2007, 2008 Canonical Ltd
|
2370.4.1
by Robert Collins
New SmartServer hooks facility. There are two initial hooks documented |
2 |
#
|
3 |
# This program is free software; you can redistribute it and/or modify
|
|
4 |
# it under the terms of the GNU General Public License as published by
|
|
5 |
# the Free Software Foundation; either version 2 of the License, or
|
|
6 |
# (at your option) any later version.
|
|
7 |
#
|
|
8 |
# This program is distributed in the hope that it will be useful,
|
|
9 |
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
10 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
11 |
# GNU General Public License for more details.
|
|
12 |
#
|
|
13 |
# You should have received a copy of the GNU General Public License
|
|
14 |
# along with this program; if not, write to the Free Software
|
|
4183.7.1
by Sabin Iacob
update FSF mailing address |
15 |
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
2370.4.1
by Robert Collins
New SmartServer hooks facility. There are two initial hooks documented |
16 |
|
17 |
||
18 |
"""Support for plugin hooking logic."""
|
|
3948.3.3
by Martin Pool
merge trunk |
19 |
from bzrlib import registry |
2370.4.1
by Robert Collins
New SmartServer hooks facility. There are two initial hooks documented |
20 |
from bzrlib.lazy_import import lazy_import |
3948.3.2
by Martin Pool
Remove APIs deprecated up to and including 1.6 |
21 |
from bzrlib.symbol_versioning import deprecated_method |
2370.4.1
by Robert Collins
New SmartServer hooks facility. There are two initial hooks documented |
22 |
lazy_import(globals(), """ |
4098.2.1
by Robert Collins
Allow self documenting hooks. |
23 |
import textwrap
|
24 |
||
2370.4.1
by Robert Collins
New SmartServer hooks facility. There are two initial hooks documented |
25 |
from bzrlib import (
|
4098.2.1
by Robert Collins
Allow self documenting hooks. |
26 |
_format_version_tuple,
|
2370.4.1
by Robert Collins
New SmartServer hooks facility. There are two initial hooks documented |
27 |
errors,
|
28 |
)
|
|
4119.3.2
by Robert Collins
Migrate existing hooks over to the new HookPoint infrastructure. |
29 |
from bzrlib.help_topics import help_as_plain_text
|
2370.4.1
by Robert Collins
New SmartServer hooks facility. There are two initial hooks documented |
30 |
""") |
31 |
||
32 |
||
4119.3.1
by Robert Collins
Create a single registry of all Hooks classes, removing the test suite knowledge of such hooks and allowing plugins to sensibly and safely define new hooks. |
33 |
known_hooks = registry.Registry() |
4230.1.1
by James Westby
Add msgeditor hooks to known_hooks. |
34 |
# known_hooks registry contains
|
4230.1.2
by James Westby
Update the comments based on those in the test. |
35 |
# tuple of (module, member name) which is the hook point
|
4230.1.1
by James Westby
Add msgeditor hooks to known_hooks. |
36 |
# module where the specific hooks are defined
|
4230.1.2
by James Westby
Update the comments based on those in the test. |
37 |
# callable to get the empty specific Hooks for that attribute
|
4119.3.1
by Robert Collins
Create a single registry of all Hooks classes, removing the test suite knowledge of such hooks and allowing plugins to sensibly and safely define new hooks. |
38 |
known_hooks.register_lazy(('bzrlib.branch', 'Branch.hooks'), 'bzrlib.branch', |
39 |
'BranchHooks') |
|
4160.1.1
by Robert Collins
Add a BzrDir.pre_open hook for use by the smart server gaol. |
40 |
known_hooks.register_lazy(('bzrlib.bzrdir', 'BzrDir.hooks'), 'bzrlib.bzrdir', |
41 |
'BzrDirHooks') |
|
4119.3.1
by Robert Collins
Create a single registry of all Hooks classes, removing the test suite knowledge of such hooks and allowing plugins to sensibly and safely define new hooks. |
42 |
known_hooks.register_lazy(('bzrlib.commands', 'Command.hooks'), |
43 |
'bzrlib.commands', 'CommandHooks') |
|
4119.3.2
by Robert Collins
Migrate existing hooks over to the new HookPoint infrastructure. |
44 |
known_hooks.register_lazy(('bzrlib.lock', 'Lock.hooks'), 'bzrlib.lock', |
45 |
'LockHooks') |
|
4230.1.1
by James Westby
Add msgeditor hooks to known_hooks. |
46 |
known_hooks.register_lazy(('bzrlib.msgeditor', 'hooks'), 'bzrlib.msgeditor', |
47 |
'MessageEditorHooks') |
|
4119.3.1
by Robert Collins
Create a single registry of all Hooks classes, removing the test suite knowledge of such hooks and allowing plugins to sensibly and safely define new hooks. |
48 |
known_hooks.register_lazy(('bzrlib.mutabletree', 'MutableTree.hooks'), |
49 |
'bzrlib.mutabletree', 'MutableTreeHooks') |
|
50 |
known_hooks.register_lazy(('bzrlib.smart.client', '_SmartClient.hooks'), |
|
51 |
'bzrlib.smart.client', 'SmartClientHooks') |
|
52 |
known_hooks.register_lazy(('bzrlib.smart.server', 'SmartTCPServer.hooks'), |
|
53 |
'bzrlib.smart.server', 'SmartServerHooks') |
|
4216.4.2
by Jelmer Vernooij
Register RioVersionInfoBuilderHooks in the hooks registry. |
54 |
known_hooks.register_lazy( |
55 |
('bzrlib.version_info_formats.format_rio', 'RioVersionInfoBuilder.hooks'), |
|
56 |
'bzrlib.version_info_formats.format_rio', 'RioVersionInfoBuilderHooks') |
|
4119.3.1
by Robert Collins
Create a single registry of all Hooks classes, removing the test suite knowledge of such hooks and allowing plugins to sensibly and safely define new hooks. |
57 |
|
58 |
||
59 |
def known_hooks_key_to_object((module_name, member_name)): |
|
60 |
"""Convert a known_hooks key to a object.
|
|
61 |
||
62 |
:param key: A tuple (module_name, member_name) as found in the keys of
|
|
63 |
the known_hooks registry.
|
|
64 |
:return: The object this specifies.
|
|
65 |
"""
|
|
66 |
return registry._LazyObjectGetter(module_name, member_name).get_obj() |
|
67 |
||
68 |
||
69 |
def known_hooks_key_to_parent_and_attribute((module_name, member_name)): |
|
70 |
"""Convert a known_hooks key to a object.
|
|
71 |
||
72 |
:param key: A tuple (module_name, member_name) as found in the keys of
|
|
73 |
the known_hooks registry.
|
|
74 |
:return: The object this specifies.
|
|
75 |
"""
|
|
76 |
member_list = member_name.rsplit('.', 1) |
|
77 |
if len(member_list) == 2: |
|
78 |
parent_name, attribute = member_list |
|
79 |
else: |
|
80 |
parent_name = None |
|
81 |
attribute = member_name |
|
82 |
parent = known_hooks_key_to_object((module_name, parent_name)) |
|
83 |
return parent, attribute |
|
84 |
||
85 |
||
2370.4.1
by Robert Collins
New SmartServer hooks facility. There are two initial hooks documented |
86 |
class Hooks(dict): |
87 |
"""A dictionary mapping hook name to a list of callables.
|
|
3943.8.1
by Marius Kruger
remove all trailing whitespace from bzr source |
88 |
|
2370.4.1
by Robert Collins
New SmartServer hooks facility. There are two initial hooks documented |
89 |
e.g. ['FOO'] Is the list of items to be called when the
|
90 |
FOO hook is triggered.
|
|
91 |
"""
|
|
92 |
||
2553.1.1
by Robert Collins
Give Hooks names. |
93 |
def __init__(self): |
94 |
dict.__init__(self) |
|
95 |
self._callable_names = {} |
|
96 |
||
4098.2.1
by Robert Collins
Allow self documenting hooks. |
97 |
def create_hook(self, hook): |
98 |
"""Create a hook which can have callbacks registered for it.
|
|
99 |
||
100 |
:param hook: The hook to create. An object meeting the protocol of
|
|
4098.2.2
by Robert Collins
Review feedback. |
101 |
bzrlib.hooks.HookPoint. It's name is used as the key for future
|
4098.2.1
by Robert Collins
Allow self documenting hooks. |
102 |
lookups.
|
103 |
"""
|
|
104 |
if hook.name in self: |
|
105 |
raise errors.DuplicateKey(hook.name) |
|
106 |
self[hook.name] = hook |
|
107 |
||
108 |
def docs(self): |
|
109 |
"""Generate the documentation for this Hooks instance.
|
|
110 |
||
111 |
This introspects all the individual hooks and returns their docs as well.
|
|
112 |
"""
|
|
113 |
hook_names = sorted(self.keys()) |
|
114 |
hook_docs = [] |
|
4108.3.1
by Robert Collins
Include the Hooks class in the Hooks docs. |
115 |
name = self.__class__.__name__ |
116 |
hook_docs.append(name) |
|
4119.3.2
by Robert Collins
Migrate existing hooks over to the new HookPoint infrastructure. |
117 |
hook_docs.append("-"*len(name)) |
4108.3.1
by Robert Collins
Include the Hooks class in the Hooks docs. |
118 |
hook_docs.append("") |
4098.2.1
by Robert Collins
Allow self documenting hooks. |
119 |
for hook_name in hook_names: |
120 |
hook = self[hook_name] |
|
121 |
try: |
|
122 |
hook_docs.append(hook.docs()) |
|
123 |
except AttributeError: |
|
124 |
# legacy hook
|
|
125 |
strings = [] |
|
126 |
strings.append(hook_name) |
|
4119.3.2
by Robert Collins
Migrate existing hooks over to the new HookPoint infrastructure. |
127 |
strings.append("~" * len(hook_name)) |
4098.2.1
by Robert Collins
Allow self documenting hooks. |
128 |
strings.append("") |
129 |
strings.append("An old-style hook. For documentation see the __init__ " |
|
4108.3.1
by Robert Collins
Include the Hooks class in the Hooks docs. |
130 |
"method of '%s'\n" % (name,)) |
4098.2.1
by Robert Collins
Allow self documenting hooks. |
131 |
hook_docs.extend(strings) |
132 |
return "\n".join(hook_docs) |
|
133 |
||
2553.1.1
by Robert Collins
Give Hooks names. |
134 |
def get_hook_name(self, a_callable): |
135 |
"""Get the name for a_callable for UI display.
|
|
136 |
||
137 |
If no name has been registered, the string 'No hook name' is returned.
|
|
2553.1.3
by Robert Collins
Increase docs in response to review feedback. |
138 |
We use a fixed string rather than repr or the callables module because
|
3943.8.1
by Marius Kruger
remove all trailing whitespace from bzr source |
139 |
the code names are rarely meaningful for end users and this is not
|
2553.1.3
by Robert Collins
Increase docs in response to review feedback. |
140 |
intended for debugging.
|
2553.1.1
by Robert Collins
Give Hooks names. |
141 |
"""
|
142 |
return self._callable_names.get(a_callable, "No hook name") |
|
143 |
||
3256.2.5
by Daniel Watkins
Added install_named_hook. |
144 |
def install_named_hook(self, hook_name, a_callable, name): |
145 |
"""Install a_callable in to the hook hook_name, and label it name.
|
|
2370.4.1
by Robert Collins
New SmartServer hooks facility. There are two initial hooks documented |
146 |
|
147 |
:param hook_name: A hook name. See the __init__ method of BranchHooks
|
|
148 |
for the complete list of hooks.
|
|
149 |
:param a_callable: The callable to be invoked when the hook triggers.
|
|
3943.8.1
by Marius Kruger
remove all trailing whitespace from bzr source |
150 |
The exact signature will depend on the hook - see the __init__
|
2370.4.1
by Robert Collins
New SmartServer hooks facility. There are two initial hooks documented |
151 |
method of BranchHooks for details on each hook.
|
3256.2.3
by Daniel Watkins
Added docs. |
152 |
:param name: A name to associate a_callable with, to show users what is
|
153 |
running.
|
|
2370.4.1
by Robert Collins
New SmartServer hooks facility. There are two initial hooks documented |
154 |
"""
|
155 |
try: |
|
4098.2.1
by Robert Collins
Allow self documenting hooks. |
156 |
hook = self[hook_name] |
2370.4.1
by Robert Collins
New SmartServer hooks facility. There are two initial hooks documented |
157 |
except KeyError: |
158 |
raise errors.UnknownHook(self.__class__.__name__, hook_name) |
|
4098.2.1
by Robert Collins
Allow self documenting hooks. |
159 |
try: |
160 |
# list hooks, old-style, not yet deprecated but less useful.
|
|
161 |
hook.append(a_callable) |
|
162 |
except AttributeError: |
|
163 |
hook.hook(a_callable, name) |
|
3256.2.10
by Daniel Watkins
Tightened exception scope, as suggested by Aaron. |
164 |
if name is not None: |
165 |
self.name_hook(a_callable, name) |
|
2553.1.1
by Robert Collins
Give Hooks names. |
166 |
|
167 |
def name_hook(self, a_callable, name): |
|
168 |
"""Associate name with a_callable to show users what is running."""
|
|
169 |
self._callable_names[a_callable] = name |
|
4098.2.1
by Robert Collins
Allow self documenting hooks. |
170 |
|
171 |
||
4098.2.2
by Robert Collins
Review feedback. |
172 |
class HookPoint(object): |
4098.2.1
by Robert Collins
Allow self documenting hooks. |
173 |
"""A single hook that clients can register to be called back when it fires.
|
174 |
||
175 |
:ivar name: The name of the hook.
|
|
176 |
:ivar introduced: A version tuple specifying what version the hook was
|
|
177 |
introduced in. None indicates an unknown version.
|
|
178 |
:ivar deprecated: A version tuple specifying what version the hook was
|
|
179 |
deprecated or superceded in. None indicates that the hook is not
|
|
180 |
superceded or deprecated. If the hook is superceded then the doc
|
|
181 |
should describe the recommended replacement hook to register for.
|
|
182 |
:ivar doc: The docs for using the hook.
|
|
183 |
"""
|
|
184 |
||
185 |
def __init__(self, name, doc, introduced, deprecated): |
|
4098.2.2
by Robert Collins
Review feedback. |
186 |
"""Create a HookPoint.
|
4098.2.3
by Robert Collins
White space difference-of-opinion. |
187 |
|
4098.2.1
by Robert Collins
Allow self documenting hooks. |
188 |
:param name: The name of the hook, for clients to use when registering.
|
189 |
:param doc: The docs for the hook.
|
|
190 |
:param introduced: When the hook was introduced (e.g. (0, 15)).
|
|
191 |
:param deprecated: When the hook was deprecated, None for
|
|
192 |
not-deprecated.
|
|
193 |
"""
|
|
194 |
self.name = name |
|
195 |
self.__doc__ = doc |
|
196 |
self.introduced = introduced |
|
197 |
self.deprecated = deprecated |
|
198 |
self._callbacks = [] |
|
199 |
self._callback_names = {} |
|
200 |
||
201 |
def docs(self): |
|
4098.2.2
by Robert Collins
Review feedback. |
202 |
"""Generate the documentation for this HookPoint.
|
4098.2.3
by Robert Collins
White space difference-of-opinion. |
203 |
|
4098.2.1
by Robert Collins
Allow self documenting hooks. |
204 |
:return: A string terminated in \n.
|
205 |
"""
|
|
206 |
strings = [] |
|
207 |
strings.append(self.name) |
|
4119.3.2
by Robert Collins
Migrate existing hooks over to the new HookPoint infrastructure. |
208 |
strings.append('~'*len(self.name)) |
4098.2.1
by Robert Collins
Allow self documenting hooks. |
209 |
strings.append('') |
210 |
if self.introduced: |
|
211 |
introduced_string = _format_version_tuple(self.introduced) |
|
212 |
else: |
|
213 |
introduced_string = 'unknown' |
|
214 |
strings.append('Introduced in: %s' % introduced_string) |
|
215 |
if self.deprecated: |
|
216 |
deprecated_string = _format_version_tuple(self.deprecated) |
|
217 |
else: |
|
218 |
deprecated_string = 'Not deprecated' |
|
219 |
strings.append('Deprecated in: %s' % deprecated_string) |
|
220 |
strings.append('') |
|
221 |
strings.extend(textwrap.wrap(self.__doc__)) |
|
222 |
strings.append('') |
|
223 |
return '\n'.join(strings) |
|
224 |
||
4119.3.2
by Robert Collins
Migrate existing hooks over to the new HookPoint infrastructure. |
225 |
def __eq__(self, other): |
226 |
return (type(other) == type(self) and |
|
227 |
other.__dict__ == self.__dict__) |
|
228 |
||
4098.2.1
by Robert Collins
Allow self documenting hooks. |
229 |
def hook(self, callback, callback_label): |
4098.2.2
by Robert Collins
Review feedback. |
230 |
"""Register a callback to be called when this HookPoint fires.
|
4098.2.1
by Robert Collins
Allow self documenting hooks. |
231 |
|
4098.2.2
by Robert Collins
Review feedback. |
232 |
:param callback: The callable to use when this HookPoint fires.
|
4098.2.1
by Robert Collins
Allow self documenting hooks. |
233 |
:param callback_label: A label to show in the UI while this callback is
|
234 |
processing.
|
|
235 |
"""
|
|
236 |
self._callbacks.append(callback) |
|
4119.3.2
by Robert Collins
Migrate existing hooks over to the new HookPoint infrastructure. |
237 |
if callback_label is not None: |
238 |
self._callback_names[callback] = callback_label |
|
4098.2.1
by Robert Collins
Allow self documenting hooks. |
239 |
|
240 |
def __iter__(self): |
|
241 |
return iter(self._callbacks) |
|
242 |
||
4119.3.3
by Robert Collins
Provide a __len__ on HookPoint so that 'if somehookpoint' will behave as it did when they were lists. |
243 |
def __len__(self): |
244 |
return len(self._callbacks) |
|
245 |
||
4098.2.1
by Robert Collins
Allow self documenting hooks. |
246 |
def __repr__(self): |
247 |
strings = [] |
|
4098.2.2
by Robert Collins
Review feedback. |
248 |
strings.append("<%s(" % type(self).__name__) |
4098.2.1
by Robert Collins
Allow self documenting hooks. |
249 |
strings.append(self.name) |
250 |
strings.append("), callbacks=[") |
|
251 |
for callback in self._callbacks: |
|
252 |
strings.append(repr(callback)) |
|
253 |
strings.append("(") |
|
254 |
strings.append(self._callback_names[callback]) |
|
255 |
strings.append("),") |
|
256 |
if len(self._callbacks) == 1: |
|
257 |
strings[-1] = ")" |
|
258 |
strings.append("]>") |
|
259 |
return ''.join(strings) |
|
4119.3.2
by Robert Collins
Migrate existing hooks over to the new HookPoint infrastructure. |
260 |
|
261 |
||
262 |
_help_prefix = \ |
|
263 |
"""
|
|
264 |
Hooks
|
|
265 |
=====
|
|
266 |
||
267 |
Introduction
|
|
268 |
------------
|
|
269 |
||
270 |
A hook of type *xxx* of class *yyy* needs to be registered using::
|
|
271 |
||
272 |
yyy.hooks.install_named_hook("xxx", ...)
|
|
273 |
||
274 |
See `Using hooks`_ in the User Guide for examples.
|
|
275 |
||
276 |
.. _Using hooks: ../user-guide/index.html#using-hooks
|
|
277 |
||
278 |
The class that contains each hook is given before the hooks it supplies. For
|
|
279 |
instance, BranchHooks as the class is the hooks class for
|
|
280 |
`bzrlib.branch.Branch.hooks`.
|
|
281 |
||
282 |
Each description also indicates whether the hook runs on the client (the
|
|
283 |
machine where bzr was invoked) or the server (the machine addressed by
|
|
284 |
the branch URL). These may be, but are not necessarily, the same machine.
|
|
285 |
||
286 |
Plugins (including hooks) are run on the server if all of these is true:
|
|
287 |
||
288 |
* The connection is via a smart server (accessed with a URL starting with
|
|
289 |
"bzr://", "bzr+ssh://" or "bzr+http://", or accessed via a "http://"
|
|
290 |
URL when a smart server is available via HTTP).
|
|
291 |
||
292 |
* The hook is either server specific or part of general infrastructure rather
|
|
293 |
than client specific code (such as commit).
|
|
294 |
||
295 |
"""
|
|
296 |
||
297 |
def hooks_help_text(topic): |
|
298 |
segments = [_help_prefix] |
|
299 |
for hook_key in sorted(known_hooks.keys()): |
|
300 |
hooks = known_hooks_key_to_object(hook_key) |
|
301 |
segments.append(hooks.docs()) |
|
302 |
return '\n'.join(segments) |