~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/lazy_import.py

  • Committer: Vincent Ladeuil
  • Date: 2011-07-06 09:22:00 UTC
  • mfrom: (6008 +trunk)
  • mto: (6012.1.1 trunk)
  • mto: This revision was merged to the branch mainline in revision 6013.
  • Revision ID: v.ladeuil+lp@free.fr-20110706092200-7iai2mwzc0sqdsvf
MergingĀ inĀ trunk

Show diffs side-by-side

added added

removed removed

Lines of Context:
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
 
160
161
            None, indicating the module is being imported.
161
162
        :param children: Children entries to be imported later.
162
163
            This should be a map of children specifications.
163
 
            {'foo':(['bzrlib', 'foo'], None,
164
 
                {'bar':(['bzrlib', 'foo', 'bar'], None {})})
165
 
            }
166
 
        Examples:
 
164
            ::
 
165
            
 
166
                {'foo':(['bzrlib', 'foo'], None,
 
167
                    {'bar':(['bzrlib', 'foo', 'bar'], None {})})
 
168
                }
 
169
 
 
170
        Examples::
 
171
 
167
172
            import foo => name='foo' module_path='foo',
168
173
                          member=None, children={}
169
174
            import foo.bar => name='foo' module_path='foo', member=None,
370
375
def lazy_import(scope, text, lazy_import_class=None):
371
376
    """Create lazy imports for all of the imports in text.
372
377
 
373
 
    This is typically used as something like:
374
 
    from bzrlib.lazy_import import lazy_import
375
 
    lazy_import(globals(), '''
376
 
    from bzrlib import (
377
 
        foo,
378
 
        bar,
379
 
        baz,
380
 
        )
381
 
    import bzrlib.branch
382
 
    import bzrlib.transport
383
 
    ''')
 
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
        ''')
384
390
 
385
391
    Then 'foo, bar, baz' and 'bzrlib' will exist as lazy-loaded
386
392
    objects which will be replaced with a real object on first use.