/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: Jelmer Vernooij
  • Date: 2011-01-23 00:16:49 UTC
  • mto: (5622.4.1 uninstall-hook)
  • mto: This revision was merged to the branch mainline in revision 5669.
  • Revision ID: jelmer@samba.org-20110123001649-3580qqqxvu2odcj0
Put module/member information on Hooks, not individual hook points.

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=None, module=None,
235
 
                 member_name=None):
 
264
    def __init__(self, name, doc, introduced, deprecated=None, callbacks=None):
236
265
        """Create a HookPoint.
237
266
 
238
267
        :param name: The name of the hook, for clients to use when registering.
240
269
        :param introduced: When the hook was introduced (e.g. (0, 15)).
241
270
        :param deprecated: When the hook was deprecated, None for
242
271
            not-deprecated.
243
 
        :param module: The module from which this hook point should be loaded
244
 
            (used for lazy hooks)
245
 
        :param member_name: Name under which these hook points will be available.
246
 
            (used for lazy hooks)
247
272
        """
248
273
        self.name = name
249
274
        self.__doc__ = doc
250
275
        self.introduced = introduced
251
276
        self.deprecated = deprecated
252
 
        self._direct_callbacks = []
253
 
        self._module = module
254
 
        self._member_name = member_name
 
277
        if callbacks is None:
 
278
            self._callbacks = []
 
279
        else:
 
280
            self._callbacks = callbacks
255
281
 
256
282
    def docs(self):
257
283
        """Generate the documentation for this HookPoint.
277
303
        return '\n'.join(strings)
278
304
 
279
305
    def __eq__(self, other):
280
 
        return (type(other) == type(self) and 
281
 
            other.__dict__ == self.__dict__)
 
306
        return (type(other) == type(self) and other.__dict__ == self.__dict__)
282
307
 
283
308
    def hook_lazy(self, callback_module, callback_member, callback_label):
284
309
        """Lazily register a callback to be called when this HookPoint fires.
291
316
        """
292
317
        obj_getter = registry._LazyObjectGetter(callback_module,
293
318
            callback_member)
294
 
        self._direct_callbacks.append((obj_getter, callback_label))
 
319
        self._callbacks.append((obj_getter, callback_label))
295
320
 
296
321
    def hook(self, callback, callback_label):
297
322
        """Register a callback to be called when this HookPoint fires.
301
326
            processing.
302
327
        """
303
328
        obj_getter = registry._ObjectGetter(callback)
304
 
        self._direct_callbacks.append((obj_getter, callback_label))
305
 
 
306
 
    def _get_callbacks(self):
307
 
        ret = list(self._direct_callbacks)
308
 
        if self._module:
309
 
            ret += _lazy_hooks[
310
 
                (self._module, self._member_name, self.name)]
311
 
        return ret
 
329
        self._callbacks.append((obj_getter, callback_label))
312
330
 
313
331
    def __iter__(self):
314
 
        return (callback.get_obj() for callback, name in self._get_callbacks())
 
332
        return (callback.get_obj() for callback, name in self._callbacks)
315
333
 
316
334
    def __len__(self):
317
 
        return len(self._get_callbacks())
 
335
        return len(self._callbacks)
318
336
 
319
337
    def __repr__(self):
320
338
        strings = []
321
339
        strings.append("<%s(" % type(self).__name__)
322
340
        strings.append(self.name)
323
341
        strings.append("), callbacks=[")
324
 
        callbacks = self._get_callbacks()
 
342
        callbacks = self._callbacks
325
343
        for (callback, callback_name) in callbacks:
326
344
            strings.append(repr(callback.get_obj()))
327
345
            strings.append("(")