~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/ui/__init__.py

first cut at merge from integration.

Show diffs side-by-side

added added

removed removed

Lines of Context:
28
28
displays no output.
29
29
"""
30
30
 
31
 
import sys
32
 
 
33
31
import bzrlib.progress
34
 
from bzrlib.symbol_versioning import *
35
32
 
36
33
 
37
34
class UIFactory(object):
40
37
    This tells the library how to display things to the user.  Through this
41
38
    layer different applications can choose the style of UI.
42
39
    """
43
 
 
44
 
    def __init__(self):
45
 
        super(UIFactory, self).__init__()
46
 
        self._progress_bar_stack = None
47
 
 
48
 
    @deprecated_method(zero_eight)
49
40
    def progress_bar(self):
50
 
        """See UIFactory.nested_progress_bar()."""
51
 
        raise NotImplementedError(self.progress_bar)
 
41
        """Return a progress bar object"""
 
42
        raise NotImplementedError
52
43
 
53
44
    def get_password(self, prompt='', **kwargs):
54
45
        """Prompt the user for a password.
60
51
        :return: The password string, return None if the user 
61
52
                 canceled the request.
62
53
        """
63
 
        raise NotImplementedError(self.get_password)
 
54
        raise NotImplementedError
64
55
        
65
 
    def nested_progress_bar(self):
66
 
        """Return a nested progress bar.
67
 
 
68
 
        When the bar has been finished with, it should be released bu calling
69
 
        bar.finished().
70
 
        """
71
 
        raise NotImplementedError(self.nested_progress_bar)
72
 
 
73
 
    def clear_term(self):
74
 
        """Prepare the terminal for output.
75
 
 
76
 
        This will, for example, clear text progress bars, and leave the
77
 
        cursor at the leftmost position."""
78
 
        raise NotImplementedError(self.clear_term)
79
 
 
80
 
    def get_boolean(self, prompt):
81
 
        """Get a boolean question answered from the user. 
82
 
 
83
 
        :param prompt: a message to prompt the user with. Should be a single
84
 
        line without terminating \n.
85
 
        :return: True or False for y/yes or n/no.
86
 
        """
87
 
        raise NotImplementedError(self.get_boolean)
88
 
 
89
 
 
90
 
class CLIUIFactory(UIFactory):
91
 
    """Common behaviour for command line UI factories."""
92
 
 
93
 
    def __init__(self):
94
 
        super(CLIUIFactory, self).__init__()
95
 
        self.stdin = sys.stdin
96
 
 
97
 
    def get_boolean(self, prompt):
98
 
        self.clear_term()
99
 
        # FIXME: make a regexp and handle case variations as well.
100
 
        while True:
101
 
            self.prompt(prompt)
102
 
            line = self.stdin.readline()
103
 
            if line in ('y\n', 'yes\n'):
104
 
                return True
105
 
            if line in ('n\n', 'no\n'):
106
 
                return False
107
 
 
108
 
    def prompt(self, prompt):
109
 
        """Emit prompt on the CLI."""
110
 
 
111
 
 
112
 
class SilentUIFactory(CLIUIFactory):
 
56
 
 
57
class SilentUIFactory(UIFactory):
113
58
    """A UI Factory which never prints anything.
114
59
 
115
60
    This is the default UI, if another one is never registered.
116
61
    """
117
 
 
118
 
    @deprecated_method(zero_eight)
119
62
    def progress_bar(self):
120
 
        """See UIFactory.nested_progress_bar()."""
121
63
        return bzrlib.progress.DummyProgress()
122
64
 
123
65
    def get_password(self, prompt='', **kwargs):
124
66
        return None
125
67
 
126
 
    def nested_progress_bar(self):
127
 
        if self._progress_bar_stack is None:
128
 
            self._progress_bar_stack = bzrlib.progress.ProgressBarStack(
129
 
                klass=bzrlib.progress.DummyProgress)
130
 
        return self._progress_bar_stack.get_nested()
131
 
 
132
 
    def clear_term(self):
133
 
        pass
134
 
 
135
 
 
136
 
def clear_decorator(func, *args, **kwargs):
137
 
    """Decorator that clears the term"""
138
 
    ui_factory.clear_term()
139
 
    func(*args, **kwargs)
140
 
 
141
68
 
142
69
ui_factory = SilentUIFactory()
143
 
"""IMPORTANT: never import this symbol directly. ONLY ever access it as 
144
 
ui.ui_factory."""