~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/registry.py

  • Committer: Robert Collins
  • Date: 2010-04-08 04:34:03 UTC
  • mfrom: (5138 +trunk)
  • mto: This revision was merged to the branch mainline in revision 5139.
  • Revision ID: robertc@robertcollins.net-20100408043403-56z0d07vdqrx7f3t
Update bugfix for 528114 to trunk.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2006 Canonical Ltd
 
1
# Copyright (C) 2006, 2008 Canonical Ltd
2
2
#
3
3
# This program is free software; you can redistribute it and/or modify
4
4
# it under the terms of the GNU General Public License as published by
12
12
#
13
13
# You should have received a copy of the GNU General Public License
14
14
# along with this program; if not, write to the Free Software
15
 
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 
15
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
16
16
 
17
17
"""Classes to provide name-to-object registry-like support."""
18
18
 
62
62
        return super(_LazyObjectGetter, self).get_obj()
63
63
 
64
64
    def _do_import(self):
65
 
        obj = __import__(self._module_name, globals(), locals(),
66
 
                         [self._member_name])
67
 
        if self._member_name:
68
 
            obj = getattr(obj, self._member_name)
 
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)
69
74
        self._obj = obj
70
75
        self._imported = True
71
76
 
107
112
        :param obj: The object to register.
108
113
        :param help: Help text for this entry. This may be a string or
109
114
                a callable. If it is a callable, it should take two
110
 
                parameters (registry, key): this registry and the key that 
 
115
                parameters (registry, key): this registry and the key that
111
116
                the help was registered under.
112
117
        :param info: More information for this entry. Registry.get_info()
113
118
                can be used to get this information. Registry treats this as an
128
133
        """Register a new object to be loaded on request.
129
134
 
130
135
        :param module_name: The python path to the module. Such as 'os.path'.
131
 
        :param member_name: The member of the module to return.  If empty or 
 
136
        :param member_name: The member of the module to return.  If empty or
132
137
                None, get() will return the module itself.
133
138
        :param help: Help text for this entry. This may be a string or
134
139
                a callable.
135
 
        :param info: More information for this entry. Registry 
 
140
        :param info: More information for this entry. Registry
136
141
        :param override_existing: If True, replace the existing object
137
142
                with the new one. If False, if there is already something
138
143
                registered with the same key, raise a KeyError
152
157
        """Return the object register()'ed to the given key.
153
158
 
154
159
        May raise ImportError if the object was registered lazily and
155
 
        there are any problems, or AttributeError if the module does not 
 
160
        there are any problems, or AttributeError if the module does not
156
161
        have the supplied member.
157
162
 
158
163
        :param key: The key to obtain the object for. If no object has been
167
172
        """
168
173
        return self._dict[self._get_key_or_default(key)].get_obj()
169
174
 
 
175
    def get_prefix(self, fullname):
 
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
        """
 
182
        for key in self.keys():
 
183
            if fullname.startswith(key):
 
184
                return self.get(key), fullname[len(key):]
 
185
 
170
186
    def _get_key_or_default(self, key=None):
171
187
        """Return either 'key' or the default key if key is None"""
172
188
        if key is not None:
205
221
        for key, getter in self._dict.iteritems():
206
222
            yield key, getter.get_obj()
207
223
 
 
224
    def items(self):
 
225
        # We should not use the iteritems() implementation below (see bug
 
226
        # #430510)
 
227
        return sorted([(key, getter.get_obj())
 
228
                       for key, getter in self._dict.items()])
 
229
 
208
230
    def _set_default_key(self, key):
209
231
        if not self._dict.has_key(key):
210
232
            raise KeyError('No object registered under key %s.' % key)
217
239
    default_key = property(_get_default_key, _set_default_key,
218
240
                            doc="Current value of the default key."
219
241
                                " Can be set to any existing key.")
 
242
 
 
243
 
 
244
class FormatRegistry(Registry):
 
245
    """Registry specialised for handling formats."""
 
246
 
 
247
    def __init__(self, other_registry=None):
 
248
        Registry.__init__(self)
 
249
        self._other_registry = other_registry
 
250
 
 
251
    def register_lazy(self, key, module_name, member_name,
 
252
                      help=None, info=None,
 
253
                      override_existing=False):
 
254
        # Overridden to allow capturing registrations to two seperate
 
255
        # registries in a single call.
 
256
        Registry.register_lazy(self, key, module_name, member_name,
 
257
                help=help, info=info, override_existing=override_existing)
 
258
        if self._other_registry is not None:
 
259
            self._other_registry.register_lazy(key, module_name, member_name,
 
260
                help=help, info=info, override_existing=override_existing)
 
261
 
 
262
    def get(self, format_string):
 
263
        r = Registry.get(self, format_string)
 
264
        if callable(r):
 
265
            r = r()
 
266
        return r
 
267
 
 
268