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
name = self.__class__.__name__
61
hook_docs.append(name)
62
hook_docs.append("="*len(name))
64
for hook_name in hook_names:
65
hook = self[hook_name]
67
hook_docs.append(hook.docs())
68
except AttributeError:
71
strings.append(hook_name)
72
strings.append("-" * len(hook_name))
74
strings.append("An old-style hook. For documentation see the __init__ "
75
"method of '%s'\n" % (name,))
76
hook_docs.extend(strings)
77
return "\n".join(hook_docs)
39
79
def get_hook_name(self, a_callable):
40
80
"""Get the name for a_callable for UI display.
73
self[hook_name].append(a_callable)
113
hook = self[hook_name]
75
115
raise errors.UnknownHook(self.__class__.__name__, hook_name)
117
# list hooks, old-style, not yet deprecated but less useful.
118
hook.append(a_callable)
119
except AttributeError:
120
hook.hook(a_callable, name)
76
121
if name is not None:
77
122
self.name_hook(a_callable, name)
79
124
def name_hook(self, a_callable, name):
80
125
"""Associate name with a_callable to show users what is running."""
81
126
self._callable_names[a_callable] = name
129
class HookPoint(object):
130
"""A single hook that clients can register to be called back when it fires.
132
:ivar name: The name of the hook.
133
:ivar introduced: A version tuple specifying what version the hook was
134
introduced in. None indicates an unknown version.
135
:ivar deprecated: A version tuple specifying what version the hook was
136
deprecated or superceded in. None indicates that the hook is not
137
superceded or deprecated. If the hook is superceded then the doc
138
should describe the recommended replacement hook to register for.
139
:ivar doc: The docs for using the hook.
142
def __init__(self, name, doc, introduced, deprecated):
143
"""Create a HookPoint.
145
:param name: The name of the hook, for clients to use when registering.
146
:param doc: The docs for the hook.
147
:param introduced: When the hook was introduced (e.g. (0, 15)).
148
:param deprecated: When the hook was deprecated, None for
153
self.introduced = introduced
154
self.deprecated = deprecated
156
self._callback_names = {}
159
"""Generate the documentation for this HookPoint.
161
:return: A string terminated in \n.
164
strings.append(self.name)
165
strings.append('-'*len(self.name))
168
introduced_string = _format_version_tuple(self.introduced)
170
introduced_string = 'unknown'
171
strings.append('Introduced in: %s' % introduced_string)
173
deprecated_string = _format_version_tuple(self.deprecated)
175
deprecated_string = 'Not deprecated'
176
strings.append('Deprecated in: %s' % deprecated_string)
178
strings.extend(textwrap.wrap(self.__doc__))
180
return '\n'.join(strings)
182
def hook(self, callback, callback_label):
183
"""Register a callback to be called when this HookPoint fires.
185
:param callback: The callable to use when this HookPoint fires.
186
:param callback_label: A label to show in the UI while this callback is
189
self._callbacks.append(callback)
190
self._callback_names[callback] = callback_label
193
return iter(self._callbacks)
197
strings.append("<%s(" % type(self).__name__)
198
strings.append(self.name)
199
strings.append("), callbacks=[")
200
for callback in self._callbacks:
201
strings.append(repr(callback))
203
strings.append(self._callback_names[callback])
205
if len(self._callbacks) == 1:
208
return ''.join(strings)