/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar
6621.22.1 by Martin
Refactor bzrlib.ui to be based on unicode streams
1
# Copyright (C) 2005-2016 Canonical Ltd, 2017 Bazaar developers
2
#
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.
7
#
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.
12
#
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
16
17
from __future__ import absolute_import
18
19
import io
20
6624 by Jelmer Vernooij
Merge Python3 porting work ('py3 pokes')
21
from .. import (
6621.22.1 by Martin
Refactor bzrlib.ui to be based on unicode streams
22
    ui,
23
)
6695.1.1 by Martin
Make ui package pass tests on Python 3
24
from ..ui import text as ui_text
6621.22.1 by Martin
Refactor bzrlib.ui to be based on unicode streams
25
26
27
class StringIOWithEncoding(io.StringIO):
28
29
    encoding = "ascii"
30
6621.22.2 by Martin
Use BytesIO or StringIO from bzrlib.sixish
31
    def write(self, string):
32
        if isinstance(string, bytes):
33
            string = string.decode(self.encoding)
34
        io.StringIO.write(self, string)
35
6621.22.1 by Martin
Refactor bzrlib.ui to be based on unicode streams
36
37
class BytesIOWithEncoding(io.BytesIO):
38
39
    encoding = "ascii"
40
41
42
class StringIOAsTTY(StringIOWithEncoding):
43
    """A helper class which makes a StringIO look like a terminal"""
44
45
    def isatty(self):
46
        return True
47
48
6695.1.1 by Martin
Make ui package pass tests on Python 3
49
class TextUIFactory(ui_text.TextUIFactory):
6621.22.1 by Martin
Refactor bzrlib.ui to be based on unicode streams
50
51
    def __init__(self, stdin=None, stdout=None, stderr=None):
52
        if isinstance(stdin, bytes):
53
            stdin = stdin.decode()
7479.2.1 by Jelmer Vernooij
Drop python2 support.
54
        if isinstance(stdin, str):
6621.22.1 by Martin
Refactor bzrlib.ui to be based on unicode streams
55
            stdin = StringIOWithEncoding(stdin)
56
        if stdout is None:
57
            stdout = StringIOWithEncoding()
58
        if stderr is None:
59
            stderr = StringIOWithEncoding()
60
        super(TextUIFactory, self).__init__(stdin, stdout, stderr)
61
6910.3.1 by Martin
Delay stream wrapping and fix choose in char based mode
62
    def _setup_streams(self):
63
        self.raw_stdin = self.stdin
64
        self.raw_stdout = self.stdout
65
        self.raw_stderr = self.stderr
66
6621.22.1 by Martin
Refactor bzrlib.ui to be based on unicode streams
67
68
class TestUIFactory(TextUIFactory):
69
    """A UI Factory for testing.
70
71
    Hide the progress bar but emit note()s.
72
    Redirect stdin.
73
    Allows get_password to be tested without real tty attached.
74
75
    See also CannedInputUIFactory which lets you provide programmatic input in
76
    a structured way.
77
    """
78
    # TODO: Capture progress events at the model level and allow them to be
79
    # observed by tests that care.
80
    #
81
    # XXX: Should probably unify more with CannedInputUIFactory or a
82
    # particular configuration of TextUIFactory, or otherwise have a clearer
83
    # idea of how they're supposed to be different.
84
    # See https://bugs.launchpad.net/bzr/+bug/408213
85
86
    def get_non_echoed_password(self):
87
        """Get password from stdin without trying to handle the echo mode"""
88
        password = self.stdin.readline()
89
        if not password:
90
            raise EOFError
91
        if password[-1] == '\n':
92
            password = password[:-1]
93
        return password
94
95
    def make_progress_view(self):
96
        return ui.NullProgressView()