/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/lazy_import.py

  • Committer: Robert Collins
  • Date: 2007-03-06 12:28:18 UTC
  • mto: (2321.1.1 integration)
  • mto: This revision was merged to the branch mainline in revision 2322.
  • Revision ID: robertc@robertcollins.net-20070306122818-xk0lc3l01ecl6vbc
Get merge_nested finally working: change nested tree iterators to take file_ids, and ensure the right branch is connected to in the merge logic. May not be suitable for shared repositories yet.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2006-2010 Canonical Ltd
 
1
# Copyright (C) 2006 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 
15
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  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', '_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
 
51
    __slots__ = ('_scope', '_factory', '_name')
57
52
 
58
53
    def __init__(self, scope, factory, name):
59
54
        """Create a temporary object in the specified scope.
64
59
            It will be passed (self, scope, name)
65
60
        :param name: The variable name in the given scope.
66
61
        """
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)
 
62
        self._scope = scope
 
63
        self._factory = factory
 
64
        self._name = name
71
65
        scope[name] = self
72
66
 
73
67
    def _replace(self):
87
81
                          " to another variable?",
88
82
                extra=e)
89
83
        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)
95
84
        scope[name] = obj
96
85
        return obj
97
86
 
103
92
        # del self._name
104
93
 
105
94
    def __getattribute__(self, attr):
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()
 
95
        _replace = object.__getattribute__(self, '_replace')
 
96
        obj = _replace()
 
97
        _cleanup = object.__getattribute__(self, '_cleanup')
 
98
        _cleanup()
112
99
        return getattr(obj, attr)
113
100
 
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
 
 
123
101
    def __call__(self, *args, **kwargs):
124
102
        _replace = object.__getattribute__(self, '_replace')
125
103
        obj = _replace()
152
130
 
153
131
        :param scope: The scope that objects should be imported into.
154
132
            Typically this is globals()
155
 
        :param name: The variable name. Often this is the same as the
 
133
        :param name: The variable name. Often this is the same as the 
156
134
            module_path. 'bzrlib'
157
135
        :param module_path: A list for the fully specified module path
158
136
            ['bzrlib', 'foo', 'bar']
160
138
            None, indicating the module is being imported.
161
139
        :param children: Children entries to be imported later.
162
140
            This should be a map of children specifications.
163
 
            {'foo':(['bzrlib', 'foo'], None,
 
141
            {'foo':(['bzrlib', 'foo'], None, 
164
142
                {'bar':(['bzrlib', 'foo', 'bar'], None {})})
165
143
            }
166
144
        Examples:
173
151
            from foo import bar, baz would get translated into 2 import
174
152
            requests. On for 'name=bar' and one for 'name=baz'
175
153
        """
176
 
        if (member is not None) and children:
177
 
            raise ValueError('Cannot supply both a member and children')
 
154
        if member is not None:
 
155
            assert not children, \
 
156
                'Cannot supply both a member and children'
178
157
 
179
 
        object.__setattr__(self, '_import_replacer_children', children)
180
 
        object.__setattr__(self, '_member', member)
181
 
        object.__setattr__(self, '_module_path', module_path)
 
158
        self._import_replacer_children = children
 
159
        self._member = member
 
160
        self._module_path = module_path
182
161
 
183
162
        # Indirecting through __class__ so that children can
184
163
        # override _import (especially our instrumented version)
262
241
 
263
242
        :param import_str: The import string to process
264
243
        """
265
 
        if not import_str.startswith('import '):
266
 
            raise ValueError('bad import string %r' % (import_str,))
 
244
        assert import_str.startswith('import ')
267
245
        import_str = import_str[len('import '):]
268
246
 
269
247
        for path in import_str.split(','):
308
286
 
309
287
        :param from_str: The import string to process
310
288
        """
311
 
        if not from_str.startswith('from '):
312
 
            raise ValueError('bad from/import %r' % from_str)
 
289
        assert from_str.startswith('from ')
313
290
        from_str = from_str[len('from '):]
314
291
 
315
292
        from_module, import_list = from_str.split(' import ')