~bzr-pqm/bzr/bzr.dev

3882.1.2 by Martin Pool
Add Registry.items()
1
# Copyright (C) 2006, 2008 Canonical Ltd
1911.4.1 by John Arbash Meinel
Creating a factory that can load modules on demand.
2
#
3
# This program is free software; you can redistribute it and/or modify
4
# it under the terms of the GNU General Public License as published by
5
# the Free Software Foundation; either version 2 of the License, or
6
# (at your option) any later version.
7
#
8
# This program is distributed in the hope that it will be useful,
9
# but WITHOUT ANY WARRANTY; without even the implied warranty of
10
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11
# GNU General Public License for more details.
12
#
13
# You should have received a copy of the GNU General Public License
14
# along with this program; if not, write to the Free Software
4183.7.1 by Sabin Iacob
update FSF mailing address
15
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
1911.4.1 by John Arbash Meinel
Creating a factory that can load modules on demand.
16
1912.5.2 by Adeodato Simó
Morph John's LazyFactory into a generalized Registry class, and
17
"""Classes to provide name-to-object registry-like support."""
18
19
1911.4.12 by John Arbash Meinel
Use helper objects to handle lazy importing
20
class _ObjectGetter(object):
1911.4.15 by John Arbash Meinel
Updated HACKING and docstrings per Martin's suggestions
21
    """Maintain a reference to an object, and return the object on request.
1911.4.12 by John Arbash Meinel
Use helper objects to handle lazy importing
22
23
    This is used by Registry to make plain objects function similarly
24
    to lazily imported objects.
25
26
    Objects can be any sort of python object (class, function, module,
27
    instance, etc)
28
    """
29
30
    __slots__ = ['_obj']
31
32
    def __init__(self, obj):
33
        self._obj = obj
34
35
    def get_obj(self):
36
        """Get the object that was saved at creation time"""
37
        return self._obj
38
39
40
class _LazyObjectGetter(_ObjectGetter):
1911.4.16 by John Arbash Meinel
cleanup doc strings as recommended by Aaron and Martin
41
    """Keep a record of a possible object.
42
43
    When requested, load and return it.
44
    """
1911.4.12 by John Arbash Meinel
Use helper objects to handle lazy importing
45
46
    __slots__ = ['_module_name', '_member_name', '_imported']
47
48
    def __init__(self, module_name, member_name):
49
        self._module_name = module_name
50
        self._member_name = member_name
51
        self._imported = False
52
        super(_LazyObjectGetter, self).__init__(None)
53
54
    def get_obj(self):
55
        """Get the referenced object.
56
1911.4.16 by John Arbash Meinel
cleanup doc strings as recommended by Aaron and Martin
57
        Upon first request, the object will be imported. Future requests will
58
        return the imported object.
1911.4.12 by John Arbash Meinel
Use helper objects to handle lazy importing
59
        """
60
        if not self._imported:
61
            self._do_import()
62
        return super(_LazyObjectGetter, self).get_obj()
63
64
    def _do_import(self):
4119.3.1 by Robert Collins
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.
65
        if self._member_name:
66
            segments = self._member_name.split('.')
67
            names = segments[0:1]
68
        else:
69
            names = [self._member_name]
70
        obj = __import__(self._module_name, globals(), locals(), names)
71
        if self._member_name:
72
            for segment in segments:
73
                obj = getattr(obj, segment)
1911.4.12 by John Arbash Meinel
Use helper objects to handle lazy importing
74
        self._obj = obj
75
        self._imported = True
76
2745.5.3 by Robert Collins
* Move transport logging into a new transport class
77
    def __repr__(self):
78
        return "<%s.%s object at %x, module=%r attribute=%r>" % (
79
            self.__class__.__module__, self.__class__.__name__, id(self),
80
            self._module_name, self._member_name)
