~bzr-pqm/bzr/bzr.dev

3331.3.6 by Martin Pool
merge trunk
1
# Copyright (C) 2007, 2008 Canonical Ltd
2370.4.1 by Robert Collins
New SmartServer hooks facility. There are two initial hooks documented
2
#
3
# This program is free software; you can redistribute it and/or modify
4
# it under the terms of the GNU General Public License as published by
5
# the Free Software Foundation; either version 2 of the License, or
6
# (at your option) any later version.
7
#
8
# This program is distributed in the hope that it will be useful,
9
# but WITHOUT ANY WARRANTY; without even the implied warranty of
10
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11
# GNU General Public License for more details.
12
#
13
# You should have received a copy of the GNU General Public License
14
# along with this program; if not, write to the Free Software
15
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
16
17
18
"""Support for plugin hooking logic."""
19
from bzrlib.lazy_import import lazy_import
3256.2.30 by Daniel Watkins
Updated deprecation warnings and tests.
20
from bzrlib.symbol_versioning import deprecated_method, one_five
2370.4.1 by Robert Collins
New SmartServer hooks facility. There are two initial hooks documented
21
lazy_import(globals(), """
22
from bzrlib import (
23
        errors,
24
        )
25
""")
26
27
28
class Hooks(dict):
29
    """A dictionary mapping hook name to a list of callables.
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
30
2370.4.1 by Robert Collins
New SmartServer hooks facility. There are two initial hooks documented
31
    e.g. ['FOO'] Is the list of items to be called when the
32
    FOO hook is triggered.
33
    """
34
2553.1.1 by Robert Collins
Give Hooks names.
35
    def __init__(self):
36
        dict.__init__(self)
37
        self._callable_names = {}
38
39
    def get_hook_name(self, a_callable):
40
        """Get the name for a_callable for UI display.
41
42
        If no name has been registered, the string 'No hook name' is returned.
2553.1.3 by Robert Collins
Increase docs in response to review feedback.
43
        We use a fixed string rather than repr or the callables module because
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
44
        the code names are rarely meaningful for end users and this is not
2553.1.3 by Robert Collins
Increase docs in response to review feedback.
45
        intended for debugging.
2553.1.1 by Robert Collins
Give Hooks names.
46
        """
47
        return self._callable_names.get(a_callable, "No hook name")
48
3256.2.30 by Daniel Watkins
Updated deprecation warnings and tests.
49
    @deprecated_method(one_five)
3256.2.8 by Daniel Watkins
Alphabetised hooks.py.
50
    def install_hook(self, hook_name, a_callable):
51
        """Install a_callable in to the hook hook_name.
52
53
        :param hook_name: A hook name. See the __init__ method of BranchHooks
54
            for the complete list of hooks.
55
        :param a_callable: The callable to be invoked when the hook triggers.
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
56
            The exact signature will depend on the hook - see the __init__
3256.2.8 by Daniel Watkins
Alphabetised hooks.py.
57
            method of BranchHooks for details on each hook.
58
        """
3256.2.11 by Daniel Watkins
Modified install_hook to call install_named_hook, as suggested by Aaron.
59
        self.install_named_hook(hook_name, a_callable, None)
3256.2.8 by Daniel Watkins
Alphabetised hooks.py.
60
3256.2.5 by Daniel Watkins
Added install_named_hook.
61
    def install_named_hook(self, hook_name, a_callable, name):
62
        """Install a_callable in to the hook hook_name, and label it name.
2370.4.1 by Robert Collins
New SmartServer hooks facility. There are two initial hooks documented
63
64
        :param hook_name: A hook name. See the __init__ method of BranchHooks
65
            for the complete list of hooks.
66
        :param a_callable: The callable to be invoked when the hook triggers.
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
67
            The exact signature will depend on the hook - see the __init__
2370.4.1 by Robert Collins
New SmartServer hooks facility. There are two initial hooks documented
68
            method of BranchHooks for details on each hook.
3256.2.3 by Daniel Watkins
Added docs.
69
        :param name: A name to associate a_callable with, to show users what is
70
            running.
2370.4.1 by Robert Collins
New SmartServer hooks facility. There are two initial hooks documented
71
        """
72
        try:
73
            self[hook_name].append(a_callable)
74
        except KeyError:
75
            raise errors.UnknownHook(self.__class__.__name__, hook_name)
3256.2.10 by Daniel Watkins
Tightened exception scope, as suggested by Aaron.
76
        if name is not None:
77
            self.name_hook(a_callable, name)
2553.1.1 by Robert Collins
Give Hooks names.
78
79
    def name_hook(self, a_callable, name):
80
        """Associate name with a_callable to show users what is running."""
81
        self._callable_names[a_callable] = name