Removes useless beets_ui and FIX input issue between python 2 and 3.

main
Olivier Mangin 11 years ago
parent fd49c3acf2
commit 52813439dd

@ -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')

@ -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

@ -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

@ -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:

Loading…
Cancel
Save