~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/hooks.py

  • Committer: Patch Queue Manager
  • Date: 2015-12-17 18:39:00 UTC
  • mfrom: (6606.1.2 fix-float)
  • Revision ID: pqm@pqm.ubuntu.com-20151217183900-0719du2uv1kwu3lc
(vila) Inline testtools private method to fix an issue in xenial (the
 private implementation has changed in an backward incompatible way).
 (Jelmer Vernooij)

Show diffs side-by-side

added added

removed removed

Lines of Context:
14
14
# along with this program; if not, write to the Free Software
15
15
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
16
16
 
17
 
 
18
17
"""Support for plugin hooking logic."""
19
18
 
 
19
from __future__ import absolute_import
 
20
 
20
21
from bzrlib import (
21
22
    registry,
22
23
    symbol_versioning,
71
72
 
72
73
_builtin_known_hooks = (
73
74
    ('bzrlib.branch', 'Branch.hooks', 'BranchHooks'),
74
 
    ('bzrlib.bzrdir', 'BzrDir.hooks', 'BzrDirHooks'),
 
75
    ('bzrlib.controldir', 'ControlDir.hooks', 'ControlDirHooks'),
75
76
    ('bzrlib.commands', 'Command.hooks', 'CommandHooks'),
76
77
    ('bzrlib.config', 'ConfigHooks', '_ConfigHooks'),
77
78
    ('bzrlib.info', 'hooks', 'InfoHooks'),
82
83
    ('bzrlib.smart.client', '_SmartClient.hooks', 'SmartClientHooks'),
83
84
    ('bzrlib.smart.server', 'SmartTCPServer.hooks', 'SmartServerHooks'),
84
85
    ('bzrlib.status', 'hooks', 'StatusHooks'),
 
86
    ('bzrlib.transport', 'Transport.hooks', 'TransportHooks'),
85
87
    ('bzrlib.version_info_formats.format_rio', 'RioVersionInfoBuilder.hooks',
86
88
        'RioVersionInfoBuilderHooks'),
87
89
    ('bzrlib.merge_directive', 'BaseMergeDirective.hooks',
104
106
    return pyutils.get_named_object(module_name, member_name)
105
107
 
106
108
 
107
 
@symbol_versioning.deprecated_function(symbol_versioning.deprecated_in((2, 3)))
108
 
def known_hooks_key_to_parent_and_attribute(key):
109
 
    """See KnownHooksRegistry.key_to_parent_and_attribute."""
110
 
    return known_hooks.key_to_parent_and_attribute(key)
111
 
 
112
 
 
113
109
class Hooks(dict):
114
110
    """A dictionary mapping hook name to a list of callables.
115
111
 
127
123
        """
128
124
        dict.__init__(self)
129
125
        self._callable_names = {}
 
126
        self._lazy_callable_names = {}
130
127
        self._module = module
131
128
        self._member_name = member_name
132
129
 
150
147
                              deprecated=deprecated, callbacks=callbacks)
151
148
        self[name] = hookpoint
152
149
 
153
 
    @symbol_versioning.deprecated_method(symbol_versioning.deprecated_in((2, 4)))
154
 
    def create_hook(self, hook):
155
 
        """Create a hook which can have callbacks registered for it.
156
 
 
157
 
        :param hook: The hook to create. An object meeting the protocol of
158
 
            bzrlib.hooks.HookPoint. It's name is used as the key for future
159
 
            lookups.
160
 
        """
161
 
        if hook.name in self:
162
 
            raise errors.DuplicateKey(hook.name)
163
 
        self[hook.name] = hook
164
 
 
165
150
    def docs(self):
166
151
        """Generate the documentation for this Hooks instance.
167
152
 
196
181
        the code names are rarely meaningful for end users and this is not
197
182
        intended for debugging.
198
183
        """
199
 
        return self._callable_names.get(a_callable, "No hook name")
 
184
        name = self._callable_names.get(a_callable, None)
 
185
        if name is None and a_callable is not None:
 
186
            name = self._lazy_callable_names.get((a_callable.__module__,
 
187
                                                  a_callable.__name__),
 
188
                                                 None)
 
189
        if name is None:
 
190
            return 'No hook name'
 
191
        return name
 
192
 
200
193
 
201
194
    def install_named_hook_lazy(self, hook_name, callable_module,
202
195
        callable_member, name):
221
214
                self)
222
215
        else:
223
216
            hook_lazy(callable_module, callable_member, name)
 
217
        if name is not None:
 
218
            self.name_hook_lazy(callable_module, callable_member, name)
224
219
 
225
220
    def install_named_hook(self, hook_name, a_callable, name):
226
221
        """Install a_callable in to the hook hook_name, and label it name.
266
261
        """Associate name with a_callable to show users what is running."""
267
262
        self._callable_names[a_callable] = name
268
263
 
 
264
    def name_hook_lazy(self, callable_module, callable_member, callable_name):
 
265
        self._lazy_callable_names[(callable_module, callable_member)]= \
 
266
            callable_name
 
267
 
269
268
 
270
269
class HookPoint(object):
271
270
    """A single hook that clients can register to be called back when it fires.