/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: Martin Pool
  • Date: 2009-03-12 02:43:02 UTC
  • mfrom: (4121 +trunk)
  • mto: This revision was merged to the branch mainline in revision 4140.
  • Revision ID: mbp@sourcefrog.net-20090312024302-aynicfx1ywm0k9dl
merge trunk

Show diffs side-by-side

added added

removed removed

Lines of Context:
19
19
from bzrlib.lazy_import import lazy_import
20
20
from bzrlib.symbol_versioning import deprecated_method, one_five
21
21
lazy_import(globals(), """
 
22
import textwrap
 
23
 
22
24
from bzrlib import (
 
25
        _format_version_tuple,
23
26
        errors,
24
27
        )
25
28
""")
36
39
        dict.__init__(self)
37
40
        self._callable_names = {}
38
41
 
 
42
    def create_hook(self, hook):
 
43
        """Create a hook which can have callbacks registered for it.
 
44
 
 
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
 
47
            lookups.
 
48
        """
 
49
        if hook.name in self:
 
50
            raise errors.DuplicateKey(hook.name)
 
51
        self[hook.name] = hook
 
52
 
 
53
    def docs(self):
 
54
        """Generate the documentation for this Hooks instance.
 
55
 
 
56
        This introspects all the individual hooks and returns their docs as well.
 
57
        """
 
58
        hook_names = sorted(self.keys())
 
59
        hook_docs = []
 
60
        name = self.__class__.__name__
 
61
        hook_docs.append(name)
 
62
        hook_docs.append("="*len(name))
 
63
        hook_docs.append("")
 
64
        for hook_name in hook_names:
 
65
            hook = self[hook_name]
 
66
            try:
 
67
                hook_docs.append(hook.docs())
 
68
            except AttributeError:
 
69
                # legacy hook
 
70
                strings = []
 
71
                strings.append(hook_name)
 
72
                strings.append("-" * len(hook_name))
 
73
                strings.append("")
 
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)
 
78
 
39
79
    def get_hook_name(self, a_callable):
40
80
        """Get the name for a_callable for UI display.
41
81
 
70
110
            running.
71
111
        """
72
112
        try:
73
 
            self[hook_name].append(a_callable)
 
113
            hook = self[hook_name]
74
114
        except KeyError:
75
115
            raise errors.UnknownHook(self.__class__.__name__, hook_name)
 
116
        try:
 
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)
78
123
 
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
 
127
 
 
128
 
 
129
class HookPoint(object):
 
130
    """A single hook that clients can register to be called back when it fires.
 
131
 
 
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.
 
140
    """
 
141
 
 
142
    def __init__(self, name, doc, introduced, deprecated):
 
143
        """Create a HookPoint.
 
144
 
 
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
 
149
            not-deprecated.
 
150
        """
 
151
        self.name = name
 
152
        self.__doc__ = doc
 
153
        self.introduced = introduced
 
154
        self.deprecated = deprecated
 
155
        self._callbacks = []
 
156
        self._callback_names = {}
 
157
 
 
158
    def docs(self):
 
159
        """Generate the documentation for this HookPoint.
 
160
 
 
161
        :return: A string terminated in \n.
 
162
        """
 
163
        strings = []
 
164
        strings.append(self.name)
 
165
        strings.append('-'*len(self.name))
 
166
        strings.append('')
 
167
        if self.introduced:
 
168
            introduced_string = _format_version_tuple(self.introduced)
 
169
        else:
 
170
            introduced_string = 'unknown'
 
171
        strings.append('Introduced in: %s' % introduced_string)
 
172
        if self.deprecated:
 
173
            deprecated_string = _format_version_tuple(self.deprecated)
 
174
        else:
 
175
            deprecated_string = 'Not deprecated'
 
176
        strings.append('Deprecated in: %s' % deprecated_string)
 
177
        strings.append('')
 
178
        strings.extend(textwrap.wrap(self.__doc__))
 
179
        strings.append('')
 
180
        return '\n'.join(strings)
 
181
 
 
182
    def hook(self, callback, callback_label):
 
183
        """Register a callback to be called when this HookPoint fires.
 
184
 
 
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
 
187
            processing.
 
188
        """
 
189
        self._callbacks.append(callback)
 
190
        self._callback_names[callback] = callback_label
 
191
 
 
192
    def __iter__(self):
 
193
        return iter(self._callbacks)
 
194
 
 
195
    def __repr__(self):
 
196
        strings = []
 
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))
 
202
            strings.append("(")
 
203
            strings.append(self._callback_names[callback])
 
204
            strings.append("),")
 
205
        if len(self._callbacks) == 1:
 
206
            strings[-1] = ")"
 
207
        strings.append("]>")
 
208
        return ''.join(strings)