~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/hooks.py

  • Committer: Robert Collins
  • Date: 2009-03-12 06:24:39 UTC
  • mto: This revision was merged to the branch mainline in revision 4133.
  • Revision ID: robertc@robertcollins.net-20090312062439-gigl7rnor6t2cbcz
Migrate existing hooks over to the new HookPoint infrastructure.

Show diffs side-by-side

added added

removed removed

Lines of Context:
26
26
        _format_version_tuple,
27
27
        errors,
28
28
        )
 
29
from bzrlib.help_topics import help_as_plain_text
29
30
""")
30
31
 
31
32
 
34
35
    'BranchHooks')
35
36
known_hooks.register_lazy(('bzrlib.commands', 'Command.hooks'),
36
37
    'bzrlib.commands', 'CommandHooks')
 
38
known_hooks.register_lazy(('bzrlib.lock', 'Lock.hooks'), 'bzrlib.lock',
 
39
    'LockHooks')
37
40
known_hooks.register_lazy(('bzrlib.mutabletree', 'MutableTree.hooks'),
38
41
    'bzrlib.mutabletree', 'MutableTreeHooks')
39
42
known_hooks.register_lazy(('bzrlib.smart.client', '_SmartClient.hooks'),
100
103
        hook_docs = []
101
104
        name = self.__class__.__name__
102
105
        hook_docs.append(name)
103
 
        hook_docs.append("="*len(name))
 
106
        hook_docs.append("-"*len(name))
104
107
        hook_docs.append("")
105
108
        for hook_name in hook_names:
106
109
            hook = self[hook_name]
110
113
                # legacy hook
111
114
                strings = []
112
115
                strings.append(hook_name)
113
 
                strings.append("-" * len(hook_name))
 
116
                strings.append("~" * len(hook_name))
114
117
                strings.append("")
115
118
                strings.append("An old-style hook. For documentation see the __init__ "
116
119
                    "method of '%s'\n" % (name,))
203
206
        """
204
207
        strings = []
205
208
        strings.append(self.name)
206
 
        strings.append('-'*len(self.name))
 
209
        strings.append('~'*len(self.name))
207
210
        strings.append('')
208
211
        if self.introduced:
209
212
            introduced_string = _format_version_tuple(self.introduced)
220
223
        strings.append('')
221
224
        return '\n'.join(strings)
222
225
 
 
226
    def __eq__(self, other):
 
227
        return (type(other) == type(self) and 
 
228
            other.__dict__ == self.__dict__)
 
229
 
223
230
    def hook(self, callback, callback_label):
224
231
        """Register a callback to be called when this HookPoint fires.
225
232
 
228
235
            processing.
229
236
        """
230
237
        self._callbacks.append(callback)
231
 
        self._callback_names[callback] = callback_label
 
238
        if callback_label is not None:
 
239
            self._callback_names[callback] = callback_label
232
240
 
233
241
    def __iter__(self):
234
242
        return iter(self._callbacks)
247
255
            strings[-1] = ")"
248
256
        strings.append("]>")
249
257
        return ''.join(strings)
 
258
 
 
259
 
 
260
_help_prefix = \
 
261
"""
 
262
Hooks
 
263
=====
 
264
 
 
265
Introduction
 
266
------------
 
267
 
 
268
A hook of type *xxx* of class *yyy* needs to be registered using::
 
269
 
 
270
  yyy.hooks.install_named_hook("xxx", ...)
 
271
 
 
272
See `Using hooks`_ in the User Guide for examples.
 
273
 
 
274
.. _Using hooks: ../user-guide/index.html#using-hooks
 
275
 
 
276
The class that contains each hook is given before the hooks it supplies. For
 
277
instance, BranchHooks as the class is the hooks class for
 
278
`bzrlib.branch.Branch.hooks`.
 
279
 
 
280
Each description also indicates whether the hook runs on the client (the
 
281
machine where bzr was invoked) or the server (the machine addressed by
 
282
the branch URL).  These may be, but are not necessarily, the same machine.
 
283
 
 
284
Plugins (including hooks) are run on the server if all of these is true:
 
285
 
 
286
  * The connection is via a smart server (accessed with a URL starting with
 
287
    "bzr://", "bzr+ssh://" or "bzr+http://", or accessed via a "http://"
 
288
    URL when a smart server is available via HTTP).
 
289
 
 
290
  * The hook is either server specific or part of general infrastructure rather
 
291
    than client specific code (such as commit).
 
292
 
 
293
"""
 
294
 
 
295
def hooks_help_text(topic):
 
296
    segments = [_help_prefix]
 
297
    for hook_key in sorted(known_hooks.keys()):
 
298
        hooks = known_hooks_key_to_object(hook_key)
 
299
        segments.append(hooks.docs())
 
300
    return '\n'.join(segments)