~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/hooks.py

  • Committer: Andrew Bennetts
  • Date: 2011-04-08 03:31:54 UTC
  • mfrom: (5766 +trunk)
  • mto: This revision was merged to the branch mainline in revision 5767.
  • Revision ID: andrew.bennetts@canonical.com-20110408033154-la08nghd4391sw5m
Merge latest lp:bzr, move our new release notes entries to the current release.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2007-2010 Canonical Ltd
 
1
# Copyright (C) 2007-2011 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
 
19
20
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
 
        )
32
 
from bzrlib.help_topics import help_as_plain_text
 
29
    _format_version_tuple,
 
30
    errors,
 
31
    pyutils,
 
32
    )
33
33
""")
34
34
 
35
35
 
145
145
        else:
146
146
            callbacks = None
147
147
        hookpoint = HookPoint(name=name, doc=doc, introduced=introduced,
148
 
                              deprecated=deprecated,
149
 
                              callbacks=callbacks)
 
148
                              deprecated=deprecated, callbacks=callbacks)
150
149
        self[name] = hookpoint
151
150
 
 
151
    @symbol_versioning.deprecated_method(symbol_versioning.deprecated_in((2, 4)))
152
152
    def create_hook(self, hook):
153
153
        """Create a hook which can have callbacks registered for it.
154
154
 
243
243
        if name is not None:
244
244
            self.name_hook(a_callable, name)
245
245
 
 
246
    def uninstall_named_hook(self, hook_name, label):
 
247
        """Uninstall named hooks.
 
248
 
 
249
        :param hook_name: Hook point name
 
250
        :param label: Label of the callable to uninstall
 
251
        """
 
252
        try:
 
253
            hook = self[hook_name]
 
254
        except KeyError:
 
255
            raise errors.UnknownHook(self.__class__.__name__, hook_name)
 
256
        try:
 
257
            uninstall = getattr(hook, "uninstall")
 
258
        except AttributeError:
 
259
            raise errors.UnsupportedOperation(self.install_named_hook_lazy,
 
260
                self)
 
261
        else:
 
262
            uninstall(label)
 
263
 
246
264
    def name_hook(self, a_callable, name):
247
265
        """Associate name with a_callable to show users what is running."""
248
266
        self._callable_names[a_callable] = name
328
346
        obj_getter = registry._ObjectGetter(callback)
329
347
        self._callbacks.append((obj_getter, callback_label))
330
348
 
 
349
    def uninstall(self, label):
 
350
        """Uninstall the callback with the specified label.
 
351
 
 
352
        :param label: Label of the entry to uninstall
 
353
        """
 
354
        entries_to_remove = []
 
355
        for entry in self._callbacks:
 
356
            (entry_callback, entry_label) = entry
 
357
            if entry_label == label:
 
358
                entries_to_remove.append(entry)
 
359
        if entries_to_remove == []:
 
360
            raise KeyError("No entry with label %r" % label)
 
361
        for entry in entries_to_remove:
 
362
            self._callbacks.remove(entry)
 
363
 
331
364
    def __iter__(self):
332
365
        return (callback.get_obj() for callback, name in self._callbacks)
333
366
 
392
425
    return '\n'.join(segments)
393
426
 
394
427
 
 
428
# Lazily registered hooks. Maps (module, name, hook_name) tuples
 
429
# to lists of tuples with objectgetters and names
395
430
_lazy_hooks = {}
396
431
 
397
432