/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar

« back to all changes in this revision

Viewing changes to bzrlib/hooks.py

  • Committer: Richard Wilbur
  • Date: 2016-02-04 19:07:28 UTC
  • mto: This revision was merged to the branch mainline in revision 6618.
  • Revision ID: richard.wilbur@gmail.com-20160204190728-p0zvfii6zase0fw7
Update COPYING.txt from the original http://www.gnu.org/licenses/gpl-2.0.txt  (Only differences were in whitespace.)  Thanks to Petr Stodulka for pointing out the discrepancy.

Show diffs side-by-side

added added

removed removed

Lines of Context:
16
16
 
17
17
"""Support for plugin hooking logic."""
18
18
 
19
 
from . import (
20
 
    errors,
 
19
from __future__ import absolute_import
 
20
 
 
21
from bzrlib import (
21
22
    registry,
 
23
    symbol_versioning,
22
24
    )
23
 
from .lazy_import import lazy_import
 
25
from bzrlib.lazy_import import lazy_import
24
26
lazy_import(globals(), """
25
27
import textwrap
26
28
 
27
 
from breezy import (
 
29
from bzrlib import (
28
30
    _format_version_tuple,
 
31
    errors,
29
32
    pyutils,
30
33
    )
31
 
from breezy.i18n import gettext
 
34
from bzrlib.i18n import gettext
32
35
""")
33
36
 
34
37
 
35
 
class UnknownHook(errors.BzrError):
36
 
 
37
 
    _fmt = "The %(type)s hook '%(hook)s' is unknown in this version of breezy."
38
 
 
39
 
    def __init__(self, hook_type, hook_name):
40
 
        errors.BzrError.__init__(self)
41
 
        self.type = hook_type
42
 
        self.hook = hook_name
43
 
 
44
 
 
45
38
class KnownHooksRegistry(registry.Registry):
46
39
    # known_hooks registry contains
47
40
    # tuple of (module, member name) which is the hook point
49
42
    # callable to get the empty specific Hooks for that attribute
50
43
 
51
44
    def register_lazy_hook(self, hook_module_name, hook_member_name,
52
 
                           hook_factory_member_name):
 
45
            hook_factory_member_name):
53
46
        self.register_lazy((hook_module_name, hook_member_name),
54
 
                           hook_module_name, hook_factory_member_name)
 
47
            hook_module_name, hook_factory_member_name)
55
48
 
56
49
    def iter_parent_objects(self):
57
50
        """Yield (hook_key, (parent_object, attr)) tuples for every registered
59
52
        instance.
60
53
 
61
54
        This is useful for resetting/restoring all the hooks to a known state,
62
 
        as is done in breezy.tests.TestCase._clear_hooks.
 
55
        as is done in bzrlib.tests.TestCase._clear_hooks.
63
56
        """
64
57
        for key in self.keys():
65
58
            yield key, self.key_to_parent_and_attribute(key)
66
59
 
67
 
    def key_to_parent_and_attribute(self, key):
 
60
    def key_to_parent_and_attribute(self, (module_name, member_name)):
68
61
        """Convert a known_hooks key to a (parent_obj, attr) pair.
69
62
 
70
63
        :param key: A tuple (module_name, member_name) as found in the keys of
72
65
        :return: The parent_object of the hook and the name of the attribute on
73
66
            that parent object where the hook is kept.
74
67
        """
75
 
        parent_mod, parent_member, attr = pyutils.calc_parent_name(*key)
 
68
        parent_mod, parent_member, attr = pyutils.calc_parent_name(module_name,
 
69
            member_name)
76
70
        return pyutils.get_named_object(parent_mod, parent_member), attr
77
71
 
78
72
 
