~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/library_state.py

  • Committer: Canonical.com Patch Queue Manager
  • Date: 2011-04-01 02:46:35 UTC
  • mfrom: (5728.4.2 initialize)
  • Revision ID: pqm@pqm.ubuntu.com-20110401024635-dkc6q8xysk46kej9
(mbp) bzrlib.initialize() now actually initializes,
 as well as returning a context manager (Martin Pool)

Show diffs side-by-side

added added

removed removed

Lines of Context:
31
31
    This is the core state needed to make use of bzr. The current instance is
32
32
    currently always exposed as bzrlib.global_state, but we desired to move
33
33
    to a point where no global state is needed at all.
34
 
    
 
34
 
35
35
    :ivar saved_state: The bzrlib.global_state at the time __enter__ was
36
36
        called.
37
37
    :ivar cleanups: An ObjectWithCleanups which can be used for cleanups that
61
61
        """
62
62
        self._ui = ui
63
63
        self._trace = trace
 
64
        self.started = False
64
65
 
65
66
    def __enter__(self):
 
67
        if not self.started:
 
68
            self._start()
 
69
        return self # This is bound to the 'as' clause in a with statement.
 
70
 
 
71
    def _start(self):
 
72
        """Do all initialization.
 
73
        """        
66
74
        # NB: This function tweaks so much global state it's hard to test it in
67
75
        # isolation within the same interpreter.  It's not reached on normal
68
76
        # in-process run_bzr calls.  If it's broken, we expect that
86
94
 
87
95
        self.saved_state = bzrlib.global_state
88
96
        bzrlib.global_state = self
89
 
        return self # This is bound to the 'as' clause in a with statement.
 
97
        self.started = True
90
98
 
91
99
    def __exit__(self, exc_type, exc_val, exc_tb):
92
100
        self.cleanups.cleanup_now()