~bzr-pqm/bzr/bzr.dev

1185.1.29 by Robert Collins
merge merge tweaks from aaron, which includes latest .dev
1
# Copyright (C) 2005 Canonical Ltd
1887.1.1 by Adeodato Simó
Do not separate paragraphs in the copyright statement with blank lines,
2
#
1185.1.29 by Robert Collins
merge merge tweaks from aaron, which includes latest .dev
3
# This program is free software; you can redistribute it and/or modify
4
# it under the terms of the GNU General Public License as published by
5
# the Free Software Foundation; either version 2 of the License, or
6
# (at your option) any later version.
1887.1.1 by Adeodato Simó
Do not separate paragraphs in the copyright statement with blank lines,
7
#
1185.1.29 by Robert Collins
merge merge tweaks from aaron, which includes latest .dev
8
# This program is distributed in the hope that it will be useful,
9
# but WITHOUT ANY WARRANTY; without even the implied warranty of
10
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11
# GNU General Public License for more details.
1887.1.1 by Adeodato Simó
Do not separate paragraphs in the copyright statement with blank lines,
12
#
1185.1.29 by Robert Collins
merge merge tweaks from aaron, which includes latest .dev
13
# You should have received a copy of the GNU General Public License
14
# along with this program; if not, write to the Free Software
15
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
16
17
18
19
"""UI abstraction.
20
21
This tells the library how to display things to the user.  Through this
22
layer different applications can choose the style of UI.
23
24
At the moment this layer is almost trivial: the application can just
25
choose the style of progress bar.
26
27
Set the ui_factory member to define the behaviour.  The default
28
displays no output.
29
"""
30
1687.1.4 by Robert Collins
Add bzrlib.ui.ui_factory.get_boolean().
31
import sys
1594.1.1 by Robert Collins
Introduce new bzr progress bar api. ui_factory.nested_progress_bar.
32
1185.1.29 by Robert Collins
merge merge tweaks from aaron, which includes latest .dev
33
import bzrlib.progress
1773.4.1 by Martin Pool
Add pyflakes makefile target; fix many warnings
34
from bzrlib.symbol_versioning import (deprecated_method, zero_eight)
1185.1.29 by Robert Collins
merge merge tweaks from aaron, which includes latest .dev
35
36
1185.49.21 by John Arbash Meinel
Refactored bzrlib/ui.py into a module with the possibility for multiple ui forms.
37
class UIFactory(object):
38
    """UI abstraction.
39
40
    This tells the library how to display things to the user.  Through this
41
    layer different applications can choose the style of UI.
42
    """
1594.1.1 by Robert Collins
Introduce new bzr progress bar api. ui_factory.nested_progress_bar.
43
1594.1.3 by Robert Collins
Fixup pb usage to use nested_progress_bar.
44
    def __init__(self):
45
        super(UIFactory, self).__init__()
46
        self._progress_bar_stack = None
47
1594.1.2 by Robert Collins
Update news and deprecated the old progress bar api.
48
    @deprecated_method(zero_eight)
1185.1.29 by Robert Collins
merge merge tweaks from aaron, which includes latest .dev
49
    def progress_bar(self):
1594.1.1 by Robert Collins
Introduce new bzr progress bar api. ui_factory.nested_progress_bar.
50
        """See UIFactory.nested_progress_bar()."""
51
        raise NotImplementedError(self.progress_bar)
1185.49.22 by John Arbash Meinel
Added get_password to the UIFactory, using it inside of sftp.py
52
53
    def get_password(self, prompt='', **kwargs):
54
        """Prompt the user for a password.
55
56
        :param prompt: The prompt to present the user
57
        :param kwargs: Arguments which will be expanded into the prompt.
58
                       This lets front ends display different things if
59
                       they so choose.
60
        :return: The password string, return None if the user 
61
                 canceled the request.
62
        """
1594.1.1 by Robert Collins
Introduce new bzr progress bar api. ui_factory.nested_progress_bar.
63
        raise NotImplementedError(self.get_password)
1185.49.22 by John Arbash Meinel
Added get_password to the UIFactory, using it inside of sftp.py
64
        
1594.1.1 by Robert Collins
Introduce new bzr progress bar api. ui_factory.nested_progress_bar.
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
1558.8.1 by Aaron Bentley
Fix overall progress bar's interaction with 'note' and 'warning'
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
1687.1.4 by Robert Collins
Add bzrlib.ui.ui_factory.get_boolean().
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):
1185.49.21 by John Arbash Meinel
Refactored bzrlib/ui.py into a module with the possibility for multiple ui forms.
113
    """A UI Factory which never prints anything.
114
115
    This is the default UI, if another one is never registered.
116
    """
1594.1.1 by Robert Collins
Introduce new bzr progress bar api. ui_factory.nested_progress_bar.
117
1594.1.2 by Robert Collins
Update news and deprecated the old progress bar api.
118
    @deprecated_method(zero_eight)
1185.1.29 by Robert Collins
merge merge tweaks from aaron, which includes latest .dev
119
    def progress_bar(self):
1594.1.1 by Robert Collins
Introduce new bzr progress bar api. ui_factory.nested_progress_bar.
120
        """See UIFactory.nested_progress_bar()."""
1185.1.29 by Robert Collins
merge merge tweaks from aaron, which includes latest .dev
121
        return bzrlib.progress.DummyProgress()
122
1185.49.22 by John Arbash Meinel
Added get_password to the UIFactory, using it inside of sftp.py
123
    def get_password(self, prompt='', **kwargs):
124
        return None
125
1594.1.1 by Robert Collins
Introduce new bzr progress bar api. ui_factory.nested_progress_bar.
126
    def nested_progress_bar(self):
1594.1.3 by Robert Collins
Fixup pb usage to use nested_progress_bar.
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()
1594.1.1 by Robert Collins
Introduce new bzr progress bar api. ui_factory.nested_progress_bar.
131
1558.8.1 by Aaron Bentley
Fix overall progress bar's interaction with 'note' and 'warning'
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
1185.49.22 by John Arbash Meinel
Added get_password to the UIFactory, using it inside of sftp.py
141
1185.1.29 by Robert Collins
merge merge tweaks from aaron, which includes latest .dev
142
ui_factory = SilentUIFactory()
1534.5.9 by Robert Collins
Advise users running upgrade on a checkout to also run it on the branch.
143
"""IMPORTANT: never import this symbol directly. ONLY ever access it as 
144
ui.ui_factory."""