81
1911.4.12 by John Arbash Meinel
Use helper objects to handle lazy importing
82
1912.5.2 by Adeodato Simó
Morph John's LazyFactory into a generalized Registry class, and
83
class Registry(object):
1911.4.7 by John Arbash Meinel
Add help and info parameters, and tests for them
84
    """A class that registers objects to a name.
85
1911.4.15 by John Arbash Meinel
Updated HACKING and docstrings per Martin's suggestions
86
    There are many places that want to collect related objects and access them
87
    by a key. This class is designed to allow registering the mapping from key
88
    to object. It goes one step further, and allows registering a name to a
89
    hypothetical object which has not been imported yet. It also supports
90
    adding additional information at registration time so that decisions can be
91
    made without having to import the object (which may be expensive).
92
93
    The functions 'get', 'get_info', and 'get_help' also support a
94
    'default_key' (settable through my_registry.default_key = XXX, XXX must
95
    already be registered.) Calling my_registry.get() or my_registry.get(None),
96
    will return the entry for the default key.
1911.4.7 by John Arbash Meinel
Add help and info parameters, and tests for them
97
    """
1911.4.1 by John Arbash Meinel
Creating a factory that can load modules on demand.
98
1911.4.6 by John Arbash Meinel
first_is_default was not a good design
99
    def __init__(self):
100
        """Create a new Registry."""
1912.5.2 by Adeodato Simó
Morph John's LazyFactory into a generalized Registry class, and
101
        self._default_key = None
1911.4.5 by John Arbash Meinel
Make a Registry look more like a dict, and allow anyone to register stuff lazily.
102
        # Map from key => (is_lazy, info)
1911.4.1 by John Arbash Meinel
Creating a factory that can load modules on demand.
103
        self._dict = {}
1911.4.7 by John Arbash Meinel
Add help and info parameters, and tests for them
104
        self._help_dict = {}
105
        self._info_dict = {}
1911.4.1 by John Arbash Meinel
Creating a factory that can load modules on demand.
106
1911.4.12 by John Arbash Meinel
Use helper objects to handle lazy importing
107
    def register(self, key, obj, help=None, info=None,
1911.4.7 by John Arbash Meinel
Add help and info parameters, and tests for them
108
                 override_existing=False):
1912.5.2 by Adeodato Simó
Morph John's LazyFactory into a generalized Registry class, and
109
        """Register a new object to a name.
110
111
        :param key: This is the key to use to request the object later.
1911.4.12 by John Arbash Meinel
Use helper objects to handle lazy importing
112
        :param obj: The object to register.
1911.4.7 by John Arbash Meinel
Add help and info parameters, and tests for them
113
        :param help: Help text for this entry. This may be a string or
1911.4.9 by John Arbash Meinel
A help callable should take the registry as the first parameter
114
                a callable. If it is a callable, it should take two
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
115
                parameters (registry, key): this registry and the key that
1911.4.16 by John Arbash Meinel
cleanup doc strings as recommended by Aaron and Martin
116
                the help was registered under.
1911.4.7 by John Arbash Meinel
Add help and info parameters, and tests for them
117
        :param info: More information for this entry. Registry.get_info()
1911.4.16 by John Arbash Meinel
cleanup doc strings as recommended by Aaron and Martin
118
                can be used to get this information. Registry treats this as an
119
                opaque storage location (it is defined by the caller).
120
        :param override_existing: Raise KeyErorr if False and something has
121
                already been registered for that key. If True, ignore if there
122
                is an existing key (always register the new value).
1912.5.2 by Adeodato Simó
Morph John's LazyFactory into a generalized Registry class, and
123
        """
1911.4.7 by John Arbash Meinel
Add help and info parameters, and tests for them
124
        if not override_existing:
125
            if key in self._dict:
126
                raise KeyError('Key %r already registered' % key)
1911.4.12 by John Arbash Meinel
Use helper objects to handle lazy importing
127
        self._dict[key] = _ObjectGetter(obj)
1911.4.7 by John Arbash Meinel
Add help and info parameters, and tests for them
128
        self._add_help_and_info(key, help=help, info=info)
129
130
    def register_lazy(self, key, module_name, member_name,
131
                      help=None, info=None,
132
                      override_existing=False):
