/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: Robert Collins
  • Date: 2009-03-12 02:43:46 UTC
  • mto: This revision was merged to the branch mainline in revision 4133.
  • Revision ID: robertc@robertcollins.net-20090312024346-jx3vpibkrwo1qxar
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.

Show diffs side-by-side

added added

removed removed

Lines of Context:
17
17
 
18
18
"""Support for plugin hooking logic."""
19
19
from bzrlib.lazy_import import lazy_import
 
20
from bzrlib import registry
20
21
from bzrlib.symbol_versioning import deprecated_method, one_five
21
22
lazy_import(globals(), """
22
23
import textwrap
28
29
""")
29
30
 
30
31
 
 
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
 
31
72
class Hooks(dict):
32
73
    """A dictionary mapping hook name to a list of callables.
33
74