79
73
_builtin_known_hooks = (
80
 
    ('breezy.branch', 'Branch.hooks', 'BranchHooks'),
81
 
    ('breezy.controldir', 'ControlDir.hooks', 'ControlDirHooks'),
82
 
    ('breezy.commands', 'Command.hooks', 'CommandHooks'),
83
 
    ('breezy.config', 'ConfigHooks', '_ConfigHooks'),
84
 
    ('breezy.info', 'hooks', 'InfoHooks'),
85
 
    ('breezy.lock', 'Lock.hooks', 'LockHooks'),
86
 
    ('breezy.merge', 'Merger.hooks', 'MergeHooks'),
87
 
    ('breezy.msgeditor', 'hooks', 'MessageEditorHooks'),
88
 
    ('breezy.mutabletree', 'MutableTree.hooks', 'MutableTreeHooks'),
89
 
    ('breezy.bzr.smart.client', '_SmartClient.hooks', 'SmartClientHooks'),
90
 
    ('breezy.bzr.smart.server', 'SmartTCPServer.hooks', 'SmartServerHooks'),
91
 
    ('breezy.status', 'hooks', 'StatusHooks'),
92
 
    ('breezy.transport', 'Transport.hooks', 'TransportHooks'),
93
 
    ('breezy.version_info_formats.format_rio', 'RioVersionInfoBuilder.hooks',
 
74
    ('bzrlib.branch', 'Branch.hooks', 'BranchHooks'),
 
75
    ('bzrlib.controldir', 'ControlDir.hooks', 'ControlDirHooks'),
 
76
    ('bzrlib.commands', 'Command.hooks', 'CommandHooks'),
 
77
    ('bzrlib.config', 'ConfigHooks', '_ConfigHooks'),
 
78
    ('bzrlib.info', 'hooks', 'InfoHooks'),
 
79
    ('bzrlib.lock', 'Lock.hooks', 'LockHooks'),
 
80
    ('bzrlib.merge', 'Merger.hooks', 'MergeHooks'),
 
81
    ('bzrlib.msgeditor', 'hooks', 'MessageEditorHooks'),
 
82
    ('bzrlib.mutabletree', 'MutableTree.hooks', 'MutableTreeHooks'),
 
83
    ('bzrlib.smart.client', '_SmartClient.hooks', 'SmartClientHooks'),
 
84
    ('bzrlib.smart.server', 'SmartTCPServer.hooks', 'SmartServerHooks'),
 
85
    ('bzrlib.status', 'hooks', 'StatusHooks'),
 
86
    ('bzrlib.transport', 'Transport.hooks', 'TransportHooks'),
 
87
    ('bzrlib.version_info_formats.format_rio', 'RioVersionInfoBuilder.hooks',
94
88
        'RioVersionInfoBuilderHooks'),
95
 
    ('breezy.merge_directive', 'BaseMergeDirective.hooks',
 
89
    ('bzrlib.merge_directive', 'BaseMergeDirective.hooks',
96
90
        'MergeDirectiveHooks'),
97
91
    )
98
92
 
102
96
del _builtin_known_hooks, _hook_module, _hook_attribute, _hook_class
103
97
 
104
98
 
105
 
def known_hooks_key_to_object(key):
 
99
def known_hooks_key_to_object((module_name, member_name)):
106
100
    """Convert a known_hooks key to a object.
107
101
 
108
102
    :param key: A tuple (module_name, member_name) as found in the keys of
109
103
        the known_hooks registry.
110
104
    :return: The object this specifies.
111
105
    """
112
 
    return pyutils.get_named_object(*key)
 
106
    return pyutils.get_named_object(module_name, member_name)
113
107
 
114
108
 
115
109
class Hooks(dict):
162
156
        hook_docs = []
163
157
        name = self.__class__.__name__
164
158
        hook_docs.append(name)
165
 
        hook_docs.append("-" * len(name))
 
159
        hook_docs.append("-"*len(name))
166
160
        hook_docs.append("")
167
161
        for hook_name in hook_names:
168
162
            hook = self[hook_name]
175
169
                strings.append("~" * len(hook_name))
176
170
                strings.append("")
177
171
                strings.append("An old-style hook. For documentation see the __init__ "
178
 
                               "method of '%s'\n" % (name,))
 
172
                    "method of '%s'\n" % (name,))
179
173
                hook_docs.extend(strings)
180
174
        return "\n".join(hook_docs)
181
175
 
196
190
            return 'No hook name'
197
191
        return name
198
192
 
 
193
 
199
194
    def install_named_hook_lazy(self, hook_name, callable_module,
200
 
                                callable_member, name):
 
195
        callable_member, name):
