/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

  • Committer: Jelmer Vernooij
  • Date: 2009-10-19 18:16:56 UTC
  • mfrom: (4757 +trunk)
  • mto: This revision was merged to the branch mainline in revision 4833.
  • Revision ID: jelmer@samba.org-20091019181656-b1gy3j0a2u7h2gcv
merge bzr.dev.

Show diffs side-by-side

added added

removed removed

Lines of Context:
22
22
Several levels are supported, and you can also register new factories such as
23
23
for a GUI.
24
24
 
25
 
UIFactory
 
25
bzrlib.ui.UIFactory
26
26
    Semi-abstract base class
27
27
 
28
 
SilentUIFactory
 
28
bzrlib.ui.SilentUIFactory
29
29
    Produces no output and cannot take any input; useful for programs using
30
30
    bzrlib in batch mode or for programs such as loggerhead.
31
31
 
32
 
CannedInputUIFactory
 
32
bzrlib.ui.CannedInputUIFactory
33
33
    For use in testing; the input values to be returned are provided 
34
34
    at construction.
35
35
 
36
 
TextUIFactory
 
36
bzrlib.ui.text.TextUIFactory
37
37
    Standard text command-line interface, with stdin, stdout, stderr.
38
38
    May make more or less advanced use of them, eg in drawing progress bars,
39
39
    depending on the detected capabilities of the terminal.
208
208
        """
209
209
        pass
210
210
 
211
 
 
212
 
 
213
 
class CLIUIFactory(UIFactory):
214
 
    """Deprecated in favor of TextUIFactory."""
215
 
 
216
 
    @deprecated_method(deprecated_in((1, 18, 0)))
217
 
    def __init__(self, stdin=None, stdout=None, stderr=None):
218
 
        UIFactory.__init__(self)
219
 
        self.stdin = stdin or sys.stdin
220
 
        self.stdout = stdout or sys.stdout
221
 
        self.stderr = stderr or sys.stderr
222
 
 
223
 
    _accepted_boolean_strings = dict(y=True, n=False, yes=True, no=False)
224
 
 
225
 
    def get_boolean(self, prompt):
226
 
        while True:
227
 
            self.prompt(prompt + "? [y/n]: ")
228
 
            line = self.stdin.readline()
229
 
            line = line.rstrip('\n')
230
 
            val = bool_from_string(line, self._accepted_boolean_strings)
231
 
            if val is not None:
232
 
                return val
233
 
 
234
 
    def get_non_echoed_password(self):
235
 
        isatty = getattr(self.stdin, 'isatty', None)
236
 
        if isatty is not None and isatty():
237
 
            # getpass() ensure the password is not echoed and other
238
 
            # cross-platform niceties
239
 
            password = getpass.getpass('')
240
 
        else:
241
 
            # echo doesn't make sense without a terminal
242
 
            password = self.stdin.readline()
243
 
            if not password:
244
 
                password = None
245
 
            elif password[-1] == '\n':
246
 
                password = password[:-1]
247
 
        return password
248
 
 
249
 
    def get_password(self, prompt='', **kwargs):
250
 
        """Prompt the user for a password.
251
 
 
252
 
        :param prompt: The prompt to present the user
253
 
        :param kwargs: Arguments which will be expanded into the prompt.
254
 
                       This lets front ends display different things if
255
 
                       they so choose.
256
 
        :return: The password string, return None if the user
257
 
                 canceled the request.
258
 
        """
259
 
        prompt += ': '
260
 
        self.prompt(prompt, **kwargs)
261
 
        # There's currently no way to say 'i decline to enter a password'
262
 
        # as opposed to 'my password is empty' -- does it matter?
263
 
        return self.get_non_echoed_password()
264
 
 
265
 
    def get_username(self, prompt, **kwargs):
266
 
        """Prompt the user for a username.
267
 
 
268
 
        :param prompt: The prompt to present the user
269
 
        :param kwargs: Arguments which will be expanded into the prompt.
270
 
                       This lets front ends display different things if
271
 
                       they so choose.
272
 
        :return: The username string, return None if the user
273
 
                 canceled the request.
274
 
        """
275
 
        prompt += ': '
276
 
        self.prompt(prompt, **kwargs)
277
 
        username = self.stdin.readline()
278
 
        if not username:
279
 
            username = None
280
 
        elif username[-1] == '\n':
281
 
            username = username[:-1]
282
 
        return username
283
 
 
284
 
    def prompt(self, prompt, **kwargs):
285
 
        """Emit prompt on the CLI.
 
211
    def show_error(self, msg):
 
212
        """Show an error message (not an exception) to the user.
286
213
        
287
 
        :param kwargs: Dictionary of arguments to insert into the prompt,
288
 
            to allow UIs to reformat the prompt.
 
214
        The message should not have an error prefix or trailing newline.  That
 
215
        will be added by the factory if appropriate. 
289
216
        """
290
 
        if kwargs:
291
 
            # See <https://launchpad.net/bugs/365891>
292
 
            prompt = prompt % kwargs
293
 
        prompt = prompt.encode(osutils.get_terminal_encoding(), 'replace')
294
 
        self.clear_term()
295
 
        self.stderr.write(prompt)
296
 
 
297
 
    def note(self, msg):
298
 
        """Write an already-formatted message."""
299
 
        self.stdout.write(msg + '\n')
 
217
        raise NotImplementedError(self.show_error)
 
218
 
 
219
    def show_message(self, msg):
 
220
        """Show a message to the user."""
 
221
        raise NotImplementedError(self.show_message)
 
222
 
 
223
    def show_warning(self, msg):
 
224
        """Show a warning to the user."""
 
225
        raise NotImplementedError(self.show_warning)
 
226
 
300
227
 
301
228
 
302
229
class SilentUIFactory(UIFactory):
318
245
    def get_username(self, prompt, **kwargs):
319
246
        return None
320
247
 
 
248
    def show_error(self, msg):
 
249
        pass
 
250
 
 
251
    def show_message(self, msg):
 
252
        pass
 
253
 
 
254
    def show_warning(self, msg):
 
255
        pass
 
256
 
321
257
 
322
258
class CannedInputUIFactory(SilentUIFactory):
323
259
    """A silent UI that return canned input."""
343
279
                % (self,))
344
280
 
345
281
 
346
 
@deprecated_function(deprecated_in((1, 18, 0)))
347
 
def clear_decorator(func, *args, **kwargs):
348
 
    """Decorator that clears the term"""
349
 
    ui_factory.clear_term()
350
 
    func(*args, **kwargs)
351
 
 
352
 
 
353
282
ui_factory = SilentUIFactory()
354
283
# IMPORTANT: never import this symbol directly. ONLY ever access it as
355
284
# ui.ui_factory, so that you refer to the current value.