~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/registry.py

  • Committer: John Arbash Meinel
  • Date: 2011-05-11 11:35:28 UTC
  • mto: This revision was merged to the branch mainline in revision 5851.
  • Revision ID: john@arbash-meinel.com-20110511113528-qepibuwxicjrbb2h
Break compatibility with python <2.6.

This includes auditing the code for places where we were doing
explicit 'sys.version' checks and removing them as appropriate.

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
17
17
"""Classes to provide name-to-object registry-like support."""
18
18
 
19
19
 
 
20
from bzrlib.pyutils import get_named_object
 
21
 
 
22
 
20
23
class _ObjectGetter(object):
21
24
    """Maintain a reference to an object, and return the object on request.
22
25
 
32
35
    def __init__(self, obj):
33
36
        self._obj = obj
34
37
 
 
38
    def get_module(self):
 
39
        """Get the module the object was loaded from."""
 
40
        return self._obj.__module__
 
41
 
35
42
    def get_obj(self):
36
43
        """Get the object that was saved at creation time"""
37
44
        return self._obj
51
58
        self._imported = False
52
59
        super(_LazyObjectGetter, self).__init__(None)
53
60
 
 
61
    def get_module(self):
 
62
        """Get the module the referenced object will be loaded from.
 
63
        """
 
64
        return self._module_name
 
65
 
54
66
    def get_obj(self):
55
67
        """Get the referenced object.
56
68
 
58
70
        return the imported object.
59
71
        """
60
72
        if not self._imported:
61
 
            self._do_import()
 
73
            self._obj = get_named_object(self._module_name, self._member_name)
 
74
            self._imported = True
62
75
        return super(_LazyObjectGetter, self).get_obj()
63
76
 
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
77
    def __repr__(self):
78
 
        return "<%s.%s object at %x, module=%r attribute=%r>" % (
 
78
        return "<%s.%s object at %x, module=%r attribute=%r imported=%r>" % (
79
79
            self.__class__.__module__, self.__class__.__name__, id(self),
80
 
            self._module_name, self._member_name)
 
80
            self._module_name, self._member_name, self._imported)
81
81
 
82
82
 
83
83
class Registry(object):
172
172
        """
173
173
        return self._dict[self._get_key_or_default(key)].get_obj()
174
174
 
 
175
    def _get_module(self, key):
 
176
        """Return the module the object will be or was loaded from.
 
177
 
 
178
        :param key: The key to obtain the module for.
 
179
        :return: The name of the module
 
180
        """
 
181
        return self._dict[key].get_module()
 
182
 
175
183
    def get_prefix(self, fullname):
176
184
        """Return an object whose key is a prefix of the supplied value.
177
185
 
248
256
        Registry.__init__(self)
249
257
        self._other_registry = other_registry
250
258
 
 
259
    def register(self, key, obj, help=None, info=None,
 
260
                 override_existing=False):
 
261
        Registry.register(self, key, obj, help=help, info=info,
 
262
            override_existing=override_existing)
 
263
        if self._other_registry is not None:
 
264
            self._other_registry.register(key, obj, help=help,
 
265
                info=info, override_existing=override_existing)
 
266
 
251
267
    def register_lazy(self, key, module_name, member_name,
252
268
                      help=None, info=None,
253
269
                      override_existing=False):
259
275
            self._other_registry.register_lazy(key, module_name, member_name,
260
276
                help=help, info=info, override_existing=override_existing)
261
277
 
 
278
    def remove(self, key):
 
279
        Registry.remove(self, key)
 
280
        if self._other_registry is not None:
 
281
            self._other_registry.remove(key)
 
282
 
262
283
    def get(self, format_string):
263
284
        r = Registry.get(self, format_string)
264
285
        if callable(r):
265
286
            r = r()
266
287
        return r
267
 
 
268