~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/lazy_import.py

  • Committer: Robert Collins
  • Date: 2007-07-04 08:08:13 UTC
  • mfrom: (2572 +trunk)
  • mto: This revision was merged to the branch mainline in revision 2587.
  • Revision ID: robertc@robertcollins.net-20070704080813-wzebx0r88fvwj5rq
Merge bzr.dev.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2006 by 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
40
40
    to inherit from them).
41
41
"""
42
42
 
43
 
import re
44
 
import sys
45
 
 
46
 
from bzrlib import (
47
 
    errors,
48
 
    )
49
 
 
50
43
 
51
44
class ScopeReplacer(object):
52
45
    """A lazy object that will replace itself in the appropriate scope.
55
48
    needed.
56
49
    """
57
50
 
58
 
    __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
59
57
 
60
58
    def __init__(self, scope, factory, name):
61
59
        """Create a temporary object in the specified scope.
69
67
        self._scope = scope
70
68
        self._factory = factory
71
69
        self._name = name
 
70
        self._real_obj = None
72
71
        scope[name] = self
73
72
 
74
73
    def _replace(self):
88
87
                          " to another variable?",
89
88
                extra=e)
90
89
        obj = factory(self, scope, name)
 
90
        if ScopeReplacer._should_proxy:
 
91
            self._real_obj = obj
91
92
        scope[name] = obj
92
93
        return obj
93
94
 
99
100
        # del self._name
100
101
 
101
102
    def __getattribute__(self, attr):
102
 
        _replace = object.__getattribute__(self, '_replace')
103
 
        obj = _replace()
104
 
        _cleanup = object.__getattribute__(self, '_cleanup')
105
 
        _cleanup()
 
103
        obj = object.__getattribute__(self, '_real_obj')
 
104
        if obj is None:
 
105
            _replace = object.__getattribute__(self, '_replace')
 
106
            obj = _replace()
 
107
            _cleanup = object.__getattribute__(self, '_cleanup')
 
108
            _cleanup()
106
109
        return getattr(obj, attr)
107
110
 
108
111
    def __call__(self, *args, **kwargs):
377
380
    # This is just a helper around ImportProcessor.lazy_import
378
381
    proc = ImportProcessor(lazy_import_class=lazy_import_class)
379
382
    return proc.lazy_import(scope, text)
 
383
 
 
384
 
 
385
# The only module that this module depends on is 'bzrlib.errors'. But it
 
386
# can actually be imported lazily, since we only need it if there is a
 
387
# problem.
 
388
 
 
389
lazy_import(globals(), """
 
390
from bzrlib import errors
 
391
""")