bzr branch
http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar
3331.3.6
by Martin Pool
merge trunk |
1 |
# Copyright (C) 2007, 2008 Canonical Ltd
|
2370.4.1
by Robert Collins
New SmartServer hooks facility. There are two initial hooks documented |
2 |
#
|
3 |
# This program is free software; you can redistribute it and/or modify
|
|
4 |
# it under the terms of the GNU General Public License as published by
|
|
5 |
# the Free Software Foundation; either version 2 of the License, or
|
|
6 |
# (at your option) any later version.
|
|
7 |
#
|
|
8 |
# This program is distributed in the hope that it will be useful,
|
|
9 |
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
10 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
11 |
# GNU General Public License for more details.
|
|
12 |
#
|
|
13 |
# You should have received a copy of the GNU General Public License
|
|
14 |
# along with this program; if not, write to the Free Software
|
|
15 |
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
|
16 |
||
17 |
||
18 |
"""Support for plugin hooking logic."""
|
|
19 |
from bzrlib.lazy_import import lazy_import |
|
4119.3.1
by Robert Collins
Create a single registry of all Hooks classes, removing the test suite knowledge of such hooks and allowing plugins to sensibly and safely define new hooks. |
20 |
from bzrlib import registry |
3256.2.30
by Daniel Watkins
Updated deprecation warnings and tests. |
21 |
from bzrlib.symbol_versioning import deprecated_method, one_five |
2370.4.1
by Robert Collins
New SmartServer hooks facility. There are two initial hooks documented |
22 |
lazy_import(globals(), """ |
4098.2.1
by Robert Collins
Allow self documenting hooks. |
23 |
import textwrap
|
24 |
||
2370.4.1
by Robert Collins
New SmartServer hooks facility. There are two initial hooks documented |
25 |
from bzrlib import (
|
4098.2.1
by Robert Collins
Allow self documenting hooks. |
26 |
_format_version_tuple,
|
2370.4.1
by Robert Collins
New SmartServer hooks facility. There are two initial hooks documented |
27 |
errors,
|
28 |
)
|
|
29 |
""") |
|
30 |
||
31 |
||
4119.3.1
by Robert Collins
Create a single registry of all Hooks classes, removing the test suite knowledge of such hooks and allowing plugins to sensibly and safely define new hooks. |
32 |
known_hooks = registry.Registry() |
33 |
known_hooks.register_lazy(('bzrlib.branch', 'Branch.hooks'), 'bzrlib.branch', |
|
34 |
'BranchHooks') |
|
35 |
known_hooks.register_lazy(('bzrlib.commands', 'Command.hooks'), |
|
36 |
'bzrlib.commands', 'CommandHooks') |
|
37 |
known_hooks.register_lazy(('bzrlib.mutabletree', 'MutableTree.hooks'), |
|
38 |
'bzrlib.mutabletree', 'MutableTreeHooks') |
|
39 |
known_hooks.register_lazy(('bzrlib.smart.client', '_SmartClient.hooks'), |
|
40 |
'bzrlib.smart.client', 'SmartClientHooks') |
|
41 |
known_hooks.register_lazy(('bzrlib.smart.server', 'SmartTCPServer.hooks'), |
|
42 |
'bzrlib.smart.server', 'SmartServerHooks') |
|
43 |
||
44 |
||
45 |
def known_hooks_key_to_object((module_name, member_name)): |
|
46 |
"""Convert a known_hooks key to a object. |
|
47 |
||
48 |
:param key: A tuple (module_name, member_name) as found in the keys of
|
|
49 |
the known_hooks registry.
|
|
50 |
:return: The object this specifies.
|
|
51 |
"""
|
|
52 |
return registry._LazyObjectGetter(module_name, member_name).get_obj() |
|
53 |
||
54 |
||
55 |
def known_hooks_key_to_parent_and_attribute((module_name, member_name)): |
|
56 |
"""Convert a known_hooks key to a object. |
|
57 |
||
58 |
:param key: A tuple (module_name, member_name) as found in the keys of
|
|
59 |
the known_hooks registry.
|
|
60 |
:return: The object this specifies.
|
|
61 |
"""
|
|
62 |
member_list = member_name.rsplit('.', 1) |
|
63 |
if len(member_list) == 2: |
|
64 |
parent_name, attribute = member_list |
|
65 |
else: |
|
66 |
parent_name = None |
|
67 |
attribute = member_name |
|
68 |
parent = known_hooks_key_to_object((module_name, parent_name)) |
|
69 |
return parent, attribute |
|
70 |
||
71 |
||
2370.4.1
by Robert Collins
New SmartServer hooks facility. There are two initial hooks documented |
72 |
class Hooks(dict): |
73 |
"""A dictionary mapping hook name to a list of callables. |
|
3943.8.1
by Marius Kruger
remove all trailing whitespace from bzr source |
74 |
|
2370.4.1
by Robert Collins
New SmartServer hooks facility. There are two initial hooks documented |
75 |
e.g. ['FOO'] Is the list of items to be called when the
|
76 |
FOO hook is triggered.
|
|
77 |
"""
|
|
78 |
||
2553.1.1
by Robert Collins
Give Hooks names. |
79 |
def __init__(self): |
80 |
dict.__init__(self) |
|
81 |
self._callable_names = {} |
|
82 |
||
4098.2.1
by Robert Collins
Allow self documenting hooks. |
83 |
def create_hook(self, hook): |
84 |
"""Create a hook which can have callbacks registered for it. |
|
85 |
||
86 |
:param hook: The hook to create. An object meeting the protocol of
|
|
4098.2.2
by Robert Collins
Review feedback. |
87 |
bzrlib.hooks.HookPoint. It's name is used as the key for future
|
4098.2.1
by Robert Collins
Allow self documenting hooks. |
88 |
lookups.
|
89 |
"""
|
|
90 |
if hook.name in self: |
|
91 |
raise errors.DuplicateKey(hook.name) |
|
92 |
self[hook.name] = hook |
|
93 |
||
94 |
def docs(self): |
|
95 |
"""Generate the documentation for this Hooks instance. |
|
96 |
||
97 |
This introspects all the individual hooks and returns their docs as well.
|
|
98 |
"""
|
|
99 |
hook_names = sorted(self.keys()) |
|
100 |
hook_docs = [] |
|
4108.3.1
by Robert Collins
Include the Hooks class in the Hooks docs. |
101 |
name = self.__class__.__name__ |
102 |
hook_docs.append(name) |
|
103 |
hook_docs.append("="*len(name)) |
|
104 |
hook_docs.append("") |
|
4098.2.1
by Robert Collins
Allow self documenting hooks. |
105 |
for hook_name in hook_names: |
106 |
hook = self[hook_name] |
|
107 |
try: |
|
108 |
hook_docs.append(hook.docs()) |
|
109 |
except AttributeError: |
|
110 |
# legacy hook
|
|
111 |
strings = [] |
|
112 |
strings.append(hook_name) |
|
113 |
strings.append("-" * len(hook_name)) |
|
114 |
strings.append("") |
|
115 |
strings.append("An old-style hook. For documentation see the __init__ " |
|
4108.3.1
by Robert Collins
Include the Hooks class in the Hooks docs. |
116 |
"method of '%s'\n" % (name,)) |
4098.2.1
by Robert Collins
Allow self documenting hooks. |
117 |
hook_docs.extend(strings) |
118 |
return "\n".join(hook_docs) |
|
119 |
||
2553.1.1
by Robert Collins
Give Hooks names. |
120 |
def get_hook_name(self, a_callable): |
121 |
"""Get the name for a_callable for UI display. |
|
122 |
||
123 |
If no name has been registered, the string 'No hook name' is returned.
|
|
2553.1.3
by Robert Collins
Increase docs in response to review feedback. |
124 |
We use a fixed string rather than repr or the callables module because
|
3943.8.1
by Marius Kruger
remove all trailing whitespace from bzr source |
125 |
the code names are rarely meaningful for end users and this is not
|
2553.1.3
by Robert Collins
Increase docs in response to review feedback. |
126 |
intended for debugging.
|
2553.1.1
by Robert Collins
Give Hooks names. |
127 |
"""
|
128 |
return self._callable_names.get(a_callable, "No hook name") |
|
129 |
||
3256.2.30
by Daniel Watkins
Updated deprecation warnings and tests. |
130 |
@deprecated_method(one_five) |
3256.2.8
by Daniel Watkins
Alphabetised hooks.py. |
131 |
def install_hook(self, hook_name, a_callable): |
132 |
"""Install a_callable in to the hook hook_name. |
|
133 |
||
134 |
:param hook_name: A hook name. See the __init__ method of BranchHooks
|
|
135 |
for the complete list of hooks.
|
|
136 |
:param a_callable: The callable to be invoked when the hook triggers.
|
|
3943.8.1
by Marius Kruger
remove all trailing whitespace from bzr source |
137 |
The exact signature will depend on the hook - see the __init__
|
3256.2.8
by Daniel Watkins
Alphabetised hooks.py. |
138 |
method of BranchHooks for details on each hook.
|
139 |
"""
|
|
3256.2.11
by Daniel Watkins
Modified install_hook to call install_named_hook, as suggested by Aaron. |
140 |
self.install_named_hook(hook_name, a_callable, None) |
3256.2.8
by Daniel Watkins
Alphabetised hooks.py. |
141 |
|
3256.2.5
by Daniel Watkins
Added install_named_hook. |
142 |
def install_named_hook(self, hook_name, a_callable, name): |
143 |
"""Install a_callable in to the hook hook_name, and label it name. |
|
2370.4.1
by Robert Collins
New SmartServer hooks facility. There are two initial hooks documented |
144 |
|
145 |
:param hook_name: A hook name. See the __init__ method of BranchHooks
|
|
146 |
for the complete list of hooks.
|
|
147 |
:param a_callable: The callable to be invoked when the hook triggers.
|
|
3943.8.1
by Marius Kruger
remove all trailing whitespace from bzr source |
148 |
The exact signature will depend on the hook - see the __init__
|
2370.4.1
by Robert Collins
New SmartServer hooks facility. There are two initial hooks documented |
149 |
method of BranchHooks for details on each hook.
|
3256.2.3
by Daniel Watkins
Added docs. |
150 |
:param name: A name to associate a_callable with, to show users what is
|
151 |
running.
|
|
2370.4.1
by Robert Collins
New SmartServer hooks facility. There are two initial hooks documented |
152 |
"""
|
153 |
try: |
|
4098.2.1
by Robert Collins
Allow self documenting hooks. |
154 |
hook = self[hook_name] |
2370.4.1
by Robert Collins
New SmartServer hooks facility. There are two initial hooks documented |
155 |
except KeyError: |
156 |
raise errors.UnknownHook(self.__class__.__name__, hook_name) |
|
4098.2.1
by Robert Collins
Allow self documenting hooks. |
157 |
try: |
158 |
# list hooks, old-style, not yet deprecated but less useful.
|
|
159 |
hook.append(a_callable) |
|
160 |
except AttributeError: |
|
161 |
hook.hook(a_callable, name) |
|
3256.2.10
by Daniel Watkins
Tightened exception scope, as suggested by Aaron. |
162 |
if name is not None: |
163 |
self.name_hook(a_callable, name) |
|
2553.1.1
by Robert Collins
Give Hooks names. |
164 |
|
165 |
def name_hook(self, a_callable, name): |
|
166 |
"""Associate name with a_callable to show users what is running.""" |
|
167 |
self._callable_names[a_callable] = name |
|
4098.2.1
by Robert Collins
Allow self documenting hooks. |
168 |
|
169 |
||
4098.2.2
by Robert Collins
Review feedback. |
170 |
class HookPoint(object): |
4098.2.1
by Robert Collins
Allow self documenting hooks. |
171 |
"""A single hook that clients can register to be called back when it fires. |
172 |
||
173 |
:ivar name: The name of the hook.
|
|
174 |
:ivar introduced: A version tuple specifying what version the hook was
|
|
175 |
introduced in. None indicates an unknown version.
|
|
176 |
:ivar deprecated: A version tuple specifying what version the hook was
|
|
177 |
deprecated or superceded in. None indicates that the hook is not
|
|
178 |
superceded or deprecated. If the hook is superceded then the doc
|
|
179 |
should describe the recommended replacement hook to register for.
|
|
180 |
:ivar doc: The docs for using the hook.
|
|
181 |
"""
|
|
182 |
||
183 |
def __init__(self, name, doc, introduced, deprecated): |
|
4098.2.2
by Robert Collins
Review feedback. |
184 |
"""Create a HookPoint. |
4098.2.3
by Robert Collins
White space difference-of-opinion. |
185 |
|
4098.2.1
by Robert Collins
Allow self documenting hooks. |
186 |
:param name: The name of the hook, for clients to use when registering.
|
187 |
:param doc: The docs for the hook.
|
|
188 |
:param introduced: When the hook was introduced (e.g. (0, 15)).
|
|
189 |
:param deprecated: When the hook was deprecated, None for
|
|
190 |
not-deprecated.
|
|
191 |
"""
|
|
192 |
self.name = name |
|
193 |
self.__doc__ = doc |
|
194 |
self.introduced = introduced |
|
195 |
self.deprecated = deprecated |
|
196 |
self._callbacks = [] |
|
197 |
self._callback_names = {} |
|
198 |
||
199 |
def docs(self): |
|
4098.2.2
by Robert Collins
Review feedback. |
200 |
"""Generate the documentation for this HookPoint. |
4098.2.3
by Robert Collins
White space difference-of-opinion. |
201 |
|
4098.2.1
by Robert Collins
Allow self documenting hooks. |
202 |
:return: A string terminated in \n.
|
203 |
"""
|
|
204 |
strings = [] |
|
205 |
strings.append(self.name) |
|
206 |
strings.append('-'*len(self.name)) |
|
207 |
strings.append('') |
|
208 |
if self.introduced: |
|
209 |
introduced_string = _format_version_tuple(self.introduced) |
|
210 |
else: |
|
211 |
introduced_string = 'unknown' |
|
212 |
strings.append('Introduced in: %s' % introduced_string) |
|
213 |
if self.deprecated: |
|
214 |
deprecated_string = _format_version_tuple(self.deprecated) |
|
215 |
else: |
|
216 |
deprecated_string = 'Not deprecated' |
|
217 |
strings.append('Deprecated in: %s' % deprecated_string) |
|
218 |
strings.append('') |
|
219 |
strings.extend(textwrap.wrap(self.__doc__)) |
|
220 |
strings.append('') |
|
221 |
return '\n'.join(strings) |
|
222 |
||
223 |
def hook(self, callback, callback_label): |
|
4098.2.2
by Robert Collins
Review feedback. |
224 |
"""Register a callback to be called when this HookPoint fires. |
4098.2.1
by Robert Collins
Allow self documenting hooks. |
225 |
|
4098.2.2
by Robert Collins
Review feedback. |
226 |
:param callback: The callable to use when this HookPoint fires.
|
4098.2.1
by Robert Collins
Allow self documenting hooks. |
227 |
:param callback_label: A label to show in the UI while this callback is
|
228 |
processing.
|
|
229 |
"""
|
|
230 |
self._callbacks.append(callback) |
|
231 |
self._callback_names[callback] = callback_label |
|
232 |
||
233 |
def __iter__(self): |
|
234 |
return iter(self._callbacks) |
|
235 |
||
236 |
def __repr__(self): |
|
237 |
strings = [] |
|
4098.2.2
by Robert Collins
Review feedback. |
238 |
strings.append("<%s(" % type(self).__name__) |
4098.2.1
by Robert Collins
Allow self documenting hooks. |
239 |
strings.append(self.name) |
240 |
strings.append("), callbacks=[") |
|
241 |
for callback in self._callbacks: |
|
242 |
strings.append(repr(callback)) |
|
243 |
strings.append("(") |
|
244 |
strings.append(self._callback_names[callback]) |
|
245 |
strings.append("),") |
|
246 |
if len(self._callbacks) == 1: |
|
247 |
strings[-1] = ")" |
|
248 |
strings.append("]>") |
|
249 |
return ''.join(strings) |