/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 breezy/hooks.py

  • Committer: Breezy landing bot
  • Author(s): Colin Watson
  • Date: 2020-11-16 21:47:08 UTC
  • mfrom: (7521.1.1 remove-lp-workaround)
  • Revision ID: breezy.the.bot@gmail.com-20201116214708-jos209mgxi41oy15
Remove breezy.git workaround for bazaar.launchpad.net.

Merged from https://code.launchpad.net/~cjwatson/brz/remove-lp-workaround/+merge/393710

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