~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/registry.py

  • Committer: Jelmer Vernooij
  • Date: 2011-12-19 10:58:39 UTC
  • mfrom: (6383 +trunk)
  • mto: This revision was merged to the branch mainline in revision 6386.
  • Revision ID: jelmer@canonical.com-20111219105839-uji05ck4rkm1mj4j
Merge bzr.dev.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2006, 2008 Canonical Ltd
 
1
# Copyright (C) 2006-2010 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
14
14
# along with this program; if not, write to the Free Software
15
15
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
16
16
 
 
17
from __future__ import absolute_import
 
18
 
17
19
"""Classes to provide name-to-object registry-like support."""
18
20
 
19
21
 
 
22
from bzrlib.pyutils import get_named_object
 
23
 
 
24
 
20
25
class _ObjectGetter(object):
21
26
    """Maintain a reference to an object, and return the object on request.
22
27
 
32
37
    def __init__(self, obj):
33
38
        self._obj = obj
34
39
 
 
40
    def get_module(self):
 
41
        """Get the module the object was loaded from."""
 
42
        return self._obj.__module__
 
43
 
35
44
    def get_obj(self):
36
45
        """Get the object that was saved at creation time"""
37
46
        return self._obj
51
60
        self._imported = False
52
61
        super(_LazyObjectGetter, self).__init__(None)
53
62
 
 
63
    def get_module(self):
 
64
        """Get the module the referenced object will be loaded from.
 
65
        """
 
66
        return self._module_name
 
67
 
54
68
    def get_obj(self):
55
69
        """Get the referenced object.
56
70
 
58
72
        return the imported object.
59
73
        """
60
74
        if not self._imported:
61
 
            self._do_import()
 
75
            self._obj = get_named_object(self._module_name, self._member_name)
 
76
            self._imported = True
62
77
        return super(_LazyObjectGetter, self).get_obj()
63
78
 
64
 
    def _do_import(self):
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)
74
 
        self._obj = obj
75
 
        self._imported = True
76
 
 
77
79
    def __repr__(self):
78
 
        return "<%s.%s object at %x, module=%r attribute=%r>" % (
 
80
        return "<%s.%s object at %x, module=%r attribute=%r imported=%r>" % (
79
81
            self.__class__.__module__, self.__class__.__name__, id(self),
80
 
            self._module_name, self._member_name)
 
82
            self._module_name, self._member_name, self._imported)
81
83
 
82
84
 
83
85
class Registry(object):
132
134
                      override_existing=False):
133
135
        """Register a new object to be loaded on request.
134
136
 
 
137
        :param key: This is the key to use to request the object later.
135
138
        :param module_name: The python path to the module. Such as 'os.path'.
136
139
        :param member_name: The member of the module to return.  If empty or
137
140
                None, get() will return the module itself.
138
141
        :param help: Help text for this entry. This may be a string or
139
142
                a callable.
140
 
        :param info: More information for this entry. Registry
 
143
        :param info: More information for this entry. Registry.get_info()
 
144
                can be used to get this information. Registry treats this as an
 
145
                opaque storage location (it is defined by the caller).
141
146
        :param override_existing: If True, replace the existing object
142
147
                with the new one. If False, if there is already something
143
148
                registered with the same key, raise a KeyError
172
177
        """
173
178
        return self._dict[self._get_key_or_default(key)].get_obj()
174
179
 
 
180
    def _get_module(self, key):
 
181
        """Return the module the object will be or was loaded from.
 
182
 
 
183
        :param key: The key to obtain the module for.
 
184
        :return: The name of the module
 
185
        """
 
186
        return self._dict[key].get_module()
 
187
 
175
188
    def get_prefix(self, fullname):
176
189
        """Return an object whose key is a prefix of the supplied value.
177
190
 
248
261
        Registry.__init__(self)
249
262
        self._other_registry = other_registry
250
263
 
 
264
    def register(self, key, obj, help=None, info=None,
 
265
                 override_existing=False):
 
266
        Registry.register(self, key, obj, help=help, info=info,
 
267
            override_existing=override_existing)
 
268
        if self._other_registry is not None:
 
269
            self._other_registry.register(key, obj, help=help,
 
270
                info=info, override_existing=override_existing)
 
271
 
251
272
    def register_lazy(self, key, module_name, member_name,
252
273
                      help=None, info=None,
253
274
                      override_existing=False):
259
280
            self._other_registry.register_lazy(key, module_name, member_name,
260
281
                help=help, info=info, override_existing=override_existing)
261
282
 
 
283
    def remove(self, key):
 
284
        Registry.remove(self, key)
 
285
        if self._other_registry is not None:
 
286
            self._other_registry.remove(key)
 
287
 
262
288
    def get(self, format_string):
263
289
        r = Registry.get(self, format_string)
264
290
        if callable(r):
265
291
            r = r()
266
292
        return r
267
 
 
268