1
# Copyright (C) 2005-2016 Canonical Ltd, 2017 Bazaar developers
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.
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.
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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
22
from ..ui import text as ui_text
25
class StringIOWithEncoding(io.StringIO):
29
def write(self, string):
30
if isinstance(string, bytes):
31
string = string.decode(self.encoding)
32
io.StringIO.write(self, string)
35
class BytesIOWithEncoding(io.BytesIO):
40
class StringIOAsTTY(StringIOWithEncoding):
41
"""A helper class which makes a StringIO look like a terminal"""
47
class TextUIFactory(ui_text.TextUIFactory):
49
def __init__(self, stdin=None, stdout=None, stderr=None):
50
if isinstance(stdin, bytes):
51
stdin = stdin.decode()
52
if isinstance(stdin, str):
53
stdin = StringIOWithEncoding(stdin)
55
stdout = StringIOWithEncoding()
57
stderr = StringIOWithEncoding()
58
super(TextUIFactory, self).__init__(stdin, stdout, stderr)
60
def _setup_streams(self):
61
self.raw_stdin = self.stdin
62
self.raw_stdout = self.stdout
63
self.raw_stderr = self.stderr
66
class TestUIFactory(TextUIFactory):
67
"""A UI Factory for testing.
69
Hide the progress bar but emit note()s.
71
Allows get_password to be tested without real tty attached.
73
See also CannedInputUIFactory which lets you provide programmatic input in
76
# TODO: Capture progress events at the model level and allow them to be
77
# observed by tests that care.
79
# XXX: Should probably unify more with CannedInputUIFactory or a
80
# particular configuration of TextUIFactory, or otherwise have a clearer
81
# idea of how they're supposed to be different.
82
# See https://bugs.launchpad.net/bzr/+bug/408213
84
def get_non_echoed_password(self):
85
"""Get password from stdin without trying to handle the echo mode"""
86
password = self.stdin.readline()
89
if password[-1] == '\n':
90
password = password[:-1]
93
def make_progress_view(self):
94
return ui.NullProgressView()