36
39
dict.__init__(self)
37
40
self._callable_names = {}
42
def create_hook(self, hook):
43
"""Create a hook which can have callbacks registered for it.
45
:param hook: The hook to create. An object meeting the protocol of
46
bzrlib.hooks.HookPoint. It's name is used as the key for future
50
raise errors.DuplicateKey(hook.name)
51
self[hook.name] = hook
54
"""Generate the documentation for this Hooks instance.
56
This introspects all the individual hooks and returns their docs as well.
58
hook_names = sorted(self.keys())
60
for hook_name in hook_names:
61
hook = self[hook_name]
63
hook_docs.append(hook.docs())
64
except AttributeError:
67
strings.append(hook_name)
68
strings.append("-" * len(hook_name))
70
strings.append("An old-style hook. For documentation see the __init__ "
71
"method of '%s'\n" % (self.__class__.__name__,))
72
hook_docs.extend(strings)
73
return "\n".join(hook_docs)
39
75
def get_hook_name(self, a_callable):
40
76
"""Get the name for a_callable for UI display.
73
self[hook_name].append(a_callable)
109
hook = self[hook_name]
75
111
raise errors.UnknownHook(self.__class__.__name__, hook_name)
113
# list hooks, old-style, not yet deprecated but less useful.
114
hook.append(a_callable)
115
except AttributeError:
116
hook.hook(a_callable, name)
76
117
if name is not None:
77
118
self.name_hook(a_callable, name)
79
120
def name_hook(self, a_callable, name):
80
121
"""Associate name with a_callable to show users what is running."""
81
122
self._callable_names[a_callable] = name
125
class HookPoint(object):
126
"""A single hook that clients can register to be called back when it fires.
128
:ivar name: The name of the hook.
129
:ivar introduced: A version tuple specifying what version the hook was
130
introduced in. None indicates an unknown version.
131
:ivar deprecated: A version tuple specifying what version the hook was
132
deprecated or superceded in. None indicates that the hook is not
133
superceded or deprecated. If the hook is superceded then the doc
134
should describe the recommended replacement hook to register for.
135
:ivar doc: The docs for using the hook.
138
def __init__(self, name, doc, introduced, deprecated):
139
"""Create a HookPoint.
141
:param name: The name of the hook, for clients to use when registering.
142
:param doc: The docs for the hook.
143
:param introduced: When the hook was introduced (e.g. (0, 15)).
144
:param deprecated: When the hook was deprecated, None for
149
self.introduced = introduced
150
self.deprecated = deprecated
152
self._callback_names = {}
155
"""Generate the documentation for this HookPoint.
157
:return: A string terminated in \n.
160
strings.append(self.name)
161
strings.append('-'*len(self.name))
164
introduced_string = _format_version_tuple(self.introduced)
166
introduced_string = 'unknown'
167
strings.append('Introduced in: %s' % introduced_string)
169
deprecated_string = _format_version_tuple(self.deprecated)
171
deprecated_string = 'Not deprecated'
172
strings.append('Deprecated in: %s' % deprecated_string)
174
strings.extend(textwrap.wrap(self.__doc__))
176
return '\n'.join(strings)
178
def hook(self, callback, callback_label):
179
"""Register a callback to be called when this HookPoint fires.
181
:param callback: The callable to use when this HookPoint fires.
182
:param callback_label: A label to show in the UI while this callback is
185
self._callbacks.append(callback)
186
self._callback_names[callback] = callback_label
189
return iter(self._callbacks)
193
strings.append("<%s(" % type(self).__name__)
194
strings.append(self.name)
195
strings.append("), callbacks=[")
196
for callback in self._callbacks:
197
strings.append(repr(callback))
199
strings.append(self._callback_names[callback])
201
if len(self._callbacks) == 1:
204
return ''.join(strings)