/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar

« back to all changes in this revision

Viewing changes to bzrlib/registry.py

  • Committer: Martin Pool
  • Date: 2011-05-20 14:46:02 UTC
  • mto: This revision was merged to the branch mainline in revision 5923.
  • Revision ID: mbp@canonical.com-20110520144602-bqli0t6dj01gl0pv
Various pyflakes import fixes.

Some modules were used for subclassing or at module load time, so there is no
point loading them lazily.

Some were not imported when they should be.

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):
132
132
                      override_existing=False):
133
133
        """Register a new object to be loaded on request.
134
134
 
 
135
        :param key: This is the key to use to request the object later.
135
136
        :param module_name: The python path to the module. Such as 'os.path'.
136
137
        :param member_name: The member of the module to return.  If empty or
137
138
                None, get() will return the module itself.
138
139
        :param help: Help text for this entry. This may be a string or
139
140
                a callable.
140
 
        :param info: More information for this entry. Registry
 
141
        :param info: More information for this entry. Registry.get_info()
 
142
                can be used to get this information. Registry treats this as an
 
143
                opaque storage location (it is defined by the caller).
141
144
        :param override_existing: If True, replace the existing object
142
145
                with the new one. If False, if there is already something
143
146
                registered with the same key, raise a KeyError
172
175
        """
173
176
        return self._dict[self._get_key_or_default(key)].get_obj()
174
177
 
 
178
    def _get_module(self, key):
 
179
        """Return the module the object will be or was loaded from.
 
180
 
 
181
        :param key: The key to obtain the module for.
 
182
        :return: The name of the module
 
183
        """
 
184
        return self._dict[key].get_module()
 
185
 
175
186
    def get_prefix(self, fullname):
176
187
        """Return an object whose key is a prefix of the supplied value.
177
188
 
248
259
        Registry.__init__(self)
249
260
        self._other_registry = other_registry
250
261
 
 
262
    def register(self, key, obj, help=None, info=None,
 
263
                 override_existing=False):
 
264
        Registry.register(self, key, obj, help=help, info=info,
 
265
            override_existing=override_existing)
 
266
        if self._other_registry is not None:
 
267
            self._other_registry.register(key, obj, help=help,
 
268
                info=info, override_existing=override_existing)
 
269
 
251
270
    def register_lazy(self, key, module_name, member_name,
252
271
                      help=None, info=None,
253
272
                      override_existing=False):
259
278
            self._other_registry.register_lazy(key, module_name, member_name,
260
279
                help=help, info=info, override_existing=override_existing)
261
280
 
 
281
    def remove(self, key):
 
282
        Registry.remove(self, key)
 
283
        if self._other_registry is not None:
 
284
            self._other_registry.remove(key)
 
285
 
262
286
    def get(self, format_string):
263
287
        r = Registry.get(self, format_string)
264
288
        if callable(r):
265
289
            r = r()
266
290
        return r
267
 
 
268