/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar

« back to all changes in this revision

Viewing changes to breezy/ui/__init__.py

  • Committer: Jelmer Vernooij
  • Date: 2017-05-22 00:56:52 UTC
  • mfrom: (6621.2.26 py3_pokes)
  • Revision ID: jelmer@jelmer.uk-20170522005652-yjahcr9hwmjkno7n
Merge Python3 porting work ('py3 pokes')

Show diffs side-by-side

added added

removed removed

Lines of Context:
30
30
    breezy in batch mode or for programs such as loggerhead.
31
31
 
32
32
breezy.ui.CannedInputUIFactory
33
 
    For use in testing; the input values to be returned are provided 
 
33
    For use in testing; the input values to be returned are provided
34
34
    at construction.
35
35
 
36
36
breezy.ui.text.TextUIFactory
45
45
 
46
46
import warnings
47
47
 
48
 
from breezy.lazy_import import lazy_import
 
48
from ..lazy_import import lazy_import
49
49
lazy_import(globals(), """
50
50
from breezy import (
51
51
    config,
52
52
    osutils,
53
53
    progress,
54
 
    trace,
55
54
    )
56
55
""")
57
56
 
 
57
from ..sixish import (
 
58
    PY3,
 
59
    string_types,
 
60
    text_type,
 
61
    )
 
62
 
58
63
 
59
64
_valid_boolean_strings = dict(yes=True, no=False,
60
65
                              y=True, n=False,
84
89
    if accepted_values is None:
85
90
        accepted_values = _valid_boolean_strings
86
91
    val = None
87
 
    if type(s) in (str, unicode):
 
92
    if isinstance(s, string_types):
88
93
        try:
89
94
            val = accepted_values[s.lower()]
90
95
        except KeyError:
115
120
        return '%s(%r, %r, %r)' % (
116
121
            self.__class__.__name__,
117
122
            self.wrapped_ui,
118
 
            self.default_answer, 
 
123
            self.default_answer,
119
124
            self.specific_answers)
120
125
 
121
126
    def confirm_action(self, prompt, confirmation_id, prompt_kwargs):
137
142
    UI Factories are also context managers, for some syntactic sugar some users
138
143
    need.
139
144
 
140
 
    :ivar suppressed_warnings: Identifiers for user warnings that should 
 
145
    :ivar suppressed_warnings: Identifiers for user warnings that should
141
146
        no be emitted.
142
147
    """
143
148
 
236
241
    def is_quiet(self):
237
242
        return self._quiet
238
243
 
239
 
    def make_output_stream(self, encoding=None, encoding_type=None):
 
244
    def make_output_stream(self, encoding=None, encoding_type='replace'):
240
245
        """Get a stream for sending out bulk text data.
241
246
 
242
247
        This is used for commands that produce bulk text, such as log or diff
243
248
        output, as opposed to user interaction.  This should work even for
244
249
        non-interactive user interfaces.  Typically this goes to a decorated
245
 
        version of stdout, but in a GUI it might be appropriate to send it to a 
 
250
        version of stdout, but in a GUI it might be appropriate to send it to a
246
251
        window displaying the text.
247
 
     
248
 
        :param encoding: Unicode encoding for output; if not specified 
249
 
            uses the configured 'output_encoding' if any; otherwise the 
250
 
            terminal encoding. 
 
252
 
 
253
        The caller may flush but should not close the returned stream.
 
254
 
 
255
        :param encoding: Unicode encoding for output; if not specified
 
256
            uses the configured 'output_encoding' if any; otherwise the
 
257
            terminal encoding.
251
258
            (See get_terminal_encoding.)
252
259
 
253
260
        :param encoding_type: How to handle encoding errors:
254
261
            replace/strict/escape/exact.  Default is replace.
255
262
        """
256
 
        # XXX: is the caller supposed to close the resulting object?
257
 
        if encoding is None:
258
 
            encoding = config.GlobalStack().get('output_encoding')
259
 
        if encoding is None:
260
 
            encoding = osutils.get_terminal_encoding(trace=True)
261
 
        if encoding_type is None:
262
 
            encoding_type = 'replace'
263
263
        out_stream = self._make_output_stream_explicit(encoding, encoding_type)
264
264
        return out_stream
265
265
 
318
318
        except KeyError:
319
319
            fail = "brz warning: %r, %r" % (warning_id, message_args)
320
320
            warnings.warn("no template for warning: " + fail)   # so tests will fail etc
321
 
            return fail
 
321
            return text_type(fail)
322
322
        try:
323
 
            return template % message_args
324
 
        except ValueError, e:
 
323
            return text_type(template) % message_args
 
324
        except ValueError as e:
325
325
            fail = "brz unprintable warning: %r, %r, %s" % (
326
326
                warning_id, message_args, e)
327
327
            warnings.warn(fail)   # so tests will fail etc
328
 
            return fail
 
328
            return text_type(fail)
329
329
 
330
330
    def choose(self, msg, choices, default=None):
331
331
        """Prompt the user for a list of alternatives.
412
412
        outdated formats), not for internal program warnings like deprecated
413
413
        APIs.
414
414
 
415
 
        This can be overridden by UIFactory subclasses to show it in some 
 
415
        This can be overridden by UIFactory subclasses to show it in some
416
416
        appropriate way; the default UIFactory is noninteractive and does
417
417
        nothing.  format_user_warning maps it to a string, though other
418
418
        presentations can be used for particular UIs.
419
419
 
420
 
        :param warning_id: An identifier like 'cross_format_fetch' used to 
 
420
        :param warning_id: An identifier like 'cross_format_fetch' used to
421
421
            check if the message is suppressed and to look up the string.
422
422
        :param message_args: Arguments to be interpolated into the message.
423
423
        """
425
425
 
426
426
    def show_error(self, msg):
427
427
        """Show an error message (not an exception) to the user.
428
 
        
 
428
 
429
429
        The message should not have an error prefix or trailing newline.  That
430
430
        will be added by the factory if appropriate.
431
431
        """
522
522
    """
523
523
    # this is now always TextUIFactory, which in turn decides whether it
524
524
    # should display progress bars etc
525
 
    from breezy.ui.text import TextUIFactory
 
525
    from .text import TextUIFactory, _wrap_in_stream, _wrap_out_stream
 
526
    # GZ 2017-05-21: May want to rewrap streams on Python 3 if encoding config
 
527
    if not PY3:
 
528
        stdin = _wrap_in_stream(stdin)
 
529
        stdout = _wrap_out_stream(stdout)
 
530
        stderr = _wrap_out_stream(stderr)
526
531
    return TextUIFactory(stdin, stdout, stderr)
527
532
 
528
533