~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/ui/__init__.py

  • Committer: Canonical.com Patch Queue Manager
  • Date: 2009-03-06 06:48:25 UTC
  • mfrom: (4070.8.6 debug-config)
  • Revision ID: pqm@pqm.ubuntu.com-20090306064825-kbpwggw21dygeix6
(mbp) debug_flags configuration option

Show diffs side-by-side

added added

removed removed

Lines of Context:
12
12
#
13
13
# You should have received a copy of the GNU General Public License
14
14
# along with this program; if not, write to the Free Software
15
 
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 
15
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
16
16
 
17
17
"""UI abstraction.
18
18
 
157
157
        self.stderr = stderr or sys.stderr
158
158
 
159
159
    def get_boolean(self, prompt):
 
160
        self.clear_term()
160
161
        # FIXME: make a regexp and handle case variations as well.
161
162
        while True:
162
163
            self.prompt(prompt + "? [y/n]: ")
166
167
            if line in ('n\n', 'no\n'):
167
168
                return False
168
169
 
169
 
    def get_non_echoed_password(self):
170
 
        isatty = getattr(self.stdin, 'isatty', None)
171
 
        if isatty is not None and isatty():
172
 
            # getpass() ensure the password is not echoed and other
173
 
            # cross-platform niceties
174
 
            password = getpass.getpass('')
175
 
        else:
176
 
            # echo doesn't make sense without a terminal
177
 
            password = self.stdin.readline()
178
 
            if not password:
179
 
                password = None
180
 
            elif password[-1] == '\n':
181
 
                password = password[:-1]
182
 
        return password
 
170
    def get_non_echoed_password(self, prompt):
 
171
        if not sys.stdin.isatty():
 
172
            raise errors.NotATerminal()
 
173
        encoding = osutils.get_terminal_encoding()
 
174
        return getpass.getpass(prompt.encode(encoding, 'replace'))
183
175
 
184
176
    def get_password(self, prompt='', **kwargs):
185
177
        """Prompt the user for a password.
192
184
                 canceled the request.
193
185
        """
194
186
        prompt += ': '
195
 
        self.prompt(prompt, **kwargs)
 
187
        prompt = (prompt % kwargs)
196
188
        # There's currently no way to say 'i decline to enter a password'
197
189
        # as opposed to 'my password is empty' -- does it matter?
198
 
        return self.get_non_echoed_password()
199
 
 
200
 
    def get_username(self, prompt, **kwargs):
201
 
        """Prompt the user for a username.
202
 
 
203
 
        :param prompt: The prompt to present the user
204
 
        :param kwargs: Arguments which will be expanded into the prompt.
205
 
                       This lets front ends display different things if
206
 
                       they so choose.
207
 
        :return: The username string, return None if the user
208
 
                 canceled the request.
209
 
        """
210
 
        prompt += ': '
211
 
        self.prompt(prompt, **kwargs)
212
 
        username = self.stdin.readline()
213
 
        if not username:
214
 
            username = None
215
 
        elif username[-1] == '\n':
216
 
            username = username[:-1]
217
 
        return username
218
 
 
219
 
    def prompt(self, prompt, **kwargs):
 
190
        return self.get_non_echoed_password(prompt)
 
191
 
 
192
    def prompt(self, prompt):
220
193
        """Emit prompt on the CLI."""
221
 
        prompt = prompt % kwargs
222
 
        prompt = prompt.encode(osutils.get_terminal_encoding(), 'replace')
223
 
        self.clear_term()
224
194
        self.stdout.write(prompt)
225
195
 
226
196
    def note(self, msg):
240
210
    def get_password(self, prompt='', **kwargs):
241
211
        return None
242
212
 
243
 
    def get_username(self, prompt='', **kwargs):
244
 
        return None
245
 
 
246
 
    def prompt(self, prompt, **kwargs):
 
213
    def prompt(self, prompt):
247
214
        pass
248
215
 
249
216
    def note(self, msg):
273
240
        cls = CLIUIFactory
274
241
    elif not isatty():
275
242
        cls = CLIUIFactory
276
 
    elif os.environ.get('TERM') in ('dumb', ''):
 
243
    elif os.environ.get('TERM') in (None, 'dumb', ''):
277
244
        # e.g. emacs compile window
278
245
        cls = CLIUIFactory
279
246
    # User may know better, otherwise default to TextUIFactory