/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: Jelmer Vernooij
  • Date: 2011-03-10 13:23:20 UTC
  • mto: (5582.10.90 weave-plugin)
  • mto: This revision was merged to the branch mainline in revision 5717.
  • Revision ID: jelmer@samba.org-20110310132320-k6uclq04lapawivt
Fix imports.

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
38
45
 
 
46
    def __eq__(self, other):
 
47
        if other == self.get_obj():
 
48
            return True
 
49
        if isinstance(other, self.__class__):
 
50
            return (self.get_obj() == other.get_obj())
 
51
        return False
 
52
 
39
53
 
40
54
class _LazyObjectGetter(_ObjectGetter):
41
55
    """Keep a record of a possible object.
51
65
        self._imported = False
52
66
        super(_LazyObjectGetter, self).__init__(None)
53
67
 
 
68
    def get_module(self):
 
69
        """Get the module the referenced object will be loaded from.
 
70
        """
 
71
        return self._module_name
 
72
 
54
73
    def get_obj(self):
55
74
        """Get the referenced object.
56
75
 
58
77
        return the imported object.
59
78
        """
60
79
        if not self._imported:
61
 
            self._do_import()
 
80
            self._obj = get_named_object(self._module_name, self._member_name)
 
81
            self._imported = True
62
82
        return super(_LazyObjectGetter, self).get_obj()
63
83
 
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
 
84
    def __eq__(self, other):
 
85
        if isinstance(other, self.__class__):
 
86
            return (self._member_name == other._member_name and
 
87
                    self._module_name == other._module_name)
 
88
        return super(_LazyObjectGetter, self).__eq__(other)
76
89
 
77
90
    def __repr__(self):
78
 
        return "<%s.%s object at %x, module=%r attribute=%r>" % (
 
91
        return "<%s.%s object at %x, module=%r attribute=%r imported=%r>" % (
79
92
            self.__class__.__module__, self.__class__.__name__, id(self),
80
 
            self._module_name, self._member_name)
 
93
            self._module_name, self._member_name, self._imported)
81
94
 
82
95
 
83
96
class Registry(object):
172
185
        """
173
186
        return self._dict[self._get_key_or_default(key)].get_obj()
174
187
 
 
188
    def _get_module(self, key):
 
189
        """Return the module the object will be or was loaded from.
 
190
 
 
191
        :param key: The key to obtain the module for.
 
192
        :return: The name of the module
 
193
        """
 
194
        return self._dict[key].get_module()
 
195
 
175
196
    def get_prefix(self, fullname):
176
197
        """Return an object whose key is a prefix of the supplied value.
177
198
 
248
269
        Registry.__init__(self)
249
270
        self._other_registry = other_registry
250
271
 
 
272
    def register(self, key, obj, help=None, info=None,
 
273
                 override_existing=False):
 
274
        Registry.register(self, key, obj, help=help, info=info,
 
275
            override_existing=override_existing)
 
276
        if self._other_registry is not None:
 
277
            self._other_registry.register(key, obj, help=help,
 
278
                info=info, override_existing=override_existing)
 
279
 
251
280
    def register_lazy(self, key, module_name, member_name,
252
281
                      help=None, info=None,
253
282
                      override_existing=False):
259
288
            self._other_registry.register_lazy(key, module_name, member_name,
260
289
                help=help, info=info, override_existing=override_existing)
261
290
 
 
291
    def remove(self, key):
 
292
        Registry.remove(self, key)
 
293
        if self._other_registry is not None:
 
294
            self._other_registry.remove(key)
 
295
 
262
296
    def get(self, format_string):
263
297
        r = Registry.get(self, format_string)
264
298
        if callable(r):
265
299
            r = r()
266
300
        return r
267
 
 
268