~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/lazy_import.py

  • Committer: Canonical.com Patch Queue Manager
  • Date: 2011-05-04 12:10:51 UTC
  • mfrom: (5819.1.4 777007-developer-doc)
  • Revision ID: pqm@pqm.ubuntu.com-20110504121051-aovlsmqiivjmc4fc
(jelmer) Small fixes to developer documentation. (Jonathan Riddell)

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2006 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
12
12
#
13
13
# You should have received a copy of the GNU General Public License
14
14
# along with this program; if not, write to the Free Software
15
 
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 
15
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
16
16
 
17
17
"""Functionality to create lazy evaluation objects.
18
18
 
48
48
    needed.
49
49
    """
50
50
 
51
 
    __slots__ = ('_scope', '_factory', '_name')
 
51
    __slots__ = ('_scope', '_factory', '_name', '_real_obj')
 
52
 
 
53
    # Setting this to True will allow you to do x = y, and still access members
 
54
    # from both variables. This should not normally be enabled, but is useful
 
55
    # when building documentation.
 
56
    _should_proxy = False
52
57
 
53
58
    def __init__(self, scope, factory, name):
54
59
        """Create a temporary object in the specified scope.
59
64
            It will be passed (self, scope, name)
60
65
        :param name: The variable name in the given scope.
61
66
        """
62
 
        self._scope = scope
63
 
        self._factory = factory
64
 
        self._name = name
 
67
        object.__setattr__(self, '_scope', scope)
 
68
        object.__setattr__(self, '_factory', factory)
 
69
        object.__setattr__(self, '_name', name)
 
70
        object.__setattr__(self, '_real_obj', None)
65
71
        scope[name] = self
66
72
 
67
73
    def _replace(self):
81
87
                          " to another variable?",
82
88
                extra=e)
83
89
        obj = factory(self, scope, name)
 
90
        if obj is self:
 
91
            raise errors.IllegalUseOfScopeReplacer(name, msg="Object tried"
 
92
                " to replace itself, check it's not using its own scope.")
 
93
        if ScopeReplacer._should_proxy:
 
94
            object.__setattr__(self, '_real_obj', obj)
84
95
        scope[name] = obj
85
96
        return obj
86
97
 
92
103
        # del self._name
93
104
 
94
105
    def __getattribute__(self, attr):
95
 
        _replace = object.__getattribute__(self, '_replace')
96
 
        obj = _replace()
97
 
        _cleanup = object.__getattribute__(self, '_cleanup')
98
 
        _cleanup()
 
106
        obj = object.__getattribute__(self, '_real_obj')
 
107
        if obj is None:
 
108
            _replace = object.__getattribute__(self, '_replace')
 
109
            obj = _replace()
 
110
            _cleanup = object.__getattribute__(self, '_cleanup')
 
111
            _cleanup()
99
112
        return getattr(obj, attr)
100
113
 
 
114
    def __setattr__(self, attr, value):
 
115
        obj = object.__getattribute__(self, '_real_obj')
 
116
        if obj is None:
 
117
            _replace = object.__getattribute__(self, '_replace')
 
118
            obj = _replace()
 
119
            _cleanup = object.__getattribute__(self, '_cleanup')
 
120
            _cleanup()
 
121
        return setattr(obj, attr, value)
 
122
 
101
123
    def __call__(self, *args, **kwargs):
102
124
        _replace = object.__getattribute__(self, '_replace')
103
125
        obj = _replace()
130
152
 
131
153
        :param scope: The scope that objects should be imported into.
132
154
            Typically this is globals()
133
 
        :param name: The variable name. Often this is the same as the 
 
155
        :param name: The variable name. Often this is the same as the
134
156
            module_path. 'bzrlib'
135
157
        :param module_path: A list for the fully specified module path
136
158
            ['bzrlib', 'foo', 'bar']
138
160
            None, indicating the module is being imported.
139
161
        :param children: Children entries to be imported later.
140
162
            This should be a map of children specifications.
141
 
            {'foo':(['bzrlib', 'foo'], None, 
 
163
            {'foo':(['bzrlib', 'foo'], None,
142
164
                {'bar':(['bzrlib', 'foo', 'bar'], None {})})
143
165
            }
144
166
        Examples:
151
173
            from foo import bar, baz would get translated into 2 import
152
174
            requests. On for 'name=bar' and one for 'name=baz'
153
175
        """
154
 
        if member is not None:
155
 
            assert not children, \
156
 
                'Cannot supply both a member and children'
 
176
        if (member is not None) and children:
 
177
            raise ValueError('Cannot supply both a member and children')
157
178
 
158
 
        self._import_replacer_children = children
159
 
        self._member = member
160
 
        self._module_path = module_path
 
179
        object.__setattr__(self, '_import_replacer_children', children)
 
180
        object.__setattr__(self, '_member', member)
 
181
        object.__setattr__(self, '_module_path', module_path)
161
182
 
162
183
        # Indirecting through __class__ so that children can
163
184
        # override _import (especially our instrumented version)
241
262
 
242
263
        :param import_str: The import string to process
243
264
        """
244
 
        assert import_str.startswith('import ')
 
265
        if not import_str.startswith('import '):
 
266
            raise ValueError('bad import string %r' % (import_str,))
245
267
        import_str = import_str[len('import '):]
246
268
 
247
269
        for path in import_str.split(','):
286
308
 
287
309
        :param from_str: The import string to process
288
310
        """
289
 
        assert from_str.startswith('from ')
 
311
        if not from_str.startswith('from '):
 
312
            raise ValueError('bad from/import %r' % from_str)
290
313
        from_str = from_str[len('from '):]
291
314
 
292
315
        from_module, import_list = from_str.split(' import ')