~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/hooks.py

  • Committer: INADA Naoki
  • Date: 2011-05-14 09:58:36 UTC
  • mfrom: (5830.2.22 i18n-msgextract)
  • mto: This revision was merged to the branch mainline in revision 5891.
  • Revision ID: songofacandy@gmail.com-20110514095836-38l651fp8bmb4bni
merge from i18n-msgextract

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
 
17
18
"""Support for plugin hooking logic."""
18
19
 
19
 
from __future__ import absolute_import
20
 
 
21
20
from bzrlib import (
22
21
    registry,
23
22
    symbol_versioning,
31
30
    errors,
32
31
    pyutils,
33
32
    )
34
 
from bzrlib.i18n import gettext
35
33
""")
36
34
 
37
35
 
72
70
 
73
71
_builtin_known_hooks = (
74
72
    ('bzrlib.branch', 'Branch.hooks', 'BranchHooks'),
75
 
    ('bzrlib.controldir', 'ControlDir.hooks', 'ControlDirHooks'),
 
73
    ('bzrlib.bzrdir', 'BzrDir.hooks', 'BzrDirHooks'),
76
74
    ('bzrlib.commands', 'Command.hooks', 'CommandHooks'),
77
 
    ('bzrlib.config', 'ConfigHooks', '_ConfigHooks'),
78
75
    ('bzrlib.info', 'hooks', 'InfoHooks'),
79
76
    ('bzrlib.lock', 'Lock.hooks', 'LockHooks'),
80
77
    ('bzrlib.merge', 'Merger.hooks', 'MergeHooks'),
83
80
    ('bzrlib.smart.client', '_SmartClient.hooks', 'SmartClientHooks'),
84
81
    ('bzrlib.smart.server', 'SmartTCPServer.hooks', 'SmartServerHooks'),
85
82
    ('bzrlib.status', 'hooks', 'StatusHooks'),
86
 
    ('bzrlib.transport', 'Transport.hooks', 'TransportHooks'),
87
83
    ('bzrlib.version_info_formats.format_rio', 'RioVersionInfoBuilder.hooks',
88
84
        'RioVersionInfoBuilderHooks'),
89
85
    ('bzrlib.merge_directive', 'BaseMergeDirective.hooks',
106
102
    return pyutils.get_named_object(module_name, member_name)
107
103
 
108
104
 
 
105
@symbol_versioning.deprecated_function(symbol_versioning.deprecated_in((2, 3)))
 
106
def known_hooks_key_to_parent_and_attribute(key):
 
107
    """See KnownHooksRegistry.key_to_parent_and_attribute."""
 
108
    return known_hooks.key_to_parent_and_attribute(key)
 
109
 
 
110
 
109
111
class Hooks(dict):
110
112
    """A dictionary mapping hook name to a list of callables.
111
113
 
123
125
        """
124
126
        dict.__init__(self)
125
127
        self._callable_names = {}
126
 
        self._lazy_callable_names = {}
127
128
        self._module = module
128
129
        self._member_name = member_name
129
130
 
147
148
                              deprecated=deprecated, callbacks=callbacks)
148
149
        self[name] = hookpoint
149
150
 
 
151
    @symbol_versioning.deprecated_method(symbol_versioning.deprecated_in((2, 4)))
 
152
    def create_hook(self, hook):
 
153
        """Create a hook which can have callbacks registered for it.
 
154
 
 
155
        :param hook: The hook to create. An object meeting the protocol of
 
156
            bzrlib.hooks.HookPoint. It's name is used as the key for future
 
157
            lookups.
 
158
        """
 
159
        if hook.name in self:
 
160
            raise errors.DuplicateKey(hook.name)
 
161
        self[hook.name] = hook
 
162
 
150
163
    def docs(self):
151
164
        """Generate the documentation for this Hooks instance.
152
165
 
181
194
        the code names are rarely meaningful for end users and this is not
182
195
        intended for debugging.
183
196
        """
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
 
 
 
197
        return self._callable_names.get(a_callable, "No hook name")
193
198
 
194
199
    def install_named_hook_lazy(self, hook_name, callable_module,
195
200
        callable_member, name):
214
219
                self)
215
220
        else:
216
221
            hook_lazy(callable_module, callable_member, name)
217
 
        if name is not None:
218
 
            self.name_hook_lazy(callable_module, callable_member, name)
219
222
 
220
223
    def install_named_hook(self, hook_name, a_callable, name):
221
224
        """Install a_callable in to the hook hook_name, and label it name.
253
256
        try:
254
257
            uninstall = getattr(hook, "uninstall")
255
258
        except AttributeError:
256
 
            raise errors.UnsupportedOperation(self.uninstall_named_hook, self)
 
259
            raise errors.UnsupportedOperation(self.install_named_hook_lazy,
 
260
                self)
257
261
        else:
258
262
            uninstall(label)
259
263
 
261
265
        """Associate name with a_callable to show users what is running."""
262
266
        self._callable_names[a_callable] = name
263
267
 
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
 
 
268
268
 
269
269
class HookPoint(object):
270
270
    """A single hook that clients can register to be called back when it fires.
310
310
            introduced_string = _format_version_tuple(self.introduced)
311
311
        else:
312
312
            introduced_string = 'unknown'
313
 
        strings.append(gettext('Introduced in: %s') % introduced_string)
 
313
        strings.append('Introduced in: %s' % introduced_string)
314
314
        if self.deprecated:
315
315
            deprecated_string = _format_version_tuple(self.deprecated)
316
 
            strings.append(gettext('Deprecated in: %s') % deprecated_string)
 
316
            strings.append('Deprecated in: %s' % deprecated_string)
317
317
        strings.append('')
318
318
        strings.extend(textwrap.wrap(self.__doc__,
319
319
            break_long_words=False))