/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 bzrlib/ui/__init__.py

(spiv) Fix BZR_TEST_PDB. (#504070) (Martin [gz])

Show diffs side-by-side

added added

removed removed

Lines of Context:
100
100
    return val
101
101
 
102
102
 
 
103
class ConfirmationUserInterfacePolicy(object):
 
104
    """Wrapper for a UIFactory that allows or denies all confirmed actions."""
 
105
 
 
106
    def __init__(self, wrapped_ui, default_answer, specific_answers):
 
107
        """Generate a proxy UI that does no confirmations.
 
108
 
 
109
        :param wrapped_ui: Underlying UIFactory.
 
110
        :param default_answer: Bool for whether requests for
 
111
            confirmation from the user should be noninteractively accepted or
 
112
            denied.
 
113
        :param specific_answers: Map from confirmation_id to bool answer.
 
114
        """
 
115
        self.wrapped_ui = wrapped_ui
 
116
        self.default_answer = default_answer
 
117
        self.specific_answers = specific_answers
 
118
 
 
119
    def __getattr__(self, name):
 
120
        return getattr(self.wrapped_ui, name)
 
121
 
 
122
    def __repr__(self):
 
123
        return '%s(%r, %r, %r)' % (
 
124
            self.__class__.__name__,
 
125
            self.wrapped_ui,
 
126
            self.default_answer, 
 
127
            self.specific_answers)
 
128
 
 
129
    def confirm_action(self, prompt, confirmation_id, prompt_kwargs):
 
130
        if confirmation_id in self.specific_answers:
 
131
            return self.specific_answers[confirmation_id]
 
132
        elif self.default_answer is not None:
 
133
            return self.default_answer
 
134
        else:
 
135
            return self.wrapped_ui.confirm_action(
 
136
                prompt, confirmation_id, prompt_kwargs)
 
137
 
 
138
 
103
139
class UIFactory(object):
104
140
    """UI abstraction.
105
141
 
106
142
    This tells the library how to display things to the user.  Through this
107
143
    layer different applications can choose the style of UI.
108
144
 
 
145
    UI Factories are also context managers, for some syntactic sugar some users
 
146
    need.
 
147
 
109
148
    :ivar suppressed_warnings: Identifiers for user warnings that should 
110
149
        no be emitted.
111
150
    """
123
162
        self.suppressed_warnings = set()
124
163
        self._quiet = False
125
164
 
 
165
    def __enter__(self):
 
166
        """Context manager entry support.
 
167
 
 
168
        Override in a concrete factory class if initialisation before use is
 
169
        needed.
 
170
        """
 
171
        return self # This is bound to the 'as' clause in a with statement.
 
172
 
 
173
    def __exit__(self, exc_type, exc_val, exc_tb):
 
174
        """Context manager exit support.
 
175
 
 
176
        Override in a concrete factory class if more cleanup than a simple
 
177
        self.clear_term() is needed when the UIFactory is finished with.
 
178
        """
 
179
        self.clear_term()
 
180
        return False # propogate exceptions.
 
181
 
126
182
    def be_quiet(self, state):
127
183
        """Tell the UI to be more quiet, or not.
128
184
 
131
187
        """
132
188
        self._quiet = state
133
189
 
 
190
    def confirm_action(self, prompt, confirmation_id, prompt_kwargs):
 
191
        """Seek user confirmation for an action.
 
192
 
 
193
        If the UI is noninteractive, or the user does not want to be asked
 
194
        about this action, True is returned, indicating bzr should just
 
195
        proceed.
 
196
 
 
197
        The confirmation id allows the user to configure certain actions to
 
198
        always be confirmed or always denied, and for UIs to specialize the
 
199
        display of particular confirmations.
 
200
 
 
201
        :param prompt: Suggested text to display to the user.
 
202
        :param prompt_kwargs: A dictionary of arguments that can be
 
203
            string-interpolated into the prompt.
 
204
        :param confirmation_id: Unique string identifier for the confirmation.
 
205
        """
 
206
        return self.get_boolean(prompt % prompt_kwargs)
 
207
 
134
208
    def get_password(self, prompt='', **kwargs):
135
209
        """Prompt the user for a password.
136
210
 
158
232
        version of stdout, but in a GUI it might be appropriate to send it to a 
159
233
        window displaying the text.
160
234
     
161
 
        :param encoding: Unicode encoding for output; default is the 
162
 
            terminal encoding, which may be different from the user encoding.
 
235
        :param encoding: Unicode encoding for output; if not specified 
 
236
            uses the configured 'output_encoding' if any; otherwise the 
 
237
            terminal encoding. 
163
238
            (See get_terminal_encoding.)
164
239
 
165
240
        :param encoding_type: How to handle encoding errors:
167
242
        """
168
243
        # XXX: is the caller supposed to close the resulting object?
169
244
        if encoding is None:
170
 
            encoding = osutils.get_terminal_encoding()
 
245
            from bzrlib import config
 
246
            encoding = config.GlobalConfig().get_user_option(
 
247
                'output_encoding')
 
248
        if encoding is None:
 
249
            encoding = osutils.get_terminal_encoding(trace=True)
171
250
        if encoding_type is None:
172
251
            encoding_type = 'replace'
173
252
        out_stream = self._make_output_stream_explicit(encoding, encoding_type)
352
431
                "without an upgrade path.\n" % (inter.target._format,))
353
432
 
354
433
 
355
 
 
356
 
class SilentUIFactory(UIFactory):
 
434
class NoninteractiveUIFactory(UIFactory):
 
435
    """Base class for UIs with no user."""
 
436
 
 
437
    def confirm_action(self, prompt, confirmation_id, prompt_kwargs):
 
438
        return True
 
439
 
 
440
    def __repr__(self):
 
441
        return '%s()' % (self.__class__.__name__, )
 
442
 
 
443
 
 
444
class SilentUIFactory(NoninteractiveUIFactory):
357
445
    """A UI Factory which never prints anything.
358
446
 
359
447
    This is the default UI, if another one is never registered by a program
394
482
    def __repr__(self):
395
483
        return "%s(%r)" % (self.__class__.__name__, self.responses)
396
484
 
 
485
    def confirm_action(self, prompt, confirmation_id, args):
 
486
        return self.get_boolean(prompt % args)
 
487
 
397
488
    def get_boolean(self, prompt):
398
489
        return self.responses.pop(0)
399
490