1911.4.5 by John Arbash Meinel
Make a Registry look more like a dict, and allow anyone to register stuff lazily.
133
        """Register a new object to be loaded on request.
134
135
        :param module_name: The python path to the module. Such as 'os.path'.
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
136
        :param member_name: The member of the module to return.  If empty or
1911.4.16 by John Arbash Meinel
cleanup doc strings as recommended by Aaron and Martin
137
                None, get() will return the module itself.
1911.4.7 by John Arbash Meinel
Add help and info parameters, and tests for them
138
        :param help: Help text for this entry. This may be a string or
139
                a callable.
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
140
        :param info: More information for this entry. Registry
1911.4.7 by John Arbash Meinel
Add help and info parameters, and tests for them
141
        :param override_existing: If True, replace the existing object
142
                with the new one. If False, if there is already something
143
                registered with the same key, raise a KeyError
1911.4.5 by John Arbash Meinel
Make a Registry look more like a dict, and allow anyone to register stuff lazily.
144
        """
1911.4.7 by John Arbash Meinel
Add help and info parameters, and tests for them
145
        if not override_existing:
146
            if key in self._dict:
147
                raise KeyError('Key %r already registered' % key)
1911.4.12 by John Arbash Meinel
Use helper objects to handle lazy importing
148
        self._dict[key] = _LazyObjectGetter(module_name, member_name)
1911.4.7 by John Arbash Meinel
Add help and info parameters, and tests for them
149
        self._add_help_and_info(key, help=help, info=info)
150
151
    def _add_help_and_info(self, key, help=None, info=None):
152
        """Add the help and information about this key"""
153
        self._help_dict[key] = help
154
        self._info_dict[key] = info
1912.5.2 by Adeodato Simó
Morph John's LazyFactory into a generalized Registry class, and
155
1912.5.6 by Adeodato Simó
Kill fallback_key parameter in Registry.get(), as discussed with John.
156
    def get(self, key=None):
157
        """Return the object register()'ed to the given key.
158
1911.4.5 by John Arbash Meinel
Make a Registry look more like a dict, and allow anyone to register stuff lazily.
159
        May raise ImportError if the object was registered lazily and
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
160
        there are any problems, or AttributeError if the module does not
1911.4.5 by John Arbash Meinel
Make a Registry look more like a dict, and allow anyone to register stuff lazily.
161
        have the supplied member.
162
1912.5.6 by Adeodato Simó
Kill fallback_key parameter in Registry.get(), as discussed with John.
163
        :param key: The key to obtain the object for. If no object has been
164
            registered to that key, the object registered for self.default_key
165
            will be returned instead, if it exists. Otherwise KeyError will be
166
            raised.
1912.5.2 by Adeodato Simó
Morph John's LazyFactory into a generalized Registry class, and
167
        :return: The previously registered object.
1911.4.15 by John Arbash Meinel
Updated HACKING and docstrings per Martin's suggestions
168
        :raises ImportError: If the object was registered lazily, and there are
169
            problems during import.
170
        :raises AttributeError: If registered lazily, and the module does not
171
            contain the registered member.
1912.5.2 by Adeodato Simó
Morph John's LazyFactory into a generalized Registry class, and
172
        """
1911.4.12 by John Arbash Meinel
Use helper objects to handle lazy importing
173
        return self._dict[self._get_key_or_default(key)].get_obj()
1911.4.7 by John Arbash Meinel
Add help and info parameters, and tests for them
174
3251.3.1 by Aaron Bentley
Add support for directory services
175
    def get_prefix(self, fullname):
3251.3.2 by Aaron Bentley
Add testing of Registry.get_prefix
176
        """Return an object whose key is a prefix of the supplied value.
177
178
        :fullname: The name to find a prefix for
179
        :return: a tuple of (object, remainder), where the remainder is the
180
            portion of the name that did not match the key.
181
        """
4000.3.1 by Jelmer Vernooij
Avoid unnecessarily resolving lazy objects in Registry.get_prefix().
182
        for key in self.keys():
3251.3.1 by Aaron Bentley
Add support for directory services
183
            if fullname.startswith(key):
4000.3.1 by Jelmer Vernooij
Avoid unnecessarily resolving lazy objects in Registry.get_prefix().
184
                return self.get(key), fullname[len(key):]
