/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:
18
18
 
19
19
from __future__ import absolute_import
20
20
 
21
 
from . import (
22
 
    errors,
 
21
from bzrlib import (
23
22
    registry,
 
23
    symbol_versioning,
24
24
    )
25
 
from .lazy_import import lazy_import
 
25
from bzrlib.lazy_import import lazy_import
26
26
lazy_import(globals(), """
27
27
import textwrap
28
28
 
29
 
from breezy import (
 
29
from bzrlib import (
30
30
    _format_version_tuple,
 
31
    errors,
31
32
    pyutils,
32
33
    )
33
 
from breezy.i18n import gettext
 
34
from bzrlib.i18n import gettext
34
35
""")
35
36
 
36
37
 
37
 
class UnknownHook(errors.BzrError):
38
 
 
39
 
    _fmt = "The %(type)s hook '%(hook)s' is unknown in this version of breezy."
40
 
 
41
 
    def __init__(self, hook_type, hook_name):
42
 
        errors.BzrError.__init__(self)
43
 
        self.type = hook_type
44
 
        self.hook = hook_name
45
 
 
46
 
 
47
38
class KnownHooksRegistry(registry.Registry):
48
39
    # known_hooks registry contains
49
40
    # tuple of (module, member name) which is the hook point
61
52
        instance.
62
53
 
63
54
        This is useful for resetting/restoring all the hooks to a known state,
64
 
        as is done in breezy.tests.TestCase._clear_hooks.
 
55
        as is done in bzrlib.tests.TestCase._clear_hooks.
65
56
        """
66
57
        for key in self.keys():
67
58
            yield key, self.key_to_parent_and_attribute(key)
68
59
 
69
 
    def key_to_parent_and_attribute(self, key):
 
60
    def key_to_parent_and_attribute(self, (module_name, member_name)):
70
61
        """Convert a known_hooks key to a (parent_obj, attr) pair.
71
62
 
72
63
        :param key: A tuple (module_name, member_name) as found in the keys of
74
65
        :return: The parent_object of the hook and the name of the attribute on
75
66
            that parent object where the hook is kept.
76
67
        """
77
 
        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)
78
70
        return pyutils.get_named_object(parent_mod, parent_member), attr
79
71
 
80
72
 
81
73
_builtin_known_hooks = (
82
 
    ('breezy.branch', 'Branch.hooks', 'BranchHooks'),
83
 
    ('breezy.controldir', 'ControlDir.hooks', 'ControlDirHooks'),
84
 
    ('breezy.commands', 'Command.hooks', 'CommandHooks'),
85
 
    ('breezy.config', 'ConfigHooks', '_ConfigHooks'),
86
 
    ('breezy.info', 'hooks', 'InfoHooks'),
87
 
    ('breezy.lock', 'Lock.hooks', 'LockHooks'),
88
 
    ('breezy.merge', 'Merger.hooks', 'MergeHooks'),
89
 
    ('breezy.msgeditor', 'hooks', 'MessageEditorHooks'),
90
 
    ('breezy.mutabletree', 'MutableTree.hooks', 'MutableTreeHooks'),
91
 
    ('breezy.bzr.smart.client', '_SmartClient.hooks', 'SmartClientHooks'),
92
 
    ('breezy.bzr.smart.server', 'SmartTCPServer.hooks', 'SmartServerHooks'),
93
 
    ('breezy.status', 'hooks', 'StatusHooks'),
94
 
    ('breezy.transport', 'Transport.hooks', 'TransportHooks'),
95
 
    ('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',
96
88
        'RioVersionInfoBuilderHooks'),
97
 
    ('breezy.merge_directive', 'BaseMergeDirective.hooks',
 
89
    ('bzrlib.merge_directive', 'BaseMergeDirective.hooks',
98
90
        'MergeDirectiveHooks'),
99
91
    )
100
92
 
104
96
del _builtin_known_hooks, _hook_module, _hook_attribute, _hook_class
105
97
 
106
98
 
107
 
def known_hooks_key_to_object(key):
 
99
def known_hooks_key_to_object((module_name, member_name)):
108
100
    """Convert a known_hooks key to a object.
109
101
 
110
102
    :param key: A tuple (module_name, member_name) as found in the keys of
111
103
        the known_hooks registry.
112
104
    :return: The object this specifies.
113
105
    """
114
 
    return pyutils.get_named_object(*key)
 
106
    return pyutils.get_named_object(module_name, member_name)
115
107
 
116
108
 
117
109
class Hooks(dict):
214
206
        try:
215
207
            hook = self[hook_name]
216
208
        except KeyError:
217
 
            raise UnknownHook(self.__class__.__name__, hook_name)
 
209
            raise errors.UnknownHook(self.__class__.__name__, hook_name)
218
210
        try:
219
211
            hook_lazy = getattr(hook, "hook_lazy")
220
212
        except AttributeError:
239
231
        try:
240
232
            hook = self[hook_name]
241
233
        except KeyError:
242
 
            raise UnknownHook(self.__class__.__name__, hook_name)
 
234
            raise errors.UnknownHook(self.__class__.__name__, hook_name)
243
235
        try:
244
236
            # list hooks, old-style, not yet deprecated but less useful.
245
237
            hook.append(a_callable)
257
249
        try:
258
250
            hook = self[hook_name]
259
251
        except KeyError:
260
 
            raise UnknownHook(self.__class__.__name__, hook_name)
 
252
            raise errors.UnknownHook(self.__class__.__name__, hook_name)
261
253
        try:
262
254
            uninstall = getattr(hook, "uninstall")
263
255
        except AttributeError:
329
321
        return '\n'.join(strings)
330
322
 
331
323
    def __eq__(self, other):
332
 
        return (isinstance(other, type(self)) and other.__dict__ == self.__dict__)
 
324
        return (type(other) == type(self) and other.__dict__ == self.__dict__)
333
325
 
334
326
    def hook_lazy(self, callback_module, callback_member, callback_label):
335
327
        """Lazily register a callback to be called when this HookPoint fires.
408
400
 
409
401
The class that contains each hook is given before the hooks it supplies. For
410
402
instance, BranchHooks as the class is the hooks class for
411
 
`breezy.branch.Branch.hooks`.
 
403
`bzrlib.branch.Branch.hooks`.
412
404
 
413
405
Each description also indicates whether the hook runs on the client (the
414
406
machine where bzr was invoked) or the server (the machine addressed by