1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
|
# Copyright (C) 2005-2016 Canonical Ltd, 2017 Bazaar developers
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
from __future__ import absolute_import
import io
from .. import (
ui,
)
from ..ui import text as ui_text
from ..sixish import (
text_type,
)
class StringIOWithEncoding(io.StringIO):
encoding = "ascii"
def write(self, string):
if isinstance(string, bytes):
string = string.decode(self.encoding)
io.StringIO.write(self, string)
class BytesIOWithEncoding(io.BytesIO):
encoding = "ascii"
class StringIOAsTTY(StringIOWithEncoding):
"""A helper class which makes a StringIO look like a terminal"""
def isatty(self):
return True
class TextUIFactory(ui_text.TextUIFactory):
def __init__(self, stdin=None, stdout=None, stderr=None):
if isinstance(stdin, bytes):
stdin = stdin.decode()
if isinstance(stdin, text_type):
stdin = StringIOWithEncoding(stdin)
if stdout is None:
stdout = StringIOWithEncoding()
if stderr is None:
stderr = StringIOWithEncoding()
super(TextUIFactory, self).__init__(stdin, stdout, stderr)
class TestUIFactory(TextUIFactory):
"""A UI Factory for testing.
Hide the progress bar but emit note()s.
Redirect stdin.
Allows get_password to be tested without real tty attached.
See also CannedInputUIFactory which lets you provide programmatic input in
a structured way.
"""
# TODO: Capture progress events at the model level and allow them to be
# observed by tests that care.
#
# XXX: Should probably unify more with CannedInputUIFactory or a
# particular configuration of TextUIFactory, or otherwise have a clearer
# idea of how they're supposed to be different.
# See https://bugs.launchpad.net/bzr/+bug/408213
def get_non_echoed_password(self):
"""Get password from stdin without trying to handle the echo mode"""
password = self.stdin.readline()
if not password:
raise EOFError
if password[-1] == '\n':
password = password[:-1]
return password
def make_progress_view(self):
return ui.NullProgressView()
|