~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/hooks.py

(gz) Fix test failure on alpha by correcting format string for
 gc_chk_sha1_record (Martin [gz])

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2007-2011 Canonical Ltd
 
1
# Copyright (C) 2007-2010 Canonical Ltd
2
2
#
3
3
# This program is free software; you can redistribute it and/or modify
4
4
# it under the terms of the GNU General Public License as published by
16
16
 
17
17
 
18
18
"""Support for plugin hooking logic."""
19
 
 
20
19
from bzrlib import (
 
20
    pyutils,
21
21
    registry,
22
22
    symbol_versioning,
23
23
    )
26
26
import textwrap
27
27
 
28
28
from bzrlib import (
29
 
    _format_version_tuple,
30
 
    errors,
31
 
    pyutils,
32
 
    )
33
 
from bzrlib.i18n import gettext
 
29
        _format_version_tuple,
 
30
        errors,
 
31
        )
 
32
from bzrlib.help_topics import help_as_plain_text
34
33
""")
35
34
 
36
35
 
73
72
    ('bzrlib.branch', 'Branch.hooks', 'BranchHooks'),
74
73
    ('bzrlib.bzrdir', 'BzrDir.hooks', 'BzrDirHooks'),
75
74
    ('bzrlib.commands', 'Command.hooks', 'CommandHooks'),
76
 
    ('bzrlib.config', 'ConfigHooks', '_ConfigHooks'),
77
75
    ('bzrlib.info', 'hooks', 'InfoHooks'),
78
76
    ('bzrlib.lock', 'Lock.hooks', 'LockHooks'),
79
77
    ('bzrlib.merge', 'Merger.hooks', 'MergeHooks'),
147
145
        else:
148
146
            callbacks = None
149
147
        hookpoint = HookPoint(name=name, doc=doc, introduced=introduced,
150
 
                              deprecated=deprecated, callbacks=callbacks)
 
148
                              deprecated=deprecated,
 
149
                              callbacks=callbacks)
151
150
        self[name] = hookpoint
152
151
 
153
 
    @symbol_versioning.deprecated_method(symbol_versioning.deprecated_in((2, 4)))
154
152
    def create_hook(self, hook):
155
153
        """Create a hook which can have callbacks registered for it.
156
154
 
245
243
        if name is not None:
246
244
            self.name_hook(a_callable, name)
247
245
 
248
 
    def uninstall_named_hook(self, hook_name, label):
249
 
        """Uninstall named hooks.
250
 
 
251
 
        :param hook_name: Hook point name
252
 
        :param label: Label of the callable to uninstall
253
 
        """
254
 
        try:
255
 
            hook = self[hook_name]
256
 
        except KeyError:
257
 
            raise errors.UnknownHook(self.__class__.__name__, hook_name)
258
 
        try:
259
 
            uninstall = getattr(hook, "uninstall")
260
 
        except AttributeError:
261
 
            raise errors.UnsupportedOperation(self.uninstall_named_hook, self)
262
 
        else:
263
 
            uninstall(label)
264
 
 
265
246
    def name_hook(self, a_callable, name):
266
247
        """Associate name with a_callable to show users what is running."""
267
248
        self._callable_names[a_callable] = name
311
292
            introduced_string = _format_version_tuple(self.introduced)
312
293
        else:
313
294
            introduced_string = 'unknown'
314
 
        strings.append(gettext('Introduced in: %s') % introduced_string)
 
295
        strings.append('Introduced in: %s' % introduced_string)
315
296
        if self.deprecated:
316
297
            deprecated_string = _format_version_tuple(self.deprecated)
317
 
            strings.append(gettext('Deprecated in: %s') % deprecated_string)
 
298
            strings.append('Deprecated in: %s' % deprecated_string)
318
299
        strings.append('')
319
300
        strings.extend(textwrap.wrap(self.__doc__,
320
301
            break_long_words=False))
347
328
        obj_getter = registry._ObjectGetter(callback)
348
329
        self._callbacks.append((obj_getter, callback_label))
349
330
 
350
 
    def uninstall(self, label):
351
 
        """Uninstall the callback with the specified label.
352
 
 
353
 
        :param label: Label of the entry to uninstall
354
 
        """
355
 
        entries_to_remove = []
356
 
        for entry in self._callbacks:
357
 
            (entry_callback, entry_label) = entry
358
 
            if entry_label == label:
359
 
                entries_to_remove.append(entry)
360
 
        if entries_to_remove == []:
361
 
            raise KeyError("No entry with label %r" % label)
362
 
        for entry in entries_to_remove:
363
 
            self._callbacks.remove(entry)
364
 
 
365
331
    def __iter__(self):
366
332
        return (callback.get_obj() for callback, name in self._callbacks)
367
333
 
426
392
    return '\n'.join(segments)
427
393
 
428
394
 
429
 
# Lazily registered hooks. Maps (module, name, hook_name) tuples
430
 
# to lists of tuples with objectgetters and names
431
395
_lazy_hooks = {}
432
396
 
433
397