~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/lazy_import.py

  • Committer: John Arbash Meinel
  • Date: 2007-04-09 23:19:39 UTC
  • mto: This revision was merged to the branch mainline in revision 2566.
  • Revision ID: john@arbash-meinel.com-20070409231939-prjmbgzw9fj3zqon
Change lazy_import to allow proxying when necessary.
Write a monkey-patch for epydoc.uid.ObjectUID so that it can understand
a lazy_import object.
With the monkey-patch in place, epydoc seems to be able to process bzrlib.
Now we still need to fix the errors. :)

Show diffs side-by-side

added added

removed removed

Lines of Context:
40
40
    to inherit from them).
41
41
"""
42
42
 
 
43
_scope_replacer_should_proxy = False
 
44
 
43
45
 
44
46
class ScopeReplacer(object):
45
47
    """A lazy object that will replace itself in the appropriate scope.
48
50
    needed.
49
51
    """
50
52
 
51
 
    __slots__ = ('_scope', '_factory', '_name')
 
53
    __slots__ = ('_scope', '_factory', '_name', '_real_obj')
52
54
 
53
55
    def __init__(self, scope, factory, name):
54
56
        """Create a temporary object in the specified scope.
62
64
        self._scope = scope
63
65
        self._factory = factory
64
66
        self._name = name
 
67
        self._real_obj = None
65
68
        scope[name] = self
66
69
 
67
70
    def _replace(self):
81
84
                          " to another variable?",
82
85
                extra=e)
83
86
        obj = factory(self, scope, name)
 
87
        if _scope_replacer_should_proxy:
 
88
            self._real_obj = obj
84
89
        scope[name] = obj
85
90
        return obj
86
91
 
92
97
        # del self._name
93
98
 
94
99
    def __getattribute__(self, attr):
95
 
        _replace = object.__getattribute__(self, '_replace')
96
 
        obj = _replace()
97
 
        _cleanup = object.__getattribute__(self, '_cleanup')
98
 
        _cleanup()
 
100
        obj = object.__getattribute__(self, '_real_obj')
 
101
        if obj is None:
 
102
            _replace = object.__getattribute__(self, '_replace')
 
103
            obj = _replace()
 
104
            _cleanup = object.__getattribute__(self, '_cleanup')
 
105
            _cleanup()
99
106
        return getattr(obj, attr)
100
107
 
101
108
    def __call__(self, *args, **kwargs):