/usr/lib/python2.7/dist-packages/eyed3/utils/prompt.py is in python-eyed3 0.7.10-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 | # -*- coding: utf-8 -*-
################################################################################
# Copyright (C) 2013 Travis Shirk <travis@pobox.com>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, see <http://www.gnu.org/licenses/>.
#
################################################################################
import sys as _sys
from .. import LOCAL_ENCODING
from .console import Fore as fg
DISABLE_PROMPT = None
'''Whenever a prompt occurs and this value is not ``None`` it can be ``exit``
to call sys.exit (see EXIT_STATUS) or ``raise`` to throw a RunttimeError,
which can be caught if desired.'''
EXIT_STATUS = 2
BOOL_TRUE_RESPONSES = ("yes", "y", "true")
class PromptExit(RuntimeError):
'''Raised when ``DISABLE_PROMPT`` is 'raise' and ``prompt`` is called.'''
pass
def parseIntList(resp):
ints = set()
resp = resp.replace(',', ' ')
for c in resp.split():
i = int(c)
ints.add(i)
return list(ints)
def prompt(msg, default=None, required=True, type_=unicode,
validate=None, choices=None):
'''Prompt user for imput, the prequest is in ``msg``. If ``default`` is
not ``None`` it will be displayed as the default and returned if not
input is entered. The value ``None`` is only returned if ``required`` is
``False``. The response is passed to ``type_`` for conversion (default
is unicode) before being returned. An optional list of valid responses can
be provided in ``choices`.'''
yes_no_prompt = default is True or default is False
if yes_no_prompt:
default_str = "Yn" if default is True else "yN"
else:
default_str = str(default) if default else None
if default is not None:
msg = "%s [%s]" % (msg, default_str)
msg += ": " if not yes_no_prompt else "? "
if DISABLE_PROMPT:
if DISABLE_PROMPT == "exit":
print(msg + "\nPrompting is disabled, exiting.")
_sys.exit(EXIT_STATUS)
else:
raise PromptExit(msg)
resp = None
while resp is None:
try:
resp = raw_input(msg).decode(LOCAL_ENCODING)
except EOFError:
# COnverting this allows main functions to catch without
# catching other eofs
raise PromptExit()
if not resp and default not in (None, ""):
resp = str(default)
if resp:
if yes_no_prompt:
resp = True if resp.lower() in BOOL_TRUE_RESPONSES else False
else:
resp = resp.strip()
try:
resp = type_(resp)
except Exception as ex:
print(fg.red(str(ex)))
resp = None
elif not required:
return None
else:
resp = None
if ((choices and resp not in choices) or
(validate and not validate(resp))):
if choices:
print(fg.red("Invalid response, choose from: ") + str(choices))
else:
print(fg.red("Invalid response"))
resp = None
return resp
|