3251.3.1 by Aaron Bentley
Add support for directory services
185
1911.4.7 by John Arbash Meinel
Add help and info parameters, and tests for them
186
    def _get_key_or_default(self, key=None):
187
        """Return either 'key' or the default key if key is None"""
188
        if key is not None:
189
            return key
190
        if self.default_key is None:
191
            raise KeyError('Key is None, and no default key is set')
192
        else:
193
            return self.default_key
1911.4.5 by John Arbash Meinel
Make a Registry look more like a dict, and allow anyone to register stuff lazily.
194
1911.4.7 by John Arbash Meinel
Add help and info parameters, and tests for them
195
    def get_help(self, key=None):
196
        """Get the help text associated with the given key"""
197
        the_help = self._help_dict[self._get_key_or_default(key)]
198
        if callable(the_help):
1911.4.9 by John Arbash Meinel
A help callable should take the registry as the first parameter
199
            return the_help(self, key)
1911.4.7 by John Arbash Meinel
Add help and info parameters, and tests for them
200
        return the_help
201
202
    def get_info(self, key=None):
203
        """Get the extra information associated with the given key"""
204
        return self._info_dict[self._get_key_or_default(key)]
205
1911.4.5 by John Arbash Meinel
Make a Registry look more like a dict, and allow anyone to register stuff lazily.
206
    def remove(self, key):
207
        """Remove a registered entry.
208
209
        This is mostly for the test suite, but it can be used by others
210
        """
211
        del self._dict[key]
212
213
    def __contains__(self, key):
214
        return key in self._dict
215
1912.5.2 by Adeodato Simó
Morph John's LazyFactory into a generalized Registry class, and
216
    def keys(self):
217
        """Get a list of registered entries"""
218
        return sorted(self._dict.keys())
219
1911.4.5 by John Arbash Meinel
Make a Registry look more like a dict, and allow anyone to register stuff lazily.
220
    def iteritems(self):
1911.4.12 by John Arbash Meinel
Use helper objects to handle lazy importing
221
        for key, getter in self._dict.iteritems():
222
            yield key, getter.get_obj()
1911.4.5 by John Arbash Meinel
Make a Registry look more like a dict, and allow anyone to register stuff lazily.
223
3882.1.2 by Martin Pool
Add Registry.items()
224
    def items(self):
225
        return sorted(self.iteritems())
226
1912.5.2 by Adeodato Simó
Morph John's LazyFactory into a generalized Registry class, and
227
    def _set_default_key(self, key):
228
        if not self._dict.has_key(key):
229
            raise KeyError('No object registered under key %s.' % key)
230
        else:
231
            self._default_key = key
232
233
    def _get_default_key(self):
234
        return self._default_key
235
1911.4.5 by John Arbash Meinel
Make a Registry look more like a dict, and allow anyone to register stuff lazily.
236
    default_key = property(_get_default_key, _set_default_key,
237
                            doc="Current value of the default key."
1911.4.15 by John Arbash Meinel
Updated HACKING and docstrings per Martin's suggestions
238
                                " Can be set to any existing key.")
4032.3.1 by Robert Collins
Add a BranchFormat.network_name() method as preparation for creating branches via RPC calls.
239
240
241
class FormatRegistry(Registry):
242
    """Registry specialised for handling formats."""
243
244
    def __init__(self, other_registry=None):
245
        Registry.__init__(self)
246
        self._other_registry = other_registry
247
248
    def register_lazy(self, key, module_name, member_name,
249
                      help=None, info=None,
250
                      override_existing=False):
251
        # Overridden to allow capturing registrations to two seperate
252
        # registries in a single call.
253
        Registry.register_lazy(self, key, module_name, member_name,
254
                help=help, info=info, override_existing=override_existing)
255
        if self._other_registry is not None:
256
            self._other_registry.register_lazy(key, module_name, member_name,
257
                help=help, info=info, override_existing=override_existing)
258
259
    def get(self, format_string):
260
        r = Registry.get(self, format_string)
261
        if callable(r):
262
            r = r()
263
        return r
4032.3.6 by Robert Collins
Fix test_source errors.
264
4032.3.1 by Robert Collins
Add a BranchFormat.network_name() method as preparation for creating branches via RPC calls.
265