201
196
        """Install a_callable in to the hook hook_name lazily, and label it.
202
197
 
203
198
        :param hook_name: A hook name. See the __init__ method for the complete
211
206
        try:
212
207
            hook = self[hook_name]
213
208
        except KeyError:
214
 
            raise UnknownHook(self.__class__.__name__, hook_name)
 
209
            raise errors.UnknownHook(self.__class__.__name__, hook_name)
215
210
        try:
216
211
            hook_lazy = getattr(hook, "hook_lazy")
217
212
        except AttributeError:
218
213
            raise errors.UnsupportedOperation(self.install_named_hook_lazy,
219
 
                                              self)
 
214
                self)
220
215
        else:
221
216
            hook_lazy(callable_module, callable_member, name)
222
217
        if name is not None:
236
231
        try:
237
232
            hook = self[hook_name]
238
233
        except KeyError:
239
 
            raise UnknownHook(self.__class__.__name__, hook_name)
 
234
            raise errors.UnknownHook(self.__class__.__name__, hook_name)
240
235
        try:
241
236
            # list hooks, old-style, not yet deprecated but less useful.
242
237
            hook.append(a_callable)
254
249
        try:
255
250
            hook = self[hook_name]
256
251
        except KeyError:
257
 
            raise UnknownHook(self.__class__.__name__, hook_name)
 
252
            raise errors.UnknownHook(self.__class__.__name__, hook_name)
258
253
        try:
259
254
            uninstall = getattr(hook, "uninstall")
260
255
        except AttributeError:
267
262
        self._callable_names[a_callable] = name
268
263
 
269
264
    def name_hook_lazy(self, callable_module, callable_member, callable_name):
270
 
        self._lazy_callable_names[(callable_module, callable_member)] = \
 
265
        self._lazy_callable_names[(callable_module, callable_member)]= \
271
266
            callable_name
272
267
 
273
268
 
309
304
        """
310
305
        strings = []
311
306
        strings.append(self.name)
312
 
        strings.append('~' * len(self.name))
 
307
        strings.append('~'*len(self.name))
313
308
        strings.append('')
314
309
        if self.introduced:
315
310
            introduced_string = _format_version_tuple(self.introduced)
321
316
            strings.append(gettext('Deprecated in: %s') % deprecated_string)
322
317
        strings.append('')
323
318
        strings.extend(textwrap.wrap(self.__doc__,
324
 
                                     break_long_words=False))
 
319
            break_long_words=False))
325
320
        strings.append('')
326
321
        return '\n'.join(strings)
327
322
 
328
323
    def __eq__(self, other):
329
 
        return (isinstance(other, type(self)) and other.__dict__ == self.__dict__)
 
324
        return (type(other) == type(self) and other.__dict__ == self.__dict__)
330
325
 
331
326
    def hook_lazy(self, callback_module, callback_member, callback_label):
332
327
        """Lazily register a callback to be called when this HookPoint fires.
338
333
            processing.
339
334
        """
340
335
        obj_getter = registry._LazyObjectGetter(callback_module,
341
 
                                                callback_member)
 
336
            callback_member)
342
337
        self._callbacks.append((obj_getter, callback_label))
343
338
 
344
339
    def hook(self, callback, callback_label):
390
385
 
391
386
 
392
387
_help_prefix = \
393
 
    """
 
388
"""
394
389
Hooks
395
390
=====
396
391
 
405
400
 
406
401
The class that contains each hook is given before the hooks it supplies. For
407
402
instance, BranchHooks as the class is the hooks class for
408
 
`breezy.branch.Branch.hooks`.
 
403
`bzrlib.branch.Branch.hooks`.
409
404
 
410
405
Each description also indicates whether the hook runs on the client (the
411
406
machine where bzr was invoked) or the server (the machine addressed by
422
417
 
423
418
"""
424
419
 
425
 
 
426
420
def hooks_help_text(topic):
427
421
    segments = [_help_prefix]
428
422
    for hook_key in sorted(known_hooks.keys()):
437
431
 
438
432
 
439
433
def install_lazy_named_hook(hookpoints_module, hookpoints_name, hook_name,
440
 
                            a_callable, name):
 
434
    a_callable, name):
441
435
    """Install a callable in to a hook lazily, and label it name.
442
436
 
443
437
    :param hookpoints_module: Module name of the hook points.