~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/registry.py

  • Committer: John Arbash Meinel
  • Date: 2006-09-09 17:30:34 UTC
  • mto: This revision was merged to the branch mainline in revision 2074.
  • Revision ID: john@arbash-meinel.com-20060909173034-fd1e7849d827dcf8
Add help and info parameters, and tests for them

Show diffs side-by-side

added added

removed removed

Lines of Context:
18
18
 
19
19
 
20
20
class Registry(object):
21
 
    """A class that registers objects to a name."""
 
21
    """A class that registers objects to a name.
 
22
 
 
23
    This is designed such that you can register objects in a lazy fashion,
 
24
    so that they can be imported later. While still having the help text
 
25
    available right away.
 
26
    """
22
27
 
23
28
    def __init__(self):
24
29
        """Create a new Registry."""
25
30
        self._default_key = None
26
31
        # Map from key => (is_lazy, info)
27
32
        self._dict = {}
 
33
        self._help_dict = {}
 
34
        self._info_dict = {}
28
35
 
29
 
    def register(self, key, object):
 
36
    def register(self, key, object, help=None, info=None,
 
37
                 override_existing=False):
30
38
        """Register a new object to a name.
31
39
 
32
40
        :param key: This is the key to use to request the object later.
33
41
        :param object: The object to register.
 
42
        :param help: Help text for this entry. This may be a string or
 
43
                a callable. If it is a callable, it should take a
 
44
                single parameter, which is the key that the help was
 
45
                registered under.
 
46
        :param info: More information for this entry. Registry.get_info()
 
47
                can be used to get this information. It is meant as an
 
48
                opaque storage location.
 
49
        :param override_existing: If True, replace the existing object
 
50
                with the new one. If False, if there is already something
 
51
                registered with the same key, raise a KeyError
34
52
        """
 
53
        if not override_existing:
 
54
            if key in self._dict:
 
55
                raise KeyError('Key %r already registered' % key)
35
56
        self._dict[key] = (False, object)
36
 
 
37
 
    __setitem__ = register
38
 
 
39
 
    def register_lazy(self, key, module_name, member_name):
 
57
        self._add_help_and_info(key, help=help, info=info)
 
58
 
 
59
    def register_lazy(self, key, module_name, member_name,
 
60
                      help=None, info=None,
 
61
                      override_existing=False):
40
62
        """Register a new object to be loaded on request.
41
63
 
42
64
        :param module_name: The python path to the module. Such as 'os.path'.
43
65
        :param member_name: The member of the module to return, if empty or 
44
66
                None get() will return the module itself.
 
67
        :param help: Help text for this entry. This may be a string or
 
68
                a callable.
 
69
        :param info: More information for this entry. Registry 
 
70
        :param override_existing: If True, replace the existing object
 
71
                with the new one. If False, if there is already something
 
72
                registered with the same key, raise a KeyError
45
73
        """
 
74
        if not override_existing:
 
75
            if key in self._dict:
 
76
                raise KeyError('Key %r already registered' % key)
46
77
        self._dict[key] = (True, (module_name, member_name))
 
78
        self._add_help_and_info(key, help=help, info=info)
 
79
 
 
80
    def _add_help_and_info(self, key, help=None, info=None):
 
81
        """Add the help and information about this key"""
 
82
        self._help_dict[key] = help
 
83
        self._info_dict[key] = info
47
84
 
48
85
    def get(self, key=None):
49
86
        """Return the object register()'ed to the given key.
58
95
            raised.
59
96
        :return: The previously registered object.
60
97
        """
61
 
        if key is None:
62
 
            if self.default_key is None:
63
 
                raise KeyError('Key is None, and no default key is set')
64
 
            else:
65
 
                key = self.default_key
66
 
        return self._get_one(key)
 
98
        return self._get_one(self._get_key_or_default(key))
 
99
 
 
100
    def _get_key_or_default(self, key=None):
 
101
        """Return either 'key' or the default key if key is None"""
 
102
        if key is not None:
 
103
            return key
 
104
        if self.default_key is None:
 
105
            raise KeyError('Key is None, and no default key is set')
 
106
        else:
 
107
            return self.default_key
67
108
 
68
109
    __getitem__ = get
69
110
 
88
129
        self._dict[key] = (False, obj)
89
130
        return obj
90
131
 
 
132
    def get_help(self, key=None):
 
133
        """Get the help text associated with the given key"""
 
134
        the_help = self._help_dict[self._get_key_or_default(key)]
 
135
        if callable(the_help):
 
136
            return the_help(key)
 
137
        return the_help
 
138
 
 
139
    def get_info(self, key=None):
 
140
        """Get the extra information associated with the given key"""
 
141
        return self._info_dict[self._get_key_or_default(key)]
 
142
 
91
143
    def remove(self, key):
92
144
        """Remove a registered entry.
93
145