/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar

« back to all changes in this revision

Viewing changes to bzrlib/hooks.py

  • Committer: Jelmer Vernooij
  • Date: 2009-03-18 15:38:56 UTC
  • mfrom: (4163 +trunk)
  • mto: This revision was merged to the branch mainline in revision 4164.
  • Revision ID: jelmer@samba.org-20090318153856-dr2lddyz56ajwb9c
Merge bzr.dev.

Show diffs side-by-side

added added

removed removed

Lines of Context:
17
17
 
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(), """
22
23
import textwrap
25
26
        _format_version_tuple,
26
27
        errors,
27
28
        )
 
29
from bzrlib.help_topics import help_as_plain_text
28
30
""")
29
31
 
30
32
 
 
33
known_hooks = registry.Registry()
 
34
known_hooks.register_lazy(('bzrlib.branch', 'Branch.hooks'), 'bzrlib.branch',
 
35
    'BranchHooks')
 
36
known_hooks.register_lazy(('bzrlib.bzrdir', 'BzrDir.hooks'), 'bzrlib.bzrdir',
 
37
    'BzrDirHooks')
 
38
known_hooks.register_lazy(('bzrlib.commands', 'Command.hooks'),
 
39
    'bzrlib.commands', 'CommandHooks')
 
40
known_hooks.register_lazy(('bzrlib.lock', 'Lock.hooks'), 'bzrlib.lock',
 
41
    'LockHooks')
 
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')
 
48
 
 
49
 
 
50
def known_hooks_key_to_object((module_name, member_name)):
 
51
    """Convert a known_hooks key to a object.
 
52
 
 
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.
 
56
    """
 
57
    return registry._LazyObjectGetter(module_name, member_name).get_obj()
 
58
 
 
59
 
 
60
def known_hooks_key_to_parent_and_attribute((module_name, member_name)):
 
61
    """Convert a known_hooks key to a object.
 
62
 
 
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.
 
66
    """
 
67
    member_list = member_name.rsplit('.', 1)
 
68
    if len(member_list) == 2:
 
69
        parent_name, attribute = member_list
 
70
    else:
 
71
        parent_name = None
 
72
        attribute = member_name
 
73
    parent = known_hooks_key_to_object((module_name, parent_name))
 
74
    return parent, attribute
 
75
 
 
76
 
31
77
class Hooks(dict):
32
78
    """A dictionary mapping hook name to a list of callables.
33
79
 
59
105
        hook_docs = []
60
106
        name = self.__class__.__name__
61
107
        hook_docs.append(name)
62
 
        hook_docs.append("="*len(name))
 
108
        hook_docs.append("-"*len(name))
63
109
        hook_docs.append("")
64
110
        for hook_name in hook_names:
65
111
            hook = self[hook_name]
69
115
                # legacy hook
70
116
                strings = []
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,))
162
208
        """
163
209
        strings = []
164
210
        strings.append(self.name)
165
 
        strings.append('-'*len(self.name))
 
211
        strings.append('~'*len(self.name))
166
212
        strings.append('')
167
213
        if self.introduced:
168
214
            introduced_string = _format_version_tuple(self.introduced)
179
225
        strings.append('')
180
226
        return '\n'.join(strings)
181
227
 
 
228
    def __eq__(self, other):
 
229
        return (type(other) == type(self) and 
 
230
            other.__dict__ == self.__dict__)
 
231
 
182
232
    def hook(self, callback, callback_label):
183
233
        """Register a callback to be called when this HookPoint fires.
184
234
 
187
237
            processing.
188
238
        """
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
191
242
 
192
243
    def __iter__(self):
193
244
        return iter(self._callbacks)
194
245
 
 
246
    def __len__(self):
 
247
        return len(self._callbacks)
 
248
 
195
249
    def __repr__(self):
196
250
        strings = []
197
251
        strings.append("<%s(" % type(self).__name__)
206
260
            strings[-1] = ")"
207
261
        strings.append("]>")
208
262
        return ''.join(strings)
 
263
 
 
264
 
 
265
_help_prefix = \
 
266
"""
 
267
Hooks
 
268
=====
 
269
 
 
270
Introduction
 
271
------------
 
272
 
 
273
A hook of type *xxx* of class *yyy* needs to be registered using::
 
274
 
 
275
  yyy.hooks.install_named_hook("xxx", ...)
 
276
 
 
277
See `Using hooks`_ in the User Guide for examples.
 
278
 
 
279
.. _Using hooks: ../user-guide/index.html#using-hooks
 
280
 
 
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`.
 
284
 
 
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.
 
288
 
 
289
Plugins (including hooks) are run on the server if all of these is true:
 
290
 
 
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).
 
294
 
 
295
  * The hook is either server specific or part of general infrastructure rather
 
296
    than client specific code (such as commit).
 
297
 
 
298
"""
 
299
 
 
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)