18
18
"""Support for plugin hooking logic."""
19
19
from bzrlib.lazy_import import lazy_import
20
from bzrlib import registry
20
21
from bzrlib.symbol_versioning import deprecated_method, one_five
21
22
lazy_import(globals(), """
25
26
_format_version_tuple,
29
from bzrlib.help_topics import help_as_plain_text
33
known_hooks = registry.Registry()
34
known_hooks.register_lazy(('bzrlib.branch', 'Branch.hooks'), 'bzrlib.branch',
36
known_hooks.register_lazy(('bzrlib.bzrdir', 'BzrDir.hooks'), 'bzrlib.bzrdir',
38
known_hooks.register_lazy(('bzrlib.commands', 'Command.hooks'),
39
'bzrlib.commands', 'CommandHooks')
40
known_hooks.register_lazy(('bzrlib.lock', 'Lock.hooks'), 'bzrlib.lock',
42
known_hooks.register_lazy(('bzrlib.mutabletree', 'MutableTree.hooks'),
43
'bzrlib.mutabletree', 'MutableTreeHooks')
44
known_hooks.register_lazy(('bzrlib.smart.client', '_SmartClient.hooks'),
45
'bzrlib.smart.client', 'SmartClientHooks')
46
known_hooks.register_lazy(('bzrlib.smart.server', 'SmartTCPServer.hooks'),
47
'bzrlib.smart.server', 'SmartServerHooks')
50
def known_hooks_key_to_object((module_name, member_name)):
51
"""Convert a known_hooks key to a object.
53
:param key: A tuple (module_name, member_name) as found in the keys of
54
the known_hooks registry.
55
:return: The object this specifies.
57
return registry._LazyObjectGetter(module_name, member_name).get_obj()
60
def known_hooks_key_to_parent_and_attribute((module_name, member_name)):
61
"""Convert a known_hooks key to a object.
63
:param key: A tuple (module_name, member_name) as found in the keys of
64
the known_hooks registry.
65
:return: The object this specifies.
67
member_list = member_name.rsplit('.', 1)
68
if len(member_list) == 2:
69
parent_name, attribute = member_list
72
attribute = member_name
73
parent = known_hooks_key_to_object((module_name, parent_name))
74
return parent, attribute
32
78
"""A dictionary mapping hook name to a list of callables.
71
117
strings.append(hook_name)
72
strings.append("-" * len(hook_name))
118
strings.append("~" * len(hook_name))
73
119
strings.append("")
74
120
strings.append("An old-style hook. For documentation see the __init__ "
75
121
"method of '%s'\n" % (name,))
179
225
strings.append('')
180
226
return '\n'.join(strings)
228
def __eq__(self, other):
229
return (type(other) == type(self) and
230
other.__dict__ == self.__dict__)
182
232
def hook(self, callback, callback_label):
183
233
"""Register a callback to be called when this HookPoint fires.
189
239
self._callbacks.append(callback)
190
self._callback_names[callback] = callback_label
240
if callback_label is not None:
241
self._callback_names[callback] = callback_label
192
243
def __iter__(self):
193
244
return iter(self._callbacks)
247
return len(self._callbacks)
195
249
def __repr__(self):
197
251
strings.append("<%s(" % type(self).__name__)
206
260
strings[-1] = ")"
207
261
strings.append("]>")
208
262
return ''.join(strings)
273
A hook of type *xxx* of class *yyy* needs to be registered using::
275
yyy.hooks.install_named_hook("xxx", ...)
277
See `Using hooks`_ in the User Guide for examples.
279
.. _Using hooks: ../user-guide/index.html#using-hooks
281
The class that contains each hook is given before the hooks it supplies. For
282
instance, BranchHooks as the class is the hooks class for
283
`bzrlib.branch.Branch.hooks`.
285
Each description also indicates whether the hook runs on the client (the
286
machine where bzr was invoked) or the server (the machine addressed by
287
the branch URL). These may be, but are not necessarily, the same machine.
289
Plugins (including hooks) are run on the server if all of these is true:
291
* The connection is via a smart server (accessed with a URL starting with
292
"bzr://", "bzr+ssh://" or "bzr+http://", or accessed via a "http://"
293
URL when a smart server is available via HTTP).
295
* The hook is either server specific or part of general infrastructure rather
296
than client specific code (such as commit).
300
def hooks_help_text(topic):
301
segments = [_help_prefix]
302
for hook_key in sorted(known_hooks.keys()):
303
hooks = known_hooks_key_to_object(hook_key)
304
segments.append(hooks.docs())
305
return '\n'.join(segments)