~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 20:16:49 UTC
  • mfrom: (5622.2.11 lazy-hooks)
  • Revision ID: pqm@pqm.ubuntu.com-20110218201649-woj0pygeukoq6z6h
(jelmer) Allow installing hooks without importing the hook point. (Jelmer
 Vernooij)

Show diffs side-by-side

added added

removed removed

Lines of Context:
115
115
    FOO hook is triggered.
116
116
    """
117
117
 
118
 
    def __init__(self):
 
118
    def __init__(self, module=None, member_name=None):
 
119
        """Create a new hooks dictionary.
 
120
 
 
121
        :param module: The module from which this hooks dictionary should be loaded
 
122
            (used for lazy hooks)
 
123
        :param member_name: Name under which this hooks dictionary should be loaded.
 
124
            (used for lazy hooks)
 
125
        """
119
126
        dict.__init__(self)
120
127
        self._callable_names = {}
 
128
        self._module = module
 
129
        self._member_name = member_name
 
130
 
 
131
    def add_hook(self, name, doc, introduced, deprecated=None):
 
132
        """Add a hook point to this dictionary.
 
133
 
 
134
        :param name: The name of the hook, for clients to use when registering.
 
135
        :param doc: The docs for the hook.
 
136
        :param introduced: When the hook was introduced (e.g. (0, 15)).
 
137
        :param deprecated: When the hook was deprecated, None for
 
138
            not-deprecated.
 
139
        """
 
140
        if name in self:
 
141
            raise errors.DuplicateKey(name)
 
142
        if self._module:
 
143
            callbacks = _lazy_hooks.setdefault(
 
144
                (self._module, self._member_name, name), [])
 
145
        else:
 
146
            callbacks = None
 
147
        hookpoint = HookPoint(name=name, doc=doc, introduced=introduced,
 
148
                              deprecated=deprecated,
 
149
                              callbacks=callbacks)
 
150
        self[name] = hookpoint
121
151
 
122
152
    def create_hook(self, hook):
123
153
        """Create a hook which can have callbacks registered for it.
231
261
        should describe the recommended replacement hook to register for.
232
262
    """
233
263
 
234
 
    def __init__(self, name, doc, introduced, deprecated):
 
264
    def __init__(self, name, doc, introduced, deprecated=None, callbacks=None):
235
265
        """Create a HookPoint.
236
266
 
237
267
        :param name: The name of the hook, for clients to use when registering.
244
274
        self.__doc__ = doc
245
275
        self.introduced = introduced
246
276
        self.deprecated = deprecated
247
 
        self._callbacks = []
248
 
        self._callback_names = {}
 
277
        if callbacks is None:
 
278
            self._callbacks = []
 
279
        else:
 
280
            self._callbacks = callbacks
249
281
 
250
282
    def docs(self):
251
283
        """Generate the documentation for this HookPoint.
271
303
        return '\n'.join(strings)
272
304
 
273
305
    def __eq__(self, other):
274
 
        return (type(other) == type(self) and 
275
 
            other.__dict__ == self.__dict__)
 
306
        return (type(other) == type(self) and other.__dict__ == self.__dict__)
276
307
 
277
308
    def hook_lazy(self, callback_module, callback_member, callback_label):
278
309
        """Lazily register a callback to be called when this HookPoint fires.
285
316
        """
286
317
        obj_getter = registry._LazyObjectGetter(callback_module,
287
318
            callback_member)
288
 
        self._callbacks.append(obj_getter)
289
 
        if callback_label is not None:
290
 
            self._callback_names[obj_getter] = callback_label
 
319
        self._callbacks.append((obj_getter, callback_label))
291
320
 
292
321
    def hook(self, callback, callback_label):
293
322
        """Register a callback to be called when this HookPoint fires.
297
326
            processing.
298
327
        """
299
328
        obj_getter = registry._ObjectGetter(callback)
300
 
        self._callbacks.append(obj_getter)
301
 
        if callback_label is not None:
302
 
            self._callback_names[obj_getter] = callback_label
 
329
        self._callbacks.append((obj_getter, callback_label))
303
330
 
304
331
    def __iter__(self):
305
 
        return (callback.get_obj() for callback in self._callbacks)
 
332
        return (callback.get_obj() for callback, name in self._callbacks)
306
333
 
307
334
    def __len__(self):
308
335
        return len(self._callbacks)
313
340
        strings.append(self.name)
314
341
        strings.append("), callbacks=[")
315
342
        callbacks = self._callbacks
316
 
        for callback in callbacks:
 
343
        for (callback, callback_name) in callbacks:
317
344
            strings.append(repr(callback.get_obj()))
318
345
            strings.append("(")
319
 
            strings.append(self._callback_names[callback])
 
346
            strings.append(callback_name)
320
347
            strings.append("),")
321
348
        if len(callbacks) == 1:
322
349
            strings[-1] = ")"
380
407
        running.
381
408
    """
382
409
    key = (hookpoints_module, hookpoints_name, hook_name)
383
 
    _lazy_hooks.setdefault(key, []).append((a_callable, name))
 
410
    obj_getter = registry._ObjectGetter(a_callable)
 
411
    _lazy_hooks.setdefault(key, []).append((obj_getter, name))