~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/hooks.py

  • Committer: Canonical.com Patch Queue Manager
  • Date: 2011-02-18 18:13:45 UTC
  • mfrom: (5622.1.4 lazy-hooks-pt1)
  • Revision ID: pqm@pqm.ubuntu.com-20110218181345-on7vdomzesup6tos
(jelmer) Add some methods to allow lazy loading of hook callbacks. (Jelmer
 Vernooij)

Show diffs side-by-side

added added

removed removed

Lines of Context:
166
166
        """
167
167
        return self._callable_names.get(a_callable, "No hook name")
168
168
 
 
169
    def install_named_hook_lazy(self, hook_name, callable_module,
 
170
        callable_member, name):
 
171
        """Install a_callable in to the hook hook_name lazily, and label it.
 
172
 
 
173
        :param hook_name: A hook name. See the __init__ method for the complete
 
174
            list of hooks.
 
175
        :param callable_module: Name of the module in which the callable is
 
176
            present.
 
177
        :param callable_member: Member name of the callable.
 
178
        :param name: A name to associate the callable with, to show users what
 
179
            is running.
 
180
        """
 
181
        try:
 
182
            hook = self[hook_name]
 
183
        except KeyError:
 
184
            raise errors.UnknownHook(self.__class__.__name__, hook_name)
 
185
        try:
 
186
            hook_lazy = getattr(hook, "hook_lazy")
 
187
        except AttributeError:
 
188
            raise errors.UnsupportedOperation(self.install_named_hook_lazy,
 
189
                self)
 
190
        else:
 
191
            hook_lazy(callable_module, callable_member, name)
 
192
 
169
193
    def install_named_hook(self, hook_name, a_callable, name):
170
194
        """Install a_callable in to the hook hook_name, and label it name.
171
195
 
172
 
        :param hook_name: A hook name. See the __init__ method of BranchHooks
173
 
            for the complete list of hooks.
 
196
        :param hook_name: A hook name. See the __init__ method for the complete
 
197
            list of hooks.
174
198
        :param a_callable: The callable to be invoked when the hook triggers.
175
199
            The exact signature will depend on the hook - see the __init__
176
 
            method of BranchHooks for details on each hook.
 
200
            method for details on each hook.
177
201
        :param name: A name to associate a_callable with, to show users what is
178
202
            running.
179
203
        """
250
274
        return (type(other) == type(self) and 
251
275
            other.__dict__ == self.__dict__)
252
276
 
 
277
    def hook_lazy(self, callback_module, callback_member, callback_label):
 
278
        """Lazily register a callback to be called when this HookPoint fires.
 
279
 
 
280
        :param callback_module: Module of the callable to use when this
 
281
            HookPoint fires.
 
282
        :param callback_member: Member name of the callback.
 
283
        :param callback_label: A label to show in the UI while this callback is
 
284
            processing.
 
285
        """
 
286
        obj_getter = registry._LazyObjectGetter(callback_module,
 
287
            callback_member)
 
288
        self._callbacks.append(obj_getter)
 
289
        if callback_label is not None:
 
290
            self._callback_names[obj_getter] = callback_label
 
291
 
253
292
    def hook(self, callback, callback_label):
254
293
        """Register a callback to be called when this HookPoint fires.
255
294
 
257
296
        :param callback_label: A label to show in the UI while this callback is
258
297
            processing.
259
298
        """
260
 
        self._callbacks.append(callback)
 
299
        obj_getter = registry._ObjectGetter(callback)
 
300
        self._callbacks.append(obj_getter)
261
301
        if callback_label is not None:
262
 
            self._callback_names[callback] = callback_label
 
302
            self._callback_names[obj_getter] = callback_label
263
303
 
264
304
    def __iter__(self):
265
 
        return iter(self._callbacks)
 
305
        return (callback.get_obj() for callback in self._callbacks)
266
306
 
267
307
    def __len__(self):
268
308
        return len(self._callbacks)
272
312
        strings.append("<%s(" % type(self).__name__)
273
313
        strings.append(self.name)
274
314
        strings.append("), callbacks=[")
275
 
        for callback in self._callbacks:
276
 
            strings.append(repr(callback))
 
315
        callbacks = self._callbacks
 
316
        for callback in callbacks:
 
317
            strings.append(repr(callback.get_obj()))
277
318
            strings.append("(")
278
319
            strings.append(self._callback_names[callback])
279
320
            strings.append("),")
280
 
        if len(self._callbacks) == 1:
 
321
        if len(callbacks) == 1:
281
322
            strings[-1] = ")"
282
323
        strings.append("]>")
283
324
        return ''.join(strings)
322
363
        hooks = known_hooks_key_to_object(hook_key)
323
364
        segments.append(hooks.docs())
324
365
    return '\n'.join(segments)
 
366
 
 
367
 
 
368
_lazy_hooks = {}
 
369
 
 
370
 
 
371
def install_lazy_named_hook(hookpoints_module, hookpoints_name, hook_name,
 
372
    a_callable, name):
 
373
    """Install a callable in to a hook lazily, and label it name.
 
374
 
 
375
    :param hookpoints_module: Module name of the hook points.
 
376
    :param hookpoints_name: Name of the hook points.
 
377
    :param hook_name: A hook name.
 
378
    :param callable: a callable to call for the hook.
 
379
    :param name: A name to associate a_callable with, to show users what is
 
380
        running.
 
381
    """
 
382
    key = (hookpoints_module, hookpoints_name, hook_name)
 
383
    _lazy_hooks.setdefault(key, []).append((a_callable, name))