~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/library_state.py

  • Committer: Robert Collins
  • Date: 2010-06-26 01:07:16 UTC
  • mto: This revision was merged to the branch mainline in revision 5324.
  • Revision ID: robertc@robertcollins.net-20100626010716-jowzrldm4ntsaki2
Make bzrlib startup use a trace context manager.

Show diffs side-by-side

added added

removed removed

Lines of Context:
39
39
        in __enter__ and executed in __exit__.
40
40
    """
41
41
 
42
 
    def __init__(self, ui=None):
 
42
    def __init__(self, ui, trace):
43
43
        """Create library start for normal use of bzrlib.
44
44
 
45
45
        Most applications that embed bzrlib, including bzr itself, should just
56
56
        __exit__.
57
57
 
58
58
        :param ui: A bzrlib.ui.ui_factory to use.
 
59
        :param trace: A bzrlib.trace.Config context manager to use, perhaps
 
60
            bzrlib.trace.DefaultConfig.
59
61
        """
60
62
        self._ui = ui
 
63
        self._trace = trace
61
64
 
62
65
    def __enter__(self):
63
66
        # NB: This function tweaks so much global state it's hard to test it in
72
75
            warning_cleanup = None
73
76
 
74
77
        import bzrlib.cleanup
75
 
        import bzrlib.trace
76
78
        self.cleanups = bzrlib.cleanup.ObjectWithCleanups()
77
79
        if warning_cleanup:
78
80
            self.cleanups.add_cleanup(warning_cleanup)
79
 
        bzrlib.trace.enable_default_logging()
 
81
        self._trace.__enter__()
80
82
 
81
83
        self._orig_ui = bzrlib.ui.ui_factory
82
84
        bzrlib.ui.ui_factory = self._ui
94
96
        import bzrlib.osutils
95
97
        bzrlib.osutils.report_extension_load_failures()
96
98
        self._ui.__exit__(None, None, None)
 
99
        self._trace.__exit__(None, None, None)
97
100
        bzrlib.ui.ui_factory = self._orig_ui
98
101
        global global_state
99
102
        global_state = self.saved_state