~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/lazy_import.py

(vila) Open 2.4.3 for bug fixes (Vincent Ladeuil)

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
 
19
19
This includes waiting to import a module until it is actually used.
20
20
 
21
21
Most commonly, the 'lazy_import' function is used to import other modules
22
 
in an on-demand fashion. Typically use looks like:
 
22
in an on-demand fashion. Typically use looks like::
 
23
 
23
24
    from bzrlib.lazy_import import lazy_import
24
25
    lazy_import(globals(), '''
25
26
    from bzrlib import (
30
31
    import bzrlib.branch
31
32
    ''')
32
33
 
33
 
    Then 'errors, osutils, branch' and 'bzrlib' will exist as lazy-loaded
34
 
    objects which will be replaced with a real object on first use.
 
34
Then 'errors, osutils, branch' and 'bzrlib' will exist as lazy-loaded
 
35
objects which will be replaced with a real object on first use.
35
36
 
36
 
    In general, it is best to only load modules in this way. This is because
37
 
    it isn't safe to pass these variables to other functions before they
38
 
    have been replaced. This is especially true for constants, sometimes
39
 
    true for classes or functions (when used as a factory, or you want
40
 
    to inherit from them).
 
37
In general, it is best to only load modules in this way. This is because
 
38
it isn't safe to pass these variables to other functions before they
 
39
have been replaced. This is especially true for constants, sometimes
 
40
true for classes or functions (when used as a factory, or you want
 
41
to inherit from them).
41
42
"""
42
43
 
43
44
 
87
88
                          " to another variable?",
88
89
                extra=e)
89
90
        obj = factory(self, scope, name)
 
91
        if obj is self:
 
92
            raise errors.IllegalUseOfScopeReplacer(name, msg="Object tried"
 
93
                " to replace itself, check it's not using its own scope.")
90
94
        if ScopeReplacer._should_proxy:
91
95
            object.__setattr__(self, '_real_obj', obj)
92
96
        scope[name] = obj
157
161
            None, indicating the module is being imported.
158
162
        :param children: Children entries to be imported later.
159
163
            This should be a map of children specifications.
160
 
            {'foo':(['bzrlib', 'foo'], None,
161
 
                {'bar':(['bzrlib', 'foo', 'bar'], None {})})
162
 
            }
163
 
        Examples:
 
164
            ::
 
165
            
 
166
                {'foo':(['bzrlib', 'foo'], None,
 
167
                    {'bar':(['bzrlib', 'foo', 'bar'], None {})})
 
168
                }
 
169
 
 
170
        Examples::
 
171
 
164
172
            import foo => name='foo' module_path='foo',
165
173
                          member=None, children={}
166
174
            import foo.bar => name='foo' module_path='foo', member=None,
367
375
def lazy_import(scope, text, lazy_import_class=None):
368
376
    """Create lazy imports for all of the imports in text.
369
377
 
370
 
    This is typically used as something like:
371
 
    from bzrlib.lazy_import import lazy_import
372
 
    lazy_import(globals(), '''
373
 
    from bzrlib import (
374
 
        foo,
375
 
        bar,
376
 
        baz,
377
 
        )
378
 
    import bzrlib.branch
379
 
    import bzrlib.transport
380
 
    ''')
 
378
    This is typically used as something like::
 
379
 
 
380
        from bzrlib.lazy_import import lazy_import
 
381
        lazy_import(globals(), '''
 
382
        from bzrlib import (
 
383
            foo,
 
384
            bar,
 
385
            baz,
 
386
            )
 
387
        import bzrlib.branch
 
388
        import bzrlib.transport
 
389
        ''')
381
390
 
382
391
    Then 'foo, bar, baz' and 'bzrlib' will exist as lazy-loaded
383
392
    objects which will be replaced with a real object on first use.