~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-25 06:23:08 UTC
  • mto: This revision was merged to the branch mainline in revision 5324.
  • Revision ID: robertc@robertcollins.net-20100625062308-qx287gzfrehs1d21
Restore the original ui_factory when existing BzrLibraryState.

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, setup_ui=True, stdin=None, stdout=None, stderr=None):
 
42
    def __init__(self, ui=None):
43
43
        """Create library start for normal use of bzrlib.
44
44
 
45
45
        Most applications that embed bzrlib, including bzr itself, should just
46
46
        call bzrlib.initialize(), but it is possible to use the state class
47
 
        directly.
 
47
        directly. The initialize() function provides sensible defaults for a
 
48
        CLI program, such as a text UI factory.
48
49
 
49
50
        More options may be added in future so callers should use named
50
51
        arguments.
54
55
        global variables in use by bzr are set, and they are cleared on
55
56
        __exit__.
56
57
 
57
 
        :param setup_ui: If true (default) use a terminal UI; otherwise 
58
 
            some other ui_factory must be assigned to `bzrlib.ui.ui_factory` by
59
 
            the caller.
60
 
        :param stdin, stdout, stderr: If provided, use these for terminal IO;
61
 
            otherwise use the files in `sys`.
 
58
        :param ui: A bzrlib.ui.ui_factory to use.
62
59
        """
63
 
        self.setup_ui = setup_ui
64
 
        self.stdin = stdin
65
 
        self.stdout = stdout
66
 
        self.stderr = stderr
 
60
        self._ui = ui
67
61
 
68
62
    def __enter__(self):
69
63
        # NB: This function tweaks so much global state it's hard to test it in
84
78
            self.cleanups.add_cleanup(warning_cleanup)
85
79
        bzrlib.trace.enable_default_logging()
86
80
 
87
 
        if self.setup_ui:
88
 
            import bzrlib.ui
89
 
            stdin = self.stdin or sys.stdin
90
 
            stdout = self.stdout or sys.stdout
91
 
            stderr = self.stderr or sys.stderr
92
 
            bzrlib.ui.ui_factory = bzrlib.ui.make_ui_for_terminal(
93
 
                stdin, stdout, stderr)
 
81
        self._orig_ui = bzrlib.ui.ui_factory
 
82
        bzrlib.ui.ui_factory = self._ui
 
83
        self._ui.__enter__()
 
84
 
94
85
        self.saved_state = bzrlib.global_state
95
86
        bzrlib.global_state = self
96
87
        return self # This is bound to the 'as' clause in a with statement.
102
93
        bzrlib.trace._flush_trace()
103
94
        import bzrlib.osutils
104
95
        bzrlib.osutils.report_extension_load_failures()
105
 
        bzrlib.ui.ui_factory.__exit__(None, None, None)
106
 
        bzrlib.ui.ui_factory = None
 
96
        self._ui.__exit__(None, None, None)
 
97
        bzrlib.ui.ui_factory = self._orig_ui
107
98
        global global_state
108
99
        global_state = self.saved_state
109
100
        return False # propogate exceptions.