From 52813439dd60adfa852e11f4a898baa756c6d7e1 Mon Sep 17 00:00:00 2001 From: Olivier Mangin Date: Thu, 11 Sep 2014 16:46:17 +0200 Subject: [PATCH] Removes useless beets_ui and FIX input issue between python 2 and 3. --- pubs/beets_ui.py | 57 ------------------------------------------- pubs/p3.py | 5 +++- pubs/uis.py | 25 ++++++++++++++++--- tests/test_usecase.py | 4 +-- 4 files changed, 28 insertions(+), 63 deletions(-) delete mode 100644 pubs/beets_ui.py diff --git a/pubs/beets_ui.py b/pubs/beets_ui.py deleted file mode 100644 index 1adf87a..0000000 --- a/pubs/beets_ui.py +++ /dev/null @@ -1,57 +0,0 @@ -# This file contains functions taken from the user interface of the beet -# tool (http://beets.radbox.org). -# -# Copyright 2013, Adrian Sampson. -# -# Permission is hereby granted, free of charge, to any person obtaining -# a copy of this software and associated documentation files (the -# "Software"), to deal in the Software without restriction, including -# without limitation the rights to use, copy, modify, merge, publish, -# distribute, sublicense, and/or sell copies of the Software, and to -# permit persons to whom the Software is furnished to do so, subject to -# the following conditions: -# -# The above copyright notice and this permission notice shall be -# included in all copies or substantial portions of the Software. - - -import locale -import sys - -from .p3 import input - - -class UserError(Exception): - """UI exception. Commands should throw this in order to display - nonrecoverable errors to the user. - """ - pass - - -def _encoding(config): - """Tries to guess the encoding used by the terminal.""" - # Configured override? - # Determine from locale settings. - try: - default_enc = locale.getdefaultlocale()[1] or 'utf8' - except ValueError: - # Invalid locale environment variable setting. To avoid - # failing entirely for no good reason, assume UTF-8. - default_enc = 'utf8' - return config.get('terminal-encoding', default_enc) - - -def input_(): - """Get input and decodes the result to a Unicode string. - Raises a UserError if stdin is not available. The prompt is sent to - stdout rather than stderr. A printed between the prompt and the - input cursor. - """ - # raw_input incorrectly sends prompts to stderr, not stdout, so we - # use print() explicitly to display prompts. - # http://bugs.python.org/issue1927 - try: - resp = input() - except EOFError: - raise UserError('stdin stream ended while input required') - return resp.decode(sys.stdin.encoding or 'utf8', 'ignore') diff --git a/pubs/p3.py b/pubs/p3.py index 034260c..db439b9 100644 --- a/pubs/p3.py +++ b/pubs/p3.py @@ -4,7 +4,10 @@ import sys if sys.version_info[0] == 2: import ConfigParser as configparser _read_config = configparser.SafeConfigParser.readfp - input = raw_input + + def input(): + raw_input().decode(sys.stdin.encoding or 'utf8', 'ignore') + ustr = unicode uchr = unichr from urlparse import urlparse diff --git a/pubs/uis.py b/pubs/uis.py index 7a965d2..aecf480 100644 --- a/pubs/uis.py +++ b/pubs/uis.py @@ -2,9 +2,10 @@ from __future__ import print_function import sys -from .beets_ui import _encoding, input_ from .content import editor_input from . import color +import locale + # package-shared ui that can be accessed using : # from uis import get_ui @@ -13,6 +14,16 @@ from . import color _ui = None +def _get_encoding(config): + """Get local terminal encoding or user preference in config.""" + enc = 'utf8' + try: + enc = locale.getdefaultlocale()[1] or 'utf8' + except ValueError: + pass # Keep default + return config.get('terminal-encoding', enc) + + def get_ui(): if _ui is None: raise ValueError('ui not instanciated yet') @@ -29,7 +40,7 @@ class UI: """ def __init__(self, config): - self.encoding = _encoding(config) + self.encoding = _get_encoding(config) color.setup(config.color) self.editor = config.edit_cmd @@ -40,6 +51,14 @@ class UI: """ print(' '.join(strings).encode(self.encoding, 'replace')) + def input(self): + try: + data = input() + except EOFError: + self.error('Standard input ended while waiting for answer.') + self.exit(1) + return data + def input_choice(self, options, option_chars, default=None, question=''): """Ask the user to chose between a set of options. The iser is asked to input a char corresponding to the option he choses. @@ -61,7 +80,7 @@ class UI: for c, o in zip(displayed_chars, options)]) self.print_(question, option_str) while True: - answer = input_() + answer = self.input() if answer is None or answer == '': if default is not None: return default diff --git a/tests/test_usecase.py b/tests/test_usecase.py index 47fbdb5..ded9647 100644 --- a/tests/test_usecase.py +++ b/tests/test_usecase.py @@ -8,7 +8,7 @@ import dotdot import fake_env from pubs import pubs_cmd -from pubs import color, content, filebroker, uis, beets_ui, p3, endecoder, configs +from pubs import color, content, filebroker, uis, p3, endecoder, configs import str_fixtures import fixtures @@ -73,7 +73,7 @@ class CommandTestCase(unittest.TestCase): for cmd in cmds: if not isinstance(cmd, p3.ustr): if len(cmd) == 2: - input = fake_env.FakeInput(cmd[1], [content, uis, beets_ui, p3]) + input = fake_env.FakeInput(cmd[1], [content, uis, p3]) input.as_global() if capture_output: